[antlr-interest] StringTemplate and newlines

Joan Pujol joanpujol at gmail.com
Sat Nov 20 01:57:33 PST 2004


Hi,

I'm using StringTemplate to generate Java code from my compiler (that
also translates code).
And I'm very happy with it. But I have some problems with newlines
that I don't know how to resolve.


Problem 1 (The easy one)

My grammar is like

algorithm
  const
    ...
    ...
   fconst
   var
    ...
   fvar
...

where const ... fconst is optional.

Then my template for algorithm is:

algorithm(const,var...)::= <<
   class Algorithm {
     ....
    <if(const)><const:{<it.type> <it.name>;};separator="\n"><endif>
  ...
>>

The problem is that If I haven't constants then I get a newline, if I
haven't vars another, and etc. I can solve it putting all in a single
line. But then the grammar isn't readable


Problem 2 (The hard one)

My language have input/output parameters for methods and basic types.
Then to translate this to java I do something like:

f ( output integer i) ->  void f(TypeEncapsulator _i)
{
  int i=_i.deencapsulateInt();
  ...
  _i.encapsulateInt();
}

And for expression that calls a method that is where I have te problem:
I want to call f(i)  // where i is output parameter
{ TypeEncapsulator _i=new TypeEncapsulator(i);
   f(_i);
   i=_i.deencapsulateInt();
}


Well I tested two templates with function calls. To do the things more
easy, I have one template with functioncalls that have at least one
output parameter, and other for normal ones (without any output
parameter)

Template 1:
outputfunctioncall(name,parameter)::=  <<
{ <if(parameter.isoutput)> <parameter:{TypeEncapsulator _<it.name>=
.... };separator="\n"> <endif>  //The encapsulator

<name>(<parameter:{...  //The function call   

   <if(parameter.isoutput)> <parameter: ...//The deencapsulator


This works well when all the parameters are output ones. But when some
method only has some output parameters, and the last parameter is an
input one. All the info that is inside <if(parameter.isoutput)> is
lost.

The Template 2 used to solve this is to use:
<parameter:{<if(it.isoutput)> TypeEncapsulator _....};separator="\n">...
...

Then it works, but then the problem is that for each non output
parameter I get a new line.

A lot of thanks in advance.
  
-- 
Joan Jesús Pujol Espinar


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 





More information about the antlr-interest mailing list