[antlr-interest] Java - adding new keyword question

Vaios Kalpias akiskal at hol.gr
Thu May 11 05:21:59 PDT 2006


Hi everybody.

I am creating a preprocessor of .java files, that makes use of a new 
custom keyword named  /level/. Let me demonstrate an example of its use:

level(1) class BeyondClass
{  
//
//
}

In this case the preprocessor will transform the above code into

class BeyondClass_level1
{
    //
    //
 }

The idea behind level is to have different classes and methods grouped 
in a number of different levels.. So for example we could have

level(1)
{
    class Class1    //(the preprocessor will name this Class1_level1
    {  
        //
        //
    }
    class Class2   //(the preprocessor will name this Class2_level1
    {  
        //
        //
    }
}

The preprocessor will then take this code, create an ast of the java 
grammar (where a LEVEL_DEF node will have these classes as its children) 
and then output working code using the JavaEmitter class. Another use of 
/level/ is this:

class BeyondMethod {
   
        level(1) {
            public void foo() {
                //...
            }
        }
       
        level(2) {
            public void foo() {
                //...
            }
        }
       
        // 0 all the rest
        level(0) {
            public void foo() {
                //...
            }
        }
       
}

which will be transformed into

class BeyondMethod_Mutative {
   
    public void foo_level1() {
        //...
    }

    public void foo_level2() {
        //...
    }
    public void foo_level0() {
        //...
    }
}

Now to my questions. Using the java 1.5 grammar found in the public 
domain, I'm thinking about changing the typeDefinitionInternal rule to:

protected typeDefinitionInternal[AST mods]
    :    classDefinition[#mods]        // inner class
    |    interfaceDefinition[#mods]    // inner interface
    |    enumDefinition[#mods]        // inner enum
    |    annotationDefinition[#mods]    // inner annotation
    |    levelDefinition[#mods]        // inner level definition (add 
this code here)
    ;

levelDefinition should be something like this:
// Definition of a mutative level
levelDefinition![AST modifiers]
    :    "level"
        // level parameter
        lp:levelParameters
        (
            // it _might_ define a class
            cd:classDefinition[modifiers]
        |
            // now parse the body of the level
            lb:levelBlock
        )
        {#levelDefinition = #(#[LEVEL_DEF,"LEVEL_DEF"],
                                lp,cd,lb);}
    ;

and I think that levelBlock will be the same as classBlock... My problem 
is that I'm not sure if this is the right approach and also that since I 
made thiese changes, whenever I  parse a .java file I get an error like 
this: Unexpected AST node : X
I noticed that no matter if I use level in the file to be parsed, this 
error always comes up with X being the first node in the ast...

Any feedback would be greatly appreciated! Thank you





More information about the antlr-interest mailing list