[stringtemplate-interest] Region Usage
Udo Borkowski
ub at abego-software.de
Thu Feb 3 09:34:21 PST 2011
If you want to switch of the region programmatically you could add an "if" block around the preamble:
--------------
/* test1.stg */
method(name,code,withPreamble) ::= <<
public void <name>() {
<if(withPreamble)>
<@preamble()>
<endif>
<code>
}
>>
--------------
test2.stg
--------------
import "test1.stg"
@method.preamble() ::= <<System.out.println("enter");>>
main(p) ::= <<
<method(name="sample",code="beep();",withPreamble=p)>
>>
--------------
When main is called with p == false, no preamble code is written.
Udo
On 02.02.2011, at 23:42, Massimiliano Donini wrote:
> Tanks for your replu Udo, it helps me to understand bette rregion from the StringTemplate point of view, only one more thing: in question 2 i would understand how i can turn on and off from the client's code point of view (e.g from java or better C#).
> My request was ambiguos sorry, but how can i achieve same result from code assuming that i have one template in test1.stg that refences a region defined in test2.stg?
>
> e.g.
> /* test1.stg */
>
> method(name,code) ::= <<
> public void <name>() {
> <@preamble()>
> <code>
> }
> >>
>
> /* test2.stg */
>
> @method.preamble() ::= <<System.out.println("enter");>>
>
>
> Best Regards
> Max
>
> Il 02/02/2011 23:01, Udo Borkowski ha scritto:
>>
>> I can only talk about StringTemplate 4.0, but I assume it is same with ST 3:
>>
>>> 1) can i define region inside the same templategroup file?
>>
>> Yes. Rendering this group's main() template:
>> -----
>> /* RegionTest.stg */
>>
>> method(name,code) ::= <<
>> public void <name>() {
>> <@preamble()>
>> <code>
>> }
>> >>
>>
>> @method.preamble() ::= <<System.out.println("enter");>>
>>
>> main() ::= <<
>> <method(name="sample",code="beep();")>
>> >>
>> --------
>> will output:
>> --------
>> public void sample() {
>> System.out.println("enter");
>> beep();
>> }
>> --------
>>
>>> 2) how can i turn on/off region template (starting from 1° wiki region example, how could i generate code with <@preamble()> and without it?)
>> If you don't define the region referenced by <@preamble()> nothing will be generated. E.g. back to the above example: without the @method.preamble()… stuff, i.e. with this group:
>> -------------
>> /* RegionTest2.stg */
>>
>> method(name,code) ::= <<
>> public void <name>() {
>> <@preamble()>
>> <code>
>> }
>> >>
>>
>> main() ::= <<
>> <method(name="sample",code="beep();")>
>> >>
>> -------------
>> the output will be
>> -------------
>> public void sample() {
>> beep();
>> }
>> -------------
>>
>>
>> You may now create a new group and import the above group:
>> ------------
>> import "RegionTest2.stg"
>>
>> main() ::= <<
>> <method(name="makeNoise",code="beep();")>
>> >>
>> ------------
>> to use the method template from the "RegionTest" group. Here we get the following result:
>> ------------
>> public void makeNoise() {
>> beep();
>> }
>> -------------
>>
>> And in this new group you may define the region, too.
>> ------------
>> import "RegionTest2.stg"
>>
>> @method.preamble() ::= <<log("enter");>>
>>
>> main() ::= <<
>> <method(name="makeNoise",code="beep();")>
>> >>
>> ------------
>> resulting in this output:
>> ------------
>> public void makeNoise() {
>> log("enter");
>> beep();
>> }
>> -------------
>>
>>
>> Hope this helps,
>>
>> Udo
>>
>>
>>
>>
>> On 02.02.2011, at 19:59, Massimiliano Donini wrote:
>>
>>> Hi, i have read following on link http://www.antlr.org/wiki/display/ST/Template+regions region usage but i'm unable to find code usage.
>>> I have 2 simple questions:
>>> 1) can i define region inside the same templategroup file?
>>> 2) how can i turn on/off region template (starting from 1° wiki region example, how could i generate code with <@preamble()> and without it?)
>>>
>>> Best Regards
>>> Max
>>> _______________________________________________
>>> 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/20110203/d503000e/attachment.html
More information about the stringtemplate-interest
mailing list