[antlr-interest] TokenRewriteStream.replace Problem

英明 yimm8369 at hotmail.com
Fri Feb 13 22:36:21 PST 2009




Hi guys,

I want to change the "+" to "-" using antlr, there is the testcase:

package {

    public class bitwise extends Sprite
    {    var time:Number = getTimer();
        
      }
        
            function runOperatorTest():Number
                {
                for(var i:int=0; i<1000000; i++)
                {
                    var val1:int = i + 2;
                    }
                }
                }
 
There is the java file:

public void testNormal1() throws Exception {
        CharStream input = new ANTLRFileStream("TestASTBlockStatment_testcase001");
        ActionScript3Lexer lex = new ActionScript3Lexer(input);
        ASTokenRewriteStream tokens = new ASTokenRewriteStream(lex);
        ActionScript3Parser parser = new ActionScript3Parser(tokens);
        parser.setTreeAdaptor(new ASCommonTreeAdapter());
        parser.packageDeclaration();
        System.out.println("after:" + tokens);
  }


In the .g file, I use:


@members{
     TokenRewriteStream tokens;
   .....
}
....
packageDeclaration
@init {

   tokens = (TokenRewriteStream)input; 
  // Token start = input.LT(1);
   .....
}

....
additiveExpression
    :   multiplicativeExpression ( sss=(add^)  {if($add.text.equals("+")){tokens.replace($sss, "123");}}  multiplicativeExpression)*     
    ;

And I rewrite the TokenRewriteStream.java to ASTokenRewriteStream.

When I run it, here is the error:

java.lang.NullPointerException
    at org.antlr.runtime.TokenRewriteStream.replace(TokenRewriteStream.java:272)
    at heyesh.app.language.as3.parser.ASTokenRewriteStream.replace(ASTokenRewriteStream.java:120)
    at heyesh.app.language.as3.parser.ActionScript3Parser.additiveExpression(ActionScript3Parser.java:14087)
    at heyesh.app.language.as3.parser.ActionScript3Parser.shiftExpression(ActionScript3Parser.java:13945)
    at heyesh.app.language.as3.parser.ActionScript3Parser.relationalExpression(ActionScript3Parser.java:13734)
    at heyesh.app.language.as3.parser.ActionScript3Parser.equalityExpression(ActionScript3Parser.java:13532)
    at heyesh.app.language.as3.parser.ActionScript3Parser.bitwiseANDExpression(ActionScript3Parser.java:13346)
    at heyesh.app.language.as3.parser.ActionScript3Parser.bitwiseXORExpression(ActionScript3Parser.java:13160)
    at heyesh.app.language.as3.parser.ActionScript3Parser.bitwiseORExpression(ActionScript3Parser.java:12974)
    at heyesh.app.language.as3.parser.ActionScript3Parser.logicalANDExpression(ActionScript3Parser.java:12788)
    at heyesh.app.language.as3.parser.ActionScript3Parser.logicalORExpression(ActionScript3Parser.java:12602)
    at heyesh.app.language.as3.parser.ActionScript3Parser.conditionalExpression(ActionScript3Parser.java:12329)
    at heyesh.app.language.as3.parser.ActionScript3Parser.assignmentExpression(ActionScript3Parser.java:12055)
    at heyesh.app.language.as3.parser.ActionScript3Parser.variableDeclaration(ActionScript3Parser.java:10835)
    at heyesh.app.language.as3.parser.ActionScript3Parser.variableStatement(ActionScript3Parser.java:10436)
    at heyesh.app.language.as3.parser.ActionScript3Parser.statement(ActionScript3Parser.java:6888)
    at heyesh.app.language.as3.parser.ActionScript3Parser.blockStatement(ActionScript3Parser.java:7020)
    at heyesh.app.language.as3.parser.ActionScript3Parser.statement(ActionScript3Parser.java:6650)
    at heyesh.app.language.as3.parser.ActionScript3Parser.forStatement(ActionScript3Parser.java:9021)
    at heyesh.app.language.as3.parser.ActionScript3Parser.statement(ActionScript3Parser.java:6804)
    at heyesh.app.language.as3.parser.ActionScript3Parser.blockStatement(ActionScript3Parser.java:7020)
    at heyesh.app.language.as3.parser.ActionScript3Parser.functionDeclaration(ActionScript3Parser.java:5793)
    at heyesh.app.language.as3.parser.ActionScript3Parser.statement(ActionScript3Parser.java:6902)
    at heyesh.app.language.as3.parser.ActionScript3Parser.packageElement(ActionScript3Parser.java:4156)
    at heyesh.app.language.as3.parser.ActionScript3Parser.packageDeclaration(ActionScript3Parser.java:4000)
    at heyesh.app.language.as3.semantic.internal.statement.TestASTBlockStatement.testNormal1(TestASTBlockStatement.java:35)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:164)
    at junit.framework.TestCase.runBare(TestCase.java:130)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.runTest(TestSuite.java:230)
    at junit.framework.TestSuite.run(TestSuite.java:225)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

TokenRewriteStream.java:272 is :
     public void replace(Token indexT, Object text) {
                              replace(DEFAULT_PROGRAM_NAME, indexT, indexT, text);
                    }

and  ASTokenRewriteStream.java:120 is :

 public void replace(ASCommonToken indexT, Object text) {

                              replace(DEFAULT_PROGRAM_NAME, indexT, indexT, text);

                    }

ASCommonToken extends CommonToken.


any idea?


Annie



_________________________________________________________________
讲述中国特色文化,体验不同的节日习俗,快来微软春节搜索!
http://chunjie.live.com/?form=PANER01
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090214/fb23f39d/attachment.html 


More information about the antlr-interest mailing list