tags:

views:

25

answers:

0

I've got a basic preprocessor grammar with the following Tree Parser rules.

What I'd like to figure out is if I can remove the syntactic predicates in the expand_item rule.

The rule checks if it finds and identifier, if so it calls the identifier_expand rule to see if it's a macro name we know about. If it finds an expanded macro then it matches the tree based on the . and then recursively matches the rest of the tree. Finally it matches an other case where we know we don't care about the current item and the call expand_item on the next value.

What I'd like to be able to do is change macro_expand to look like so:

macro_expand
    : #( MACRO ( macro_expand )* )
;

and then use something like ~(MACRO|IDENT) instead of the . in the other tree matching rule.

Is there something like the ~ for trees? ie can I say match any tree starting with any token except for the following list?

The grammar:

expand_all
: ( expand_item )*
;

expand_item
: ( IDENTIFIER )=> identifier_expand
| ( MACRO )=> macro_expand
| other
;

identifier_expand
:! id:IDENTIFIER 
( 
        { HaveMacro(id->getText()) }? { #identifier_expand = MacroExpand(#id); }
    | { #identifier_expand = #id; }
) 
;

macro_expand
    : #( . ( macro_expand )* )
;

other
: #( . ( expand_item )* )
;

I'm using Antlr v2 if that matters