ParseTreeListener: Getting full text of line from where token came from

338 views
Skip to first unread message

henrik sorensen

unread,
Sep 16, 2014, 1:11:11 AM9/16/14
to antlr-di...@googlegroups.com
I am using the ParseTreeWalker and ParseTreeListeners to traverse the parse tree.

When a certain ParseRuleContext is met I have a requirement to get the full text of the line from where a token was parsed.

Beside doing the brute force of reading the file until the line number, is there a better way?

Many thanks
Henrik

Terence Parr

unread,
Sep 16, 2014, 10:34:46 AM9/16/14
to antlr-di...@googlegroups.com
hi. make an index from line number to token list as you parse maybe?
Ter
--
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.

henrik sorensen

unread,
Sep 16, 2014, 2:15:19 PM9/16/14
to antlr-di...@googlegroups.com
yes, make sense.
-Henrik

--
You received this message because you are subscribed to a topic in the Google Groups "antlr-discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/antlr-discussion/9F6Httm0v4E/unsubscribe.
To unsubscribe from this group and all its topics, send an email to antlr-discussi...@googlegroups.com.

Norman Dunbar

unread,
Sep 16, 2014, 3:17:56 PM9/16/14
to antlr-di...@googlegroups.com, henrik sorensen
This is what I do...

In your listener, set up a member for linenumber and or charposition.

Redefine enterEveryRule.

Token startToken = ctx.getStart ();
...
Assuming startToken is not mull then startToken.getLine () and .getCharPositionInLine ()

will get you what you want.

Cheers,
Norm.
--
Sent from my Android device with K-9 Mail. Please excuse my brevity.
Reply all
Reply to author
Forward
0 new messages