'expression' is rule 'r' wont return a Token since it is not a lexer rule. It 
will instead return 'ParserRuleReturnScope' since it is a parser rule. So 
'$expression.stop' or '$t.stop' will give you the last token of 'expr'. You can 
use it like shown below: 

r : t=expression { newLineBeforeNextToken( $t.stop.getTokenIndex() ) 
}?=>/*nothing*/ 
; 

or 

r : expression { newLineBeforeNextToken( $expression.stop.getTokenIndex() ) 
}?=>/*nothing*/ 
; 

Cheers, Indhu 

----- Original Message ----- 
From: Andreas Stefik <[email protected]> 
To: Michael Bedward <[email protected]>, [email protected] 
Sent: Saturday, April 4, 2009 10:33:02 PM GMT+0530 Asia/Calcutta 
Subject: Re: [antlr-interest] A Simple Question on Channels 

Indhu, 

Thanks, this is very helpful. One more very related question, if you'll indulge 
me. The grammar you posted works great, and I think I'm close to understanding 
what's going on here. You define @members, which dumps a custom function into 
the generated parser. Inside, you get the tokens from any channel that is not 
the default, starting from the index of the token in the parser rule. 

However, in my grammar, t cannot be set to a token, because I need that newline 
to be processed after an expression, which can have any arbitrary number of 
tokens in it, so my code (modifying your grammar), would need to look something 
like this: 

r : t=expression { newLineBeforeNextToken( $t.getTokenIndex() ) }?=>/*nothing*/ 
; 

expression : equality 
; 

equality: add (('==' | '!=') add)* 
; 

add : multiply (('+' | '-') multiply)* 
; 

multiply: atom (('*' | '/') atom)* 
; 

atom : ID 
| '(' expression ')' 
; 

I know that first rule isn't right, but I need to have ANTLR determine (if 
possible), the index of the last token in the expression, and have that value 
be passed to your function. How do I modify the above to accommodate for having 
an arbitrary rule in place of where you had ID? 

Thanks again, this was very helpful, and gets me started, 

Andreas 

--~--~---------~--~----~------------~-------~--~----~
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