From c32b703555b422afa519dff43dc40d67b342cde8 Mon Sep 17 00:00:00 2001 From: parrt Date: Sun, 11 Dec 2011 10:37:27 -0800 Subject: [PATCH] quote literal was not translated properly. updated Lexer to convert antlr literals to target literals properly. added unit test. [git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9557] --- .../v4/tool/templates/codegen/Java/Java.stg | 2 +- .../src/org/antlr/v4/codegen/model/Lexer.java | 20 +++++++++- .../test/org/antlr/v4/test/TestLexerExec.java | 39 ++++++++++++------- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg index ece6e0980..655efa2fa 100644 --- a/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg +++ b/tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg @@ -745,7 +745,7 @@ public class extends Lexer { public static final String[] tokenNames = { "\", "\", "\", - "}; separator=", ", wrap, anchor> + }; separator=", ", wrap, anchor> }; public static final String[] ruleNames = { "}; separator=", ", wrap, anchor> diff --git a/tool/src/org/antlr/v4/codegen/model/Lexer.java b/tool/src/org/antlr/v4/codegen/model/Lexer.java index 9ea1f8c4d..8be47b302 100644 --- a/tool/src/org/antlr/v4/codegen/model/Lexer.java +++ b/tool/src/org/antlr/v4/codegen/model/Lexer.java @@ -29,10 +29,16 @@ package org.antlr.v4.codegen.model; +import org.antlr.v4.codegen.CodeGenerator; import org.antlr.v4.codegen.OutputModelFactory; -import org.antlr.v4.tool.*; +import org.antlr.v4.tool.Grammar; +import org.antlr.v4.tool.LexerGrammar; +import org.antlr.v4.tool.Rule; -import java.util.*; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; public class Lexer extends OutputModelObject { public String name; @@ -64,6 +70,16 @@ public class Lexer extends OutputModelObject { } tokenNames = g.getTokenDisplayNames(); + for (int i = 0; i < tokenNames.length; i++) { + if ( tokenNames[i]==null ) continue; + CodeGenerator gen = factory.getGenerator(); + if ( tokenNames[i].charAt(0)=='\'' ) { + tokenNames[i] = gen.target.getTargetStringLiteralFromANTLRStringLiteral(gen, tokenNames[i]); + } + else { + tokenNames[i] = gen.target.getTargetStringLiteralFromString(tokenNames[i], true); + } + } ruleNames = g.rules.keySet(); } diff --git a/tool/test/org/antlr/v4/test/TestLexerExec.java b/tool/test/org/antlr/v4/test/TestLexerExec.java index 86d187a03..21316c554 100644 --- a/tool/test/org/antlr/v4/test/TestLexerExec.java +++ b/tool/test/org/antlr/v4/test/TestLexerExec.java @@ -3,20 +3,31 @@ package org.antlr.v4.test; import org.junit.Test; public class TestLexerExec extends BaseTest { - @Test public void testRefToRuleDoesNotSetTokenNorEmitAnother() throws Exception { - String grammar = - "lexer grammar L;\n"+ - "A : '-' I ;\n" + - "I : '0'..'9'+ ;\n"+ - "WS : (' '|'\\n') {skip();} ;"; - String found = execLexer("L.g", grammar, "L", "34 -21 3"); - String expecting = - "[@0,0:1='34',<4>,1:0]\n" + - "[@1,3:5='-21',<3>,1:3]\n" + - "[@2,7:7='3',<4>,1:7]\n" + - "[@3,8:7='',<-1>,1:8]\n"; // EOF has no length so range is 8:7 not 8:8 - assertEquals(expecting, found); - } + @Test public void testQuoteTranslation() throws Exception { + String grammar = + "lexer grammar L;\n"+ + "QUOTE : '\"' ;\n"; // make sure this compiles + String found = execLexer("L.g", grammar, "L", "\""); + String expecting = + "[@0,0:0='\"',<3>,1:0]\n" + + "[@1,1:0='',<-1>,1:1]\n"; + assertEquals(expecting, found); + } + + @Test public void testRefToRuleDoesNotSetTokenNorEmitAnother() throws Exception { + String grammar = + "lexer grammar L;\n"+ + "A : '-' I ;\n" + + "I : '0'..'9'+ ;\n"+ + "WS : (' '|'\\n') {skip();} ;"; + String found = execLexer("L.g", grammar, "L", "34 -21 3"); + String expecting = + "[@0,0:1='34',<4>,1:0]\n" + + "[@1,3:5='-21',<3>,1:3]\n" + + "[@2,7:7='3',<4>,1:7]\n" + + "[@3,8:7='',<-1>,1:8]\n"; // EOF has no length so range is 8:7 not 8:8 + assertEquals(expecting, found); + } @Test public void testActionExecutedInDFA() throws Exception { String grammar =