[antlr-interest] ignoring tokens baed on parser context

Edson Tirelli tirelli at post.com
Wed Feb 21 04:58:27 PST 2007


 
   All,

   You need to take some care when using that token stream but what I 
did was simply to have a semantic action in the parser switching token 
type. For instance, in my use case I wanted to preserve spaces inside a 
curly chunk block and ignoring it outside. So, I have  a "curly chunk" 
rule doing:

curly_chunk
        @init {
           Integer channel = null;
        }
    :
        LEFT_CURLY
        {
            // saving previous channel and switching WS to default channel.
            channel = 
((SwitchingCommonTokenStream)input).getTokenTypeChannel( WS );
            ((SwitchingCommonTokenStream)input).setTokenTypeChannel( WS, 
Token.DEFAULT_CHANNEL );
        }
        // DO YOUR STUFF HERE
        {
            // switching back to original channel
            ((SwitchingCommonTokenStream)input).setTokenTypeChannel(WS, 
channel.intValue());
        }
        RIGHT_CURLY
    ;

     Please note that you need to add the semantic action to switch your 
token type back to original channel just before the end delimiter of you 
context. This is because of the lookahead.

     It may not be a clean way of doing, but was the only way I found to 
do what I needed.

     []s
     Edson

Matt Benson wrote:

>For those of us (like myself) who are complete idiots
>and can't see how the example on this wiki page
>blossoms into a complete solution, could you elaborate
>on the wiki or on the list? If the latter, I will push
>to the wiki.  To be clear, how specifically do you use
>this extended TokenStream in code?  I am going to need
>this soon.
>
>Thanks,
>Matt
>
>--- Martin d'Anjou <martin.danjou at neterion.com> wrote:
>
>  
>
>>Hello,
>>
>>What do I do in my grammar to ignore tokens based on
>>parser context as 
>>described here: 
>>
>>    
>>
>http://www.antlr.org/wiki/pages/viewpage.action?pageId=1734
>  
>
>>Thanks,
>>Martin
>>
>>    
>>
>
>
>
> 
>____________________________________________________________________________________
>Any questions? Get answers on any topic at www.Answers.yahoo.com.  Try it now.
>
>  
>


-- 
 Edson Tirelli
 Software Engineer - JBoss Rules Core Developer
 Office: +55 11 3124-6000
 Mobile: +55 11 9218-4151
 JBoss, a division of Red Hat @ www.jboss.com




More information about the antlr-interest mailing list