Merge branch 'Improved-tests-granularity' into Preliminary-enhancements-for-Python-target

This commit is contained in:
ericvergnaud 2014-06-07 00:01:13 +08:00
commit 00eb4f89ca
4 changed files with 99 additions and 35 deletions

View File

@ -62,7 +62,7 @@ public class TestFullContextParsing extends BaseTest {
this.stderrDuringParse); this.stderrDuringParse);
} }
@Test public void testCtxSensitiveDFA() { public String testCtxSensitiveDFA(String input) {
String grammar = String grammar =
"grammar T;\n"+ "grammar T;\n"+
"s @after {dumpDFA();}\n" + "s @after {dumpDFA();}\n" +
@ -73,8 +73,12 @@ public class TestFullContextParsing extends BaseTest {
"ID : 'a'..'z'+ ;\n"+ "ID : 'a'..'z'+ ;\n"+
"INT : '0'..'9'+ ;\n"+ "INT : '0'..'9'+ ;\n"+
"WS : (' '|'\\t'|'\\n')+ -> skip ;\n"; "WS : (' '|'\\t'|'\\n')+ -> skip ;\n";
String result = execParser("T.g4", grammar, "TParser", "TLexer", "s", return execParser("T.g4", grammar, "TParser", "TLexer", "s", input, true);
"$ 34 abc", true); }
@Test
public void testCtxSensitiveDFA1() {
String result = testCtxSensitiveDFA("$ 34 abc");
String expecting = String expecting =
"Decision 1:\n" + "Decision 1:\n" +
"s0-INT->s1\n" + "s0-INT->s1\n" +
@ -83,10 +87,12 @@ public class TestFullContextParsing extends BaseTest {
assertEquals("line 1:5 reportAttemptingFullContext d=1 (e), input='34abc'\n" + assertEquals("line 1:5 reportAttemptingFullContext d=1 (e), input='34abc'\n" +
"line 1:2 reportContextSensitivity d=1 (e), input='34'\n", "line 1:2 reportContextSensitivity d=1 (e), input='34'\n",
this.stderrDuringParse); this.stderrDuringParse);
}
result = execParser("T.g4", grammar, "TParser", "TLexer", "s",
"@ 34 abc", true); @Test
expecting = public void testCtxSensitiveDFA2() {
String result = testCtxSensitiveDFA("@ 34 abc");
String expecting =
"Decision 1:\n" + "Decision 1:\n" +
"s0-INT->s1\n" + "s0-INT->s1\n" +
"s1-ID->:s2^=>1\n"; "s1-ID->:s2^=>1\n";

View File

@ -224,7 +224,8 @@ public class TestLexerExec extends BaseTest {
assertNull(stderrDuringParse); assertNull(stderrDuringParse);
} }
@Test public void testRecursiveLexerRuleRefWithWildcardStar() throws Exception { @Test
public void testRecursiveLexerRuleRefWithWildcardStar1() throws Exception {
String grammar = String grammar =
"lexer grammar L;\n"+ "lexer grammar L;\n"+
"CMT : '/*' (CMT | .)*? '*/' ;\n" + "CMT : '/*' (CMT | .)*? '*/' ;\n" +
@ -245,14 +246,24 @@ public class TestLexerExec extends BaseTest {
"/* /*nested*/ */\n"); "/* /*nested*/ */\n");
assertEquals(expecting, found); assertEquals(expecting, found);
assertNull(stderrDuringParse); assertNull(stderrDuringParse);
}
@Test
public void testRecursiveLexerRuleRefWithWildcardStar2() throws Exception {
String grammar =
"lexer grammar L;\n"+
"CMT : '/*' (CMT | .)*? '*/' ;\n" +
"WS : (' '|'\\n')+ ;\n"
/*+ "ANY : .;"*/;
// stuff on end of comment doesn't match another rule // stuff on end of comment doesn't match another rule
expecting = String expecting =
"[@0,0:8='/* ick */',<1>,1:0]\n" + "[@0,0:8='/* ick */',<1>,1:0]\n" +
"[@1,10:10='\\n',<2>,1:10]\n" + "[@1,10:10='\\n',<2>,1:10]\n" +
"[@2,11:36='/* /* */x\\n/* /*nested*/ */',<1>,2:0]\n" + "[@2,11:36='/* /* */x\\n/* /*nested*/ */',<1>,2:0]\n" +
"[@3,38:38='\\n',<2>,3:17]\n" + "[@3,38:38='\\n',<2>,3:17]\n" +
"[@4,39:38='<EOF>',<-1>,4:18]\n"; "[@4,39:38='<EOF>',<-1>,4:18]\n";
found = execLexer("L.g4", grammar, "L", String found = execLexer("L.g4", grammar, "L",
"/* ick */x\n" + "/* ick */x\n" +
"/* /* */x\n" + "/* /* */x\n" +
"/* /*nested*/ */x\n"); "/* /*nested*/ */x\n");
@ -262,7 +273,8 @@ public class TestLexerExec extends BaseTest {
"line 3:16 token recognition error at: 'x'\n", stderrDuringParse); "line 3:16 token recognition error at: 'x'\n", stderrDuringParse);
} }
@Test public void testRecursiveLexerRuleRefWithWildcardPlus() throws Exception { @Test
public void testRecursiveLexerRuleRefWithWildcardPlus1() throws Exception {
String grammar = String grammar =
"lexer grammar L;\n"+ "lexer grammar L;\n"+
"CMT : '/*' (CMT | .)+? '*/' ;\n" + "CMT : '/*' (CMT | .)+? '*/' ;\n" +
@ -283,14 +295,24 @@ public class TestLexerExec extends BaseTest {
"/* /*nested*/ */\n"); "/* /*nested*/ */\n");
assertEquals(expecting, found); assertEquals(expecting, found);
assertNull(stderrDuringParse); assertNull(stderrDuringParse);
}
@Test
public void testRecursiveLexerRuleRefWithWildcardPlus2() throws Exception {
String grammar =
"lexer grammar L;\n"+
"CMT : '/*' (CMT | .)+? '*/' ;\n" +
"WS : (' '|'\\n')+ ;\n"
/*+ "ANY : .;"*/;
// stuff on end of comment doesn't match another rule // stuff on end of comment doesn't match another rule
expecting = String expecting =
"[@0,0:8='/* ick */',<1>,1:0]\n" + "[@0,0:8='/* ick */',<1>,1:0]\n" +
"[@1,10:10='\\n',<2>,1:10]\n" + "[@1,10:10='\\n',<2>,1:10]\n" +
"[@2,11:36='/* /* */x\\n/* /*nested*/ */',<1>,2:0]\n" + "[@2,11:36='/* /* */x\\n/* /*nested*/ */',<1>,2:0]\n" +
"[@3,38:38='\\n',<2>,3:17]\n" + "[@3,38:38='\\n',<2>,3:17]\n" +
"[@4,39:38='<EOF>',<-1>,4:18]\n"; "[@4,39:38='<EOF>',<-1>,4:18]\n";
found = execLexer("L.g4", grammar, "L", String found = execLexer("L.g4", grammar, "L",
"/* ick */x\n" + "/* ick */x\n" +
"/* /* */x\n" + "/* /* */x\n" +
"/* /*nested*/ */x\n"); "/* /*nested*/ */x\n");

View File

@ -165,7 +165,7 @@ public class TestParserExec extends BaseTest {
"ID : 'a'..'z'+ ;\n" + "ID : 'a'..'z'+ ;\n" +
"WS : (' '|'\\n') -> channel(HIDDEN);\n"; "WS : (' '|'\\n') -> channel(HIDDEN);\n";
@Test public void testIfIfElseGreedyBinding() throws Exception { @Test public void testIfIfElseGreedyBinding1() throws Exception {
final String input = "if y if y x else x"; final String input = "if y if y x else x";
final String expectedInnerBound = "if y x else x\nif y if y x else x\n"; final String expectedInnerBound = "if y x else x\nif y if y x else x\n";
@ -173,8 +173,14 @@ public class TestParserExec extends BaseTest {
String found = execParser("T.g4", grammar, "TParser", "TLexer", "start", input, false); String found = execParser("T.g4", grammar, "TParser", "TLexer", "start", input, false);
assertEquals(expectedInnerBound, found); assertEquals(expectedInnerBound, found);
grammar = String.format(ifIfElseGrammarFormat, "('else' statement|)"); }
found = execParser("T.g4", grammar, "TParser", "TLexer", "start", input, false);
@Test public void testIfIfElseGreedyBinding2() throws Exception {
final String input = "if y if y x else x";
final String expectedInnerBound = "if y x else x\nif y if y x else x\n";
String grammar = String.format(ifIfElseGrammarFormat, "('else' statement|)");
String found = execParser("T.g4", grammar, "TParser", "TLexer", "start", input, false);
assertEquals(expectedInnerBound, found); assertEquals(expectedInnerBound, found);
} }
@ -191,20 +197,20 @@ public class TestParserExec extends BaseTest {
assertEquals(expectedOuterBound, found); assertEquals(expectedOuterBound, found);
} }
@Test public void testAStar() throws Exception { @Test public void testAStar() throws Exception {
String grammar = String grammar =
"grammar T;\n" + "grammar T;\n" +
"a : ID* {System.out.println($text);} ;\n" + "a : ID* {System.out.println($text);} ;\n" +
"ID : 'a'..'z'+ ;\n" + "ID : 'a'..'z'+ ;\n" +
"WS : (' '|'\\n') -> skip ;\n"; "WS : (' '|'\\n') -> skip ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "a", String found = execParser("T.g4", grammar, "TParser", "TLexer", "a",
"", false); "", false);
assertEquals("\n", found); assertEquals("\n", found);
found = execParser("T.g4", grammar, "TParser", "TLexer", "a", found = execParser("T.g4", grammar, "TParser", "TLexer", "a",
"a b c", false); "a b c", false);
assertEquals("abc\n", found); assertEquals("abc\n", found);
} }
@Test public void testLL1OptionalBlock() throws Exception { @Test public void testLL1OptionalBlock() throws Exception {
String grammar = String grammar =
@ -273,7 +279,7 @@ public class TestParserExec extends BaseTest {
* https://github.com/antlr/antlr4/issues/41 * https://github.com/antlr/antlr4/issues/41
*/ */
@Test @Test
public void testOptional() throws Exception { public void testOptional1() throws Exception {
String grammar = String grammar =
"grammar T;\n" + "grammar T;\n" +
"stat : ifstat | 'x';\n" + "stat : ifstat | 'x';\n" +
@ -284,16 +290,46 @@ public class TestParserExec extends BaseTest {
String found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "x", false); String found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "x", false);
assertEquals("", found); assertEquals("", found);
assertNull(this.stderrDuringParse); assertNull(this.stderrDuringParse);
}
@Test
public void testOptional2() throws Exception {
String grammar =
"grammar T;\n" +
"stat : ifstat | 'x';\n" +
"ifstat : 'if' stat ('else' stat)?;\n" +
"WS : [ \\n\\t]+ -> skip ;"
;
found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if x else x", false); String found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if x else x", false);
assertEquals("", found); assertEquals("", found);
assertNull(this.stderrDuringParse); assertNull(this.stderrDuringParse);
}
@Test
public void testOptional3() throws Exception {
String grammar =
"grammar T;\n" +
"stat : ifstat | 'x';\n" +
"ifstat : 'if' stat ('else' stat)?;\n" +
"WS : [ \\n\\t]+ -> skip ;"
;
found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if x", false); String found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if x", false);
assertEquals("", found); assertEquals("", found);
assertNull(this.stderrDuringParse); assertNull(this.stderrDuringParse);
}
found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if if x else x", false);
@Test
public void testOptional4() throws Exception {
String grammar =
"grammar T;\n" +
"stat : ifstat | 'x';\n" +
"ifstat : 'if' stat ('else' stat)?;\n" +
"WS : [ \\n\\t]+ -> skip ;"
;
String found = execParser("T.g4", grammar, "TParser", "TLexer", "stat", "if if x else x", false);
assertEquals("", found); assertEquals("", found);
assertNull(this.stderrDuringParse); assertNull(this.stderrDuringParse);
} }

View File

@ -49,7 +49,7 @@ public class TestSemPredEvalParser extends BaseTest {
"INT : '0'..'9'+;\n" + "INT : '0'..'9'+;\n" +
"WS : (' '|'\\n') -> skip ;\n"; "WS : (' '|'\\n') -> skip ;\n";
String found = execParser("T.g4", grammar, "TParser", "TLexer", "s", /*String found = */execParser("T.g4", grammar, "TParser", "TLexer", "s",
"x", false); "x", false);
String expecting = "line 1:0 no viable alternative at input 'x'\n"; String expecting = "line 1:0 no viable alternative at input 'x'\n";