[stringtemplate-interest] Using the "format" option with a template calls

Udo Borkowski ub at abego-software.de
Thu Jan 27 02:17:04 PST 2011


> Actually, do you have an STRenderer?  
No.

> t() yields an ST not a string.
I know. That's why I implemented an ModelObjectAdaptor for ST.class to call the "render()" method.

As I said <(t());format="upper"> nearly does the job, just the indentation is discarded. 

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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/stringtemplate-interest/attachments/20110127/651ff473/attachment-0001.html 


More information about the stringtemplate-interest mailing list