Range function for stringTemplate

22 views
Skip to first unread message

Carter Smith

unread,
Aug 23, 2024, 8:53:47 AM8/23/24
to stringtemplate-discussion

Hi all, 

 

I'm designing a templating plugin that uses stringTemplate4. Currently if I want to template in a number value into a template, I need to specify each of the number in the data file. For example, if I wanted to generate the following code:

String method0 = differentMethod0();

String method1 = differentMethod1();

String method2 = differentMethod2();

String method3 = differentMethod3();


 I would need to create a data file (yaml) with a list like this:

toggles:

- 0

- 1

- 2

- 3

I could use a template with a loop like this:

template(data) :: <<

%data.toggles:[toggle|

  String method%toggle% = differentMethod%toggle%();

>> 


But what if I wanted to generate 200 methods from 1-200. It would be best to not have to make an extremely long yaml file that goes all the way from 1 to 200. 

I am asking if it possible to implement a range function into stringTemplate4 (where range is like the Python function range() == inclusive 0..3). This function would look something like this:

template(data) :: <<

 %range(%data.num_of_toggles):[toggle|    

   String method%toggle% = differentMethod%toggle%();


And then the data file would look like:

num_of_toggles: 4

 

This would produce the following:

String method0 = differentMethod0();

String method1 = differentMethod1();

String method2 = differentMethod2();

String method3 = differentMethod3();

 

The range function would allow the user to specify the end of a range value and then the template is able to loop through from 0 to the end of the range value.

Is this something that is possible to implement? Let me know if you have any questions. 

Jim Idle

unread,
Aug 27, 2024, 12:08:56 PM8/27/24
to stringtempla...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "stringtemplate-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stringtemplate-dis...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/stringtemplate-discussion/2136416b-795c-4bf2-b6e3-0255d7e2261en%40googlegroups.com.

Martin d'Anjou

unread,
Aug 30, 2024, 10:24:55 AM8/30/24
to stringtemplate-discussion
I am not sure how the paper answers the question but it sounds like "for reasons explained in the paper, this should not be done by ST4". Care to elaborate? I concocted a small groovy example that shows the idea of the "range" function:

@Grab(group='org.antlr', module='ST4', version='4.3.4')
import org.stringtemplate.v4.*;
public class Hello {
    public static void main(String[] args) {
        // The following is the traditional way of doing a loop
        ST traditional = new ST("<items:{it| a_<it.id> = b[<it.id>]\n}>")
        traditional.addAggr("items.{ id }", 1)
        traditional.addAggr("items.{ id }", 2)
        traditional.addAggr("items.{ id }", 3)
        System.out.println(traditional.render())

        // The following does not work, but it represents the range feature
        ST range = new ST("<range(data):{value| a_<value> = b[<value>]}>")
        range.add("data", 3)
        System.out.println(range.render())
    }
}

Thanks,
Martin
Reply all
Reply to author
Forward
0 new messages