Try using semantic predicates to look-ahead and decide whether to emit
SLCOMMENT or TEMPLATE

 

Something like:

 

fragment SLCOMMENT

                :               ;

 

fragment TEMPLATE

                :               ;

 

SLCOMMENT_OR_TEMPLATE

                :               '//' (

                                                { input.LA(1)=='@' }?=> (
options {greedy=false;} : . )* EOL    { $type=TEMPLATE; }

                                |              ( options {greedy=false;} : .
)* EOL    { $type=SLCOMMENT; }

                                )

                ;

 

Cheers, Indhu

 

 

From: [email protected]
[mailto:[email protected]] On Behalf Of Soren Holstebroe
Sent: Thursday, September 17, 2009 8:25 PM
To: [email protected]
Subject: [antlr-interest] Parsing comments

 

Hi,

I have this project where C++ templates are being unfolded and wrapped in
.NET. This wrapping is performed by some perl-scripts which over time have
become quite messy, so I wanted to rewrite the wrapper using ANTLR.

The way the current template wrapper works is by parsing simple template
expressions defined in the comments preceding a function definition. This is
similar to documentation frameworks like doxygen, where the documentation is
embedded in the comments above functions, classes, etc.
The simple syntax contains elements like this:
// @TEMPLATE <int, float>
which tells the wrapper to unfold the template using an int for the first
template type and a float for the second.

My problem is that I am already stuck in defining the lexer. 
Since all wrapper expressions start with '@' I tried to define a token like:
WRAPPERSTART : SLASHSLASH SPACE* '@'
and some grammar rules based on that token. 
However that doesn't work since the lexer will always choose the longest
matching rule, which would always be the normal single line comment rule.

I can imagine that there are many approaches to tackle this problem and some
hints would be greatly appreciated.

Soren


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

Reply via email to