[antlr-interest] newbie's question on writing template rewrite rule
woodzltc zhou
woodzltc at gmail.com
Thu Jun 21 22:42:44 PDT 2012
Thank you, Vasan!
Yes. Your guessing is right. CLASS_TOP_LEVEL_SCOPE is a virtual token.
With your advice, I can make the first problem right.
But I am still have problem with the second one. I don't have any
template rewrite rule for classScopeDeclarations, so do I have any
other options to make the it right? I only want to insert something
just after the class header definition. What I want to achieve right
now is to change:
public class HelloWorld {
static int x = 1;
public static void main(String args[]){
System.out.println("Hello,world!" + y);
}
}
to something like this:
public class HelloWorld {
private class Verticle extends java.lang.Object {
public Verticle nextV;
public Verticle permV;
}
static int x = 1;
public static void main(String args[]){
System.out.println("Hello,world!" + y);
}
}
I tried to add new template rewrite rule for classScopeDeclarations, as follows:
classScopeDeclarations
: ^(CLASS_INSTANCE_INITIALIZER block) ->
classScopeDeclarations(arg={$text})
| ^(CLASS_STATIC_INITIALIZER block) -> classScopeDeclarations(arg={$text})
| ^(FUNCTION_METHOD_DECL modifierList genericTypeParameterList?
type IDENT formalParameterList arrayDeclaratorList? throwsClause?
block?) -> classScopeDeclarations(arg={$text})
| ^(VOID_METHOD_DECL modifierList genericTypeParameterList?
IDENT formalParameterList throwsClause? block?) ->
classScopeDeclarations(arg={$text})
| ^(VAR_DECLARATION modifierList type variableDeclaratorList) ->
classScopeDeclarations(arg={$text})
| ^(CONSTRUCTOR_DECL modifierList genericTypeParameterList?
formalParameterList throwsClause? block) ->
classScopeDeclarations(arg={$text})
| typeDeclaration -> classScopeDeclarations(arg={$typeDeclaration.st})
typeDeclaration(arg={$text}) ::= <<
$arg$
>>
It still does not work right. The source after the modification is:
public class HelloWorld {
private class Verticle extends java.lang.Object {
public Verticle nextV;
public Verticle permV;
}
public static void main(String args[]){
System.out.println("Hello,world!" + y);
}
}
Somehow, the variable definition " static int x = 1;" is droped.
Can you point me what is wrong in this case? I am using the grammers
at http://www.antlr.org/grammar/1207932239307/Java1_5Grammars.
Thanks a lot!
Best,
- Wu
On Thu, Jun 21, 2012 at 8:50 PM, srinivasan karthikeyan pitchai
<srinivasan.karthikeyan.pitchai at oracle.com> wrote:
> Hi Wu,
> Is CLASS_TOP_LEVEL_SCOPE a virtual token, ie a token that was not part
> of the original source token stream, that you created to create the
> subtree? If yes, then you have 2 choices, you can either base the
> virtual token on a real token that would have a text or you can give a
> text to the virtual token at the time of construction. This would
> ensure that $CLASS_TOP_LEVEL_SCOPE.text has some value that can be
> produced in the output. Try replacing $classScopeDeclarations.text
> with $classScopeDeclarations.st provided that you are already have a
> template rewrite rule for classScopeDeclarations rule.
>
> -Vasan
>
> On 6/22/2012 7:00 AM, woodzltc zhou wrote:
>> Hello all,
>>
>> I am a newbie to ANTLR, a great tool in my opinion! Here is a question
>> on how to write template rewrite rule for such kind of rule:
>>
>> classTopLevelScope
>> : ^(CLASS_TOP_LEVEL_SCOPE classScopeDeclarations*)
>>
>> I want to insert something fixed between CLASS_TOP_LEVEL_SCOPE and
>> classScopeDeclarations*, what should I do?
>>
>> I wrote something as follows:
>>
>> classTopLevelScope
>> : ^(CLASS_TOP_LEVEL_SCOPE classScopeDeclarations*) ->
>> embed(arg1={$CLASS_TOP_LEVEL_SCOPE.text},
>> arg2={$classScopeDeclarations.text})
>>
>> embed(arg1, arg2) ::=<<
>> $arg1$
>> blabla
>> blabla
>> $arg2$
>> It does insert what I want. But it also drop all the stuff at the two
>> sides. I guess I am wrong with what I gave as arg1 and arg2. Anyone
>> can give me some suggestions? Thanks a lot!
>>
>> Best
>> - Wu
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
More information about the antlr-interest
mailing list