antlr / stringtemplate4

StringTemplate 4
http://www.stringtemplate.org
Other
951 stars 231 forks source link

You cannot use i as variable name #306

Closed t-oster closed 2 years ago

t-oster commented 2 years ago

This works (from org.stringtemplate.v4.test.TestCoreBasics):

@Test public void testProp() throws Exception {
        String template = "<u.id>: <u.name>"; // checks field and method getter
        ST st = new ST(template);
        st.add("u", new User(1, "parrt"));
        String expected = "1: parrt";
        String result = st.render();
        assertEquals(expected, result);
    }

this does not:

@Test public void testProp() throws Exception {
        String template = "<i.id>: <i.name>"; // checks field and method getter
        ST st = new ST(template);
        st.add("i", new User(1, "parrt"));
        String expected = "1: parrt";
        String result = st.render();
        assertEquals(expected, result);
    }

all characters except i work.

parrt commented 2 years ago

hi! I believe i is a reserved word used for iteration with template apply operations. See https://github.com/antlr/stringtemplate4/blob/master/doc/cheatsheet.md

t-oster commented 2 years ago

would you accept a PR, which would change st.add(x, y) to throw an IllegalArgumentException when called with x in [true, false, import, default, key, group, implements, first, last, rest, trunc, strip, trim, length, strlen, reverse, if, else, elseif, endif, , i], because it took me a while to figure out that the variable name was the problem (and i is not listed in the reserved words section in the cheatsheet)

parrt commented 2 years ago

I'm trying to think if that could cause any trouble. Seems like the better way would be to look for improper keyword use as a variable.. hmm... maybe not. Is there a list of reserved words already in the code?