Do not require escape for $ in action when not followed by an ID start char (fixes #176)

This commit is contained in:
Sam Harwell 2013-03-06 13:13:33 -06:00
parent 56c053a5be
commit c35f5ec40c
2 changed files with 20 additions and 1 deletions

View File

@ -56,6 +56,10 @@ public List<Token> getActionTokens() {
}
return chunks;
}
private boolean isIDStartChar(int c) {
return c == '_' || Character.isLetter(c);
}
}
// ignore comments right away
@ -106,8 +110,9 @@ TEXT
@init {StringBuilder buf = new StringBuilder();}
@after {delegate.text(buf.toString());}
: ( c=~('\\'| '$') {buf.append((char)$c);}
| '\\$' {buf.append("$");}
| '\\$' {buf.append('$');}
| '\\' c=~('$') {buf.append('\\').append((char)$c);}
| {!isIDStartChar(input.LA(2))}? => '$' {buf.append('$');}
)+
;

View File

@ -73,6 +73,20 @@ public class TestActionTranslation extends BaseTest {
testActions(attributeTemplate, "inline2", action, expected);
}
/**
* Regression test for "in antlr v4 lexer, $ translation issue in action".
* https://github.com/antlr/antlr4/issues/176
*/
@Test public void testUnescaped$InAction() throws Exception {
String action = "\\$string$";
String expected = "$string$";
testActions(attributeTemplate, "members", action, expected);
testActions(attributeTemplate, "init", action, expected);
testActions(attributeTemplate, "inline", action, expected);
testActions(attributeTemplate, "finally", action, expected);
testActions(attributeTemplate, "inline2", action, expected);
}
@Test public void testEscapedSlash() throws Exception {
String action = "x = '\\n';"; // x = '\n'; -> x = '\n';
String expected = "x = '\\n';";