[Haskell-beginners] Parsec, parsing 'free text'
Franco
franco00 at gmx.com
Sat Mar 10 11:49:24 CET 2012
I am using Parsec as my parsing library and quite liking it: though, I am unsure on how to properly tackle a 'free element' problem. Let me explain:
In my file, there are some elements which look like this:
this is some <red - formatted> text.
I call this a FormatString. They can be found inside many elements, like:
[ someconditions | this is some <red - formatted> text.]
+ this is some <red - formatted> text. -> somenumberhere
To parse a FormatString I call 'generalText', which looks like this:
56 -- TEXT --
57
58 generalText = many anyText >>= \fss ->
59 return (foldl (+:+%) (toFString "" []) fss)
60
61 anyText = try ( formattedText )
62 <|> plainText
63
64 formattedText = char '<' >>
65 sepEndBy1 format spacebar >>= \fs -> -- format parser
66 string "- " >>
67 manyTill anyChar (char '>') >>= \cs ->
68 return (toFString cs fs)
69
70 plainText = many1 (noneOf "<") >>= \xs ->
71 return (toFString xs [])
It works, but causes quite some headaches in using it:
- I would like to call a "read with generalText until you find a ']' character.
- or a "read with generalText until you find a " -> " (do not consume it)
And the like, but I do not know how. If it were a plain string I would call manyTill1 anyChar myP. I thought of first parsing a raw string searching for a terminator and then feeding it to generalText. But isn't that cluncky codewise?
Thanks for your help
-F
More information about the Beginners
mailing list