How to keep empty line and comments

Aug 5, 2009 at 4:28 PM


I am working on a source formatter for our old legacy basic language.
The language is not single line type like the sample GWBASIC.
My biggest problem for multiline statements how can I handle
the comments at some line endings. I tried two methods.

First, I add Comments into NonTerminals, and write a TokenFilter,
and store every comments in a list. It works, but hard to generate
the source with comments in proper place.

Second, I remove Comments from NonTerminals, it works
with simple line statement, but caused syntax error in multiline

Is there any other possibilities?

Another problem is the empty lines, I try to keep empty lines
during formatting. How can I keep them in the result tree?


Aug 6, 2009 at 3:18 AM

Well, as for empty lines, you can detect them by tracking nodes' Span.Location property. It contains Row field which is row number in source text.

Comments tokens are saved with all other tokens in ParseTree.Tokens list - you can scan this list for comments and match them with your statement nodes using the same Span.Location.Row property.

I know, it is a bit painful, and you actually bring up an interesting case - pretty-printing the source from parse tree. I need to think how I can support it better, maybe directly provide this functionality, kinda out-of-the-box style.