Search code examples
stringtemplatestringtemplate-4

curly braclet in a StringTemplate


The following StringTemplate gives me an "invalid character '}'" exception because of the closing curly brace after return null;:

$StatementList:{statement | 
public T $statement$(X x) { return null; }  }$

I want to have an output like:

public T statement1(X x) {return null; }
public T statement2(X x) {return null; }

How can I escape this closing curly brace?


Solution

  • I couldn't find a way of escaping characters, but I did manage to get it to work using the unicode character for curly braces instead.

    statementTemplate(StatementList) ::= <<
    <StatementList:{statement | 
    public T <statement>(X x) <\u007B> return null; <\u007D> }>
    >>
    

    which produced:

    public T statement1(X x) { return null; } 
    public T statement2(X x) { return null; } 
    public T statement3(X x) { return null; }