[antlr-interest] A Simple Question on Channels

Michael Bedward michael.bedward at gmail.com
Fri Apr 3 20:25:29 PDT 2009


Hi Indhu,

Thanks very much for posting this example - it explains something that
I want to do too.

Michael

2009/4/3 Indhu Bharathi <indhu.b at s7software.com>:
> As expected, there were some bugs :-) Here is a implementation that works
> fine:
>
> grammar Test;
>
> @members{
>
>                 private boolean newLineBeforeNextToken( int index ) {
>
>                 Token tkn;
>
>                 input = (DebugTokenStream) input;
>
>                 while( input.size()>(index+1) &&
> (tkn=input.get(++index)).getChannel() != Token.DEFAULT_CHANNEL ) {
>
>                                 if( tkn.getType() == NL ) return true;
>
>                                 index++;
>
>                 }
>                 return false;
>
>                 }
>
> }
>
> r              :               t=ID { newLineBeforeNextToken(
> $t.getTokenIndex() ) }?=>/*nothing*/
>                 ;
>
> NL           :               '\n' {$channel=HIDDEN;}
>
>                 ;
>
> ID            :               'a'..'z'+
>
>                 ;
>
>
> "foo<NL>" will pass whereas "foo" will fail  :-)
>
> I casted input to 'DebugTokenStream' since I was running inside ANTLRWorks.
> You can cast it to CommonTokenStream.
>
> Cheers, Indhu
>
>


More information about the antlr-interest mailing list