[antlr-interest] Code translation: Is it possible to bring the TreeParser back?
luciano mantuaneli
mantu_lists at yahoo.com.br
Tue Oct 17 12:04:58 PDT 2006
Greetings!
I'm developing a Java2C# parser, and I'm having some trouble trying to translate constructors that call the supar class constructor.... In Java, we make that thing like this:
class MyClass extends MyOtherClass{
public MyClass(Object param){
super(param);
/*...*/
}
}
The corresponding C# code would be like this:
class MyClass: MyOtherClass{
public MyClass(Object param): base(param){
/*...*/
}
}
The tree grammar I using to translate the code from Java to C# have the folowing rules to treat the constructor definition and invocation:
ctorDef :
CTOR_DEF modifiers
( typeParameters )?
methodHead
( slist )?
;
slist :
SLIST
( stat )*
;
stat :
typeDefinition
| variableDef
| expression
| //...
;
expression :
EXPR expr
;
expr :
conditionalExpr
| //...
;
conditionalExpr :
//...
| primaryExpression
;
primaryExpression :
//...
| ctorCall
| //...
;
ctorCall :
CTOR_CALL elist
| SUPER_CTOR_CALL
( elist | primaryExpression elist )
;
elist :
ELIST
( expression )*
;
Well, in C#, the super-constructor invocation is made before the statement list. In Java, that invocations, when exists, is the first statement in the statement list.
My question is: looking to the ctorDef rule, how can I know if the super-constructor was invoked before enter the slist rule?
I need it that way because I'm constructing the translation text like this:
ctorDef{
MethodHead mh;
ArrayList<String> mods = new ArrayList<String>();
}
: #(ctor:CTOR_DEF
mods=modifiers
(typeParameters)?
mh=methodHead {
GSB.tab();
for(String mod: mods)
GSB.app(mod + " ");
GSB.app(mh);
//if invokes super constructor then GSB.app(":base(" + ??? + ")");
GSB.app("{");
}
(slist)?{
GSB.less().tab("}").nl();
}
)
;
GSB is a class of mine that just represents an improved version of StringBuilder (Global String Builder) that have only static methods, which returns an sigleton instance of GSB. I use this clas to mount the C# code.
As you can see, after I append the String returned by the methodHead rule in my GSB, I need to know, before enter the slist rule, if the current constructor invokes the super-constructor...
I tried to do this manipulating directly the "ctor" AST node defined above, in order to find an AST node of type SUPER_CTOR_CALL, and then call directly the method corresponding to ctorCall rule. Obviously, it doesn't work, because I'm not sure if there is a way to walk "two times" over a same tree path...
So, how can I know if there is a super-constructor call, before entering in the slist rule?
Thanks in advance
Luciano Mantuaneli
Analist-Programmer
---------------------------------
Novidade no Yahoo! Mail: receba alertas de novas mensagens no seu celular. Registre seu aparelho agora!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20061017/11172a2b/attachment.html
More information about the antlr-interest
mailing list