Skip to content

Commit 41e71a7

Browse files
committed
Add regression tests for antlr#157
1 parent 21ada18 commit 41e71a7

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

tool/src/test/java/org/antlr/test/TestJavaCodeGeneration.java

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,136 @@ public void testSemanticPredicateAnalysisStackOverflow() throws Exception {
158158
boolean success = rawGenerateAndBuildRecognizer("T.g", grammar, "TParser", "TLexer", false);
159159
assertTrue(success);
160160
}
161+
162+
/**
163+
* This is a regression test for antlr/antlr3#157 "missing lexer rules if
164+
* tokenVocab defined"
165+
* https://github.com/antlr/antlr3/pull/157
166+
*/
167+
@Test public void testImportedTokensInCombinedGrammar() {
168+
String tokensFile =
169+
"TypeArray=394\n" +
170+
"Null=395\n";
171+
System.out.println("dir " + tmpdir);
172+
mkdir(tmpdir);
173+
writeFile(tmpdir, "CustomVocab.tokens", tokensFile);
174+
175+
String grammar =
176+
"grammar T;\n" +
177+
"options { output = AST; tokenVocab = CustomVocab; }\n" +
178+
"tokens { TypeArray = 'array'; }\n" +
179+
"a : ('array' TypeArray 'null' Null ID)* EOF\n" +
180+
" {System.out.println(tokenNames[TypeArray] + \"=\" + TypeArray);\n" +
181+
" System.out.println(tokenNames[Null] + \"=\" + Null);};\n" +
182+
"Null : 'null';\n" +
183+
"ID : 'a'..'z'+;\n" +
184+
"WS : ' '+ {skip();};\n";
185+
String input = "array array null null foo";
186+
String found = execParser("T.g", grammar, "TParser", "TLexer", "a", input, false);
187+
String expected =
188+
"TypeArray=394\n" +
189+
"Null=395\n" +
190+
"array array null null foo <EOF>\n";
191+
assertEquals(expected, found);
192+
assertNull(stderrDuringParse);
193+
}
194+
195+
/**
196+
* This is a regression test for antlr/antlr3#157 "missing lexer rules if
197+
* tokenVocab defined"
198+
* https://github.com/antlr/antlr3/pull/157
199+
*/
200+
@Test public void testImportedTokensInCombinedGrammarNoReferences() {
201+
String tokensFile =
202+
"TypeArray=394\n" +
203+
"Null=395\n";
204+
System.out.println("dir " + tmpdir);
205+
mkdir(tmpdir);
206+
writeFile(tmpdir, "CustomVocab.tokens", tokensFile);
207+
208+
String grammar =
209+
"grammar T;\n" +
210+
"options { output = AST; tokenVocab = CustomVocab; }\n" +
211+
"tokens { TypeArray = 'array'; }\n" +
212+
"a : (ID)* EOF\n" +
213+
" {System.out.println(tokenNames[TypeArray] + \"=\" + TypeArray);\n" +
214+
" System.out.println(tokenNames[Null] + \"=\" + Null);};\n" +
215+
"Null : 'null';\n" +
216+
"ID : 'a'..'z'+;\n" +
217+
"WS : ' '+ {skip();};\n";
218+
String input = "foo";
219+
String found = execParser("T.g", grammar, "TParser", "TLexer", "a", input, false);
220+
String expected =
221+
"TypeArray=394\n" +
222+
"Null=395\n" +
223+
"foo <EOF>\n";
224+
assertEquals(expected, found);
225+
assertNull(stderrDuringParse);
226+
}
227+
228+
/**
229+
* This is a regression test for antlr/antlr3#157 "missing lexer rules if
230+
* tokenVocab defined"
231+
* https://github.com/antlr/antlr3/pull/157
232+
*/
233+
@Test public void testImportedTokensInCombinedGrammarLiteralReferencesOnly() {
234+
String tokensFile =
235+
"TypeArray=394\n" +
236+
"Null=395\n";
237+
System.out.println("dir " + tmpdir);
238+
mkdir(tmpdir);
239+
writeFile(tmpdir, "CustomVocab.tokens", tokensFile);
240+
241+
String grammar =
242+
"grammar T;\n" +
243+
"options { output = AST; tokenVocab = CustomVocab; }\n" +
244+
"tokens { TypeArray = 'array'; }\n" +
245+
"a : ('array' 'null' ID)* EOF\n" +
246+
" {System.out.println(tokenNames[TypeArray] + \"=\" + TypeArray);\n" +
247+
" System.out.println(tokenNames[Null] + \"=\" + Null);};\n" +
248+
"Null : 'null';\n" +
249+
"ID : 'a'..'z'+;\n" +
250+
"WS : ' '+ {skip();};\n";
251+
String input = "array null foo";
252+
String found = execParser("T.g", grammar, "TParser", "TLexer", "a", input, false);
253+
String expected =
254+
"TypeArray=394\n" +
255+
"Null=395\n" +
256+
"array null foo <EOF>\n";
257+
assertEquals(expected, found);
258+
assertNull(stderrDuringParse);
259+
}
260+
261+
/**
262+
* This is a regression test for antlr/antlr3#157 "missing lexer rules if
263+
* tokenVocab defined"
264+
* https://github.com/antlr/antlr3/pull/157
265+
*/
266+
@Test public void testImportedTokensInCombinedGrammarSymbolicReferencesOnly() {
267+
String tokensFile =
268+
"TypeArray=394\n" +
269+
"Null=395\n";
270+
System.out.println("dir " + tmpdir);
271+
mkdir(tmpdir);
272+
writeFile(tmpdir, "CustomVocab.tokens", tokensFile);
273+
274+
String grammar =
275+
"grammar T;\n" +
276+
"options { output = AST; tokenVocab = CustomVocab; }\n" +
277+
"tokens { TypeArray = 'array'; }\n" +
278+
"a : (TypeArray Null ID)* EOF\n" +
279+
" {System.out.println(tokenNames[TypeArray] + \"=\" + TypeArray);\n" +
280+
" System.out.println(tokenNames[Null] + \"=\" + Null);};\n" +
281+
"Null : 'null';\n" +
282+
"ID : 'a'..'z'+;\n" +
283+
"WS : ' '+ {skip();};\n";
284+
String input = "array null foo";
285+
String found = execParser("T.g", grammar, "TParser", "TLexer", "a", input, false);
286+
String expected =
287+
"TypeArray=394\n" +
288+
"Null=395\n" +
289+
"array null foo <EOF>\n";
290+
assertEquals(expected, found);
291+
assertNull(stderrDuringParse);
292+
}
161293
}

0 commit comments

Comments
 (0)