validate ParserErrors tests

This commit is contained in:
ericvergnaud 2014-10-25 00:30:20 +08:00
parent 6067aee458
commit 0c112e97c8
8 changed files with 24 additions and 408 deletions

View File

@ -536,16 +536,16 @@ public class Generator {
"line 1:1 missing 'b' at 'c'\n");
file.addParserTest(input, "ConjuringUpToken", "T", "a",
"ac",
"conjured=[@-1,-1:-1='<missing 'b'>',<1>,1:1]\n",
null);
"conjured=[@-1,-1:-1='<missing 'b'>',<2>,1:1]\n",
"line 1:1 missing 'b' at 'c'\n");
file.addParserTest(input, "SingleSetInsertion", "T", "a",
"ad",
"",
"line 1:1 missing {'b', 'c'} at 'd'\n");
file.addParserTest(input, "ConjuringUpTokenFromSet", "T", "a",
"ad",
"conjured=[@-1,-1:-1='<missing 'b'>',<1>,1:1]\n",
null);
"conjured=[@-1,-1:-1='<missing 'b'>',<2>,1:1]\n",
"line 1:1 missing {'b', 'c'} at 'd'\n");
file.addParserTest(input, "LL2", "T", "a",
"ae",
"",

View File

@ -5,4 +5,3 @@ grammar <grammarName>;
s : (a | b)+;
a : 'a' {<write("'a'")>};
b : 'b' {<write("'b'")>};
;

View File

@ -3,4 +3,3 @@ start : expr EOF;
expr : 'x'
| expr expr
;
;

View File

@ -3,4 +3,3 @@ start : ID ':' expr;
expr : primary expr? {<Pass()>} | expr '->' ID;
primary : ID;
ID : [a-z]+;
;

View File

@ -1,4 +1,3 @@
grammar <grammarName>;
start : ID+;
ID : [a-z]+;
;

View File

@ -149,7 +149,7 @@ TokenStartColumnEquals(i) ::= <%this._tokenStartCharPositionInLine==<i>%>
ImportListener(X) ::= <<var <X>Listener = require('./<X>Listener').<X>Listener;>>
GetExpectedTokenNames() ::= "this.getExpectedTokens().toString(this.tokenNames));"
GetExpectedTokenNames() ::= "this.getExpectedTokens().toString(this.tokenNames)"
WriteRuleInvocationStack() ::= "document.getElementById('output').value += antlr4.Utils.arrayToString(this.getRuleInvocationStack()) + '\\n';"
@ -330,11 +330,11 @@ this.LeafListener.prototype.constructor = this.LeafListener;
>>
DeclareContextListGettersFunction() ::= <<
function foo() {
var s = new SContext();
var a = s.a();
var b = s.b();
};
void foo() {
SContext s = null;
List\<? extends AContext> a = s.a();
List\<? extends BContext> b = s.b();
}
>>
Declare_foo() ::= "this.foo = function() {document.getElementById('output').value += 'foo\\n'};"

View File

@ -46,8 +46,8 @@ public class TestParserErrors extends BaseTest {
String grammar = "grammar T;\n" +
"a : 'a' x='b' {System.out.println(\"conjured=\"+$x);} 'c' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ac", false);
assertEquals("conjured=[@-1,-1:-1='<missing 'b'>',<1>,1:1]\n", found);
assertNull(this.stderrDuringParse);
assertEquals("conjured=[@-1,-1:-1='<missing 'b'>',<2>,1:1]\n", found);
assertEquals("line 1:1 missing 'b' at 'c'\n", this.stderrDuringParse);
}
@Test
@ -64,8 +64,8 @@ public class TestParserErrors extends BaseTest {
String grammar = "grammar T;\n" +
"a : 'a' x=('b'|'c') {System.out.println(\"conjured=\"+$x);} 'd' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ad", false);
assertEquals("conjured=[@-1,-1:-1='<missing 'b'>',<1>,1:1]\n", found);
assertNull(this.stderrDuringParse);
assertEquals("conjured=[@-1,-1:-1='<missing 'b'>',<2>,1:1]\n", found);
assertEquals("line 1:1 missing {'b', 'c'} at 'd'\n", this.stderrDuringParse);
}
@Test
@ -190,7 +190,7 @@ public class TestParserErrors extends BaseTest {
"WS : ' ' -> skip ;\n" +
"acClass\n" +
"@init\n" +
"{System.out.println(this.getExpectedTokens().toString(this.tokenNames)););}\n" +
"{System.out.println(this.getExpectedTokens().toString(this.tokenNames));}\n" +
" : ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "start", "dog and software", false);
assertEquals("{'hardware', 'software'}\n", found);
@ -201,8 +201,7 @@ public class TestParserErrors extends BaseTest {
public void testInvalidEmptyInput() throws Exception {
String grammar = "grammar T;\n" +
"start : ID+;\n" +
"ID : [a-z]+;\n" +
";";
"ID : [a-z]+;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "start", "", false);
assertEquals("", found);
assertEquals("line 1:0 missing ID at '<EOF>'\n", this.stderrDuringParse);
@ -212,16 +211,15 @@ public class TestParserErrors extends BaseTest {
public void testContextListGetters() throws Exception {
String grammar = "grammar T;\n" +
"@parser::members{\n" +
" function foo() {\n" +
" var s = new SContext();\n" +
" var a = s.a();\n" +
" var b = s.b();\n" +
" };\n" +
"void foo() {\n" +
" SContext s = null;\n" +
" List<? extends AContext> a = s.a();\n" +
" List<? extends BContext> b = s.b();\n" +
"}\n" +
"}\n" +
"s : (a | b)+;\n" +
"a : 'a' {System.out.print('a');};\n" +
"b : 'b' {System.out.print('b');};\n" +
";";
"b : 'b' {System.out.print('b');};";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "abab", false);
assertEquals("abab\n", found);
assertNull(this.stderrDuringParse);
@ -232,8 +230,7 @@ public class TestParserErrors extends BaseTest {
"start : expr EOF;\n" +
"expr : 'x'\n" +
" | expr expr\n" +
" ;\n" +
";";
" ;";
return execParser("T.g4", grammar, "TParser", "TLexer", "start", input, false);
}
@ -264,8 +261,7 @@ public class TestParserErrors extends BaseTest {
"start : ID ':' expr;\n" +
"expr : primary expr? {} | expr '->' ID;\n" +
"primary : ID;\n" +
"ID : [a-z]+;\n" +
";";
"ID : [a-z]+;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "start", "x:x", false);
assertEquals("", found);
assertNull(this.stderrDuringParse);

View File

@ -1,376 +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.antlr.v4.runtime.atn.ATNSerializer;
import org.junit.Test;
import static org.junit.Assert.*;
/** test runtime parse errors */
public class TestParseErrors extends BaseTest {
@Test public void testTokenMismatch() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' 'b' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aa", false);
String expecting = "line 1:1 mismatched input 'a' expecting 'b'\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testSingleTokenDeletion() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' 'b' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aab", false);
String expecting = "line 1:1 extraneous input 'a' expecting 'b'\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testSingleTokenDeletionExpectingSet() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' ('b'|'c') ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aab", false);
String expecting = "line 1:1 extraneous input 'a' expecting {'b', 'c'}\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testSingleTokenInsertion() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' 'b' 'c' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ac", false);
String expecting = "line 1:1 missing 'b' at 'c'\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testConjuringUpToken() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' x='b' {System.out.println(\"conjured=\"+$x);} 'c' ;";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ac", false);
String expecting = "conjured=[@-1,-1:-1='<missing 'b'>',<2>,1:1]\n";
assertEquals(expecting, result);
}
@Test public void testSingleSetInsertion() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' ('b'|'c') 'd' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ad", false);
String expecting = "line 1:1 missing {'b', 'c'} at 'd'\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testConjuringUpTokenFromSet() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' x=('b'|'c') {System.out.println(\"conjured=\"+$x);} 'd' ;";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ad", false);
String expecting = "conjured=[@-1,-1:-1='<missing 'b'>',<2>,1:1]\n";
assertEquals(expecting, result);
}
@Test public void testLL2() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' 'b'" +
" | 'a' 'c'" +
";\n" +
"q : 'e' ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ae", false);
String expecting = "line 1:1 no viable alternative at input 'ae'\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testLL3() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' 'b'* 'c'" +
" | 'a' 'b' 'd'" +
" ;\n" +
"q : 'e' ;\n";
System.out.println(grammar);
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abe", false);
String expecting = "line 1:2 no viable alternative at input 'abe'\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testLLStar() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a'+ 'b'" +
" | 'a'+ 'c'" +
";\n" +
"q : 'e' ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aaae", false);
String expecting = "line 1:3 no viable alternative at input 'aaae'\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testSingleTokenDeletionBeforeLoop() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' 'b'*;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aabc", false);
String expecting = "line 1:1 extraneous input 'a' expecting {<EOF>, 'b'}\n" +
"line 1:3 token recognition error at: 'c'\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testMultiTokenDeletionBeforeLoop() throws Exception {
// can only delete 1 before loop
String grammar =
"grammar T;\n" +
"a : 'a' 'b'* 'c';";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aacabc", false);
String expecting =
"line 1:1 extraneous input 'a' expecting {'b', 'c'}\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testSingleTokenDeletionDuringLoop() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' 'b'* 'c' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ababbc", false);
String expecting = "line 1:2 extraneous input 'a' expecting {'b', 'c'}\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testMultiTokenDeletionDuringLoop() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' 'b'* 'c' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaaababc", false);
String expecting =
"line 1:2 extraneous input 'a' expecting {'b', 'c'}\n" +
"line 1:6 extraneous input 'a' expecting {'b', 'c'}\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
// ------
@Test public void testSingleTokenDeletionBeforeLoop2() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' ('b'|'z'{;})*;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aabc", false);
String expecting = "line 1:1 extraneous input 'a' expecting {<EOF>, 'b', 'z'}\n" +
"line 1:3 token recognition error at: 'c'\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testMultiTokenDeletionBeforeLoop2() throws Exception {
// can only delete 1 before loop
String grammar =
"grammar T;\n" +
"a : 'a' ('b'|'z'{;})* 'c';";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "aacabc", false);
String expecting =
"line 1:1 extraneous input 'a' expecting {'b', 'z', 'c'}\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testSingleTokenDeletionDuringLoop2() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' ('b'|'z'{;})* 'c' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "ababbc", false);
String expecting = "line 1:2 extraneous input 'a' expecting {'b', 'z', 'c'}\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testMultiTokenDeletionDuringLoop2() throws Exception {
String grammar =
"grammar T;\n" +
"a : 'a' ('b'|'z'{;})* 'c' ;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", "abaaababc", false);
String expecting =
"line 1:2 extraneous input 'a' expecting {'b', 'z', 'c'}\n" +
"line 1:6 extraneous input 'a' expecting {'b', 'z', 'c'}\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
@Test public void testLL1ErrorInfo() throws Exception {
String grammar =
"grammar T;\n" +
"start : animal (AND acClass)? service EOF;\n" +
"animal : (DOG | CAT );\n" +
"service : (HARDWARE | SOFTWARE) ;\n" +
"AND : 'and';\n" +
"DOG : 'dog';\n" +
"CAT : 'cat';\n" +
"HARDWARE: 'hardware';\n" +
"SOFTWARE: 'software';\n" +
"WS : ' ' -> skip ;" +
"acClass\n" +
"@init\n" +
"{ System.out.println(getExpectedTokens().toString(tokenNames)); }\n" +
" : ;\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "start", "dog and software", false);
String expecting = "{'hardware', 'software'}\n";
assertEquals(expecting, result);
}
/**
* This is a regression test for #6 "NullPointerException in getMissingSymbol".
* https://github.com/antlr/antlr4/issues/6
*/
@Test
public void testInvalidEmptyInput() throws Exception {
String grammar =
"grammar T;\n" +
"start : ID+;\n" +
"ID : [a-z]+;\n" +
"\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "start", "", true);
String expecting = "";
assertEquals(expecting, result);
assertEquals("line 1:0 missing ID at '<EOF>'\n", this.stderrDuringParse);
}
/**
* Regression test for "Getter for context is not a list when it should be".
* https://github.com/antlr/antlr4/issues/19
*/
@Test
public void testContextListGetters() throws Exception {
String grammar =
"grammar T;\n" +
"@parser::members{\n" +
" void foo() {\n" +
" SContext s = null;\n" +
" List<? extends AContext> a = s.a();\n" +
" List<? extends BContext> b = s.b();\n" +
" }\n" +
"}\n" +
"s : (a | b)+;\n" +
"a : 'a' {System.out.print('a');};\n" +
"b : 'b' {System.out.print('b');};\n" +
"";
String result = execParser("T.g", grammar, "TParser", "TLexer", "s", "abab", true);
String expecting = "abab\n";
assertEquals(expecting, result);
assertNull(this.stderrDuringParse);
}
/**
* This is a regression test for #26 "an exception upon simple rule with double recursion in an alternative".
* https://github.com/antlr/antlr4/issues/26
*/
@Test
public void testDuplicatedLeftRecursiveCall() throws Exception {
String grammar =
"grammar T;\n" +
"start : expr EOF;\n" +
"expr : 'x'\n" +
" | expr expr\n" +
" ;\n" +
"\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "start", "x", true);
assertEquals("", result);
assertNull(this.stderrDuringParse);
result = execParser("T.g4", grammar, "TParser", "TLexer", "start", "xx", true);
assertEquals("", result);
assertNull(this.stderrDuringParse);
result = execParser("T.g4", grammar, "TParser", "TLexer", "start", "xxx", true);
assertEquals("", result);
assertNull(this.stderrDuringParse);
result = execParser("T.g4", grammar, "TParser", "TLexer", "start", "xxxx", true);
assertEquals("", result);
assertNull(this.stderrDuringParse);
}
/**
* This is a regression test for #45 "NullPointerException in ATNConfig.hashCode".
* https://github.com/antlr/antlr4/issues/45
* <p/>
* The original cause of this issue was an error in the tool's ATN state optimization,
* which is now detected early in {@link ATNSerializer} by ensuring that all
* serialized transitions point to states which were not removed.
*/
@Test
public void testInvalidATNStateRemoval() throws Exception {
String grammar =
"grammar T;\n" +
"start : ID ':' expr;\n" +
"expr : primary expr? {} | expr '->' ID;\n" +
"primary : ID;\n" +
"ID : [a-z]+;\n" +
"\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "start", "x:x", true);
String expecting = "";
assertEquals(expecting, result);
assertNull(this.stderrDuringParse);
}
@Test public void testNoViableAltAvoidance() throws Exception {
// "a." matches 'a' to rule e but then realizes '.' won't match.
// previously would cause noviablealt. now prediction pretends to
// have "a' predict 2nd alt of e. Will get syntax error later so
// let it get farther.
String grammar =
"grammar T;\n" +
"s : e '!' ;\n" +
"e : 'a' 'b'\n" +
" | 'a'\n" +
" ;\n" +
"DOT : '.' ;\n" +
"WS : [ \\t\\r\\n]+ -> skip;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", "a.", false);
String expecting =
"line 1:1 mismatched input '.' expecting '!'\n";
String result = stderrDuringParse;
assertEquals(expecting, result);
}
}