I'm writing a source-to-source transformation using parsec, So I have a LanguageDef
for my language and I build a TokenParser
for it using Text.Parsec.Token.makeTokenParser
:
myLanguage = LanguageDef { ...
commentStart = "/*"
, commentEnd = "*/"
...
}
-- defines 'stringLiteral', 'identifier', etc...
TokenParser {..} = makeTokenParser myLanguage
Unfortunately since I defined commentStart
and commentEnd
, each of the parser combinators in the TokenParser
is a lexeme parser implemented in terms of whiteSpace
, and whiteSpace
eats spaces as well as comments.
What is the right way to preserve comments in this situation?
Approaches I can think of:
- Don't define
commentStart
andcommentEnd
. Wrap each of the lexeme parsers in another combinator that grabs comments before parsing each token. - Implement my own version of
makeTokenParser
(or perhaps use some library that generalizesText.Parsec.Token
; if so, which library?)
What's the done thing in this situation?