mainland / language-c-quote

Other
58 stars 30 forks source link

Parse and print '#line' directives #46

Closed jameysharp closed 9 years ago

jameysharp commented 9 years ago

It would be nice to be able to manipulate #line directives with language-c-quote. I'm particularly interested in setting source locations for statements.

I can imagine two approaches to implement this:

  1. Parse #line directives and set SrcLocs accordingly. When pretty-printing, if an element of the AST has a Loc, use the file and line from its starting Pos to emit a #line directive. As an optimization, it might be nice to suppress any #line directive with the same filename as the previous directive, and where the difference in line numbers between the two Locs is the same as the number of lines pretty-printed since the last #line. If you don't want these directives you can rewrite all SrcLocs to NoLoc before pretty-printing.
  2. Add a Line constructor to Stm, like Pragma.

Which would you prefer?

mchakravarty commented 9 years ago

It's of course @mainland's call, but Option (1) sounds more attractive to me. As a special case, it should also be possible to emit #line directives even when the parsed source didn't have any directives — i.e., to emit info about the original source location of the quoted C code.

mainland commented 9 years ago

language-c-quote already parses #line directives and uses them to set source code locations. Is this not working for you?

You can also print out source code with #line pragmas. Check out the prettyPragma* functions in Text.PrettyPrint.Mainland.

I'm going to close this issue. If the line parsing doesn't work, please open a new issue.