1

I'm printing an AST in order to format the source file, from which the AST is generated using Megaparsec. Comments are missing from the AST, but I intend to keep them after reformatting. Is there a way in Megaparsec to obtain all comments (for both line and block comments) annotated with source info, either inside AST as a node or outside separately as done in Language.Haskell.Exts.Annotated?

sinoTrinity
  • 1,125
  • 2
  • 15
  • 27
  • What is wrong with the first approach? Without looking at the source code I’m not sure how Megaparsec handles comments if you give it a comment parser, but I’ve an inkling that it just strips them. So the right thing to do would be to modify your AST to include comments and give a parser that never succeeds to Megaparsec for the comment parser (or Nothing if it takes a Maybe). This is left as a comment since I didn’t research what Megaparsec does. – cole May 22 '21 at 10:09

0 Answers0