Reconstruct the original file by visiting/walking all nodes

58 views
Skip to first unread message

antlrLearner

unread,
Oct 10, 2018, 3:33:27 PM10/10/18
to antlr-discussion
Please help to visit all nodes sequentially starting from the root node in the order they appear in the input file in C#
Even though the parse rules are written in order, the text in the input file may not appear in the same order. 

Goal is to replace certain text in the file and rewrite the original file.

Thank you

Mike Lischke

unread,
Oct 11, 2018, 3:12:13 AM10/11/18
to antlr-discussion

Please help to visit all nodes sequentially starting from the root node in the order they appear in the input file in C#
Even though the parse rules are written in order, the text in the input file may not appear in the same order. 

Goal is to replace certain text in the file and rewrite the original file.

I'm also working on a project where file content is parsed (though not with ANTLR) and parts are replaced. I recommend that you not walk the entire parse tree but instead use the token informations you have. Each token comes with start and stop index in the original char stream. Use that to copy text you don't want to replace verbatim. Then add the replaced text for your token and do the same steps in a loop for all other tokens you want to replace. That avoids the need to reconstruct the original text from information you might not have (e.g. hidden whitespaces).


Mike Cargal

unread,
Oct 11, 2018, 5:08:35 PM10/11/18
to ANTLR List
Take a look at TokenStreamRewriter

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

antlrLearner

unread,
Oct 12, 2018, 1:45:30 PM10/12/18
to antlr-discussion
Mike,

As Mike pointed in the below thread TokenStreamRewriter is powerful, and in the lexer have to modify the whitespace, new line, comments rule to channel(HIDDEN) instead of SKIP.

antlrLearner

unread,
Oct 12, 2018, 1:45:46 PM10/12/18
to antlr-discussion
Thank you Mike
Reply all
Reply to author
Forward
0 new messages