4

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?

Klaus Schulz
  • 527
  • 1
  • 5
  • 20

2 Answers2

5

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; }
Andy Stabler
  • 1,309
  • 1
  • 15
  • 19
  • 3
    Nice workaround! I found another in the meantime but yours is more elegant. I added a variable $closingcurlybraclet$ at the end of the statement and defined it in my generator with template.add("closingCurlyBraclet", "}"); – Klaus Schulz Nov 10 '14 at 10:24
0

Backslash (\) worked for me:

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

It seems it is not necessary to escape the opening curly bracket.

yannick1976
  • 10,171
  • 2
  • 19
  • 27