[antlr-interest] StringTemplate and newlines
Terence Parr
parrt at cs.usfca.edu
Sat Nov 20 09:52:10 PST 2004
On Nov 20, 2004, at 1:57 AM, Joan Pujol wrote:
> 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.
I suspect that my 2.1 beta will make you happy ;)
> 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>
> ...
>>>
Hmm...it seems like I need another rule that says something if entire
line is an expression that does not evaluate to anything, then skip the
line. This like:
<foo>
<bar>
will get you two blank links if you don't have values there.
Hmm...might be hard to detect for the <if> stuff. I'm adding to list.
> 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
Agreed; i'm working hard on 2.1 to make it be better about newlines.
> 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.
It would take me a while to figure precisely your problem here. It
seems like a similar problem as above: an expression on a line by
itself that generates no output gets a newline. Is that correct?
Ter
--
CS Professor & Grad Director, University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org
Cofounder, http://www.jguru.com
Cofounder, http://www.knowspam.net enjoy email again!
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