[antlr-interest] Javadoc -style extensions

Ric Klaren ric.klaren at gmail.com
Wed May 25 15:22:04 PDT 2005


henry whittaker wrote:
> I'd like to extend the grammar I'm working up to use
> Javadoc-style language annotations so that optional
> metadata can be embedded as Javadoc comments.
> 
> I've looked at the examples of this in the
> documentation and there are two mechanisms
> highlighted, the primary one using multiplexed lexers
> and hidden tokens. 
> 
> I don't want to parse all javadoc-style comments
> though, only those preceding certain language
> structures so I am reluctant to extend the lexer.
> Instead I was considering the following:
>   Have the lexer emit javadoc comments as a single
> token

 From this point on I'd personally be inclined to just stuff the whole 
lot in an AST and then use a tree walker combined with the javadoc 
parser to insert pieces of AST in the spots where you want them. My 
guess is that it will give a simpler and more extensible solution. But 
YMMV. Both approaches should work.

>   Add a filter that strips and stores the Javadoc
> comments in a WeakHashMap mapped to the next visible
> token.
>   Create a method on the filter that returns the
> parsed Javadoc structure on-request (using a
> javadoc-specifc lexer initialised with line/col of
> javadoc comment).
>   Invoke the new filter method from my grammar at all
> locations where optional javadoc is required, called
> with the first token of the rule I want to annotate.
> 
> Is there any problems with this approach or is there a
> better way to do this?

> Also, are there any grammars already available for
> parsing javadoc comments? If not, I'd be happy to post
> mine (once it's written :)!)

If there is one it's probably listed on antlr.org.

Cheers,

Ric


More information about the antlr-interest mailing list