Saving comments in Text.Parsec.Token tokens

I am writing a source-to-source conversion using parsec , so I have LanguageDeffor my language and I build a TokenParserfor it using Text.Parsec.Token.makeTokenParser:

myLanguage = LanguageDef { ...
  commentStart = "/*"
  , commentEnd = "*/"
  ...
}

-- defines 'stringLiteral', 'identifier', etc...
TokenParser {..} = makeTokenParser myLanguage

Unfortunately, since I defined commentStartand commentEnd, each of the parser compilers in TokenParseris a token parser, implemented in terms of whiteSpace, and whiteSpace- spaces, as well as comments.

What is the correct way to save comments in this situation?

Approaches I can think of:

  • Do not define commentStartand commentEnd. Wrap each of the token parsers in a different combinator that captures comments before parsing each token.
  • makeTokenParser (, , , Text.Parsec.Token, , ?)

?

+4
1

, commentStart commentEnd , , , AST/ADT.

, , -

data Statement = Comment String | Return Expression | ......

, , , .


: makeTokenParser .

, makeTokenParser, String ().

+5

Source: https://habr.com/ru/post/1546148/


All Articles