validated ParseTrees tests

This commit is contained in:
ericvergnaud 2014-10-25 02:49:22 +08:00
parent 5ee6990f8c
commit 2716bbd62d
5 changed files with 21 additions and 176 deletions

View File

@ -485,7 +485,7 @@ public class Generator {
"xy",
"(a x y)\n",
null);
file.addParserTest(input, "test2Alts", "T", "s",
file.addParserTest(input, "2Alts", "T", "s",
"y",
"(a y)\n",
null);
@ -496,20 +496,19 @@ public class Generator {
file.addParserTest(input, "RuleRef", "T", "s",
"yx",
"(a (b y) x)\n",
null);
// ERRORs not shown. z is colored red in tree view
null);
file.addParserTest(input, "ExtraToken", "T", "s",
"xzy",
"(a x z y)\n",
null);
"(a x z y)\n", // ERRORs not shown. z is colored red in tree view
"line 1:1 extraneous input 'z' expecting 'y'\n");
file.addParserTest(input, "NoViableAlt", "T", "s",
"z",
"(a z)\n",
null);
"line 1:0 mismatched input 'z' expecting {'x', 'y'}\n");
file.addParserTest(input, "Sync", "T", "s",
"xzyy!",
"(a x z y y !)\n",
null);
"line 1:1 extraneous input 'z' expecting {'y', '!'}\n");
return file;
}

View File

@ -131,7 +131,7 @@ Pass() ::= ""
StringList() ::= "List\<String>"
BuildParseTrees() ::= "this.buildParseTrees = true;"
BuildParseTrees() ::= "setBuildParseTree(true);"
BailErrorStrategy() ::= <%setErrorHandler(new BailErrorStrategy());%>
@ -159,7 +159,7 @@ ImportListener(X) ::= ""
GetExpectedTokenNames() ::= "this.getExpectedTokens().toString(this.tokenNames)"
WriteRuleInvocationStack() ::= "document.getElementById('output').value += antlr4.Utils.arrayToString(this.getRuleInvocationStack()) + '\\n';"
WriteRuleInvocationStack() ::= "System.out.println(getRuleInvocationStack());"
LL_EXACT_AMBIG_DETECTION() ::= <<this._interp.predictionMode = antlr4.atn.PredictionMode.LL_EXACT_AMBIG_DETECTION;>>

View File

@ -10,14 +10,14 @@ public class TestParseTrees extends BaseTest {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"this.buildParseTrees = true;\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
"}\n" +
" : r=a ;\n" +
"a : 'x' { \n" +
"document.getElementById('output').value += antlr4.Utils.arrayToString(this.getRuleInvocationStack()) + '\\n';\n" +
"System.out.println(getRuleInvocationStack());\n" +
"} ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x", false);
assertEquals("[a, s]\n(a x)\n", found);
@ -29,7 +29,7 @@ public class TestParseTrees extends BaseTest {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"this.buildParseTrees = true;\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
@ -43,11 +43,11 @@ public class TestParseTrees extends BaseTest {
}
@Test
public void testtest2Alts() throws Exception {
public void test2Alts() throws Exception {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"this.buildParseTrees = true;\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
@ -65,7 +65,7 @@ public class TestParseTrees extends BaseTest {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"this.buildParseTrees = true;\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
@ -83,7 +83,7 @@ public class TestParseTrees extends BaseTest {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"this.buildParseTrees = true;\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
@ -103,7 +103,7 @@ public class TestParseTrees extends BaseTest {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"this.buildParseTrees = true;\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
@ -115,7 +115,7 @@ public class TestParseTrees extends BaseTest {
" ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xzy", false);
assertEquals("(a x z y)\n", found);
assertNull(this.stderrDuringParse);
assertEquals("line 1:1 extraneous input 'z' expecting 'y'\n", this.stderrDuringParse);
}
@Test
@ -123,7 +123,7 @@ public class TestParseTrees extends BaseTest {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"this.buildParseTrees = true;\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
@ -136,7 +136,7 @@ public class TestParseTrees extends BaseTest {
" ";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "z", false);
assertEquals("(a z)\n", found);
assertNull(this.stderrDuringParse);
assertEquals("line 1:0 mismatched input 'z' expecting {'x', 'y'}\n", this.stderrDuringParse);
}
@Test
@ -144,7 +144,7 @@ public class TestParseTrees extends BaseTest {
String grammar = "grammar T;\n" +
"s\n" +
"@init {\n" +
"this.buildParseTrees = true;\n" +
"setBuildParseTree(true);\n" +
"}\n" +
"@after {\n" +
"System.out.println($r.ctx.toStringTree(this));\n" +
@ -156,7 +156,7 @@ public class TestParseTrees extends BaseTest {
" ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xzyy!", false);
assertEquals("(a x z y y !)\n", found);
assertNull(this.stderrDuringParse);
assertEquals("line 1:1 extraneous input 'z' expecting {'y', '!'}\n", this.stderrDuringParse);
}

View File

@ -1,154 +0,0 @@
/*
* [The "BSD license"]
* Copyright (c) 2012 Terence Parr
* Copyright (c) 2012 Sam Harwell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.antlr.v4.test.tool;
import org.junit.Test;
import static org.junit.Assert.*;
public class TestParseTrees extends BaseTest {
@Test public void testTokenAndRuleContextString() throws Exception {
String grammar =
"grammar T;\n" +
"s\n" +
"@init {setBuildParseTree(true);}\n" +
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
" :r=a ;\n" +
"a : 'x' {System.out.println(getRuleInvocationStack());} ;\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "x", false);
String expecting = "[a, s]\n(a x)\n";
assertEquals(expecting, result);
}
@Test public void testToken2() throws Exception {
String grammar =
"grammar T;\n" +
"s\n" +
"@init {setBuildParseTree(true);}\n" +
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
" :r=a ;\n" +
"a : 'x' 'y'\n" +
" ;\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xy", false);
String expecting = "(a x y)\n";
assertEquals(expecting, result);
}
@Test public void test2Alts() throws Exception {
String grammar =
"grammar T;\n" +
"s\n" +
"@init {setBuildParseTree(true);}\n" +
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
" :r=a ;\n" +
"a : 'x' | 'y'\n" +
" ;\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "y", false);
String expecting = "(a y)\n";
assertEquals(expecting, result);
}
@Test public void test2AltLoop() throws Exception {
String grammar =
"grammar T;\n" +
"s\n" +
"@init {setBuildParseTree(true);}\n" +
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
" :r=a ;\n" +
"a : ('x' | 'y')* 'z'\n" +
" ;\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xyyxyxz", false);
String expecting = "(a x y y x y x z)\n";
assertEquals(expecting, result);
}
@Test public void testRuleRef() throws Exception {
String grammar =
"grammar T;\n" +
"s\n" +
"@init {setBuildParseTree(true);}\n" +
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
" : r=a ;\n" +
"a : b 'x'\n" +
" ;\n" +
"b : 'y' ;\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "yx", false);
String expecting = "(a (b y) x)\n";
assertEquals(expecting, result);
}
// ERRORS
@Test public void testExtraToken() throws Exception {
String grammar =
"grammar T;\n" +
"s\n" +
"@init {setBuildParseTree(true);}\n" +
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
" : r=a ;\n" +
"a : 'x' 'y'\n" +
" ;\n" +
"Z : 'z'; \n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xzy", false);
String expecting = "(a x z y)\n"; // ERRORs not shown. z is colored red in tree view
assertEquals(expecting, result);
}
@Test public void testNoViableAlt() throws Exception {
String grammar =
"grammar T;\n" +
"s\n" +
"@init {setBuildParseTree(true);}\n" +
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
" : r=a ;\n" +
"a : 'x' | 'y'\n" +
" ;\n" +
"Z : 'z'; \n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "z", false);
String expecting = "(a z)\n";
assertEquals(expecting, result);
}
@Test public void testSync() throws Exception {
String grammar =
"grammar T;\n" +
"s\n" +
"@init {setBuildParseTree(true);}\n" +
"@after {System.out.println($r.ctx.toStringTree(this));}\n" +
" : r=a ;\n" +
"a : 'x' 'y'* '!'\n" +
" ;\n" +
"Z : 'z'; \n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", "xzyy!", false);
String expecting = "(a x z y y !)\n";
assertEquals(expecting, result);
}
}