diff --git a/tool/src/org/antlr/v4/tool/Grammar.java b/tool/src/org/antlr/v4/tool/Grammar.java index ef8b52ba0..6fc288071 100644 --- a/tool/src/org/antlr/v4/tool/Grammar.java +++ b/tool/src/org/antlr/v4/tool/Grammar.java @@ -767,6 +767,16 @@ public class Grammar implements AttributeResolver { // try with action in there isLitRule = wiz.parse(r, "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL ACTION)))", nodes); + if ( isLitRule ) { + GrammarAST litNode = (GrammarAST)nodes.get("lit"); + GrammarAST nameNode = (GrammarAST)nodes.get("name"); + lexerRuleToStringLiteral.put(litNode.getText(), nameNode.getText()); + continue; + } + nodes = new HashMap(); + // try with pred in there + isLitRule = + wiz.parse(r, "(RULE %name:TOKEN_REF (BLOCK (ALT %lit:STRING_LITERAL SEMPRED)))", nodes); if ( isLitRule ) { GrammarAST litNode = (GrammarAST)nodes.get("lit"); GrammarAST nameNode = (GrammarAST)nodes.get("name"); diff --git a/tool/test/org/antlr/v4/test/TestTokenTypeAssignment.java b/tool/test/org/antlr/v4/test/TestTokenTypeAssignment.java index b3add4d38..33ff3f1ad 100644 --- a/tool/test/org/antlr/v4/test/TestTokenTypeAssignment.java +++ b/tool/test/org/antlr/v4/test/TestTokenTypeAssignment.java @@ -98,6 +98,21 @@ public class TestTokenTypeAssignment extends BaseTest { assertEquals("[E, 'x']", tokens.toString()); } + @Test public void testPredDoesNotHideNameToLiteralMapInLexer() throws Exception { + // 'x' is token and char in lexer rule + Grammar g = new Grammar( + "grammar t;\n" + + "a : 'x' X ; \n" + + "X: 'x' {true}?;\n"); // must match as alias even with pred + + assertEquals("{'x'=1}", g.stringLiteralToTypeMap.toString()); + assertEquals("{EOF=-1, X=1}", g.tokenNameToTypeMap.toString()); + + // pushed in lexer from parser + assertEquals("{'x'=1}", g.implicitLexer.stringLiteralToTypeMap.toString()); + assertEquals("{EOF=-1, X=1}", g.implicitLexer.tokenNameToTypeMap.toString()); + } + @Test public void testCombinedGrammarWithRefToLiteralButNoTokenIDRef() throws Exception { Grammar g = new Grammar( "grammar t;\n"+