forked from jasder/antlr
validated CompositeParsers tests
This commit is contained in:
parent
0c112e97c8
commit
e39ac5a197
|
@ -15,7 +15,6 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
|
||||
import org.antlr.v4.test.rt.java.BaseTest;
|
||||
import org.junit.Test;
|
||||
import org.stringtemplate.v4.ST;
|
||||
import org.stringtemplate.v4.STGroup;
|
||||
import org.stringtemplate.v4.STGroupFile;
|
||||
|
@ -1199,7 +1198,7 @@ public class Generator {
|
|||
file.importGrammar = true;
|
||||
file.addCompositeParserTest(input, "DelegatorInvokesDelegateRule", "M", "s", "b", "S.a\n", null, "S");
|
||||
file.addCompositeParserTest(input, "BringInLiteralsFromDelegate", "M", "s", "=a", "S.a\n", null, "S");
|
||||
file.addCompositeParserTest(input, "DelegatorInvokesDelegateRuleWithArgs", "M", "s", "a", "S.a1000\n", null, "S");
|
||||
file.addCompositeParserTest(input, "DelegatorInvokesDelegateRuleWithArgs", "M", "s", "b", "S.a1000\n", null, "S");
|
||||
file.addCompositeParserTest(input, "DelegatorInvokesDelegateRuleWithReturnStruct", "M", "s", "b", "S.ab\n", null, "S");
|
||||
file.addCompositeParserTest(input, "DelegatorAccessesDelegateMembers", "M", "s", "b", "foo\n", null, "S");
|
||||
file.addCompositeParserTest(input, "DelegatorInvokesFirstVersionOfDelegateRule", "M", "s", "b", "S.a\n", null, "S", "T");
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
parser grammar S;
|
||||
tokens { A, B, C }
|
||||
x : A {<write("\"S.x\"")>};
|
||||
x : A {<writeln("\"S.x\"")>};
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
parser grammar S;
|
||||
tokens { C, B, A } // reverse order
|
||||
y : A {<write("\"T.y\"")>};
|
||||
y : A {<writeln("\"T.y\"")>};
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
parser grammar S;
|
||||
A : B {<write("\"S.a\"")>};
|
||||
a : B {<write("\"S.a\"")>};
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
grammar M;
|
||||
import S, T;
|
||||
b : 'b'|'c' {<write("\"M.b\"")>}|B|A;
|
||||
b : 'b'|'c' {<writeln("\"M.b\"")>}|B|A;
|
||||
WS : (' '|'\n') -> skip ;
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
parser grammar S;
|
||||
a : b {<write("\"S.a\"")>};
|
||||
a : b {<writeln("\"S.a\"")>};
|
||||
b : 'b' ;
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
parser grammar S;
|
||||
tokens { A }
|
||||
b : 'b' {<write("\"T.b\"")>};
|
||||
b : 'b' {<writeln("\"T.b\"")>};
|
||||
|
|
|
@ -2,6 +2,6 @@ parser grammar S;
|
|||
type_ : 'int' ;
|
||||
decl : type_ ID ';'
|
||||
| type_ ID init ';' {
|
||||
<write("\"decl: \" + $text")>
|
||||
<write("\"Decl: \" + $text")>
|
||||
};
|
||||
init : '=' INT;
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
parser grammar S;
|
||||
a : @after {} : B;
|
||||
a @after {} : B;
|
||||
|
|
|
@ -337,7 +337,7 @@ void foo() {
|
|||
}
|
||||
>>
|
||||
|
||||
Declare_foo() ::= "this.foo = function() {document.getElementById('output').value += 'foo\\n'};"
|
||||
Declare_foo() ::= <<public void foo() {System.out.println("foo");}>>
|
||||
|
||||
Invoke_foo() ::= "this.foo();"
|
||||
|
||||
|
|
|
@ -53,7 +53,7 @@ public class TestCompositeParsers extends BaseTest {
|
|||
"s : label=a[3] {System.out.println($label.y);} ;\n" +
|
||||
"B : 'b' ; // defines B from inherited token space\n" +
|
||||
"WS : (' '|'\\n') -> skip ;";
|
||||
String found = execParser("M.g4", grammar, "MParser", "MLexer", "s", "a", false);
|
||||
String found = execParser("M.g4", grammar, "MParser", "MLexer", "s", "b", false);
|
||||
assertEquals("S.a1000\n", found);
|
||||
assertNull(this.stderrDuringParse);
|
||||
}
|
||||
|
@ -61,7 +61,7 @@ public class TestCompositeParsers extends BaseTest {
|
|||
@Test
|
||||
public void testDelegatorInvokesDelegateRuleWithReturnStruct() throws Exception {
|
||||
String slave_S = "parser grammar S;\n" +
|
||||
"A : B {System.out.print(\"S.a\");};";
|
||||
"a : B {System.out.print(\"S.a\");};";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave_S);
|
||||
|
||||
|
@ -79,7 +79,7 @@ public class TestCompositeParsers extends BaseTest {
|
|||
public void testDelegatorAccessesDelegateMembers() throws Exception {
|
||||
String slave_S = "parser grammar S;\n" +
|
||||
"@members {\n" +
|
||||
"this.foo = function() {document.getElementById('output').value += 'foo\\n'};\n" +
|
||||
"public void foo() {System.out.println(\"foo\");}\n" +
|
||||
"}\n" +
|
||||
"a : B;";
|
||||
mkdir(tmpdir);
|
||||
|
@ -121,13 +121,13 @@ public class TestCompositeParsers extends BaseTest {
|
|||
public void testDelegatesSeeSameTokenType() throws Exception {
|
||||
String slave_S = "parser grammar S;\n" +
|
||||
"tokens { A, B, C }\n" +
|
||||
"x : A {System.out.print(\"S.x\");};";
|
||||
"x : A {System.out.println(\"S.x\");};";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave_S);
|
||||
|
||||
String slave_T = "parser grammar S;\n" +
|
||||
"tokens { C, B, A } // reverse order\n" +
|
||||
"y : A {System.out.print(\"T.y\");};";
|
||||
"y : A {System.out.println(\"T.y\");};";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "T.g4", slave_T);
|
||||
|
||||
|
@ -210,7 +210,7 @@ public class TestCompositeParsers extends BaseTest {
|
|||
"type_ : 'int' ;\n" +
|
||||
"decl : type_ ID ';'\n" +
|
||||
" | type_ ID init ';' {\n" +
|
||||
" System.out.print(\"decl: \" + $text);\n" +
|
||||
" System.out.print(\"Decl: \" + $text);\n" +
|
||||
" };\n" +
|
||||
"init : '=' INT;";
|
||||
mkdir(tmpdir);
|
||||
|
@ -231,7 +231,7 @@ public class TestCompositeParsers extends BaseTest {
|
|||
@Test
|
||||
public void testDelegatorRuleOverridesDelegates() throws Exception {
|
||||
String slave_S = "parser grammar S;\n" +
|
||||
"a : b {System.out.print(\"S.a\");};\n" +
|
||||
"a : b {System.out.println(\"S.a\");};\n" +
|
||||
"b : 'b' ;\n" +
|
||||
" ";
|
||||
mkdir(tmpdir);
|
||||
|
@ -239,13 +239,13 @@ public class TestCompositeParsers extends BaseTest {
|
|||
|
||||
String slave_T = "parser grammar S;\n" +
|
||||
"tokens { A }\n" +
|
||||
"b : 'b' {System.out.print(\"T.b\");};";
|
||||
"b : 'b' {System.out.println(\"T.b\");};";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "T.g4", slave_T);
|
||||
|
||||
String grammar = "grammar M;\n" +
|
||||
"import S, T;\n" +
|
||||
"b : 'b'|'c' {System.out.print(\"M.b\");}|B|A;\n" +
|
||||
"b : 'b'|'c' {System.out.println(\"M.b\");}|B|A;\n" +
|
||||
"WS : (' '|'\\n') -> skip ;";
|
||||
String found = execParser("M.g4", grammar, "MParser", "MLexer", "a", "c", false);
|
||||
assertEquals("M.b\nS.a\n", found);
|
||||
|
@ -272,7 +272,7 @@ public class TestCompositeParsers extends BaseTest {
|
|||
@Test
|
||||
public void testImportedRuleWithAction() throws Exception {
|
||||
String slave_S = "parser grammar S;\n" +
|
||||
"a : @after {} : B;";
|
||||
"a @after {} : B;";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave_S);
|
||||
|
||||
|
|
|
@ -127,224 +127,7 @@ public class TestCompositeGrammars extends BaseTest {
|
|||
assertEquals(0, equeue.size());
|
||||
}
|
||||
|
||||
@Test public void testDelegatorInvokesDelegateRule() throws Exception {
|
||||
String slave =
|
||||
"parser grammar S;\n" +
|
||||
"a : B {System.out.println(\"S.a\");} ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String master =
|
||||
"grammar M;\n" +
|
||||
"import S;\n" +
|
||||
"s : a ;\n" +
|
||||
"B : 'b' ;" + // defines B from inherited token space
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"s", "b", debug);
|
||||
assertEquals("S.a\n", found);
|
||||
}
|
||||
|
||||
@Test public void testBringInLiteralsFromDelegate() throws Exception {
|
||||
String slave =
|
||||
"parser grammar S;\n" +
|
||||
"a : '=' 'a' {System.out.println(\"S.a\");} ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String master =
|
||||
"grammar M;\n" +
|
||||
"import S;\n" +
|
||||
"s : a ;\n" +
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"s", "=a", debug);
|
||||
assertEquals("S.a\n", found);
|
||||
}
|
||||
|
||||
@Test public void testDelegatorInvokesDelegateRuleWithArgs() throws Exception {
|
||||
// must generate something like:
|
||||
// public int a(int x) throws RecognitionException { return gS.a(x); }
|
||||
// in M.
|
||||
String slave =
|
||||
"parser grammar S;\n" +
|
||||
"a[int x] returns [int y] : B {System.out.print(\"S.a\"); $y=1000;} ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String master =
|
||||
"grammar M;\n" +
|
||||
"import S;\n" +
|
||||
"s : label=a[3] {System.out.println($label.y);} ;\n" +
|
||||
"B : 'b' ;" + // defines B from inherited token space
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"s", "b", debug);
|
||||
assertEquals("S.a1000\n", found);
|
||||
}
|
||||
|
||||
@Test public void testDelegatorInvokesDelegateRuleWithReturnStruct() throws Exception {
|
||||
// must generate something like:
|
||||
// public int a(int x) throws RecognitionException { return gS.a(x); }
|
||||
// in M.
|
||||
String slave =
|
||||
"parser grammar S;\n" +
|
||||
"a : B {System.out.print(\"S.a\");} ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String master =
|
||||
"grammar M;\n" +
|
||||
"import S;\n" +
|
||||
"s : a {System.out.println($a.text);} ;\n" +
|
||||
"B : 'b' ;" + // defines B from inherited token space
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"s", "b", debug);
|
||||
assertEquals("S.ab\n", found);
|
||||
}
|
||||
|
||||
@Test public void testDelegatorAccessesDelegateMembers() throws Exception {
|
||||
String slave =
|
||||
"parser grammar S;\n" +
|
||||
"@parser::members {\n" +
|
||||
" public void foo() {System.out.println(\"foo\");}\n" +
|
||||
"}\n" +
|
||||
"a : B ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String master =
|
||||
"grammar M;\n" + // uses no rules from the import
|
||||
"import S;\n" +
|
||||
"s : 'b' {foo();} ;\n" + // gS is import pointer
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"s", "b", debug);
|
||||
assertEquals("foo\n", found);
|
||||
}
|
||||
|
||||
@Test public void testDelegatorInvokesFirstVersionOfDelegateRule() throws Exception {
|
||||
String slave =
|
||||
"parser grammar S;\n" +
|
||||
"a : b {System.out.println(\"S.a\");} ;\n" +
|
||||
"b : B ;\n" ;
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String slave2 =
|
||||
"parser grammar T;\n" +
|
||||
"a : B {System.out.println(\"T.a\");} ;\n"; // hidden by S.a
|
||||
writeFile(tmpdir, "T.g4", slave2);
|
||||
String master =
|
||||
"grammar M;\n" +
|
||||
"import S,T;\n" +
|
||||
"s : a ;\n" +
|
||||
"B : 'b' ;\n" +
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"s", "b", debug);
|
||||
assertEquals("S.a\n", found);
|
||||
}
|
||||
|
||||
@Test public void testDelegatesSeeSameTokenType() throws Exception {
|
||||
String slave =
|
||||
"parser grammar S;\n" + // A, B, C token type order
|
||||
"tokens { A, B, C }\n" +
|
||||
"x : A {System.out.println(\"S.x\");} ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String slave2 =
|
||||
"parser grammar T;\n" +
|
||||
"tokens { C, B, A }\n" + // reverse order
|
||||
"y : A {System.out.println(\"T.y\");} ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "T.g4", slave2);
|
||||
// The lexer will create rules to match letters a, b, c.
|
||||
// The associated token types A, B, C must have the same value
|
||||
// and all import'd parsers. Since ANTLR regenerates all imports
|
||||
// for use with the delegator M, it can generate the same token type
|
||||
// mapping in each parser:
|
||||
// public static final int C=6;
|
||||
// public static final int EOF=-1;
|
||||
// public static final int B=5;
|
||||
// public static final int WS=7;
|
||||
// public static final int A=4;
|
||||
|
||||
String master =
|
||||
"grammar M;\n" +
|
||||
"import S,T;\n" +
|
||||
"s : x y ;\n" + // matches AA, which should be "aa"
|
||||
"B : 'b' ;\n" + // another order: B, A, C
|
||||
"A : 'a' ;\n" +
|
||||
"C : 'c' ;\n" +
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"s", "aa", debug);
|
||||
assertEquals("S.x\n" +
|
||||
"T.y\n", found);
|
||||
}
|
||||
|
||||
@Test public void testDelegatesSeeSameTokenType2() throws Exception {
|
||||
ErrorQueue equeue = new ErrorQueue();
|
||||
String slave =
|
||||
"parser grammar S;\n" + // A, B, C token type order
|
||||
"tokens { A, B, C }\n" +
|
||||
"x : A {System.out.println(\"S.x\");} ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String slave2 =
|
||||
"parser grammar T;\n" +
|
||||
"tokens { C, B, A }\n" + // reverse order
|
||||
"y : A {System.out.println(\"T.y\");} ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "T.g4", slave2);
|
||||
|
||||
String master =
|
||||
"grammar M;\n" +
|
||||
"import S,T;\n" +
|
||||
"s : x y ;\n" + // matches AA, which should be "aa"
|
||||
"B : 'b' ;\n" + // another order: B, A, C
|
||||
"A : 'a' ;\n" +
|
||||
"C : 'c' ;\n" +
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
writeFile(tmpdir, "M.g4", master);
|
||||
Grammar g = new Grammar(tmpdir+"/M.g4", master, 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 found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"s", "aa", debug);
|
||||
assertEquals("S.x\n" +
|
||||
"T.y\n", found);
|
||||
}
|
||||
|
||||
@Test public void testCombinedImportsCombined() throws Exception {
|
||||
ErrorQueue equeue = new ErrorQueue();
|
||||
String slave =
|
||||
"grammar S;\n" + // A, B, C token type order
|
||||
"tokens { A, B, C }\n" +
|
||||
"x : 'x' INT {System.out.println(\"S.x\");} ;\n" +
|
||||
"INT : '0'..'9'+ ;\n" +
|
||||
"WS : (' '|'\\n') -> skip ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
|
||||
String master =
|
||||
"grammar M;\n" +
|
||||
"import S;\n" +
|
||||
"s : x INT ;\n";
|
||||
writeFile(tmpdir, "M.g4", master);
|
||||
Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue);
|
||||
|
||||
assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
|
||||
|
||||
String found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"s", "x 34 9", debug);
|
||||
assertEquals("S.x\n", found);
|
||||
}
|
||||
|
||||
@Test public void testImportedTokenVocabIgnoredWithWarning() throws Exception {
|
||||
ErrorQueue equeue = new ErrorQueue();
|
||||
|
@ -388,145 +171,11 @@ public class TestCompositeGrammars extends BaseTest {
|
|||
"s : x ;\n" +
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
writeFile(tmpdir, "M.g4", master);
|
||||
Grammar g = new Grammar(tmpdir+"/M.g4", master, equeue);
|
||||
/*Grammar g =*/ new Grammar(tmpdir+"/M.g4", master, equeue);
|
||||
|
||||
assertEquals(ErrorType.SYNTAX_ERROR, equeue.errors.get(0).getErrorType());
|
||||
}
|
||||
|
||||
@Test public void testDelegatorRuleOverridesDelegate() throws Exception {
|
||||
String slave =
|
||||
"parser grammar S;\n" +
|
||||
"a : b {System.out.println(\"S.a\");} ;\n" +
|
||||
"b : B ;\n" ;
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String master =
|
||||
"grammar M;\n" +
|
||||
"import S;\n" +
|
||||
"b : 'b'|'c' ;\n" +
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"a", "c", debug);
|
||||
assertEquals("S.a\n", found);
|
||||
}
|
||||
|
||||
@Test public void testDelegatorRuleOverridesLookaheadInDelegate() throws Exception {
|
||||
String slave =
|
||||
"parser grammar JavaDecl;\n" +
|
||||
"type : 'int' ;\n" +
|
||||
"decl : type ID ';'\n" +
|
||||
" | type ID init ';' {System.out.println(\"JavaDecl: \"+$text);}\n" +
|
||||
" ;\n" +
|
||||
"init : '=' INT ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "JavaDecl.g4", slave);
|
||||
String master =
|
||||
"grammar Java;\n" +
|
||||
"import JavaDecl;\n" +
|
||||
"prog : decl ;\n" +
|
||||
"type : 'int' | 'float' ;\n" +
|
||||
"\n" +
|
||||
"ID : 'a'..'z'+ ;\n" +
|
||||
"INT : '0'..'9'+ ;\n" +
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
// for float to work in decl, type must be overridden
|
||||
String found = execParser("Java.g4", master, "JavaParser", "JavaLexer",
|
||||
"prog", "float x = 3;", debug);
|
||||
assertEquals("JavaDecl: floatx=3;\n", found);
|
||||
}
|
||||
|
||||
@Test public void testDelegatorRuleOverridesDelegates() throws Exception {
|
||||
String slave =
|
||||
"parser grammar S;\n" +
|
||||
"a : b {System.out.println(\"S.a\");} ;\n" +
|
||||
"b : 'b' ;\n" ;
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
|
||||
String slave2 =
|
||||
"parser grammar T;\n" +
|
||||
"tokens { A }\n" +
|
||||
"b : 'b' {System.out.println(\"T.b\");} ;\n";
|
||||
writeFile(tmpdir, "T.g4", slave2);
|
||||
|
||||
String master =
|
||||
"grammar M;\n" +
|
||||
"import S, T;\n" +
|
||||
"b : 'b'|'c' {System.out.println(\"M.b\");}|B|A ;\n" +
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"a", "c", debug);
|
||||
assertEquals("M.b\n" +
|
||||
"S.a\n", found);
|
||||
}
|
||||
// LEXER INHERITANCE
|
||||
|
||||
@Test public void testLexerDelegatorInvokesDelegateRule() throws Exception {
|
||||
String slave =
|
||||
"lexer grammar S;\n" +
|
||||
"A : 'a' {System.out.println(\"S.A\");} ;\n" +
|
||||
"C : 'c' ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String master =
|
||||
"lexer grammar M;\n" +
|
||||
"import S;\n" +
|
||||
"B : 'b' ;\n" +
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String expecting =
|
||||
"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";
|
||||
String found = execLexer("M.g4", master, "M", "abc", debug);
|
||||
assertEquals(expecting, found);
|
||||
}
|
||||
|
||||
@Test public void testLexerDelegatorRuleOverridesDelegate() throws Exception {
|
||||
String slave =
|
||||
"lexer grammar S;\n" +
|
||||
"A : 'a' {System.out.println(\"S.A\");} ;\n" +
|
||||
"B : 'b' {System.out.println(\"S.B\");} ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String master =
|
||||
"lexer grammar M;\n" +
|
||||
"import S;\n" +
|
||||
"A : 'a' B {System.out.println(\"M.A\");} ;\n" +
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String found = execLexer("M.g4", master, "M", "ab", debug);
|
||||
assertEquals("M.A\n" +
|
||||
"[@0,0:1='ab',<1>,1:0]\n" +
|
||||
"[@1,2:1='<EOF>',<-1>,1:2]\n", found);
|
||||
}
|
||||
|
||||
@Test public void testKeywordVSIDOrder() throws Exception {
|
||||
// rules in lexer are imported at END so rules in master override
|
||||
// *and* get priority over imported rules. So importing ID doesn't
|
||||
// mess up keywords in master grammar
|
||||
ErrorQueue equeue = new ErrorQueue();
|
||||
String slave =
|
||||
"lexer grammar S;\n" +
|
||||
"ID : 'a'..'z'+ ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String master =
|
||||
"grammar M;\n" +
|
||||
"import S;\n" +
|
||||
"a : A {System.out.println(\"M.a: \"+$A);} ;\n" +
|
||||
"A : 'abc' {System.out.println(\"M.A\");} ;\n" +
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"a", "abc", debug);
|
||||
|
||||
assertEquals("unexpected errors: "+equeue, 0, equeue.errors.size());
|
||||
assertEquals("unexpected warnings: "+equeue, 0, equeue.warnings.size());
|
||||
|
||||
assertEquals("M.A\n" +
|
||||
"M.a: [@0,0:2='abc',<1>,1:0]\n", found);
|
||||
}
|
||||
|
||||
// Make sure that M can import S that imports T.
|
||||
@Test public void test3LevelImport() throws Exception {
|
||||
ErrorQueue equeue = new ErrorQueue();
|
||||
|
@ -733,76 +382,13 @@ public class TestCompositeGrammars extends BaseTest {
|
|||
assertEquals(expecting, equeue.errors.size());
|
||||
}
|
||||
|
||||
@Test public void testImportedRuleWithAction() throws Exception {
|
||||
// wasn't terminating. @after was injected into M as if it were @members
|
||||
String slave =
|
||||
"parser grammar S;\n" +
|
||||
"a @after {int x;} : B ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String master =
|
||||
"grammar M;\n" +
|
||||
"import S;\n" +
|
||||
"s : a ;\n" +
|
||||
"B : 'b' ;" +
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"s", "b", debug);
|
||||
assertEquals("", found);
|
||||
}
|
||||
|
||||
@Test public void testImportedGrammarWithEmptyOptions() throws Exception {
|
||||
String slave =
|
||||
"parser grammar S;\n" +
|
||||
"options {}\n" +
|
||||
"a : B ;\n";
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "S.g4", slave);
|
||||
String master =
|
||||
"grammar M;\n" +
|
||||
"import S;\n" +
|
||||
"s : a ;\n" +
|
||||
"B : 'b' ;" +
|
||||
"WS : (' '|'\\n') -> skip ;\n" ;
|
||||
String found = execParser("M.g4", master, "MParser", "MLexer",
|
||||
"s", "b", debug);
|
||||
assertEquals("", found);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a regression test for antlr/antlr4#248 "Including grammar with
|
||||
* only fragments breaks generated lexer".
|
||||
* https://github.com/antlr/antlr4/issues/248
|
||||
*/
|
||||
@Test public void testImportLexerWithOnlyFragmentRules() {
|
||||
String slave =
|
||||
"lexer grammar Unicode;\n" +
|
||||
"\n" +
|
||||
"fragment\n" +
|
||||
"UNICODE_CLASS_Zs : '\\u0020' | '\\u00A0' | '\\u1680' | '\\u180E'\n" +
|
||||
" | '\\u2000'..'\\u200A'\n" +
|
||||
" | '\\u202F' | '\\u205F' | '\\u3000'\n" +
|
||||
" ;\n";
|
||||
String master =
|
||||
"grammar Test;\n" +
|
||||
"import Unicode;\n" +
|
||||
"\n" +
|
||||
"program : 'test' 'test' ;\n" +
|
||||
"\n" +
|
||||
"WS : (UNICODE_CLASS_Zs)+ -> skip;\n";
|
||||
|
||||
mkdir(tmpdir);
|
||||
writeFile(tmpdir, "Unicode.g4", slave);
|
||||
String found = execParser("Test.g4", master, "TestParser", "TestLexer", "program", "test test", debug);
|
||||
assertEquals("", found);
|
||||
assertNull(stderrDuringParse);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a regression test for antlr/antlr4#670 "exception when importing
|
||||
* grammar".
|
||||
* https://github.com/antlr/antlr4/issues/670
|
||||
*/
|
||||
// TODO: migrate to test framework
|
||||
@Test
|
||||
public void testImportLargeGrammar() throws Exception {
|
||||
String slave = load("Java.g4", "UTF-8");
|
||||
|
|
Loading…
Reference in New Issue