More needed files.

This commit is contained in:
David Sisson 2016-05-05 22:04:47 -07:00
parent b69076ab7f
commit 43dc1df1f2
15 changed files with 13686 additions and 0 deletions

View File

@ -0,0 +1,94 @@
package org.antlr.v4.test.runtime.python2;
import org.antlr.v4.test.runtime.python.BasePythonTest;
import org.stringtemplate.v4.ST;
public abstract class BasePython2Test extends BasePythonTest {
@Override
protected String getLanguage() {
return "Python2";
}
@Override
protected String getPythonExecutable() {
return "python2.7";
}
@Override
protected void writeLexerTestFile(String lexerName, boolean showDFA) {
ST outputFileST = new ST(
"from __future__ import print_function\n"
+ "import sys\n"
+ "from antlr4 import *\n"
+ "from <lexerName> import <lexerName>\n"
+ "\n"
+ "def main(argv):\n"
+ " input = FileStream(argv[1])\n"
+ " lexer = <lexerName>(input)\n"
+ " stream = CommonTokenStream(lexer)\n"
+ " stream.fill()\n"
+ " [ print(str(t)) for t in stream.tokens ]\n"
+ (showDFA ? " print(lexer._interp.decisionToDFA[Lexer.DEFAULT_MODE].toLexerString(), end='')\n"
: "") + "\n" + "if __name__ == '__main__':\n"
+ " main(sys.argv)\n" + "\n");
outputFileST.add("lexerName", lexerName);
writeFile(tmpdir, "Test.py", outputFileST.render());
}
@Override
protected void writeParserTestFile(String parserName, String lexerName,
String listenerName, String visitorName,
String parserStartRuleName, boolean debug, boolean trace) {
if(!parserStartRuleName.endsWith(")"))
parserStartRuleName += "()";
ST outputFileST = new ST(
"import sys\n"
+ "from antlr4 import *\n"
+ "from <lexerName> import <lexerName>\n"
+ "from <parserName> import <parserName>\n"
+ "from <listenerName> import <listenerName>\n"
+ "from <visitorName> import <visitorName>\n"
+ "\n"
+ "class TreeShapeListener(ParseTreeListener):\n"
+ "\n"
+ " def visitTerminal(self, node):\n"
+ " pass\n"
+ "\n"
+ " def visitErrorNode(self, node):\n"
+ " pass\n"
+ "\n"
+ " def exitEveryRule(self, ctx):\n"
+ " pass\n"
+ "\n"
+ " def enterEveryRule(self, ctx):\n"
+ " for child in ctx.getChildren():\n"
+ " parent = child.parentCtx\n"
+ " if not isinstance(parent, RuleNode) or parent.getRuleContext() != ctx:\n"
+ " raise IllegalStateException(\"Invalid parse tree shape detected.\")\n"
+ "\n"
+ "def main(argv):\n"
+ " input = FileStream(argv[1])\n"
+ " lexer = <lexerName>(input)\n"
+ " stream = CommonTokenStream(lexer)\n"
+ "<createParser>"
+ " parser.buildParseTrees = True\n"
+ " tree = parser.<parserStartRuleName>\n"
+ " ParseTreeWalker.DEFAULT.walk(TreeShapeListener(), tree)\n"
+ "\n" + "if __name__ == '__main__':\n"
+ " main(sys.argv)\n" + "\n");
String stSource = " parser = <parserName>(stream)\n";
if(debug)
stSource += " parser.addErrorListener(DiagnosticErrorListener())\n";
if(trace)
stSource += " parser.setTrace(True)\n";
ST createParserST = new ST(stSource);
outputFileST.add("createParser", createParserST);
outputFileST.add("parserName", parserName);
outputFileST.add("lexerName", lexerName);
outputFileST.add("listenerName", listenerName);
outputFileST.add("visitorName", visitorName);
outputFileST.add("parserStartRuleName", parserStartRuleName);
writeFile(tmpdir, "Test.py", outputFileST.render());
}
}

View File

@ -0,0 +1,70 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python2;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class TestCompositeLexers extends BasePython2Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLexerDelegatorInvokesDelegateRule() throws Exception {
mkdir(tmpdir);
String slave_S =
"lexer grammar S;\n" +
"A : 'a' {print(\"S.A\")};\n" +
"C : 'c' ;";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(61);
grammarBuilder.append("lexer grammar M;\n");
grammarBuilder.append("import S;\n");
grammarBuilder.append("B : 'b';\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execLexer("M.g4", grammar, "M", input, false);
assertEquals(
"S.A\n" +
"[@0,0:0='a',<3>,1:0]\n" +
"[@1,1:1='b',<1>,1:1]\n" +
"[@2,2:2='c',<4>,1:2]\n" +
"[@3,3:2='<EOF>',<-1>,1:3]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLexerDelegatorRuleOverridesDelegate() throws Exception {
mkdir(tmpdir);
String slave_S =
"lexer grammar S;\n" +
"A : 'a' {print(\"S.A\")} ;\n" +
"B : 'b' {print(\"S.B\")} ;";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(79);
grammarBuilder.append("lexer grammar M;\n");
grammarBuilder.append("import S;\n");
grammarBuilder.append("A : 'a' B {print(\"M.A\")} ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="ab";
String found = execLexer("M.g4", grammar, "M", input, false);
assertEquals(
"M.A\n" +
"[@0,0:1='ab',<1>,1:0]\n" +
"[@1,2:1='<EOF>',<-1>,1:2]\n", found);
assertNull(this.stderrDuringParse);
}
}

View File

@ -0,0 +1,488 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python2;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
import org.antlr.v4.test.runtime.java.ErrorQueue;
import org.antlr.v4.tool.Grammar;
@SuppressWarnings("unused")
public class TestCompositeParsers extends BasePython2Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testBringInLiteralsFromDelegate() throws Exception {
mkdir(tmpdir);
String slave_S =
"parser grammar S;\n" +
"a : '=' 'a' {print(\"S.a\",end='')};";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(54);
grammarBuilder.append("grammar M;\n");
grammarBuilder.append("import S;\n");
grammarBuilder.append("s : a ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="=a";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "s", input, false);
assertEquals("S.a\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testCombinedImportsCombined() throws Exception {
mkdir(tmpdir);
String slave_S =
"parser grammar S;\n" +
"tokens { A, B, C }\n" +
"x : 'x' INT {print(\"S.x\")};\n" +
"INT : '0'..'9'+ ;\n" +
"WS : (' '|'\\n') -> skip ;";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(31);
grammarBuilder.append("grammar M;\n");
grammarBuilder.append("import S;\n");
grammarBuilder.append("s : x INT;");
String grammar = grammarBuilder.toString();
writeFile(tmpdir, "M.g4", grammar);
ErrorQueue equeue = new ErrorQueue();
new Grammar(tmpdir+"/M.g4", grammar, equeue);
assertEquals("unexpected errors: " + equeue, 0, equeue.errors.size());
String input ="x 34 9";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "s", input, false);
assertEquals("S.x\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDelegatesSeeSameTokenType() throws Exception {
mkdir(tmpdir);
String slave_T =
"parser grammar T;\n" +
"tokens { C, B, A } // reverse order\n" +
"y : A {print(\"T.y\")};";
writeFile(tmpdir, "T.g4", slave_T);
String slave_S =
"parser grammar S;\n" +
"tokens { A, B, C }\n" +
"x : A {print(\"S.x\")};";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(598);
grammarBuilder.append("// The lexer will create rules to match letters a, b, c.\n");
grammarBuilder.append("// The associated token types A, B, C must have the same value\n");
grammarBuilder.append("// and all import'd parsers. Since ANTLR regenerates all imports\n");
grammarBuilder.append("// for use with the delegator M, it can generate the same token type\n");
grammarBuilder.append("// mapping in each parser:\n");
grammarBuilder.append("// public static final int C=6;\n");
grammarBuilder.append("// public static final int EOF=-1;\n");
grammarBuilder.append("// public static final int B=5;\n");
grammarBuilder.append("// public static final int WS=7;\n");
grammarBuilder.append("// public static final int A=4;\n");
grammarBuilder.append("grammar M;\n");
grammarBuilder.append("import S,T;\n");
grammarBuilder.append("s : x y ; // matches AA, which should be 'aa'\n");
grammarBuilder.append("B : 'b' ; // another order: B, A, C\n");
grammarBuilder.append("A : 'a' ; \n");
grammarBuilder.append("C : 'c' ; \n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
writeFile(tmpdir, "M.g4", grammar);
ErrorQueue equeue = new ErrorQueue();
Grammar g = new Grammar(tmpdir+"/M.g4", grammar, equeue);
String expectedTokenIDToTypeMap = "{EOF=-1, B=1, A=2, C=3, WS=4}";
String expectedStringLiteralToTypeMap = "{'a'=2, 'b'=1, 'c'=3}";
String expectedTypeToTokenList = "[B, A, C, WS]";
assertEquals(expectedTokenIDToTypeMap, g.tokenNameToTypeMap.toString());
assertEquals(expectedStringLiteralToTypeMap, sort(g.stringLiteralToTypeMap).toString());
assertEquals(expectedTypeToTokenList, realElements(g.typeToTokenList).toString());
assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
String input ="aa";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "s", input, false);
assertEquals(
"S.x\n" +
"T.y\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDelegatorAccessesDelegateMembers() throws Exception {
mkdir(tmpdir);
String slave_S =
"parser grammar S;\n" +
"@parser::members {\n" +
"def foo(self):\n" +
" print('foo')\n" +
"}\n" +
"a : B;";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(126);
grammarBuilder.append("grammar M; // uses no rules from the import\n");
grammarBuilder.append("import S;\n");
grammarBuilder.append("s : 'b' {self.foo()} ; // gS is import pointer\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="b";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "s", input, false);
assertEquals("foo\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDelegatorInvokesDelegateRule() throws Exception {
mkdir(tmpdir);
String slave_S =
"parser grammar S;\n" +
"a : B {print(\"S.a\")};";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(104);
grammarBuilder.append("grammar M;\n");
grammarBuilder.append("import S;\n");
grammarBuilder.append("s : a ;\n");
grammarBuilder.append("B : 'b' ; // defines B from inherited token space\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="b";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "s", input, false);
assertEquals("S.a\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDelegatorInvokesDelegateRuleWithArgs() throws Exception {
mkdir(tmpdir);
String slave_S =
"parser grammar S;\n" +
"a[int x] returns [int y] : B {print(\"S.a\",end='')} {$y=1000;} ;";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(131);
grammarBuilder.append("grammar M;\n");
grammarBuilder.append("import S;\n");
grammarBuilder.append("s : label=a[3] {print($label.y)} ;\n");
grammarBuilder.append("B : 'b' ; // defines B from inherited token space\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="b";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "s", input, false);
assertEquals("S.a1000\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDelegatorInvokesDelegateRuleWithReturnStruct() throws Exception {
mkdir(tmpdir);
String slave_S =
"parser grammar S;\n" +
"a : B {print(\"S.a\",end='')} ;";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(128);
grammarBuilder.append("grammar M;\n");
grammarBuilder.append("import S;\n");
grammarBuilder.append("s : a {print($a.text,end='')} ;\n");
grammarBuilder.append("B : 'b' ; // defines B from inherited token space\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="b";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "s", input, false);
assertEquals("S.ab\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDelegatorInvokesFirstVersionOfDelegateRule() throws Exception {
mkdir(tmpdir);
String slave_T =
"parser grammar T;\n" +
"a : B {print(\"T.a\")};";
writeFile(tmpdir, "T.g4", slave_T);
String slave_S =
"parser grammar S;\n" +
"a : b {print(\"S.a\")};\n" +
"b : B;";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(106);
grammarBuilder.append("grammar M;\n");
grammarBuilder.append("import S,T;\n");
grammarBuilder.append("s : a ;\n");
grammarBuilder.append("B : 'b' ; // defines B from inherited token space\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="b";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "s", input, false);
assertEquals("S.a\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDelegatorRuleOverridesDelegate() throws Exception {
mkdir(tmpdir);
String slave_S =
"parser grammar S;\n" +
"a : b {print(\"S.a\",end='')};\n" +
"b : B ;";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(59);
grammarBuilder.append("grammar M;\n");
grammarBuilder.append("import S;\n");
grammarBuilder.append("b : 'b'|'c';\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="c";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "a", input, false);
assertEquals("S.a\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDelegatorRuleOverridesDelegates() throws Exception {
mkdir(tmpdir);
String slave_T =
"parser grammar T;\n" +
"tokens { A }\n" +
"b : 'b' {print(\"T.b\")};";
writeFile(tmpdir, "T.g4", slave_T);
String slave_S =
"parser grammar S;\n" +
"a : b {print(\"S.a\")};\n" +
"b : 'b' ;";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(81);
grammarBuilder.append("grammar M;\n");
grammarBuilder.append("import S, T;\n");
grammarBuilder.append("b : 'b'|'c' {print(\"M.b\")}|B|A;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="c";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "a", input, false);
assertEquals(
"M.b\n" +
"S.a\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDelegatorRuleOverridesLookaheadInDelegate() throws Exception {
mkdir(tmpdir);
String slave_S =
"parser grammar S;\n" +
"type_ : 'int' ;\n" +
"decl : type_ ID ';'\n" +
" | type_ ID init ';' {print(\"JavaDecl: \" + $text,end='')};\n" +
"init : '=' INT;";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(121);
grammarBuilder.append("grammar M;\n");
grammarBuilder.append("import S;\n");
grammarBuilder.append("prog : decl ;\n");
grammarBuilder.append("type_ : 'int' | 'float' ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+ ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="float x = 3;";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "prog", input, false);
assertEquals("JavaDecl: floatx=3;\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testImportLexerWithOnlyFragmentRules() throws Exception {
mkdir(tmpdir);
String slave_Unicode =
"lexer grammar Unicode;\n" +
"\n" +
"fragment\n" +
"UNICODE_CLASS_Zs : '\\u0020' | '\\u00A0' | '\\u1680' | '\\u180E'\n" +
" | '\\u2000'..'\\u200A'\n" +
" | '\\u202F' | '\\u205F' | '\\u3000'\n" +
" ;\n";
writeFile(tmpdir, "Unicode.g4", slave_Unicode);
StringBuilder grammarBuilder = new StringBuilder(91);
grammarBuilder.append("grammar Test;\n");
grammarBuilder.append("import Unicode;\n");
grammarBuilder.append("\n");
grammarBuilder.append("program : 'test' 'test';\n");
grammarBuilder.append("\n");
grammarBuilder.append("WS : (UNICODE_CLASS_Zs)+ -> skip;\n");
String grammar = grammarBuilder.toString();
String input ="test test";
String found = execParser("Test.g4", grammar, "TestParser", "TestLexer", "TestListener", "TestVisitor", "program", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testImportedGrammarWithEmptyOptions() throws Exception {
mkdir(tmpdir);
String slave_S =
"parser grammar S;\n" +
"options {}\n" +
"a : B ;";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(64);
grammarBuilder.append("grammar M;\n");
grammarBuilder.append("import S;\n");
grammarBuilder.append("s : a ;\n");
grammarBuilder.append("B : 'b' ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="b";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "s", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testImportedRuleWithAction() throws Exception {
mkdir(tmpdir);
String slave_S =
"parser grammar S;\n" +
"a @after {x = 0} : B;";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(62);
grammarBuilder.append("grammar M;\n");
grammarBuilder.append("import S;\n");
grammarBuilder.append("s : a;\n");
grammarBuilder.append("B : 'b';\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="b";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "s", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testKeywordVSIDOrder() throws Exception {
mkdir(tmpdir);
String slave_S =
"lexer grammar S;\n" +
"ID : 'a'..'z'+;";
writeFile(tmpdir, "S.g4", slave_S);
StringBuilder grammarBuilder = new StringBuilder(106);
grammarBuilder.append("grammar M;\n");
grammarBuilder.append("import S;\n");
grammarBuilder.append("a : A {print(\"M.a: \" + str($A))};\n");
grammarBuilder.append("A : 'abc' {print(\"M.A\")};\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("M.g4", grammar, "MParser", "MLexer", "MListener", "MVisitor", "a", input, false);
assertEquals(
"M.A\n" +
"M.a: [@0,0:2='abc',<1>,1:0]\n", found);
assertNull(this.stderrDuringParse);
}
}

View File

@ -0,0 +1,506 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python2;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class TestFullContextParsing extends BasePython2Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAmbigYieldsCtxSensitiveDFA() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(100);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s @after {self.dumpDFA()}\n");
grammarBuilder.append(" : ID | ID {} ;\n");
grammarBuilder.append("ID : 'a'..'z'+;\n");
grammarBuilder.append("WS : (' '|'\\t'|'\\n')+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals(
"Decision 0:\n" +
"s0-ID->:s1^=>1\n", found);
assertEquals("line 1:0 reportAttemptingFullContext d=0 (s), input='abc'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAmbiguityNoLoop() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(206);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("prog\n");
grammarBuilder.append("@init {self._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION}\n");
grammarBuilder.append(" : expr expr {print(\"alt 1\")}\n");
grammarBuilder.append(" | expr\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("expr: '@'\n");
grammarBuilder.append(" | ID '@'\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\r\\n\\t]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="a@";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "prog", input, true);
assertEquals("alt 1\n", found);
assertEquals(
"line 1:2 reportAttemptingFullContext d=0 (prog), input='a@'\n" +
"line 1:2 reportAmbiguity d=0 (prog): ambigAlts={1, 2}, input='a@'\n" +
"line 1:2 reportAttemptingFullContext d=1 (expr), input='a@'\n" +
"line 1:2 reportContextSensitivity d=1 (expr), input='a@'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testCtxSensitiveDFATwoDiffInput() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(164);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s @after {self.dumpDFA()}\n");
grammarBuilder.append(" : ('$' a | '@' b)+ ;\n");
grammarBuilder.append("a : e ID ;\n");
grammarBuilder.append("b : e INT ID ;\n");
grammarBuilder.append("e : INT | ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+ ;\n");
grammarBuilder.append("WS : (' '|'\\t'|'\\n')+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="$ 34 abc @ 34 abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals(
"Decision 2:\n" +
"s0-INT->s1\n" +
"s1-ID->:s2^=>1\n", found);
assertEquals(
"line 1:5 reportAttemptingFullContext d=2 (e), input='34abc'\n" +
"line 1:2 reportContextSensitivity d=2 (e), input='34'\n" +
"line 1:14 reportAttemptingFullContext d=2 (e), input='34abc'\n" +
"line 1:14 reportContextSensitivity d=2 (e), input='34abc'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testCtxSensitiveDFA_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(161);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s @after {self.dumpDFA()}\n");
grammarBuilder.append(" : '$' a | '@' b ;\n");
grammarBuilder.append("a : e ID ;\n");
grammarBuilder.append("b : e INT ID ;\n");
grammarBuilder.append("e : INT | ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+ ;\n");
grammarBuilder.append("WS : (' '|'\\t'|'\\n')+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="$ 34 abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals(
"Decision 1:\n" +
"s0-INT->s1\n" +
"s1-ID->:s2^=>1\n", found);
assertEquals(
"line 1:5 reportAttemptingFullContext d=1 (e), input='34abc'\n" +
"line 1:2 reportContextSensitivity d=1 (e), input='34'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testCtxSensitiveDFA_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(161);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s @after {self.dumpDFA()}\n");
grammarBuilder.append(" : '$' a | '@' b ;\n");
grammarBuilder.append("a : e ID ;\n");
grammarBuilder.append("b : e INT ID ;\n");
grammarBuilder.append("e : INT | ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+ ;\n");
grammarBuilder.append("WS : (' '|'\\t'|'\\n')+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="@ 34 abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals(
"Decision 1:\n" +
"s0-INT->s1\n" +
"s1-ID->:s2^=>1\n", found);
assertEquals(
"line 1:5 reportAttemptingFullContext d=1 (e), input='34abc'\n" +
"line 1:5 reportContextSensitivity d=1 (e), input='34abc'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testExprAmbiguity_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(286);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@init {self._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION}\n");
grammarBuilder.append(": expr[0] {print($expr.ctx.toStringTree(recog=self))};\n");
grammarBuilder.append(" expr[int _p]\n");
grammarBuilder.append(" : ID \n");
grammarBuilder.append(" ( \n");
grammarBuilder.append(" {5 >= $_p}? '*' expr[6]\n");
grammarBuilder.append(" | {4 >= $_p}? '+' expr[5]\n");
grammarBuilder.append(" )*\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : [a-zA-Z]+ ;\n");
grammarBuilder.append("WS : [ \\r\\n\\t]+ -> skip ;\n");
String grammar = grammarBuilder.toString();
String input ="a+b";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals("(expr a + (expr b))\n", found);
assertEquals(
"line 1:1 reportAttemptingFullContext d=1 (expr), input='+'\n" +
"line 1:2 reportContextSensitivity d=1 (expr), input='+b'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testExprAmbiguity_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(286);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@init {self._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION}\n");
grammarBuilder.append(": expr[0] {print($expr.ctx.toStringTree(recog=self))};\n");
grammarBuilder.append(" expr[int _p]\n");
grammarBuilder.append(" : ID \n");
grammarBuilder.append(" ( \n");
grammarBuilder.append(" {5 >= $_p}? '*' expr[6]\n");
grammarBuilder.append(" | {4 >= $_p}? '+' expr[5]\n");
grammarBuilder.append(" )*\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : [a-zA-Z]+ ;\n");
grammarBuilder.append("WS : [ \\r\\n\\t]+ -> skip ;\n");
String grammar = grammarBuilder.toString();
String input ="a+b*c";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals("(expr a + (expr b * (expr c)))\n", found);
assertEquals(
"line 1:1 reportAttemptingFullContext d=1 (expr), input='+'\n" +
"line 1:2 reportContextSensitivity d=1 (expr), input='+b'\n" +
"line 1:3 reportAttemptingFullContext d=1 (expr), input='*'\n" +
"line 1:5 reportAmbiguity d=1 (expr): ambigAlts={1, 2}, input='*c'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testFullContextIF_THEN_ELSEParse_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(240);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s \n");
grammarBuilder.append("@init {self._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION}\n");
grammarBuilder.append("@after {self.dumpDFA()}\n");
grammarBuilder.append(" : '{' stat* '}' ;\n");
grammarBuilder.append("stat: 'if' ID 'then' stat ('else' ID)?\n");
grammarBuilder.append(" | 'return'\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("WS : (' '|'\\t'|'\\n')+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="{ if x then return }";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals(
"Decision 1:\n" +
"s0-'}'->:s1=>2\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testFullContextIF_THEN_ELSEParse_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(240);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s \n");
grammarBuilder.append("@init {self._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION}\n");
grammarBuilder.append("@after {self.dumpDFA()}\n");
grammarBuilder.append(" : '{' stat* '}' ;\n");
grammarBuilder.append("stat: 'if' ID 'then' stat ('else' ID)?\n");
grammarBuilder.append(" | 'return'\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("WS : (' '|'\\t'|'\\n')+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="{ if x then return else foo }";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals(
"Decision 1:\n" +
"s0-'else'->:s1^=>1\n", found);
assertEquals(
"line 1:19 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 1:19 reportContextSensitivity d=1 (stat), input='else'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testFullContextIF_THEN_ELSEParse_3() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(240);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s \n");
grammarBuilder.append("@init {self._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION}\n");
grammarBuilder.append("@after {self.dumpDFA()}\n");
grammarBuilder.append(" : '{' stat* '}' ;\n");
grammarBuilder.append("stat: 'if' ID 'then' stat ('else' ID)?\n");
grammarBuilder.append(" | 'return'\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("WS : (' '|'\\t'|'\\n')+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="{ if x then if y then return else foo }";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals(
"Decision 1:\n" +
"s0-'}'->:s2=>2\n" +
"s0-'else'->:s1^=>1\n", found);
assertEquals(
"line 1:29 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 1:38 reportAmbiguity d=1 (stat): ambigAlts={1, 2}, input='elsefoo}'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testFullContextIF_THEN_ELSEParse_4() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(240);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s \n");
grammarBuilder.append("@init {self._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION}\n");
grammarBuilder.append("@after {self.dumpDFA()}\n");
grammarBuilder.append(" : '{' stat* '}' ;\n");
grammarBuilder.append("stat: 'if' ID 'then' stat ('else' ID)?\n");
grammarBuilder.append(" | 'return'\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("WS : (' '|'\\t'|'\\n')+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="{ if x then if y then return else foo else bar }";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals(
"Decision 1:\n" +
"s0-'else'->:s1^=>1\n", found);
assertEquals(
"line 1:29 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 1:38 reportContextSensitivity d=1 (stat), input='elsefooelse'\n" +
"line 1:38 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 1:38 reportContextSensitivity d=1 (stat), input='else'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testFullContextIF_THEN_ELSEParse_5() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(240);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s \n");
grammarBuilder.append("@init {self._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION}\n");
grammarBuilder.append("@after {self.dumpDFA()}\n");
grammarBuilder.append(" : '{' stat* '}' ;\n");
grammarBuilder.append("stat: 'if' ID 'then' stat ('else' ID)?\n");
grammarBuilder.append(" | 'return'\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("WS : (' '|'\\t'|'\\n')+ -> skip ;");
String grammar = grammarBuilder.toString();
String input =
"{ if x then return else foo\n" +
"if x then if y then return else foo }";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals(
"Decision 1:\n" +
"s0-'}'->:s2=>2\n" +
"s0-'else'->:s1^=>1\n", found);
assertEquals(
"line 1:19 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 1:19 reportContextSensitivity d=1 (stat), input='else'\n" +
"line 2:27 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 2:36 reportAmbiguity d=1 (stat): ambigAlts={1, 2}, input='elsefoo}'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testFullContextIF_THEN_ELSEParse_6() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(240);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s \n");
grammarBuilder.append("@init {self._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION}\n");
grammarBuilder.append("@after {self.dumpDFA()}\n");
grammarBuilder.append(" : '{' stat* '}' ;\n");
grammarBuilder.append("stat: 'if' ID 'then' stat ('else' ID)?\n");
grammarBuilder.append(" | 'return'\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("WS : (' '|'\\t'|'\\n')+ -> skip ;");
String grammar = grammarBuilder.toString();
String input =
"{ if x then return else foo\n" +
"if x then if y then return else foo }";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals(
"Decision 1:\n" +
"s0-'}'->:s2=>2\n" +
"s0-'else'->:s1^=>1\n", found);
assertEquals(
"line 1:19 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 1:19 reportContextSensitivity d=1 (stat), input='else'\n" +
"line 2:27 reportAttemptingFullContext d=1 (stat), input='else'\n" +
"line 2:36 reportAmbiguity d=1 (stat): ambigAlts={1, 2}, input='elsefoo}'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLoopsSimulateTailRecursion() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(299);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("prog\n");
grammarBuilder.append("@init {self._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION}\n");
grammarBuilder.append(" : expr_or_assign*;\n");
grammarBuilder.append("expr_or_assign\n");
grammarBuilder.append(" : expr '++' {print(\"fail.\")}\n");
grammarBuilder.append(" | expr {print(\"pass: \"+$expr.text)}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("expr: expr_primary ('<-' ID)?;\n");
grammarBuilder.append("expr_primary\n");
grammarBuilder.append(" : '(' ID ')'\n");
grammarBuilder.append(" | ID '(' ID ')'\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : [a-z]+ ;");
String grammar = grammarBuilder.toString();
String input ="a(i)<-x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "prog", input, true);
assertEquals("pass: a(i)<-x\n", found);
assertEquals(
"line 1:3 reportAttemptingFullContext d=3 (expr_primary), input='a(i)'\n" +
"line 1:7 reportAmbiguity d=3 (expr_primary): ambigAlts={2, 3}, input='a(i)<-x'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSLLSeesEOFInLLGrammar() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(148);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s @after {self.dumpDFA()}\n");
grammarBuilder.append(" : a;\n");
grammarBuilder.append("a : e ID ;\n");
grammarBuilder.append("b : e INT ID ;\n");
grammarBuilder.append("e : INT | ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+ ;\n");
grammarBuilder.append("WS : (' '|'\\t'|'\\n')+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="34 abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals(
"Decision 0:\n" +
"s0-INT->s1\n" +
"s1-ID->:s2^=>1\n", found);
assertEquals(
"line 1:3 reportAttemptingFullContext d=0 (e), input='34abc'\n" +
"line 1:0 reportContextSensitivity d=0 (e), input='34'\n", this.stderrDuringParse);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,258 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python2;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class TestLexerErrors extends BasePython2Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDFAToATNThatFailsBackToDFA() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(39);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("A : 'ab' ;\n");
grammarBuilder.append("B : 'abc' ;");
String grammar = grammarBuilder.toString();
String input ="ababx";
String found = execLexer("L.g4", grammar, "L", input, false);
assertEquals(
"[@0,0:1='ab',<1>,1:0]\n" +
"[@1,2:3='ab',<1>,1:2]\n" +
"[@2,5:4='<EOF>',<-1>,1:5]\n", found);
assertEquals("line 1:4 token recognition error at: 'x'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDFAToATNThatMatchesThenFailsInATN() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(52);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("A : 'ab' ;\n");
grammarBuilder.append("B : 'abc' ;\n");
grammarBuilder.append("C : 'abcd' ;");
String grammar = grammarBuilder.toString();
String input ="ababcx";
String found = execLexer("L.g4", grammar, "L", input, false);
assertEquals(
"[@0,0:1='ab',<1>,1:0]\n" +
"[@1,2:4='abc',<2>,1:2]\n" +
"[@2,6:5='<EOF>',<-1>,1:6]\n", found);
assertEquals("line 1:5 token recognition error at: 'x'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testEnforcedGreedyNestedBrances_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(77);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("ACTION : '{' (ACTION | ~[{}])* '}';\n");
grammarBuilder.append("WS : [ \\r\\n\\t]+ -> skip;");
String grammar = grammarBuilder.toString();
String input ="{ { } }";
String found = execLexer("L.g4", grammar, "L", input, false);
assertEquals(
"[@0,0:6='{ { } }',<1>,1:0]\n" +
"[@1,7:6='<EOF>',<-1>,1:7]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testEnforcedGreedyNestedBrances_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(77);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("ACTION : '{' (ACTION | ~[{}])* '}';\n");
grammarBuilder.append("WS : [ \\r\\n\\t]+ -> skip;");
String grammar = grammarBuilder.toString();
String input ="{ { }";
String found = execLexer("L.g4", grammar, "L", input, false);
assertEquals("[@0,5:4='<EOF>',<-1>,1:5]\n", found);
assertEquals("line 1:0 token recognition error at: '{ { }'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testErrorInMiddle() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(28);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("A : 'abc' ;");
String grammar = grammarBuilder.toString();
String input ="abx";
String found = execLexer("L.g4", grammar, "L", input, false);
assertEquals("[@0,3:2='<EOF>',<-1>,1:3]\n", found);
assertEquals("line 1:0 token recognition error at: 'abx'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testInvalidCharAtStart() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(30);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("A : 'a' 'b' ;");
String grammar = grammarBuilder.toString();
String input ="x";
String found = execLexer("L.g4", grammar, "L", input, false);
assertEquals("[@0,1:0='<EOF>',<-1>,1:1]\n", found);
assertEquals("line 1:0 token recognition error at: 'x'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testInvalidCharAtStartAfterDFACache() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(30);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("A : 'a' 'b' ;");
String grammar = grammarBuilder.toString();
String input ="abx";
String found = execLexer("L.g4", grammar, "L", input, false);
assertEquals(
"[@0,0:1='ab',<1>,1:0]\n" +
"[@1,3:2='<EOF>',<-1>,1:3]\n", found);
assertEquals("line 1:2 token recognition error at: 'x'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testInvalidCharInToken() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(30);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("A : 'a' 'b' ;");
String grammar = grammarBuilder.toString();
String input ="ax";
String found = execLexer("L.g4", grammar, "L", input, false);
assertEquals("[@0,2:1='<EOF>',<-1>,1:2]\n", found);
assertEquals("line 1:0 token recognition error at: 'ax'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testInvalidCharInTokenAfterDFACache() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(30);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("A : 'a' 'b' ;");
String grammar = grammarBuilder.toString();
String input ="abax";
String found = execLexer("L.g4", grammar, "L", input, false);
assertEquals(
"[@0,0:1='ab',<1>,1:0]\n" +
"[@1,4:3='<EOF>',<-1>,1:4]\n", found);
assertEquals("line 1:2 token recognition error at: 'ax'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLexerExecDFA() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(98);
grammarBuilder.append("grammar L;\n");
grammarBuilder.append("start : ID ':' expr;\n");
grammarBuilder.append("expr : primary expr? {} | expr '->' ID;\n");
grammarBuilder.append("primary : ID;\n");
grammarBuilder.append("ID : [a-z]+;");
String grammar = grammarBuilder.toString();
String input ="x : x";
String found = execLexer("L.g4", grammar, "LLexer", input, false);
assertEquals(
"[@0,0:0='x',<3>,1:0]\n" +
"[@1,2:2=':',<1>,1:2]\n" +
"[@2,4:4='x',<3>,1:4]\n" +
"[@3,5:4='<EOF>',<-1>,1:5]\n", found);
assertEquals(
"line 1:1 token recognition error at: ' '\n" +
"line 1:3 token recognition error at: ' '\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testStringsEmbeddedInActions_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(109);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("ACTION2 : '[' (STRING | ~'\"')*? ']';\n");
grammarBuilder.append("STRING : '\"' ('\\\"' | .)*? '\"';\n");
grammarBuilder.append("WS : [ \\t\\r\\n]+ -> skip;");
String grammar = grammarBuilder.toString();
String input ="[\"foo\"]";
String found = execLexer("L.g4", grammar, "L", input, false);
assertEquals(
"[@0,0:6='[\"foo\"]',<1>,1:0]\n" +
"[@1,7:6='<EOF>',<-1>,1:7]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testStringsEmbeddedInActions_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(109);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("ACTION2 : '[' (STRING | ~'\"')*? ']';\n");
grammarBuilder.append("STRING : '\"' ('\\\"' | .)*? '\"';\n");
grammarBuilder.append("WS : [ \\t\\r\\n]+ -> skip;");
String grammar = grammarBuilder.toString();
String input ="[\"foo]";
String found = execLexer("L.g4", grammar, "L", input, false);
assertEquals("[@0,6:5='<EOF>',<-1>,1:6]\n", found);
assertEquals("line 1:0 token recognition error at: '[\"foo]'\n", this.stderrDuringParse);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,391 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python2;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class TestListeners extends BasePython2Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testBasic() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(511);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("if __name__ is not None and \".\" in __name__:\n");
grammarBuilder.append(" from .TListener import TListener\n");
grammarBuilder.append("else:\n");
grammarBuilder.append(" from TListener import TListener\n");
grammarBuilder.append("\n");
grammarBuilder.append("class LeafListener(TListener):\n");
grammarBuilder.append(" def visitTerminal(self, node):\n");
grammarBuilder.append(" print(node.symbol.text)\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("walker = ParseTreeWalker()\n");
grammarBuilder.append("walker.walk(TParser.LeafListener(), $ctx.r)\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a 1 2)\n" +
"1\n" +
"2\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLR() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(672);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("if __name__ is not None and \".\" in __name__:\n");
grammarBuilder.append(" from .TListener import TListener\n");
grammarBuilder.append("else:\n");
grammarBuilder.append(" from TListener import TListener\n");
grammarBuilder.append("\n");
grammarBuilder.append("class LeafListener(TListener):\n");
grammarBuilder.append(" def exitE(self, ctx):\n");
grammarBuilder.append(" if ctx.getChildCount()==3:\n");
grammarBuilder.append(" print(ctx.e(0).start.text + ' ' + ctx.e(1).start.text + ' ' + ctx.e()[0].start.text)\n");
grammarBuilder.append(" else:\n");
grammarBuilder.append(" print(ctx.INT().symbol.text)\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("walker = ParseTreeWalker()\n");
grammarBuilder.append("walker.walk(TParser.LeafListener(), $ctx.r)\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=e ;\n");
grammarBuilder.append("e : e op='*' e\n");
grammarBuilder.append(" | e op='+' e\n");
grammarBuilder.append(" | INT\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1+2*3";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(e (e 1) + (e (e 2) * (e 3)))\n" +
"1\n" +
"2\n" +
"3\n" +
"2 3 2\n" +
"1 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLRWithLabels() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(652);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("if __name__ is not None and \".\" in __name__:\n");
grammarBuilder.append(" from .TListener import TListener\n");
grammarBuilder.append("else:\n");
grammarBuilder.append(" from TListener import TListener\n");
grammarBuilder.append("\n");
grammarBuilder.append("class LeafListener(TListener):\n");
grammarBuilder.append(" def exitCall(self, ctx):\n");
grammarBuilder.append(" print(ctx.e().start.text + ' ' + str(ctx.eList()))\n");
grammarBuilder.append(" def exitInt(self, ctx):\n");
grammarBuilder.append(" print(ctx.INT().symbol.text)\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("walker = ParseTreeWalker()\n");
grammarBuilder.append("walker.walk(TParser.LeafListener(), $ctx.r)\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=e ;\n");
grammarBuilder.append("e : e '(' eList ')' # Call\n");
grammarBuilder.append(" | INT # Int\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("eList : e (',' e)* ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1(2,3)";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(e (e 1) ( (eList (e 2) , (e 3)) ))\n" +
"1\n" +
"2\n" +
"3\n" +
"1 [13 6]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testRuleGetters_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(697);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("if __name__ is not None and \".\" in __name__:\n");
grammarBuilder.append(" from .TListener import TListener\n");
grammarBuilder.append("else:\n");
grammarBuilder.append(" from TListener import TListener\n");
grammarBuilder.append("\n");
grammarBuilder.append("class LeafListener(TListener):\n");
grammarBuilder.append(" def exitA(self, ctx):\n");
grammarBuilder.append(" if ctx.getChildCount()==2:\n");
grammarBuilder.append(" print(ctx.b(0).start.text + ' ' + ctx.b(1).start.text + ' ' + ctx.b()[0].start.text)\n");
grammarBuilder.append(" else:\n");
grammarBuilder.append(" print(ctx.b(0).start.text)\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("walker = ParseTreeWalker()\n");
grammarBuilder.append("walker.walk(TParser.LeafListener(), $ctx.r)\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b b // forces list\n");
grammarBuilder.append(" | b // a list still\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : ID | INT;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a (b 1) (b 2))\n" +
"1 2 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testRuleGetters_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(697);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("if __name__ is not None and \".\" in __name__:\n");
grammarBuilder.append(" from .TListener import TListener\n");
grammarBuilder.append("else:\n");
grammarBuilder.append(" from TListener import TListener\n");
grammarBuilder.append("\n");
grammarBuilder.append("class LeafListener(TListener):\n");
grammarBuilder.append(" def exitA(self, ctx):\n");
grammarBuilder.append(" if ctx.getChildCount()==2:\n");
grammarBuilder.append(" print(ctx.b(0).start.text + ' ' + ctx.b(1).start.text + ' ' + ctx.b()[0].start.text)\n");
grammarBuilder.append(" else:\n");
grammarBuilder.append(" print(ctx.b(0).start.text)\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("walker = ParseTreeWalker()\n");
grammarBuilder.append("walker.walk(TParser.LeafListener(), $ctx.r)\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b b // forces list\n");
grammarBuilder.append(" | b // a list still\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : ID | INT;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a (b abc))\n" +
"abc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testTokenGetters_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(660);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("if __name__ is not None and \".\" in __name__:\n");
grammarBuilder.append(" from .TListener import TListener\n");
grammarBuilder.append("else:\n");
grammarBuilder.append(" from TListener import TListener\n");
grammarBuilder.append("\n");
grammarBuilder.append("class LeafListener(TListener):\n");
grammarBuilder.append(" def exitA(self, ctx):\n");
grammarBuilder.append(" if ctx.getChildCount()==2:\n");
grammarBuilder.append(" print(ctx.INT(0).symbol.text + ' ' + ctx.INT(1).symbol.text + ' ' + str_list(ctx.INT()))\n");
grammarBuilder.append(" else:\n");
grammarBuilder.append(" print(str(ctx.ID().symbol))\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("walker = ParseTreeWalker()\n");
grammarBuilder.append("walker.walk(TParser.LeafListener(), $ctx.r)\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="1 2";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a 1 2)\n" +
"1 2 [1, 2]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testTokenGetters_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(660);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::header {\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("if __name__ is not None and \".\" in __name__:\n");
grammarBuilder.append(" from .TListener import TListener\n");
grammarBuilder.append("else:\n");
grammarBuilder.append(" from TListener import TListener\n");
grammarBuilder.append("\n");
grammarBuilder.append("class LeafListener(TListener):\n");
grammarBuilder.append(" def exitA(self, ctx):\n");
grammarBuilder.append(" if ctx.getChildCount()==2:\n");
grammarBuilder.append(" print(ctx.INT(0).symbol.text + ' ' + ctx.INT(1).symbol.text + ' ' + str_list(ctx.INT()))\n");
grammarBuilder.append(" else:\n");
grammarBuilder.append(" print(str(ctx.ID().symbol))\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.r.toStringTree(recog=self))\n");
grammarBuilder.append("walker = ParseTreeWalker()\n");
grammarBuilder.append("walker.walk(TParser.LeafListener(), $ctx.r)\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : INT INT\n");
grammarBuilder.append(" | ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("MULT: '*' ;\n");
grammarBuilder.append("ADD : '+' ;\n");
grammarBuilder.append("INT : [0-9]+ ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"(a abc)\n" +
"[@0,0:2='abc',<4>,1:0]\n", found);
assertNull(this.stderrDuringParse);
}
}

View File

@ -0,0 +1,300 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python2;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class TestParseTrees extends BasePython2Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void test2AltLoop() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(136);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n");
grammarBuilder.append("self._buildParseTrees = True\n");
grammarBuilder.append("}\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($r.ctx.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : ('x' | 'y')* 'z'\n");
grammarBuilder.append(" ;");
String grammar = grammarBuilder.toString();
String input ="xyyxyxz";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("(a x y y x y x z)\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void test2Alts() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(129);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n");
grammarBuilder.append("self._buildParseTrees = True\n");
grammarBuilder.append("}\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($r.ctx.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : 'x' | 'y'\n");
grammarBuilder.append(" ;");
String grammar = grammarBuilder.toString();
String input ="y";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("(a y)\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAltNum() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(562);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("\n");
grammarBuilder.append("options { contextSuperClass=MyRuleNode; }\n");
grammarBuilder.append("\n");
grammarBuilder.append("@parser::members {\n");
grammarBuilder.append("class MyRuleNode(ParserRuleContext):\n");
grammarBuilder.append(" def __init__(self, parent = None, invokingStateNumber = None ):\n");
grammarBuilder.append(" super(TParser.MyRuleNode, self).__init__(parent, invokingStateNumber)\n");
grammarBuilder.append(" self.altNum = 0;\n");
grammarBuilder.append(" def getAltNumber(self):\n");
grammarBuilder.append(" return self.altNum\n");
grammarBuilder.append(" def setAltNumber(self, altNum):\n");
grammarBuilder.append(" self.altNum = altNum\n");
grammarBuilder.append("}\n");
grammarBuilder.append("\n");
grammarBuilder.append("\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n");
grammarBuilder.append("self._buildParseTrees = True\n");
grammarBuilder.append("}\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($r.ctx.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("\n");
grammarBuilder.append("a : 'f'\n");
grammarBuilder.append(" | 'g'\n");
grammarBuilder.append(" | 'x' b 'z'\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : 'e' {} | 'y'\n");
grammarBuilder.append(" ;");
String grammar = grammarBuilder.toString();
String input ="xyz";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("(a:3 x (b:2 y) z)\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testExtraToken() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(142);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n");
grammarBuilder.append("self._buildParseTrees = True\n");
grammarBuilder.append("}\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($r.ctx.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : 'x' 'y'\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("Z : 'z' \n");
grammarBuilder.append(" ;\n");
grammarBuilder.append(" ");
String grammar = grammarBuilder.toString();
String input ="xzy";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("(a x z y)\n", found);
assertEquals("line 1:1 extraneous input 'z' expecting 'y'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testNoViableAlt() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(144);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n");
grammarBuilder.append("self._buildParseTrees = True\n");
grammarBuilder.append("}\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($r.ctx.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : 'x' | 'y'\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("Z : 'z' \n");
grammarBuilder.append(" ;\n");
grammarBuilder.append(" ");
String grammar = grammarBuilder.toString();
String input ="z";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("(a z)\n", found);
assertEquals("line 1:0 mismatched input 'z' expecting {'x', 'y'}\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testRuleRef() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(138);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n");
grammarBuilder.append("self._buildParseTrees = True\n");
grammarBuilder.append("}\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($r.ctx.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : b 'x'\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : 'y' \n");
grammarBuilder.append(" ;");
String grammar = grammarBuilder.toString();
String input ="yx";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("(a (b y) x)\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSync() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(145);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n");
grammarBuilder.append("self._buildParseTrees = True\n");
grammarBuilder.append("}\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($r.ctx.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : 'x' 'y'* '!'\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("Z : 'z' \n");
grammarBuilder.append(" ;");
String grammar = grammarBuilder.toString();
String input ="xzyy!";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("(a x z y y !)\n", found);
assertEquals("line 1:1 extraneous input 'z' expecting {'y', '!'}\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testToken2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(127);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n");
grammarBuilder.append("self._buildParseTrees = True\n");
grammarBuilder.append("}\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($r.ctx.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : 'x' 'y'\n");
grammarBuilder.append(" ;");
String grammar = grammarBuilder.toString();
String input ="xy";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("(a x y)\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testTokenAndRuleContextString() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(173);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s\n");
grammarBuilder.append("@init {\n");
grammarBuilder.append("self._buildParseTrees = True\n");
grammarBuilder.append("}\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($r.ctx.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : r=a ;\n");
grammarBuilder.append("a : 'x' { \n");
grammarBuilder.append("print(str_list(self.getRuleInvocationStack()))\n");
grammarBuilder.append("} ;");
String grammar = grammarBuilder.toString();
String input ="x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"[a, s]\n" +
"(a x)\n", found);
assertNull(this.stderrDuringParse);
}
}

View File

@ -0,0 +1,718 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python2;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class TestParserErrors extends BasePython2Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testConjuringUpToken() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(61);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' x='b' {print(\"conjured=\" + str($x))} 'c' ;");
String grammar = grammarBuilder.toString();
String input ="ac";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("conjured=[@-1,-1:-1='<missing 'b'>',<2>,1:1]\n", found);
assertEquals("line 1:1 missing 'b' at 'c'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testConjuringUpTokenFromSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(67);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' x=('b'|'c') {print(\"conjured=\" + str($x))} 'd' ;");
String grammar = grammarBuilder.toString();
String input ="ad";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("conjured=[@-1,-1:-1='<missing 'b'>',<2>,1:1]\n", found);
assertEquals("line 1:1 missing {'b', 'c'} at 'd'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testContextListGetters() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(160);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@parser::members{\n");
grammarBuilder.append("def foo():\n");
grammarBuilder.append(" s = SContext()\n");
grammarBuilder.append(" a = s.a()\n");
grammarBuilder.append(" b = s.b()\n");
grammarBuilder.append("}\n");
grammarBuilder.append("s : (a | b)+;\n");
grammarBuilder.append("a : 'a' {print('a',end='')};\n");
grammarBuilder.append("b : 'b' {print('b',end='')};");
String grammar = grammarBuilder.toString();
String input ="abab";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals("abab\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDuplicatedLeftRecursiveCall_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(63);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("start : expr EOF;\n");
grammarBuilder.append("expr : 'x'\n");
grammarBuilder.append(" | expr expr\n");
grammarBuilder.append(" ;");
String grammar = grammarBuilder.toString();
String input ="x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "start", input, true);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDuplicatedLeftRecursiveCall_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(63);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("start : expr EOF;\n");
grammarBuilder.append("expr : 'x'\n");
grammarBuilder.append(" | expr expr\n");
grammarBuilder.append(" ;");
String grammar = grammarBuilder.toString();
String input ="xx";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "start", input, true);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDuplicatedLeftRecursiveCall_3() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(63);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("start : expr EOF;\n");
grammarBuilder.append("expr : 'x'\n");
grammarBuilder.append(" | expr expr\n");
grammarBuilder.append(" ;");
String grammar = grammarBuilder.toString();
String input ="xxx";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "start", input, true);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDuplicatedLeftRecursiveCall_4() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(63);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("start : expr EOF;\n");
grammarBuilder.append("expr : 'x'\n");
grammarBuilder.append(" | expr expr\n");
grammarBuilder.append(" ;");
String grammar = grammarBuilder.toString();
String input ="xxxx";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "start", input, true);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testInvalidATNStateRemoval() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(102);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("start : ID ':' expr;\n");
grammarBuilder.append("expr : primary expr? {pass} | expr '->' ID;\n");
grammarBuilder.append("primary : ID;\n");
grammarBuilder.append("ID : [a-z]+;");
String grammar = grammarBuilder.toString();
String input ="x:x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "start", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testInvalidEmptyInput() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(36);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("start : ID+;\n");
grammarBuilder.append("ID : [a-z]+;");
String grammar = grammarBuilder.toString();
String input ="";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "start", input, true);
assertEquals("", found);
assertEquals("line 1:0 missing ID at '<EOF>'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLL1ErrorInfo() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(314);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("start : animal (AND acClass)? service EOF;\n");
grammarBuilder.append("animal : (DOG | CAT );\n");
grammarBuilder.append("service : (HARDWARE | SOFTWARE) ;\n");
grammarBuilder.append("AND : 'and';\n");
grammarBuilder.append("DOG : 'dog';\n");
grammarBuilder.append("CAT : 'cat';\n");
grammarBuilder.append("HARDWARE: 'hardware';\n");
grammarBuilder.append("SOFTWARE: 'software';\n");
grammarBuilder.append("WS : ' ' -> skip ;\n");
grammarBuilder.append("acClass\n");
grammarBuilder.append("@init\n");
grammarBuilder.append("{print(self.getExpectedTokens().toString(self.literalNames, self.symbolicNames))}\n");
grammarBuilder.append(" : ;");
String grammar = grammarBuilder.toString();
String input ="dog and software";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "start", input, false);
assertEquals("{'hardware', 'software'}\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLL2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(46);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' 'b'\n");
grammarBuilder.append(" | 'a' 'c'\n");
grammarBuilder.append(";\n");
grammarBuilder.append("q : 'e' ;");
String grammar = grammarBuilder.toString();
String input ="ae";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:1 no viable alternative at input 'ae'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLL3() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(55);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' 'b'* 'c'\n");
grammarBuilder.append(" | 'a' 'b' 'd'\n");
grammarBuilder.append(";\n");
grammarBuilder.append("q : 'e' ;");
String grammar = grammarBuilder.toString();
String input ="abe";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:2 no viable alternative at input 'abe'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLLStar() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(48);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a'+ 'b'\n");
grammarBuilder.append(" | 'a'+ 'c'\n");
grammarBuilder.append(";\n");
grammarBuilder.append("q : 'e' ;");
String grammar = grammarBuilder.toString();
String input ="aaae";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:3 no viable alternative at input 'aaae'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testMultiTokenDeletionBeforeLoop() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(28);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' 'b'* 'c';");
String grammar = grammarBuilder.toString();
String input ="aacabc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:1 extraneous input 'a' expecting {'b', 'c'}\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testMultiTokenDeletionBeforeLoop2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(40);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' ('b'|'z'{pass})* 'c';");
String grammar = grammarBuilder.toString();
String input ="aacabc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:1 extraneous input 'a' expecting {'b', 'z', 'c'}\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testMultiTokenDeletionDuringLoop() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(29);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' 'b'* 'c' ;");
String grammar = grammarBuilder.toString();
String input ="abaaababc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals(
"line 1:2 extraneous input 'a' expecting {'b', 'c'}\n" +
"line 1:6 extraneous input 'a' expecting {'b', 'c'}\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testMultiTokenDeletionDuringLoop2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(41);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' ('b'|'z'{pass})* 'c' ;");
String grammar = grammarBuilder.toString();
String input ="abaaababc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals(
"line 1:2 extraneous input 'a' expecting {'b', 'z', 'c'}\n" +
"line 1:6 extraneous input 'a' expecting {'b', 'z', 'c'}\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testNoViableAltAvoidance() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(83);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : e '!' ;\n");
grammarBuilder.append("e : 'a' 'b'\n");
grammarBuilder.append(" | 'a'\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("DOT : '.' ;\n");
grammarBuilder.append("WS : [ \\t\\r\\n]+ -> skip;");
String grammar = grammarBuilder.toString();
String input ="a.";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("", found);
assertEquals("line 1:1 mismatched input '.' expecting '!'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSingleSetInsertion() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(34);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' ('b'|'c') 'd' ;");
String grammar = grammarBuilder.toString();
String input ="ad";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:1 missing {'b', 'c'} at 'd'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSingleSetInsertionConsumption() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(80);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("myset: ('b'|'c') ;\n");
grammarBuilder.append("a: 'a' myset 'd' {print(\"\" + str($myset.stop))} ; ");
String grammar = grammarBuilder.toString();
String input ="ad";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("[@0,0:0='a',<3>,1:0]\n", found);
assertEquals("line 1:1 missing {'b', 'c'} at 'd'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSingleTokenDeletion() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(24);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' 'b' ;");
String grammar = grammarBuilder.toString();
String input ="aab";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:1 extraneous input 'a' expecting 'b'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSingleTokenDeletionBeforeAlt() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(38);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : ('b' | 'c')\n");
grammarBuilder.append(";\n");
grammarBuilder.append("q : 'a'\n");
grammarBuilder.append(";");
String grammar = grammarBuilder.toString();
String input ="ac";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:0 extraneous input 'a' expecting {'b', 'c'}\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSingleTokenDeletionBeforeLoop() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(25);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' 'b'* ;");
String grammar = grammarBuilder.toString();
String input ="aabc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals(
"line 1:1 extraneous input 'a' expecting {<EOF>, 'b'}\n" +
"line 1:3 token recognition error at: 'c'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSingleTokenDeletionBeforeLoop2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(36);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' ('b'|'z'{pass})*;");
String grammar = grammarBuilder.toString();
String input ="aabc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals(
"line 1:1 extraneous input 'a' expecting {<EOF>, 'b', 'z'}\n" +
"line 1:3 token recognition error at: 'c'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSingleTokenDeletionBeforePredict() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(48);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a'+ 'b'\n");
grammarBuilder.append(" | 'a'+ 'c'\n");
grammarBuilder.append(";\n");
grammarBuilder.append("q : 'e' ;");
String grammar = grammarBuilder.toString();
String input ="caaab";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:0 extraneous input 'c' expecting 'a'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSingleTokenDeletionConsumption() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(80);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("myset: ('b'|'c') ;\n");
grammarBuilder.append("a: 'a' myset 'd' {print(\"\" + str($myset.stop))} ; ");
String grammar = grammarBuilder.toString();
String input ="aabd";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("[@2,2:2='b',<1>,1:2]\n", found);
assertEquals("line 1:1 extraneous input 'a' expecting {'b', 'c'}\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSingleTokenDeletionDuringLoop() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(29);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' 'b'* 'c' ;");
String grammar = grammarBuilder.toString();
String input ="ababbc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:2 extraneous input 'a' expecting {'b', 'c'}\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSingleTokenDeletionDuringLoop2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(41);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' ('b'|'z'{pass})* 'c' ;");
String grammar = grammarBuilder.toString();
String input ="ababbc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:2 extraneous input 'a' expecting {'b', 'z', 'c'}\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSingleTokenDeletionExpectingSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(30);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' ('b'|'c') ;");
String grammar = grammarBuilder.toString();
String input ="aab";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:1 extraneous input 'a' expecting {'b', 'c'}\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSingleTokenInsertion() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(28);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' 'b' 'c' ;");
String grammar = grammarBuilder.toString();
String input ="ac";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:1 missing 'b' at 'c'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testTokenMismatch() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(24);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : 'a' 'b' ;");
String grammar = grammarBuilder.toString();
String input ="aa";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertEquals("line 1:1 mismatched input 'a' expecting 'b'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testTokenMismatch2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(165);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("\n");
grammarBuilder.append("stat: ( '(' expr? ')' )? EOF ;\n");
grammarBuilder.append("expr: ID '=' STR ;\n");
grammarBuilder.append("\n");
grammarBuilder.append("ERR : '~FORCE_ERROR~' ;\n");
grammarBuilder.append("ID : [a-zA-Z]+ ;\n");
grammarBuilder.append("STR : '\"' ~[\"]* '\"' ;\n");
grammarBuilder.append("WS : [ \\t\\r\\n]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="( ~FORCE_ERROR~ ";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "stat", input, false);
assertEquals("", found);
assertEquals("line 1:2 mismatched input '~FORCE_ERROR~' expecting ')'\n", this.stderrDuringParse);
}
}

View File

@ -0,0 +1,790 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python2;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class TestParserExec extends BasePython2Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAPlus() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(77);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : ID+ {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="a b c";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("abc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAStar_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(77);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : ID* {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAStar_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(77);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : ID* {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="a b c";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("abc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAorAPlus() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(82);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : (ID|ID)+ {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="a b c";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("abc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAorAStar_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(82);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : (ID|ID)* {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAorAStar_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(82);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : (ID|ID)* {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="a b c";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("abc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAorB() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(122);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : ID {\n");
grammarBuilder.append("print(\"alt 1\")\n");
grammarBuilder.append("} | INT {\n");
grammarBuilder.append("print(\"alt 2\")\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="34";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("alt 2\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAorBPlus() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(105);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : (ID|INT{\n");
grammarBuilder.append("})+ {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="a 34 c";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("a34c\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAorBStar_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(105);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : (ID|INT{\n");
grammarBuilder.append("})* {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAorBStar_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(105);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : (ID|INT{\n");
grammarBuilder.append("})* {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="a 34 c";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("a34c\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testBasic() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(98);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : ID INT {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="abc 34";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("abc34\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testEOFInClosure() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(53);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("prog : stat EOF;\n");
grammarBuilder.append("stat : 'x' ('y' | EOF)*?;");
String grammar = grammarBuilder.toString();
String input ="x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "prog", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testIfIfElseGreedyBinding1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(186);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("start : statement+ ;\n");
grammarBuilder.append("statement : 'x' | ifStatement;\n");
grammarBuilder.append("ifStatement : 'if' 'y' statement ('else' statement)? {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> channel(HIDDEN);");
String grammar = grammarBuilder.toString();
String input ="if y if y x else x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "start", input, false);
assertEquals(
"if y x else x\n" +
"if y if y x else x\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testIfIfElseGreedyBinding2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(186);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("start : statement+ ;\n");
grammarBuilder.append("statement : 'x' | ifStatement;\n");
grammarBuilder.append("ifStatement : 'if' 'y' statement ('else' statement|) {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> channel(HIDDEN);");
String grammar = grammarBuilder.toString();
String input ="if y if y x else x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "start", input, false);
assertEquals(
"if y x else x\n" +
"if y if y x else x\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testIfIfElseNonGreedyBinding1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(187);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("start : statement+ ;\n");
grammarBuilder.append("statement : 'x' | ifStatement;\n");
grammarBuilder.append("ifStatement : 'if' 'y' statement ('else' statement)?? {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> channel(HIDDEN);");
String grammar = grammarBuilder.toString();
String input ="if y if y x else x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "start", input, false);
assertEquals(
"if y x\n" +
"if y if y x else x\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testIfIfElseNonGreedyBinding2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(186);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("start : statement+ ;\n");
grammarBuilder.append("statement : 'x' | ifStatement;\n");
grammarBuilder.append("ifStatement : 'if' 'y' statement (|'else' statement) {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> channel(HIDDEN);");
String grammar = grammarBuilder.toString();
String input ="if y if y x else x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "start", input, false);
assertEquals(
"if y x\n" +
"if y if y x else x\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLL1OptionalBlock_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(103);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : (ID|{}INT)? {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+;\n");
grammarBuilder.append("INT : '0'..'9'+ ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLL1OptionalBlock_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(103);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : (ID|{}INT)? {\n");
grammarBuilder.append("print($text)\n");
grammarBuilder.append("};\n");
grammarBuilder.append("ID : 'a'..'z'+;\n");
grammarBuilder.append("INT : '0'..'9'+ ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="a";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("a\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLabelAliasingAcrossLabeledAlternatives() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(157);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("start : a* EOF;\n");
grammarBuilder.append("a\n");
grammarBuilder.append(" : label=subrule {print($label.text)} #One\n");
grammarBuilder.append(" | label='y' {print($label.text)} #Two\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("subrule : 'x';\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="xy";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "start", input, false);
assertEquals(
"x\n" +
"y\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLabels() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(118);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : b1=b b2+=b* b3+=';' ;\n");
grammarBuilder.append("b : id_=ID val+=INT*;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc 34;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testListLabelForClosureContext() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(456);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("ifStatement\n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("assert isinstance(v, (list, tuple))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : 'if' expression\n");
grammarBuilder.append(" ( ( 'then'\n");
grammarBuilder.append(" executableStatement*\n");
grammarBuilder.append(" elseIfStatement* // <--- problem is here; should yield a list not node\n");
grammarBuilder.append(" elseStatement?\n");
grammarBuilder.append(" 'end' 'if'\n");
grammarBuilder.append(" ) | executableStatement )\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("\n");
grammarBuilder.append("elseIfStatement\n");
grammarBuilder.append(" : 'else' 'if' expression 'then' executableStatement*\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("expression : 'a' ;\n");
grammarBuilder.append("executableStatement : 'a' ;\n");
grammarBuilder.append("elseStatement : 'a' ;");
String grammar = grammarBuilder.toString();
String input ="a";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "expression", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testListLabelsOnSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(140);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : b b* ';' ;\n");
grammarBuilder.append("b : ID val+=(INT | FLOAT)*;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("FLOAT : [0-9]+ '.' [0-9]+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc 34;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testMultipleEOFHandling() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(42);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("prog : ('x' | 'x' 'y') EOF EOF;");
String grammar = grammarBuilder.toString();
String input ="x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "prog", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testOptional_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(90);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("stat : ifstat | 'x';\n");
grammarBuilder.append("ifstat : 'if' stat ('else' stat)?;\n");
grammarBuilder.append("WS : [ \\n\\t]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "stat", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testOptional_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(90);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("stat : ifstat | 'x';\n");
grammarBuilder.append("ifstat : 'if' stat ('else' stat)?;\n");
grammarBuilder.append("WS : [ \\n\\t]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="if x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "stat", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testOptional_3() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(90);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("stat : ifstat | 'x';\n");
grammarBuilder.append("ifstat : 'if' stat ('else' stat)?;\n");
grammarBuilder.append("WS : [ \\n\\t]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="if x else x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "stat", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testOptional_4() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(90);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("stat : ifstat | 'x';\n");
grammarBuilder.append("ifstat : 'if' stat ('else' stat)?;\n");
grammarBuilder.append("WS : [ \\n\\t]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="if if x else x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "stat", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testParserProperty() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(153);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@members {\n");
grammarBuilder.append("def Property(self):\n");
grammarBuilder.append(" return True\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("a : {$parser.Property()}? ID {print(\"valid\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("valid\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testPredicatedIfIfElse() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(175);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : stmt EOF ;\n");
grammarBuilder.append("stmt : ifStmt | ID;\n");
grammarBuilder.append("ifStmt : 'if' ID stmt ('else' stmt | { self._input.LA(1)!=TParser.ELSE }?);\n");
grammarBuilder.append("ELSE : 'else';\n");
grammarBuilder.append("ID : [a-zA-Z]+;\n");
grammarBuilder.append("WS : [ \\n\\t]+ -> skip;");
String grammar = grammarBuilder.toString();
String input ="if x if x a else b";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testPredictionIssue334() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(244);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("file_ @init{\n");
grammarBuilder.append("self._errHandler = BailErrorStrategy()\n");
grammarBuilder.append("} \n");
grammarBuilder.append("@after {\n");
grammarBuilder.append("print($ctx.toStringTree(recog=self))\n");
grammarBuilder.append("}\n");
grammarBuilder.append(" : item (SEMICOLON item)* SEMICOLON? EOF ;\n");
grammarBuilder.append("item : A B?;\n");
grammarBuilder.append("SEMICOLON: ';';\n");
grammarBuilder.append("A : 'a'|'A';\n");
grammarBuilder.append("B : 'b'|'B';\n");
grammarBuilder.append("WS : [ \\r\\t\\n]+ -> skip;");
String grammar = grammarBuilder.toString();
String input ="a";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "file_", input, false);
assertEquals("(file_ (item a) <EOF>)\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testReferenceToATN_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(106);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : (ID|ATN)* ATN? {print($text)} ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("ATN : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testReferenceToATN_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(106);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : (ID|ATN)* ATN? {print($text)} ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("ATN : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="a 34 c";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("a34c\n", found);
assertNull(this.stderrDuringParse);
}
}

View File

@ -0,0 +1,217 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python2;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class TestPerformance extends BasePython2Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testExpressionGrammar_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(164);
grammarBuilder.append("grammar Expr;\n");
grammarBuilder.append("\n");
grammarBuilder.append("program: expr EOF;\n");
grammarBuilder.append("\n");
grammarBuilder.append("expr\n");
grammarBuilder.append(" : ID\n");
grammarBuilder.append(" | 'not' expr\n");
grammarBuilder.append(" | expr 'and' expr\n");
grammarBuilder.append(" | expr 'or' expr\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("\n");
grammarBuilder.append("ID: [a-zA-Z_][a-zA-Z_0-9]*;\n");
grammarBuilder.append("WS: [ \\t\\n\\r\\f]+ -> skip;\n");
grammarBuilder.append("ERROR: .;");
String grammar = grammarBuilder.toString();
String input =
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
" X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and X12";
String found = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "ExprListener", "ExprVisitor", "program", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testExpressionGrammar_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(164);
grammarBuilder.append("grammar Expr;\n");
grammarBuilder.append("\n");
grammarBuilder.append("program: expr EOF;\n");
grammarBuilder.append("\n");
grammarBuilder.append("expr\n");
grammarBuilder.append(" : ID\n");
grammarBuilder.append(" | 'not' expr\n");
grammarBuilder.append(" | expr 'and' expr\n");
grammarBuilder.append(" | expr 'or' expr\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("\n");
grammarBuilder.append("ID: [a-zA-Z_][a-zA-Z_0-9]*;\n");
grammarBuilder.append("WS: [ \\t\\n\\r\\f]+ -> skip;\n");
grammarBuilder.append("ERROR: .;");
String grammar = grammarBuilder.toString();
String input =
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
" X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
" X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
" X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
" X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
" X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
" X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
" X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
" X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
" X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
" X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and X6 and not X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and X7 and not X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and X8 and not X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and X9 and not X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and X10 and not X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and X11 and not X12 or\n" +
"not X1 and not X2 and not X3 and not X4 and not X5 and not X6 and not X7 and not X8 and not X9 and not X10 and not X11 and X12";
String found = execParser("Expr.g4", grammar, "ExprParser", "ExprLexer", "ExprListener", "ExprVisitor", "program", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
}

View File

@ -0,0 +1,217 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python2;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class TestSemPredEvalLexer extends BasePython2Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDisableRule() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(131);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("E1 : 'enum' { False }? ;\n");
grammarBuilder.append("E2 : 'enum' { True }? ; // winner not E1 or ID\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="enum abc";
String found = execLexer("L.g4", grammar, "L", input, true);
assertEquals(
"[@0,0:3='enum',<2>,1:0]\n" +
"[@1,5:7='abc',<3>,1:5]\n" +
"[@2,8:7='<EOF>',<-1>,1:8]\n" +
"s0-' '->:s5=>4\n" +
"s0-'a'->:s6=>3\n" +
"s0-'e'->:s1=>3\n" +
":s1=>3-'n'->:s2=>3\n" +
":s2=>3-'u'->:s3=>3\n" +
":s6=>3-'b'->:s6=>3\n" +
":s6=>3-'c'->:s6=>3\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testEnumNotID() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(96);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("ENUM : [a-z]+ { self.text==\"enum\" }? ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="enum abc enum";
String found = execLexer("L.g4", grammar, "L", input, true);
assertEquals(
"[@0,0:3='enum',<1>,1:0]\n" +
"[@1,5:7='abc',<2>,1:5]\n" +
"[@2,9:12='enum',<1>,1:9]\n" +
"[@3,13:12='<EOF>',<-1>,1:13]\n" +
"s0-' '->:s3=>3\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testIDnotEnum() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(84);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("ENUM : [a-z]+ { False }? ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="enum abc enum";
String found = execLexer("L.g4", grammar, "L", input, true);
assertEquals(
"[@0,0:3='enum',<2>,1:0]\n" +
"[@1,5:7='abc',<2>,1:5]\n" +
"[@2,9:12='enum',<2>,1:9]\n" +
"[@3,13:12='<EOF>',<-1>,1:13]\n" +
"s0-' '->:s2=>3\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testIDvsEnum() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(85);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("ENUM : 'enum' { False }? ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input ="enum abc enum";
String found = execLexer("L.g4", grammar, "L", input, true);
assertEquals(
"[@0,0:3='enum',<2>,1:0]\n" +
"[@1,5:7='abc',<2>,1:5]\n" +
"[@2,9:12='enum',<2>,1:9]\n" +
"[@3,13:12='<EOF>',<-1>,1:13]\n" +
"s0-' '->:s5=>3\n" +
"s0-'a'->:s4=>2\n" +
"s0-'e'->:s1=>2\n" +
":s1=>2-'n'->:s2=>2\n" +
":s2=>2-'u'->:s3=>2\n" +
":s4=>2-'b'->:s4=>2\n" +
":s4=>2-'c'->:s4=>2\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testIndent() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(135);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("INDENT : [ \\t]+ { self._tokenStartColumn==0 }?\n");
grammarBuilder.append(" { print(\"INDENT\") } ;\n");
grammarBuilder.append("NL : '\\n';\n");
grammarBuilder.append("WS : [ \\t]+ ;");
String grammar = grammarBuilder.toString();
String input =
"abc\n" +
" def \n";
String found = execLexer("L.g4", grammar, "L", input, true);
assertEquals(
"INDENT\n" +
"[@0,0:2='abc',<1>,1:0]\n" +
"[@1,3:3='\\n',<3>,1:3]\n" +
"[@2,4:5=' ',<2>,2:0]\n" +
"[@3,6:8='def',<1>,2:2]\n" +
"[@4,9:10=' ',<4>,2:5]\n" +
"[@5,11:11='\\n',<3>,2:7]\n" +
"[@6,12:11='<EOF>',<-1>,3:0]\n" +
"s0-'\n" +
"'->:s2=>3\n" +
"s0-'a'->:s1=>1\n" +
"s0-'d'->:s1=>1\n" +
":s1=>1-'b'->:s1=>1\n" +
":s1=>1-'c'->:s1=>1\n" +
":s1=>1-'e'->:s1=>1\n" +
":s1=>1-'f'->:s1=>1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLexerInputPositionSensitivePredicates() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(206);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("WORD1 : ID1+ { print(self.text) } ;\n");
grammarBuilder.append("WORD2 : ID2+ { print(self.text) } ;\n");
grammarBuilder.append("fragment ID1 : { self.column < 2 }? [a-zA-Z];\n");
grammarBuilder.append("fragment ID2 : { self.column >= 2 }? [a-zA-Z];\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip;");
String grammar = grammarBuilder.toString();
String input =
"a cde\n" +
"abcde\n";
String found = execLexer("L.g4", grammar, "L", input, true);
assertEquals(
"a\n" +
"cde\n" +
"ab\n" +
"cde\n" +
"[@0,0:0='a',<1>,1:0]\n" +
"[@1,2:4='cde',<2>,1:2]\n" +
"[@2,6:7='ab',<1>,2:0]\n" +
"[@3,8:10='cde',<2>,2:2]\n" +
"[@4,12:11='<EOF>',<-1>,3:0]\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testPredicatedKeywords() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(142);
grammarBuilder.append("lexer grammar L;\n");
grammarBuilder.append("ENUM : [a-z]+ { self.text==\"enum\" }? { print(\"enum!\") } ;\n");
grammarBuilder.append("ID : [a-z]+ { print(\"ID \" + self.text) } ;\n");
grammarBuilder.append("WS : [ \\n] -> skip ;");
String grammar = grammarBuilder.toString();
String input ="enum enu a";
String found = execLexer("L.g4", grammar, "L", input, false);
assertEquals(
"enum!\n" +
"ID enu\n" +
"ID a\n" +
"[@0,0:3='enum',<1>,1:0]\n" +
"[@1,5:7='enu',<2>,1:5]\n" +
"[@2,9:9='a',<2>,1:9]\n" +
"[@3,10:9='<EOF>',<-1>,1:10]\n", found);
assertNull(this.stderrDuringParse);
}
}

View File

@ -0,0 +1,761 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python2;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class TestSemPredEvalParser extends BasePython2Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void test2UnpredicatedAlts() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(276);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : {self._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION} a ';' a; // do 2x: once in ATN, next in DFA\n");
grammarBuilder.append("a : ID {print(\"alt 1\")}\n");
grammarBuilder.append(" | ID {print(\"alt 2\")}\n");
grammarBuilder.append(" | {False}? ID {print(\"alt 3\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="x; y";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals(
"alt 1\n" +
"alt 1\n", found);
assertEquals(
"line 1:0 reportAttemptingFullContext d=0 (a), input='x'\n" +
"line 1:0 reportAmbiguity d=0 (a): ambigAlts={1, 2}, input='x'\n" +
"line 1:3 reportAttemptingFullContext d=0 (a), input='y'\n" +
"line 1:3 reportAmbiguity d=0 (a): ambigAlts={1, 2}, input='y'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void test2UnpredicatedAltsAndOneOrthogonalAlt() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(321);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : {self._interp.predictionMode = PredictionMode.LL_EXACT_AMBIG_DETECTION} a ';' a ';' a;\n");
grammarBuilder.append("a : INT {print(\"alt 1\")}\n");
grammarBuilder.append(" | ID {print(\"alt 2\")} // must pick this one for ID since pred is false\n");
grammarBuilder.append(" | ID {print(\"alt 3\")}\n");
grammarBuilder.append(" | {False}? ID {print(\"alt 4\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="34; x; y";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, true);
assertEquals(
"alt 1\n" +
"alt 2\n" +
"alt 2\n", found);
assertEquals(
"line 1:4 reportAttemptingFullContext d=0 (a), input='x'\n" +
"line 1:4 reportAmbiguity d=0 (a): ambigAlts={2, 3}, input='x'\n" +
"line 1:7 reportAttemptingFullContext d=0 (a), input='y'\n" +
"line 1:7 reportAmbiguity d=0 (a): ambigAlts={2, 3}, input='y'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testActionHidesPreds() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(204);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@members {i = 0}\n");
grammarBuilder.append("s : a+ ;\n");
grammarBuilder.append("a : {self.i = 1} ID {self.i == 1}? {print(\"alt 1\")}\n");
grammarBuilder.append(" | {self.i = 2} ID {self.i == 2}? {print(\"alt 2\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="x x y";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"alt 1\n" +
"alt 1\n" +
"alt 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testActionsHidePredsInGlobalFOLLOW() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(269);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@members {\n");
grammarBuilder.append("def pred(self, v):\n");
grammarBuilder.append(" print('eval=' + str(v).lower())\n");
grammarBuilder.append(" return v\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("s : e {} {self.pred(True)}? {print(\"parse\")} '!' ;\n");
grammarBuilder.append("t : e {} {self.pred(False)}? ID ;\n");
grammarBuilder.append("e : ID | ; // non-LL(1) so we use ATN\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="a!";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"eval=true\n" +
"parse\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testAtomWithClosureInTranslatedLRRule() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(94);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("start : e[0] EOF;\n");
grammarBuilder.append("e[int _p]\n");
grammarBuilder.append(" : ( 'a' | 'b'+ ) ( {3 >= $_p}? '+' e[4] )*\n");
grammarBuilder.append(" ;\n");
String grammar = grammarBuilder.toString();
String input ="a+b+a";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "start", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDepedentPredsInGlobalFOLLOW() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(292);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@members {\n");
grammarBuilder.append("def pred(self, v):\n");
grammarBuilder.append(" print('eval=' + str(v).lower())\n");
grammarBuilder.append(" return v\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("s : a[99] ;\n");
grammarBuilder.append("a[int i] : e {self.pred($i==99)}? {print(\"parse\")} '!' ;\n");
grammarBuilder.append("b[int i] : e {self.pred($i==99)}? ID ;\n");
grammarBuilder.append("e : ID | ; // non-LL(1) so we use ATN\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="a!";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"eval=true\n" +
"parse\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDependentPredNotInOuterCtxShouldBeIgnored() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(256);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : b[2] ';' | b[2] '.' ; // decision in s drills down to ctx-dependent pred in a;\n");
grammarBuilder.append("b[int i] : a[i] ;\n");
grammarBuilder.append("a[int i]\n");
grammarBuilder.append(" : {$i==1}? ID {print(\"alt 1\")}\n");
grammarBuilder.append(" | {$i==2}? ID {print(\"alt 2\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;\n");
String grammar = grammarBuilder.toString();
String input ="a;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("alt 2\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testDisabledAlternative() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(121);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("cppCompilationUnit : content+ EOF;\n");
grammarBuilder.append("content: anything | {False}? .;\n");
grammarBuilder.append("anything: ANY_CHAR;\n");
grammarBuilder.append("ANY_CHAR: [_a-zA-Z0-9];");
String grammar = grammarBuilder.toString();
String input ="hello";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "cppCompilationUnit", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testIndependentPredNotPassedOuterCtxToAvoidCastException() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(169);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : b ';' | b '.' ;\n");
grammarBuilder.append("b : a ;\n");
grammarBuilder.append("a\n");
grammarBuilder.append(" : {False}? ID {print(\"alt 1\")}\n");
grammarBuilder.append(" | {True}? ID {print(\"alt 2\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="a;";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("alt 2\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testNoTruePredsThrowsNoViableAlt() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(157);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : a a;\n");
grammarBuilder.append("a : {False}? ID INT {print(\"alt 1\")}\n");
grammarBuilder.append(" | {False}? ID INT {print(\"alt 2\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="y 3 x 4";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("", found);
assertEquals("line 1:0 no viable alternative at input 'y'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testOrder() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(283);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : a {} a; // do 2x: once in ATN, next in DFA;\n");
grammarBuilder.append("// action blocks lookahead from falling off of 'a'\n");
grammarBuilder.append("// and looking into 2nd 'a' ref. !ctx dependent pred\n");
grammarBuilder.append("a : ID {print(\"alt 1\")}\n");
grammarBuilder.append(" | {True}? ID {print(\"alt 2\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="x y";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"alt 1\n" +
"alt 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testPredFromAltTestedInLoopBack_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(203);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("file_\n");
grammarBuilder.append("@after {print($ctx.toStringTree(recog=self))}\n");
grammarBuilder.append(" : para para EOF ;\n");
grammarBuilder.append("para: paraContent NL NL ;\n");
grammarBuilder.append("paraContent : ('s'|'x'|{self._input.LA(2)!=TParser.NL}? NL)+ ;\n");
grammarBuilder.append("NL : '\\n' ;\n");
grammarBuilder.append("s : 's' ;\n");
grammarBuilder.append("X : 'x' ;");
String grammar = grammarBuilder.toString();
String input =
"s\n" +
"\n" +
"\n" +
"x\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "file_", input, true);
assertEquals("(file_ (para (paraContent s) \\n \\n) (para (paraContent \\n x \\n)) <EOF>)\n", found);
assertEquals(
"line 5:0 mismatched input '<EOF>' expecting '\n" +
"'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testPredFromAltTestedInLoopBack_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(203);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("file_\n");
grammarBuilder.append("@after {print($ctx.toStringTree(recog=self))}\n");
grammarBuilder.append(" : para para EOF ;\n");
grammarBuilder.append("para: paraContent NL NL ;\n");
grammarBuilder.append("paraContent : ('s'|'x'|{self._input.LA(2)!=TParser.NL}? NL)+ ;\n");
grammarBuilder.append("NL : '\\n' ;\n");
grammarBuilder.append("s : 's' ;\n");
grammarBuilder.append("X : 'x' ;");
String grammar = grammarBuilder.toString();
String input =
"s\n" +
"\n" +
"\n" +
"x\n" +
"\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "file_", input, true);
assertEquals("(file_ (para (paraContent s) \\n \\n) (para (paraContent \\n x) \\n \\n) <EOF>)\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testPredTestedEvenWhenUnAmbig_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(184);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@members {enumKeyword = True}\n");
grammarBuilder.append("primary\n");
grammarBuilder.append(" : ID {print(\"ID \"+$ID.text)}\n");
grammarBuilder.append(" | {not self.enumKeyword}? 'enum' {print(\"enum\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n\\r]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="abc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "primary", input, false);
assertEquals("ID abc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testPredTestedEvenWhenUnAmbig_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(184);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@members {enumKeyword = True}\n");
grammarBuilder.append("primary\n");
grammarBuilder.append(" : ID {print(\"ID \"+$ID.text)}\n");
grammarBuilder.append(" | {not self.enumKeyword}? 'enum' {print(\"enum\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : [a-z]+ ;\n");
grammarBuilder.append("WS : [ \\t\\n\\r]+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="enum";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "primary", input, false);
assertEquals("", found);
assertEquals("line 1:0 no viable alternative at input 'enum'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testPredicateDependentOnArg() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(181);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@members {i = 0}\n");
grammarBuilder.append("s : a[2] a[1];\n");
grammarBuilder.append("a[int i]\n");
grammarBuilder.append(" : {$i==1}? ID {print(\"alt 1\")}\n");
grammarBuilder.append(" | {$i==2}? ID {print(\"alt 2\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="a b";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"alt 2\n" +
"alt 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testPredicateDependentOnArg2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(149);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@members {i = 0}\n");
grammarBuilder.append("s : a[2] a[1];\n");
grammarBuilder.append("a[int i]\n");
grammarBuilder.append(" : {$i==1}? ID \n");
grammarBuilder.append(" | {$i==2}? ID \n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="a b";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testPredsInGlobalFOLLOW() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(263);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@members {\n");
grammarBuilder.append("def pred(self, v):\n");
grammarBuilder.append(" print('eval=' + str(v).lower())\n");
grammarBuilder.append(" return v\n");
grammarBuilder.append("\n");
grammarBuilder.append("}\n");
grammarBuilder.append("s : e {self.pred(True)}? {print(\"parse\")} '!' ;\n");
grammarBuilder.append("t : e {self.pred(False)}? ID ;\n");
grammarBuilder.append("e : ID | ; // non-LL(1) so we use ATN\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="a!";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"eval=true\n" +
"parse\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testRewindBeforePredEval() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(201);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : a a;\n");
grammarBuilder.append("a : {self._input.LT(1).text==\"x\"}? ID INT {print(\"alt 1\")}\n");
grammarBuilder.append(" | {self._input.LT(1).text==\"y\"}? ID INT {print(\"alt 2\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="y 3 x 4";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"alt 2\n" +
"alt 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSimple() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(235);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : a a a; // do 3x: once in ATN, next in DFA then INT in ATN\n");
grammarBuilder.append("a : {False}? ID {print(\"alt 1\")}\n");
grammarBuilder.append(" | {True}? ID {print(\"alt 2\")}\n");
grammarBuilder.append(" | INT {print(\"alt 3\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="x y 3";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"alt 2\n" +
"alt 2\n" +
"alt 3\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSimpleValidate() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(150);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : a ;\n");
grammarBuilder.append("a : {False}? ID {print(\"alt 1\")}\n");
grammarBuilder.append(" | {True}? INT {print(\"alt 2\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("", found);
assertEquals("line 1:0 no viable alternative at input 'x'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSimpleValidate2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(153);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : a a a;\n");
grammarBuilder.append("a : {False}? ID {print(\"alt 1\")}\n");
grammarBuilder.append(" | {True}? INT {print(\"alt 2\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="3 4 x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"alt 2\n" +
"alt 2\n", found);
assertEquals("line 1:4 no viable alternative at input 'x'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testToLeft() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(150);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append(" s : a+ ;\n");
grammarBuilder.append("a : {False}? ID {print(\"alt 1\")}\n");
grammarBuilder.append(" | {True}? ID {print(\"alt 2\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="x x y";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"alt 2\n" +
"alt 2\n" +
"alt 2\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testToLeftWithVaryingPredicate() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(228);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("@members {i = 0}\n");
grammarBuilder.append("s : ({self.i += 1\n");
grammarBuilder.append("print(\"i=\" + str(self.i))} a)+ ;\n");
grammarBuilder.append("a : {self.i % 2 == 0}? ID {print(\"alt 1\")}\n");
grammarBuilder.append(" | {self.i % 2 != 0}? ID {print(\"alt 2\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="x x y";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals(
"i=1\n" +
"alt 2\n" +
"i=2\n" +
"alt 1\n" +
"i=3\n" +
"alt 2\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testUnpredicatedPathsInAlt() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(169);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : a {print(\"alt 1\")}\n");
grammarBuilder.append(" | b {print(\"alt 2\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("a : {False}? ID INT\n");
grammarBuilder.append(" | ID INT\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("b : ID ID\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="x 4";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("alt 1\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testValidateInDFA() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(318);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("s : a ';' a;\n");
grammarBuilder.append("// ';' helps us to resynchronize without consuming\n");
grammarBuilder.append("// 2nd 'a' reference. We our testing that the DFA also\n");
grammarBuilder.append("// throws an exception if the validating predicate fails\n");
grammarBuilder.append("a : {False}? ID {print(\"alt 1\")}\n");
grammarBuilder.append(" | {True}? INT {print(\"alt 2\")}\n");
grammarBuilder.append(" ;\n");
grammarBuilder.append("ID : 'a'..'z'+ ;\n");
grammarBuilder.append("INT : '0'..'9'+;\n");
grammarBuilder.append("WS : (' '|'\\n') -> skip ;");
String grammar = grammarBuilder.toString();
String input ="x ; y";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "s", input, false);
assertEquals("", found);
assertEquals(
"line 1:0 no viable alternative at input 'x'\n" +
"line 1:4 no viable alternative at input 'y'\n", this.stderrDuringParse);
}
}

View File

@ -0,0 +1,476 @@
/* This file is generated by TestGenerator, any edits will be overwritten by the next generation. */
package org.antlr.v4.test.runtime.python2;
import org.junit.Ignore;
import org.junit.Test;
import static org.junit.Assert.*;
@SuppressWarnings("unused")
public class TestSets extends BasePython2Test {
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testCharSetLiteral() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(78);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : (A {print($A.text)})+ ;\n");
grammarBuilder.append("A : [AaBb] ;\n");
grammarBuilder.append("WS : (' '|'\\n')+ -> skip ;");
String grammar = grammarBuilder.toString();
String input ="A a B b";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals(
"A\n" +
"a\n" +
"B\n" +
"b\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testComplementSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(51);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("parse : ~NEW_LINE;\n");
grammarBuilder.append("NEW_LINE: '\\r'? '\\n';");
String grammar = grammarBuilder.toString();
String input ="a";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "parse", input, false);
assertEquals("", found);
assertEquals(
"line 1:0 token recognition error at: 'a'\n" +
"line 1:1 missing {} at '<EOF>'\n", this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLexerOptionalSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(70);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : A {print(self._input.getText())} ;\n");
grammarBuilder.append("A : ('a'|'b')? 'c' ;");
String grammar = grammarBuilder.toString();
String input ="ac";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("ac\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLexerPlusSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(70);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : A {print(self._input.getText())} ;\n");
grammarBuilder.append("A : ('a'|'b')+ 'c' ;");
String grammar = grammarBuilder.toString();
String input ="abaac";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("abaac\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testLexerStarSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(70);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : A {print(self._input.getText())} ;\n");
grammarBuilder.append("A : ('a'|'b')* 'c' ;");
String grammar = grammarBuilder.toString();
String input ="abaac";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("abaac\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testNotChar() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(46);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : A {print($A.text)} ;\n");
grammarBuilder.append("A : ~'b' ;");
String grammar = grammarBuilder.toString();
String input ="x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("x\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testNotCharSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(52);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : A {print($A.text)} ;\n");
grammarBuilder.append("A : ~('b'|'c') ;");
String grammar = grammarBuilder.toString();
String input ="x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("x\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testNotCharSetWithLabel() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(54);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : A {print($A.text)} ;\n");
grammarBuilder.append("A : h=~('b'|'c') ;");
String grammar = grammarBuilder.toString();
String input ="x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("x\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testNotCharSetWithRuleRef3() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(118);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : A {print($A.text)} ;\n");
grammarBuilder.append("A : ('a'|B) ; // this doesn't collapse to set but works\n");
grammarBuilder.append("fragment\n");
grammarBuilder.append("B : ~('a'|'c') ;");
String grammar = grammarBuilder.toString();
String input ="x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("x\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testOptionalLexerSingleElement() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(64);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : A {print(self._input.getText())} ;\n");
grammarBuilder.append("A : 'b'? 'c' ;");
String grammar = grammarBuilder.toString();
String input ="bc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("bc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testOptionalSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(62);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : ('a'|'b')? 'c' {print(self._input.getText())} ;");
String grammar = grammarBuilder.toString();
String input ="ac";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("ac\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testOptionalSingleElement() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(64);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : A? 'c' {print(self._input.getText())} ;\n");
grammarBuilder.append("A : 'b' ;");
String grammar = grammarBuilder.toString();
String input ="bc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("bc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testParserNotSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(50);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : t=~('x'|'y') 'z' {print($t.text)} ;");
String grammar = grammarBuilder.toString();
String input ="zz";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("z\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testParserNotToken() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(56);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : ~'x' 'z' {print(self._input.getText())} ;");
String grammar = grammarBuilder.toString();
String input ="zz";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("zz\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testParserNotTokenWithLabel() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(44);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : t=~'x' 'z' {print($t.text)} ;");
String grammar = grammarBuilder.toString();
String input ="zz";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("z\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testParserSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(45);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : t=('x'|'y') {print($t.text)} ;");
String grammar = grammarBuilder.toString();
String input ="x";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("x\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testPlusLexerSingleElement() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(64);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : A {print(self._input.getText())} ;\n");
grammarBuilder.append("A : 'b'+ 'c' ;");
String grammar = grammarBuilder.toString();
String input ="bbbbc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("bbbbc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testPlusSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(62);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : ('a'|'b')+ 'c' {print(self._input.getText())} ;");
String grammar = grammarBuilder.toString();
String input ="abaac";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("abaac\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testRuleAsSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(69);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a @after {print(self._input.getText())} : 'a' | 'b' |'c' ;");
String grammar = grammarBuilder.toString();
String input ="b";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("b\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testSeqDoesNotBecomeSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(106);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : C {print(self._input.getText())} ;\n");
grammarBuilder.append("fragment A : '1' | '2';\n");
grammarBuilder.append("fragment B : '3' '4';\n");
grammarBuilder.append("C : A | B;");
String grammar = grammarBuilder.toString();
String input ="34";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("34\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testStarLexerSingleElement_1() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(64);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : A {print(self._input.getText())} ;\n");
grammarBuilder.append("A : 'b'* 'c' ;");
String grammar = grammarBuilder.toString();
String input ="bbbbc";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("bbbbc\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testStarLexerSingleElement_2() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(64);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : A {print(self._input.getText())} ;\n");
grammarBuilder.append("A : 'b'* 'c' ;");
String grammar = grammarBuilder.toString();
String input ="c";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("c\n", found);
assertNull(this.stderrDuringParse);
}
/* This file and method are generated by TestGenerator, any edits will be overwritten by the next generation. */
@Test
public void testStarSet() throws Exception {
mkdir(tmpdir);
StringBuilder grammarBuilder = new StringBuilder(62);
grammarBuilder.append("grammar T;\n");
grammarBuilder.append("a : ('a'|'b')* 'c' {print(self._input.getText())} ;");
String grammar = grammarBuilder.toString();
String input ="abaac";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "TListener", "TVisitor", "a", input, false);
assertEquals("abaac\n", found);
assertNull(this.stderrDuringParse);
}
}