[stringtemplate-interest] Using the "format" option with a template calls
Udo Borkowski
ub at abego-software.de
Fri Jan 28 16:48:49 PST 2011
Hi Ter,
> I think that this is actually correct. <(t())> should evaluate immediately, which means it evaluates outside of the context of the outer expression ...
I agree.
> … so I cannot see the indentation.
This I don't understand.
For me "the outer expression" is the invocation in the body of the main template. We agree this does not affect the outcome of "<(t())>". BTW: the invocation is not indented in main().
So "<(t())>" is evaluated in the context of t. As you will recall, t is defined as
t() ::= <<
abc
>>
And here we have the indentation, that is part of the definition of t. Maybe this is more obvious when I write the template definition in this equivalent way:
t() ::= " abc"
For me <(t())> mean "evaluate the template t()" (to a string). And evaluating " abc" to a string results in " abc" (with the spaces).
Could you please have another look at the issue? I think this could be a quite important detail we should have a clear understanding of.
Udo
P.S.: If it is not too much trouble, could you please explain why you are using the NoIndentWriter in Interpreter#toString(ST self, Object value) in the first place? As the NoIndentWriter is only used in this one method it was probably created just for this case. But I don't see why we need this. Could you give me an example when the AutoIndentWriter would not work?
On 28.01.2011, at 22:31, Terence Parr wrote:
> Hi Udo, i looked again at your example:
>
> main() ::= <<
> <t()>
> <t();format="upper">
> <(t());format="upper">
> <t().render;format="upper">
>>>
> -----------------
> Rendering main will produce this output:
> -----------------
> abc
> abc
> ABC
> ABC
> -----------------
> 3rd line is the (t()) case, 4th line produced by my ST ObjectAdapter.
>
> I think that this is actually correct. <(t())> should evaluate immediately, which means it evaluates outside of the context of the outer expression so I cannot see the indentation. It's different than:
>
> <t();format="upper">
>
> Ter
>
> On Jan 27, 2011, at 2:17 AM, Udo Borkowski wrote:
>
>> I checked the toString method of the Interpreter (called by the tostr instruction) and noticed this code:
>>
>> writeObjectNoOptions(new NoIndentWriter(sw), self, value);
>>
>> I.e. the indentation is discarded "by intend" (by using the NoIndentWriter). I assume there is a good reason for this (Maybe you can give me a little hint or example?).
>>
>> However when writing the string representation of an ST object I think also writing the indentation is a good idea. Otherwise you loose all the nice formatting etc.
>>
>> So what about this replacement for the above line in Interpreter#toString(ST self, Object value):
>>
>> STWriter out = (value instanceof ST)
>> ? new AutoIndentWriter(sw) : new NoIndentWriter(sw);
>> writeObjectNoOptions(out, self, value);
>>
>> I.e. when writing an ST object use the AutoIndentWriter, otherwise the NoIndentWriter.
>>
>> With this little change
>>
>> <(t());format="upper">
>>
>> now writes out the content of the template t in upper case, perfectly indented (as intended ;-).
>>
>>
>> Udo
>>
>>
>>
>> On 27.01.2011, at 01:49, Terence Parr wrote:
>>
>>> Actually, do you have an STRenderer? t() yields an ST not a string.
>>> Ter
>>> On Jan 25, 2011, at 4:07 PM, Udo Borkowski wrote:
>>>
>>>>> Hi. does <(t()); format="upper"> work?
>>>>
>>>> Nearly. It seems it eats (leading) white spaces.
>>>>
>>>> Here my little example group:
>>>> -------------------
>>>> t() ::= <<
>>>> abc
>>>>>>
>>>>
>>>> main() ::= <<
>>>> «t()»
>>>> «t();format="upper"»
>>>> «(t());format="upper"»
>>>> «t().render;format="upper"»
>>>>>>
>>>> -----------------
>>>> Rendering main will produce this output:
>>>> -----------------
>>>> abc
>>>> abc
>>>> ABC
>>>> ABC
>>>> -----------------
>>>> 3rd line is the (t()) case, 4th line produced by my ST ObjectAdapter.
>>>>
>>>> Udo
>>>>
>>>> On 25.01.2011, at 20:45, Terence Parr wrote:
>>>>
>>>>> Hi. does <(t()); format="upper"> work?
>>>>> Ter
>>>>> On Jan 25, 2011, at 12:56 AM, Udo Borkowski wrote:
>>>>>
>>>>>> While the "format" option work fine with attributes, like in
>>>>>>
>>>>>> <attr;format="upper">
>>>>>>
>>>>>> it does not seem to work when used together with template invocations, like:
>>>>>>
>>>>>> <t();format="upper">
>>>>>>
>>>>>> I don't know if this is intended (e.g. because of performance reasons) or a bug. I personally would like to have the format option also work in the latter situation. E.g. when I "refactor" my templates it happens data once stored in an attribute moves into a template. This was not possible when the "format" option is used.
>>>>>>
>>>>>>
>>>>>> I am currently working around this by using a custom ObjectModelAdapter for ST, using this code:
>>>>>>
>>>>>> public Object getProperty(ST self, Object o, Object property,
>>>>>> String propertyName) throws STNoSuchPropertyException {
>>>>>> if ("render".equals(propertyName) && (o instanceof ST)) {
>>>>>> return ((ST) o).render();
>>>>>> }
>>>>>> return super.getProperty(self, o, property, propertyName);
>>>>>> }
>>>>>>
>>>>>> This way the above example looks like this:
>>>>>>
>>>>>> <t().render;format="upper">
>>>>>>
>>>>>> While this works fine I am still wondering if the "format" options should also work for the simple template call, too.
>>>>>>
>>>>>> What do you think?
>>>>>>
>>>>>> Udo
>>>>>>
>>>>>> _______________________________________________
>>>>>> stringtemplate-interest mailing list
>>>>>> stringtemplate-interest at antlr.org
>>>>>> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>>>>>
>>>>> _______________________________________________
>>>>> stringtemplate-interest mailing list
>>>>> stringtemplate-interest at antlr.org
>>>>> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>>>>
>>>
>>> _______________________________________________
>>> stringtemplate-interest mailing list
>>> stringtemplate-interest at antlr.org
>>> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
>>
>
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org/mailman/listinfo/stringtemplate-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110129/6b747f08/attachment-0001.html
More information about the stringtemplate-interest
mailing list