Verify behavior of CompositeGrammars tests, and correct discrepancies

This commit is contained in:
Sam Harwell 2015-06-06 16:21:19 -05:00
parent 990fd9d811
commit 364d984437
14 changed files with 47 additions and 31 deletions

View File

@ -21,12 +21,12 @@ Errors() ::= ""
masterGrammar(grammarName, slaveGrammarName) ::= <<
lexer grammar <grammarName>;
import <slaveGrammarName>;
A : 'a' B {<writeln("\"M.A\"")>};
A : 'a' B {<writeln("\"M.A\"")>} ;
WS : (' '|'\n') -> skip ;
>>
slaveGrammar(grammarName) ::= <<
lexer grammar <grammarName>;
A : 'a' {<writeln("\"S.A\"")>};
B : 'b' {<writeln("\"S.B\"")>};
A : 'a' {<writeln("\"S.A\"")>} ;
B : 'b' {<writeln("\"S.B\"")>} ;
>>

View File

@ -60,7 +60,7 @@ x : A {<writeln("\"S.x\"")>};
>>
slaveGrammarT(grammarName) ::= <<
parser grammar S;
parser grammar T;
tokens { C, B, A } // reverse order
y : A {<writeln("\"T.y\"")>};
>>

View File

@ -21,13 +21,13 @@ Errors() ::= ""
masterGrammar(grammarName, slaveGrammarName) ::= <<
grammar M; // uses no rules from the import
import S;
s : 'b'{<Invoke_foo()>}; // gS is import pointer
s : 'b' {<Invoke_foo()>} ; // gS is import pointer
WS : (' '|'\n') -> skip ;
>>
slaveGrammar(grammarName) ::= <<
parser grammar S;
@members {
@parser::members {
<Declare_foo()>
}
a : B;

View File

@ -28,5 +28,5 @@ WS : (' '|'\n') -> skip ;
slaveGrammar(grammarName) ::= <<
parser grammar S;
a[int x] returns [int y] : B {<write("\"S.a\"")>;$y=1000;};
a[int x] returns [int y] : B {<write("\"S.a\"")> $y=1000;} ;
>>

View File

@ -28,5 +28,5 @@ WS : (' '|'\n') -> skip ;
slaveGrammar(grammarName) ::= <<
parser grammar S;
a : B {<write("\"S.a\"")>};
a : B {<write("\"S.a\"")>} ;
>>

View File

@ -29,11 +29,11 @@ WS : (' '|'\n') -> skip ;
slaveGrammarS(grammarName) ::= <<
parser grammar S;
a : B {<writeln("\"S.a\"")>};
a : b {<writeln("\"S.a\"")>};
b : B;
>>
slaveGrammarT(grammarName) ::= <<
parser grammar T;
a : B {<writeln("\"T.a\"")>};
a : B {<writeln("\"T.a\"")>};<! hidden by S.a !>
>>

View File

@ -34,7 +34,7 @@ b : 'b' ;
>>
slaveGrammarT(grammarName) ::= <<
parser grammar S;
parser grammar T;
tokens { A }
b : 'b' {<writeln("\"T.b\"")>};
>>

View File

@ -13,16 +13,17 @@ Rule() ::= "prog"
Input() ::= "float x = 3;"
Output() ::= <<
Decl: floatx=3;<\n>
JavaDecl: floatx=3;<\n>
>>
Errors() ::= ""
// for float to work in decl, type must be overridden
masterGrammar(grammarName, slaveGrammarName) ::= <<
grammar M;
import S;
prog : decl ;
type_ : 'int' | 'float' ;
type : 'int' | 'float' ;
ID : 'a'..'z'+ ;
INT : '0'..'9'+ ;
WS : (' '|'\n') -> skip;
@ -30,8 +31,8 @@ WS : (' '|'\n') -> skip;
slaveGrammar(grammarName) ::= <<
parser grammar S;
type_ : 'int' ;
decl : type_ ID ';'
| type_ ID init ';' {<write("\"Decl: \" + $text")>};
type : 'int' ;
decl : type ID ';'
| type ID init ';' {<write("\"JavaDecl: \" + $text")>};
init : '=' INT;
>>

View File

@ -1,11 +1,17 @@
/*
* This is a regression test for antlr/antlr4#248 "Including grammar with only
* fragments breaks generated lexer".
* https://github.com/antlr/antlr4/issues/248
*/
TestType() ::= "CompositeParser"
Grammar ::= [
"M": {<masterGrammar("M", "S")>}
"Test": {<masterGrammar("Test", "Unicode")>}
]
SlaveGrammars ::= [
"S": {<slaveGrammar("S")>}
"Unicode": {<slaveGrammar("Unicode")>}
]
Rule() ::= "program"
@ -18,17 +24,22 @@ Output() ::= <<
Errors() ::= ""
masterGrammar(grammarName, slaveGrammarName) ::= <<
grammar M;
import S;
grammar <grammarName>;
import <slaveGrammarName; separator=", ">;
program : 'test' 'test';
WS : (UNICODE_CLASS_Zs)+ -> skip;
>>
slaveGrammar(grammarName) ::= <<
lexer grammar S;
lexer grammar <grammarName>;
fragment
UNICODE_CLASS_Zs : '\u0020' | '\u00A0' | '\u1680' | '\u180E'
| '\u2000'..'\u200A'
| '\u202F' | '\u205F' | '\u3000'
;
>>

View File

@ -20,13 +20,13 @@ Errors() ::= ""
masterGrammar(grammarName, slaveGrammarName) ::= <<
grammar M;
import S;
s : a;
B : 'b';
s : a ;
B : 'b' ;
WS : (' '|'\n') -> skip ;
>>
slaveGrammar(grammarName) ::= <<
parser grammar S;
options {}
a : B;
a : B ;
>>

View File

@ -25,7 +25,8 @@ B : 'b';
WS : (' '|'\n') -> skip ;
>>
// wasn't terminating. @after was injected into M as if it were @members
slaveGrammar(grammarName) ::= <<
parser grammar S;
a @after {} : B;
a @after {int x;} : B;
>>

View File

@ -1,3 +1,7 @@
// 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
TestType() ::= "CompositeParser"
Grammar ::= [

View File

@ -381,9 +381,11 @@ void foo() {
}
>>
Declare_foo() ::= <<public void foo() {System.out.println("foo");}>>
Invoke_foo() ::= "this.foo();"
Declare_foo() ::= <<
public void foo() {System.out.println("foo");}
>>
Invoke_foo() ::= "foo();"
Declare_pred() ::= <<boolean pred(boolean v) {
System.out.println("eval="+v);

View File

@ -127,8 +127,6 @@ public class TestCompositeGrammars extends BaseTest {
assertEquals(0, equeue.size());
}
@Test public void testImportedTokenVocabIgnoredWithWarning() throws Exception {
ErrorQueue equeue = new ErrorQueue();
String slave =
@ -382,7 +380,6 @@ public class TestCompositeGrammars extends BaseTest {
assertEquals(expecting, equeue.errors.size());
}
/**
* This is a regression test for antlr/antlr4#670 "exception when importing
* grammar".