From 86d65cf7194cd43db55298a4707b38bac4603ea6 Mon Sep 17 00:00:00 2001 From: Peter Boyer Date: Sat, 16 Jan 2016 17:11:00 -0500 Subject: [PATCH] Fix lexer test failures --- .../antlr/v4/test/runtime/go/BaseTest.java | 8 ---- .../v4/test/runtime/go/TestLeftRecursion.java | 40 +++++++++---------- .../v4/test/runtime/go/TestParseTrees.java | 4 +- .../v4/test/runtime/go/TestParserExec.java | 3 +- runtime/Go/src/antlr4/Parser.go | 8 +++- runtime/Go/src/antlr4/Token.go | 6 +-- 6 files changed, 33 insertions(+), 36 deletions(-) diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseTest.java index de0c414de..40670f3b3 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/BaseTest.java @@ -341,14 +341,6 @@ public abstract class BaseTest { boolean defaultListener, String... extraOptions) { ErrorQueue equeue = antlr(grammarFileName, grammarFileName, grammarStr, defaultListener, extraOptions); - if (!equeue.errors.isEmpty()) { - System.err.println("PRINTING ERROR QUEUE"); - - System.err.println("++++++++++++++++++++++++++++++++++++++++++++++"); - System.err.println(equeue.toString()); - System.err.println("++++++++++++++++++++++++++++++++++++++++++++++"); - return false; - } // List files = new ArrayList(); // if (lexerName != null) { diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/TestLeftRecursion.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/TestLeftRecursion.java index 23a9668fb..8d9c20987 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/TestLeftRecursion.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/TestLeftRecursion.java @@ -1769,16 +1769,16 @@ public class TestLeftRecursion extends BaseTest { @Test public void testMultipleAlternativesWithCommonLabel_1() throws Exception { mkdir(parserpkgdir); - StringBuilder grammarBuilder = new StringBuilder(720); + StringBuilder grammarBuilder = new StringBuilder(576); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("s : e {fmt.Println($e.v)};\n"); grammarBuilder.append("e returns [int v]\n"); - grammarBuilder.append(" : e '*' e {$v = ((BinaryContext)$ctx).e(0).v * ((BinaryContext)$ctx).e(1).v;} # binary\n"); - grammarBuilder.append(" | e '+' e {$v = ((BinaryContext)$ctx).e(0).v + ((BinaryContext)$ctx).e(1).v;} # binary\n"); + grammarBuilder.append(" : e '*' e {$v = ($ctx).e(0).v * ($ctx).e(1).v;} # binary\n"); + grammarBuilder.append(" | e '+' e {$v = ($ctx).e(0).v + ($ctx).e(1).v;} # binary\n"); grammarBuilder.append(" | INT {$v = $INT.int;} # anInt\n"); grammarBuilder.append(" | '(' e ')' {$v = $e.v;} # parens\n"); - grammarBuilder.append(" | left=e INC {assert(((UnaryContext)$ctx).INC() != null)$v = $left.v + 1;} # unary\n"); - grammarBuilder.append(" | left=e DEC {assert(((UnaryContext)$ctx).DEC() != null)$v = $left.v - 1;} # unary\n"); + grammarBuilder.append(" | left=e INC {$v = $left.v + 1;} # unary\n"); + grammarBuilder.append(" | left=e DEC {$v = $left.v - 1;} # unary\n"); grammarBuilder.append(" | ID {$v = 3;} # anID\n"); grammarBuilder.append(" ;\n"); grammarBuilder.append("ID : 'a'..'z'+ ;\n"); @@ -1799,16 +1799,16 @@ public class TestLeftRecursion extends BaseTest { @Test public void testMultipleAlternativesWithCommonLabel_2() throws Exception { mkdir(parserpkgdir); - StringBuilder grammarBuilder = new StringBuilder(720); + StringBuilder grammarBuilder = new StringBuilder(576); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("s : e {fmt.Println($e.v)};\n"); grammarBuilder.append("e returns [int v]\n"); - grammarBuilder.append(" : e '*' e {$v = ((BinaryContext)$ctx).e(0).v * ((BinaryContext)$ctx).e(1).v;} # binary\n"); - grammarBuilder.append(" | e '+' e {$v = ((BinaryContext)$ctx).e(0).v + ((BinaryContext)$ctx).e(1).v;} # binary\n"); + grammarBuilder.append(" : e '*' e {$v = ($ctx).e(0).v * ($ctx).e(1).v;} # binary\n"); + grammarBuilder.append(" | e '+' e {$v = ($ctx).e(0).v + ($ctx).e(1).v;} # binary\n"); grammarBuilder.append(" | INT {$v = $INT.int;} # anInt\n"); grammarBuilder.append(" | '(' e ')' {$v = $e.v;} # parens\n"); - grammarBuilder.append(" | left=e INC {assert(((UnaryContext)$ctx).INC() != null)$v = $left.v + 1;} # unary\n"); - grammarBuilder.append(" | left=e DEC {assert(((UnaryContext)$ctx).DEC() != null)$v = $left.v - 1;} # unary\n"); + grammarBuilder.append(" | left=e INC {$v = $left.v + 1;} # unary\n"); + grammarBuilder.append(" | left=e DEC {$v = $left.v - 1;} # unary\n"); grammarBuilder.append(" | ID {$v = 3;} # anID\n"); grammarBuilder.append(" ;\n"); grammarBuilder.append("ID : 'a'..'z'+ ;\n"); @@ -1829,16 +1829,16 @@ public class TestLeftRecursion extends BaseTest { @Test public void testMultipleAlternativesWithCommonLabel_3() throws Exception { mkdir(parserpkgdir); - StringBuilder grammarBuilder = new StringBuilder(720); + StringBuilder grammarBuilder = new StringBuilder(576); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("s : e {fmt.Println($e.v)};\n"); grammarBuilder.append("e returns [int v]\n"); - grammarBuilder.append(" : e '*' e {$v = ((BinaryContext)$ctx).e(0).v * ((BinaryContext)$ctx).e(1).v;} # binary\n"); - grammarBuilder.append(" | e '+' e {$v = ((BinaryContext)$ctx).e(0).v + ((BinaryContext)$ctx).e(1).v;} # binary\n"); + grammarBuilder.append(" : e '*' e {$v = ($ctx).e(0).v * ($ctx).e(1).v;} # binary\n"); + grammarBuilder.append(" | e '+' e {$v = ($ctx).e(0).v + ($ctx).e(1).v;} # binary\n"); grammarBuilder.append(" | INT {$v = $INT.int;} # anInt\n"); grammarBuilder.append(" | '(' e ')' {$v = $e.v;} # parens\n"); - grammarBuilder.append(" | left=e INC {assert(((UnaryContext)$ctx).INC() != null)$v = $left.v + 1;} # unary\n"); - grammarBuilder.append(" | left=e DEC {assert(((UnaryContext)$ctx).DEC() != null)$v = $left.v - 1;} # unary\n"); + grammarBuilder.append(" | left=e INC {$v = $left.v + 1;} # unary\n"); + grammarBuilder.append(" | left=e DEC {$v = $left.v - 1;} # unary\n"); grammarBuilder.append(" | ID {$v = 3;} # anID\n"); grammarBuilder.append(" ;\n"); grammarBuilder.append("ID : 'a'..'z'+ ;\n"); @@ -1859,16 +1859,16 @@ public class TestLeftRecursion extends BaseTest { @Test public void testMultipleAlternativesWithCommonLabel_4() throws Exception { mkdir(parserpkgdir); - StringBuilder grammarBuilder = new StringBuilder(720); + StringBuilder grammarBuilder = new StringBuilder(576); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("s : e {fmt.Println($e.v)};\n"); grammarBuilder.append("e returns [int v]\n"); - grammarBuilder.append(" : e '*' e {$v = ((BinaryContext)$ctx).e(0).v * ((BinaryContext)$ctx).e(1).v;} # binary\n"); - grammarBuilder.append(" | e '+' e {$v = ((BinaryContext)$ctx).e(0).v + ((BinaryContext)$ctx).e(1).v;} # binary\n"); + grammarBuilder.append(" : e '*' e {$v = ($ctx).e(0).v * ($ctx).e(1).v;} # binary\n"); + grammarBuilder.append(" | e '+' e {$v = ($ctx).e(0).v + ($ctx).e(1).v;} # binary\n"); grammarBuilder.append(" | INT {$v = $INT.int;} # anInt\n"); grammarBuilder.append(" | '(' e ')' {$v = $e.v;} # parens\n"); - grammarBuilder.append(" | left=e INC {assert(((UnaryContext)$ctx).INC() != null)$v = $left.v + 1;} # unary\n"); - grammarBuilder.append(" | left=e DEC {assert(((UnaryContext)$ctx).DEC() != null)$v = $left.v - 1;} # unary\n"); + grammarBuilder.append(" | left=e INC {$v = $left.v + 1;} # unary\n"); + grammarBuilder.append(" | left=e DEC {$v = $left.v - 1;} # unary\n"); grammarBuilder.append(" | ID {$v = 3;} # anID\n"); grammarBuilder.append(" ;\n"); grammarBuilder.append("ID : 'a'..'z'+ ;\n"); diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/TestParseTrees.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/TestParseTrees.java index 1591e070f..389c0f0b9 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/TestParseTrees.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/TestParseTrees.java @@ -200,7 +200,7 @@ public class TestParseTrees extends BaseTest { @Test public void testTokenAndRuleContextString() throws Exception { mkdir(parserpkgdir); - StringBuilder grammarBuilder = new StringBuilder(163); + StringBuilder grammarBuilder = new StringBuilder(166); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("s\n"); grammarBuilder.append("@init {\n"); @@ -211,7 +211,7 @@ public class TestParseTrees extends BaseTest { grammarBuilder.append("}\n"); grammarBuilder.append(" : r=a ;\n"); grammarBuilder.append("a : 'x' { \n"); - grammarBuilder.append("fmt.Println(p.GetRuleInvocationStack())\n"); + grammarBuilder.append("fmt.Println(p.GetRuleInvocationStack(nil))\n"); grammarBuilder.append("} ;"); String grammar = grammarBuilder.toString(); String input ="x"; diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/TestParserExec.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/TestParserExec.java index 41645dd93..887cd0d25 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/go/TestParserExec.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/go/TestParserExec.java @@ -441,11 +441,10 @@ public class TestParserExec extends BaseTest { @Test public void testListLabelForClosureContext() throws Exception { mkdir(parserpkgdir); - StringBuilder grammarBuilder = new StringBuilder(462); + StringBuilder grammarBuilder = new StringBuilder(420); grammarBuilder.append("grammar T;\n"); grammarBuilder.append("ifStatement\n"); grammarBuilder.append("@after {\n"); - grammarBuilder.append("List __ttt__ = $ctx.elseIfStatement();\n"); grammarBuilder.append("}\n"); grammarBuilder.append(" : 'if' expression\n"); grammarBuilder.append(" ( ( 'then'\n"); diff --git a/runtime/Go/src/antlr4/Parser.go b/runtime/Go/src/antlr4/Parser.go index e0c4fa4a9..a87cf4c8b 100644 --- a/runtime/Go/src/antlr4/Parser.go +++ b/runtime/Go/src/antlr4/Parser.go @@ -9,7 +9,7 @@ type Parser interface { Recognizer GetInterpreter() *ParserATNSimulator - GetErrorHandler() ErrorStrategy + GetTokenStream() TokenStream GetTokenFactory() TokenFactory GetParserRuleContext() ParserRuleContext @@ -17,6 +17,8 @@ type Parser interface { Consume() Token GetParseListeners() []ParseTreeListener + GetErrorHandler() ErrorStrategy + SetErrorHandler(ErrorStrategy) GetInputStream() IntStream GetCurrentToken() Token GetExpectedTokens() *IntervalSet @@ -108,6 +110,10 @@ func (p *BaseParser) GetErrorHandler() ErrorStrategy { return p._errHandler } +func (p *BaseParser) SetErrorHandler(e ErrorStrategy) { + p._errHandler = e +} + // Match current input symbol against {@code ttype}. If the symbol type // Matches, {@link ANTLRErrorStrategy//ReportMatch} and {@link //consume} are // called to complete the Match process. diff --git a/runtime/Go/src/antlr4/Token.go b/runtime/Go/src/antlr4/Token.go index 7c25b9404..f6b157780 100644 --- a/runtime/Go/src/antlr4/Token.go +++ b/runtime/Go/src/antlr4/Token.go @@ -187,9 +187,9 @@ func (this *CommonToken) SetText(text string) { func (this *CommonToken) String() string { var txt = this.GetText() if txt != "" { - txt = strings.Replace(txt, "\n", "", -1) - txt = strings.Replace(txt, "\r", "", -1) - txt = strings.Replace(txt, "\t", "", -1) + txt = strings.Replace(txt, "\n", "\\n", -1) + txt = strings.Replace(txt, "\r", "\\r", -1) + txt = strings.Replace(txt, "\t", "\\t", -1) } else { txt = "" }