[antlr-interest] if..else in StringTemplate

YingAnnie yimm8369 at hotmail.com
Tue Feb 24 18:48:04 PST 2009


 

 

 

Hi everyone,

 

I want to use if else in the StringTemplateGroup,if (x ="org") print 1234 else print 5678, but <if(x=="org")> doesnot work. 

 

tree grammar Treegrammar;

.....

importDeclaration 

    :   ^(IMPORT_DECL importDeclarationTypeElement)
          ->assgin(x={$importDeclarationTypeElement.text})
    ;

 

.....

 

 

group T;
assgin(x)::= <<
<if(x=="org")><"1234"><else><"5678"><endif>
>>


Then I use the following, but it never replace.

 

In tree grammar Treegrammar, I add :

 

@members{

private StringTemplate template(String name)
         {
             return TestASTBlockStatement.templates.getInstanceOf(name);
         } 
}

 

TestASTBlockStatement.java is as follows:

 

 public static StringTemplateGroup templates;

 

public class TestASTBlockStatement extends BaseTestCase {

  
    public static StringTemplateGroup templates;
    public void testNormal1() throws Exception {
     FileReader groupFileR= new FileReader("T.stg");
     templates= new StringTemplateGroup(groupFileR);
    groupFileR.close();
     
     
     CharStream input = new ANTLRFileStream("TestASTBlockStatment_testcase001");
     ActionScript3Lexer lex = new ActionScript3Lexer(input); 
     ASTokenRewriteStream tokens = new ASTokenRewriteStream(lex);
    ActionScript3Parser parser = new ActionScript3Parser(tokens);
     parser.setTreeAdaptor(new ASCommonTreeAdaptor());
     packageDeclaration_return retval=parser.packageDeclaration();
      
     ASCommonTree tree = (ASCommonTree) retval.getTree();
     ASCommonTreeNodeStream nodes = new ASCommonTreeNodeStream(tree);
     nodes.setTokenStream(tokens);
    Treegrammar walker = new Treegrammar(nodes);
     walker.setTemplateLib(templates);
  
     Treegrammar.packageDeclaration_return r2= walker.packageDeclaration() ;
       System.out.println(tokens.toString());
       
    }

 

 

and in the tree grammar Treegrammar;

 

importDeclaration returns [StringTemplate code]
    :   ^(IMPORT_DECL a=importDeclarationTypeElement)
      {if($a.text.equals("org"))
        {StringTemplate code=null;
         code=template("assgin");
         code.setAttribute("x",a);
        } 
    ;  

 

 

There are no error but it does not work.

 

Any idea?

 

 

Thanks in advance.

 

 

Annie

_________________________________________________________________
怎样买车票更方便?微软春节搜索,第一时间为您提供火车票信息!
http://piao.live.com/?form=PANER01 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090225/eeb681d4/attachment.html 


More information about the antlr-interest mailing list