forked from jasder/antlr
Do not require escape for $ in action when not followed by an ID start char (fixes #176)
This commit is contained in:
parent
56c053a5be
commit
c35f5ec40c
|
@ -56,6 +56,10 @@ public List<Token> getActionTokens() {
|
||||||
}
|
}
|
||||||
return chunks;
|
return chunks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isIDStartChar(int c) {
|
||||||
|
return c == '_' || Character.isLetter(c);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ignore comments right away
|
// ignore comments right away
|
||||||
|
@ -106,8 +110,9 @@ TEXT
|
||||||
@init {StringBuilder buf = new StringBuilder();}
|
@init {StringBuilder buf = new StringBuilder();}
|
||||||
@after {delegate.text(buf.toString());}
|
@after {delegate.text(buf.toString());}
|
||||||
: ( c=~('\\'| '$') {buf.append((char)$c);}
|
: ( c=~('\\'| '$') {buf.append((char)$c);}
|
||||||
| '\\$' {buf.append("$");}
|
| '\\$' {buf.append('$');}
|
||||||
| '\\' c=~('$') {buf.append('\\').append((char)$c);}
|
| '\\' c=~('$') {buf.append('\\').append((char)$c);}
|
||||||
|
| {!isIDStartChar(input.LA(2))}? => '$' {buf.append('$');}
|
||||||
)+
|
)+
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
|
@ -73,6 +73,20 @@ public class TestActionTranslation extends BaseTest {
|
||||||
testActions(attributeTemplate, "inline2", action, expected);
|
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 {
|
@Test public void testEscapedSlash() throws Exception {
|
||||||
String action = "x = '\\n';"; // x = '\n'; -> x = '\n';
|
String action = "x = '\\n';"; // x = '\n'; -> x = '\n';
|
||||||
String expected = "x = '\\n';";
|
String expected = "x = '\\n';";
|
||||||
|
|
Loading…
Reference in New Issue