[antlr-interest] ANTLR StringTemplate interface.

rkevinburton at charter.net rkevinburton at charter.net
Thu Jul 31 16:04:14 PDT 2008


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 at 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



More information about the antlr-interest mailing list