antlr / stringtemplate4

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

Can't reference variable in dictionary #161

Closed suemi994 closed 8 years ago

suemi994 commented 8 years ago

When I'm writing a String Template Group file, I found that reference a variable in dictionary value string won't lead to the variable, but to the raw string. An example in your book is following.

javaToSQLValueMap ::= [
    "Date":"new java.sql.Date(<val>.getTime())",
    default : "<val>"
]
fieldValue(type,val="o")::="<javaToSQLValueMap.(type.simpleName)>"

When I use fieldValue , this is the result:

prep.setDate(3+1,new java.sql.Date(<val>.getTime()));

If the variable is not visible in this scope, then I will get

prep.setDate(3+1,new java.sql.Date(.getTime()));

But it'not, so I think the cause is your definition of STRING token in lexer.

sharwell commented 8 years ago

What happens if you use an anonymous template instead of a string for your dictionary values?

javaToSQLValueMap ::= [
    "Date": {new java.sql.Date(<val>.getTime())},
    default : {<val>}
]
fieldValue(type,val="o")::="<javaToSQLValueMap.(type.simpleName)>"

:memo: Looking at the code, it appears you can use any of the forms << ... >>, <% ... %>, or { ... } to have your code treated as a template instead of as a raw string.

suemi994 commented 8 years ago

Thank you for your help, it works now !