Java add extra members to contexts

15 views
Skip to first unread message

Marc Pawlowsky

unread,
Oct 3, 2018, 11:04:10 AM10/3/18
to antlr-discussion
I am writing a java tool to parse C++ code and extract information.  Something similar to ExtractInterface example in the Definitive ANTLR4 reference book.

I am using a listener, so I do not have to explicitly walk through all the child nodes.
I would like to keep extra data with the contexts to the lower level exit rules can pass extra information up to the higher level exit rules.


Is there a good way to do this in Java.

I was thinking if the context instances were created with a factory then we could have casted to our extended versions but the code creates the contextes directly.

I was playing with a decorator design pattern but had trouble since the Context's do not implement interfaces.

Is the best solution to keep a parallel AST or Map from context ot my extra data?

Thanks
  Marc

Susan Jolly

unread,
Oct 3, 2018, 2:29:36 PM10/3/18
to antlr-di...@googlegroups.com
Have you read Section 7.5 in the ANTLR 4 book?  I found its part on "Annotating Parse Trees" really useful, most especially the explanation of how to use the ParseTreeProperty helper class for keeping track of annotations.  The example uses <INTEGER> for the type of the stored values but a custom type might suit your purpose better.
 
HTH, Susan
Reply all
Reply to author
Forward
0 new messages