Re: attpoarsec and recursive parsing

PICCA Frederic-Emmanuel <[email protected]>
Newsgroups gmane.comp.lang.haskell.cafe
Message-ID <426173889.1482083.1732269915823.JavaMail.zimbra@synchrotron-soleil.fr>
Hello, I end up with this solution

in order to parse this data type.

If you have some advice in order to simplify or beautify the Parser part do not hesitate.

Cheers

Fred

data MaskLocation = MaskLocation Text
                  | MaskLocation'Tmpl Text
                  | MaskLocation'Or MaskLocation MaskLocation
    deriving (Eq, Generic, Show)
    deriving anyclass (FromJSON, ToJSON)

instance FieldEmitter MaskLocation where
  fieldEmitter (MaskLocation t)      = t
  fieldEmitter (MaskLocation'Tmpl t) = t
  fieldEmitter (MaskLocation'Or l r) = fieldEmitter l <> " | " <> fieldEmitter r

instance FieldParsable MaskLocation where
  fieldParser = do
    let loc :: Text -> MaskLocation
        loc t = if "{scannumber:" `Data.Text.isInfixOf` t
                then MaskLocation'Tmpl (strip t)
                else MaskLocation (strip t)

    t <- takeTill (== '|')
    if t == ""
      then fail "MaskLocation is Empty"
      else do mc <- peekChar
              case mc of
                Nothing -> pure $ loc t
                Just '|'  -> do
                  _ <- char '|'  -- extract the '|' char
                  MaskLocation'Or (loc t) <$> fieldParser
                Just c -> fail ("MaskLocation " <> [c])
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.