Using Megaparsec, if I want to parse a string containing comments of the form ~{content} into a Comment record, how would I go about doing that? For instance:
data Comment = { id :: Integer, content :: String }
parse :: Parser [Comment]
parse = _
parse
"hello world ~{1-sometext} bla bla ~{2-another comment}"
== [Comment { id = 1, content = "sometext" }, Comment { id = 2, content = "another comment"}]
The thing I'm stuck on is allowing for everything that's not ~{} to be ignored, including the lone char ~ and the lone brackets {}.
You can do this by dropping characters up to the next tilde, then parsing the tilde optionally followed by a valid comment, and looping.
In particular, if we define
nonTildesto discard non-tildes:and then an
optionalCommentto parse a tilde and optional following comment in braces:Then the comments can be parsed with:
This assumes that a
~{without a matching}is a parse error, rather than valid non-comment text, which seems sensible. However, the definition of thecontent_parser is probably too liberal. It gobbles everything up to the next}, meaning that:is a valid comment with content
"{{{\n". Disallowing{(and maybe~) in comments, or alternatively requiring braces to be properly nested in comments seems like a good idea.Anyway, here's a full code example for you to fiddle with: