From c35f5ec40c33416d65c0e0cb0ce29dc39649fa34 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Wed, 6 Mar 2013 13:13:33 -0600 Subject: [PATCH] Do not require escape for $ in action when not followed by an ID start char (fixes #176) --- tool/src/org/antlr/v4/parse/ActionSplitter.g | 7 ++++++- .../org/antlr/v4/test/TestActionTranslation.java | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/tool/src/org/antlr/v4/parse/ActionSplitter.g b/tool/src/org/antlr/v4/parse/ActionSplitter.g index c5ab8ad4f..99ed437f5 100644 --- a/tool/src/org/antlr/v4/parse/ActionSplitter.g +++ b/tool/src/org/antlr/v4/parse/ActionSplitter.g @@ -56,6 +56,10 @@ public List 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('$');} )+ ; diff --git a/tool/test/org/antlr/v4/test/TestActionTranslation.java b/tool/test/org/antlr/v4/test/TestActionTranslation.java index c10f6d786..03a5d063f 100644 --- a/tool/test/org/antlr/v4/test/TestActionTranslation.java +++ b/tool/test/org/antlr/v4/test/TestActionTranslation.java @@ -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';";