[prev in list] [next in list] [prev in thread] [next in thread] 

List:       antlr-interest
Subject:    Re: [antlr-interest] ANTLR StringTemplate interface.
From:       <rkevinburton () charter ! net>
Date:       2008-07-31 23:04:14
Message-ID: 20080731190414.31IUK.213333.root () fepweb09
[Download RAW message or body]

If I change the rules as follows:

functionDeclaration
	: ^( FUNCTION id=Identifier? LPAREN (fa+=functionArguments)* RPAREN block )
          -> template(name={$id}, args={$fa})
          "<name>(<args; separator=\", \">)"
	;

functionArguments
	: ^( ARGS name=Identifier )
          -> template(argument={$name}) "<argument>"
	| ^( ARGS name=Identifier TYPES type=Identifier )
          -> template(argument={$name}, type={$type}) "<argument> <type>"
	;


I stepped into the generated code using the debugger and this seems to generate a \
"template" that is what I need. I am not sure that I totally understand why this \
                seems to work. Now how do I output or access the generated (filled \
                in) template?
---- rkevinburton@charter.net wrote: 
> I am really new to ANTLR and so am even newer to StingTemplates. I decided to cut \
> and paste my tree grammar to use templates. Terrance says that StingTemplates are \
> better than print statements so I thought I would try. 
> Needless to say it didn't work. The generated files from the grammar would not \
> compile. 
> Here is the opening of my template grammar:
> 
> tree grammar ECMAScriptTemplate ;
> 
> options
> {
> 	output = template;
> 	tokenVocab = ECMAScript ;
> 	language = CSharp2 ;
> }
> 
> And the template rewrite rultes that I am dealing with right now look like:
> 
> functionDeclaration
> 	: ^( FUNCTION Identifier? LPAREN (fa+=functionArguments)* RPAREN block )
> -> template(name={$Identifier.text}, args={$fa.st})
> "<name>(<args; separator=\", \">)"
> 	;
> 
> functionArguments
> 	: ^( ARGS name=Identifier )
> -> template(argument={$name}) "<argument>"
> 	| ^( ARGS name=Identifier TYPES type=Identifier )
> -> template(argument={$name}, type={$type}) "<argument> <type>"
> 	;
> 
> Basically thiis grammar parses a JavaScript input with one modification. I have \
> added suggested types to the arugment list. The tree walker and parser both are \
> very happy right now. I am trying to get the StringTemplate to work. The above \
> should recognize 'function a(b,c)' as well as 'function a(b:B,c)' where B is a \
> type. Naturally I would like to build a template that "fills" in the argument list \
> with types or not. As it sits now when code generated with the above statements I \
> get: 
> 	// TEMPLATE REWRITE
> 	// 220:11: -> template(name=$Identifier.textargs=$fa.st) \"<name>(<args; \
> separator=\", \">)\"  {
> 	    retval.ST = new StringTemplate(templateLib, "<name>(<args; separator=\", \
> \">)",  new STAttrMap().Add("name", ((Identifier1 != null) ? Identifier1.Text : \
> null)).Add("args", list_fa.st));  }
> 
> 'object' does not contain a definition for 'Text' and no extension method 'Text' \
> accepting a first argument of type 'object' could be found (are you missing a using \
> directive or an assembly reference?) 
> Any ideas what is wrong?
> 
> Thank you.
> 
> Kevin


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic