Skip to content

Commit 5912bef

Browse files
Fix a bug in passing the path to the StringTemplate group file, add a unit test
1 parent c7dc4c8 commit 5912bef

File tree

12 files changed

+118
-68
lines changed

12 files changed

+118
-68
lines changed

runtime-testsuite/test/org/antlr/v4/test/runtime/RunOptions.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class RunOptions {
2424
public final boolean showDFA;
2525
public final Stage endStage;
2626
public final String superClass;
27+
public final String libDir;
2728
public final String actionTemplates;
2829
public final PredictionMode predictionMode;
2930
public final boolean buildParseTree;
@@ -32,7 +33,7 @@ public RunOptions(String grammarFileName, String grammarStr, String parserName,
3233
boolean useListener, boolean useVisitor, String startRuleName,
3334
String input, boolean profile, boolean showDiagnosticErrors,
3435
boolean traceATN, boolean showDFA, Stage endStage,
35-
String language, String superClass, String actionTemplates, PredictionMode predictionMode, boolean buildParseTree) {
36+
String language, String superClass, String libDir, String actionTemplates, PredictionMode predictionMode, boolean buildParseTree) {
3637
this.grammarFileName = grammarFileName;
3738
this.grammarStr = grammarStr;
3839
this.parserName = parserName;
@@ -70,6 +71,7 @@ else if (lexerName != null) {
7071
this.showDFA = showDFA;
7172
this.endStage = endStage;
7273
this.superClass = superClass;
74+
this.libDir = libDir;
7375
this.actionTemplates = actionTemplates;
7476
this.predictionMode = predictionMode;
7577
this.buildParseTree = buildParseTree;

runtime-testsuite/test/org/antlr/v4/test/runtime/RuntimeRunner.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ public State run(RunOptions runOptions) {
168168
if (runOptions.useVisitor) {
169169
options.add("-visitor");
170170
}
171+
172+
if (runOptions.libDir != null && runOptions.libDir.length() > 0) {
173+
options.add("-lib");
174+
options.add(runOptions.libDir);
175+
}
176+
171177
if (runOptions.superClass != null && runOptions.superClass.length() > 0) {
172178
options.add("-DsuperClass=" + runOptions.superClass);
173179
}

runtime-testsuite/test/org/antlr/v4/test/runtime/RuntimeTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ private static String test(RuntimeTestDescriptor descriptor, RuntimeRunner runne
168168
targetName,
169169
superClass,
170170
null,
171+
null,
171172
descriptor.predictionMode,
172173
descriptor.buildParseTree
173174
);

runtime-testsuite/test/org/antlr/v4/test/runtime/TraceATN.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public String test(RuntimeTestDescriptor descriptor, RuntimeRunner runner, Strin
159159
targetName,
160160
superClass,
161161
null,
162+
null,
162163
PredictionMode.LL,
163164
true
164165
);

tool-testsuite/test/org/antlr/v4/test/tool/TestActionTemplates.java

Lines changed: 97 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public class TestActionTemplates {
2323
String actionTemplates = tempDir + FileSeparator + "Java.st";
2424

2525
String grammar =
26-
"lexer grammar L;"+
26+
"lexer grammar L;" +
2727
"WS : (' '|'\\n') -> skip ;";
2828

2929
State state = execLexer(grammar, "34 34", tempDir, actionTemplates);
3030

31-
assertInstanceOf(GeneratedState.class, state);
31+
assertInstanceOf(GeneratedState.class, state, state.getErrorMessage());
3232

3333
GeneratedState generated = (GeneratedState) state;
3434

@@ -45,12 +45,12 @@ public class TestActionTemplates {
4545
String actionTemplates = tempDir + FileSeparator + "Java.stg";
4646

4747
String grammar =
48-
"lexer grammar L;"+
48+
"lexer grammar L;" +
4949
"WS : (' '|'\\n') -> skip ;";
5050

5151
State state = execLexer(grammar, "34 34", tempDir, actionTemplates);
5252

53-
assertInstanceOf(GeneratedState.class, state);
53+
assertInstanceOf(GeneratedState.class, state, state.getErrorMessage());
5454

5555
GeneratedState generated = (GeneratedState) state;
5656

@@ -69,13 +69,13 @@ public class TestActionTemplates {
6969
String grammarFile = tempDir + FileSeparator + "L.g4";
7070

7171
String grammar =
72-
"lexer grammar L;\n"+
73-
"I : '0'..'9'+ {<¢>} ;\n"+
72+
"lexer grammar L;\n" +
73+
"I : '0'..'9'+ {<¢>} ;\n" +
7474
"WS : (' '|'\\n') -> skip ;";
7575

7676
State state = execLexer(grammar, "34 34", tempDir, actionTemplates);
7777

78-
assertInstanceOf(GeneratedState.class, state);
78+
assertInstanceOf(GeneratedState.class, state, state.getErrorMessage());
7979

8080
GeneratedState generated = (GeneratedState) state;
8181

@@ -95,15 +95,15 @@ public class TestActionTemplates {
9595
String grammarFile = tempDir + FileSeparator + "L.g4";
9696

9797
String grammar =
98-
"lexer grammar L;\n"+
98+
"lexer grammar L;\n" +
9999
"I : '0'..'9'+ {\n" +
100100
" <¢>\n" +
101-
"};\n"+
101+
"};\n" +
102102
"WS : (' '|'\\n') -> skip ;";
103103

104104
State state = execLexer(grammar, "34 34", tempDir, actionTemplates);
105105

106-
assertInstanceOf(GeneratedState.class, state);
106+
assertInstanceOf(GeneratedState.class, state, state.getErrorMessage());
107107

108108
GeneratedState generated = (GeneratedState) state;
109109

@@ -123,8 +123,8 @@ public class TestActionTemplates {
123123
String grammarFile = tempDir + FileSeparator + "L.g4";
124124

125125
String grammar =
126-
"lexer grammar L;\n"+
127-
"I : '0'..'9'+ {<writeln(\"I\")>} ;\n"+
126+
"lexer grammar L;\n" +
127+
"I : '0'..'9'+ {<writeln(\"I\")>} ;\n" +
128128
"WS : (' '|'\\n') -> skip ;";
129129

130130
State state = execLexer(grammar, "34 34", tempDir, actionTemplates);
@@ -147,8 +147,8 @@ public class TestActionTemplates {
147147
String grammarFile = tempDir + FileSeparator + "L.g4";
148148

149149
String grammar =
150-
"lexer grammar L;\n"+
151-
"I : '0'..'9'+ {<writeln(\"I\")} ;\n"+
150+
"lexer grammar L;\n" +
151+
"I : '0'..'9'+ {<writeln(\"I\")} ;\n" +
152152
"WS : (' '|'\\n') -> skip ;";
153153

154154
State state = execLexer(grammar, "34 34", tempDir, actionTemplates);
@@ -163,17 +163,18 @@ public class TestActionTemplates {
163163
generated.getErrorMessage());
164164
}
165165

166-
@Test void testInvalidMultilineActionTemplate(@TempDir Path tempDir) {
166+
@Test
167+
void testInvalidMultilineActionTemplate(@TempDir Path tempDir) {
167168
writeActionTemplatesFile(tempDir, "writeln(s) ::= <<outStream.println(\"<s>\");>>");
168169

169170
String actionTemplates = tempDir + FileSeparator + "Java.stg";
170171
String grammarFile = tempDir + FileSeparator + "L.g4";
171172

172173
String grammar =
173-
"lexer grammar L;\n"+
174-
"I : '0'..'9'+ {\n"+
175-
" <writeln(\"I\")\n"+
176-
"};\n"+
174+
"lexer grammar L;\n" +
175+
"I : '0'..'9'+ {\n" +
176+
" <writeln(\"I\")\n" +
177+
"};\n" +
177178
"WS : (' '|'\\n') -> skip ;";
178179

179180
State state = execLexer(grammar, "34 34", tempDir, actionTemplates);
@@ -188,14 +189,15 @@ public class TestActionTemplates {
188189
generated.getErrorMessage());
189190
}
190191

191-
@Test void testValidActionTemplate(@TempDir Path tempDir) {
192+
@Test
193+
void testValidActionTemplate(@TempDir Path tempDir) {
192194
writeActionTemplatesFile(tempDir, "writeln(s) ::= <<outStream.println(\"<s>\");>>");
193195

194196
String actionTemplates = tempDir + FileSeparator + "Java.stg";
195197

196198
String grammar =
197-
"lexer grammar L;\n"+
198-
"I : '0'..'9'+ {<writeln(\"I\")>} ;\n"+
199+
"lexer grammar L;\n" +
200+
"I : '0'..'9'+ {<writeln(\"I\")>} ;\n" +
199201
"WS : (' '|'\\n') -> skip ;";
200202

201203
State state = execLexer(grammar, "34 34", tempDir, actionTemplates);
@@ -208,21 +210,22 @@ public class TestActionTemplates {
208210
"[@1,3:4='34',<1>,1:3]\n" +
209211
"[@2,5:4='<EOF>',<-1>,1:5]\n";
210212

211-
assertInstanceOf(ExecutedState.class, state);
213+
assertInstanceOf(ExecutedState.class, state, state.getErrorMessage());
212214

213215
assertEquals(expecting, ((ExecutedState) state).output);
214216
}
215217

216-
@Test void testValidMultilineActionTemplate(@TempDir Path tempDir) {
218+
@Test
219+
void testValidMultilineActionTemplate(@TempDir Path tempDir) {
217220
writeActionTemplatesFile(tempDir, "writeln(s) ::= <<outStream.println(\"<s>\");>>");
218221

219222
String actionTemplates = tempDir + FileSeparator + "Java.stg";
220223

221224
String grammar =
222-
"lexer grammar L;\n"+
223-
"I : '0'..'9'+ {\n"+
224-
" <writeln(\"I\")>\n"+
225-
"};\n"+
225+
"lexer grammar L;\n" +
226+
"I : '0'..'9'+ {\n" +
227+
" <writeln(\"I\")>\n" +
228+
"};\n" +
226229
"WS : (' '|'\\n') -> skip ;";
227230

228231
State state = execLexer(grammar, "34 34", tempDir, actionTemplates);
@@ -235,12 +238,13 @@ public class TestActionTemplates {
235238
"[@1,3:4='34',<1>,1:3]\n" +
236239
"[@2,5:4='<EOF>',<-1>,1:5]\n";
237240

238-
assertInstanceOf(ExecutedState.class, state);
241+
assertInstanceOf(ExecutedState.class, state, state.getErrorMessage());
239242

240243
assertEquals(expecting, ((ExecutedState) state).output);
241244
}
242245

243-
@Test void testActionTemplateHeader(@TempDir Path tempDir) {
246+
@Test
247+
void testActionTemplateHeader(@TempDir Path tempDir) {
244248
String actionTemplates =
245249
"normalizerImports() ::= <<\n" +
246250
"import java.text.Normalizer;\n" +
@@ -255,54 +259,55 @@ public class TestActionTemplates {
255259
String actionTemplatesFile = tempDir + FileSeparator + "Java.stg";
256260

257261
String grammar =
258-
"lexer grammar L;\n"+
259-
"@lexer::header {\n"+
260-
"<normalizerImports()>\n"+
261-
"}\n"+
262-
"ID : (ID_START ID_CONTINUE* | '_' ID_CONTINUE+) { <setText(normalize(getText()))> } ;\n"+
263-
"ID_START : [\\p{XID_Start}] ;\n"+
264-
"ID_CONTINUE: [\\p{XID_Continue}] ;\n"+
262+
"lexer grammar L;\n" +
263+
"@lexer::header {\n" +
264+
"<normalizerImports()>\n" +
265+
"}\n" +
266+
"ID : (ID_START ID_CONTINUE* | '_' ID_CONTINUE+) { <setText(normalize(getText()))> } ;\n" +
267+
"ID_START : [\\p{XID_Start}] ;\n" +
268+
"ID_CONTINUE: [\\p{XID_Continue}] ;\n" +
265269
"WS : (' '|'\\n') -> skip ;";
266270

267271
State state = execLexer(grammar, "This _is \ufb01ne", tempDir, actionTemplatesFile);
268272

269273
String expecting =
270-
"[@0,0:3='This',<1>,1:0]\n"+
271-
"[@1,5:7='_is',<1>,1:5]\n"+
272-
"[@2,9:11='fine',<1>,1:9]\n"+
274+
"[@0,0:3='This',<1>,1:0]\n" +
275+
"[@1,5:7='_is',<1>,1:5]\n" +
276+
"[@2,9:11='fine',<1>,1:9]\n" +
273277
"[@3,12:11='<EOF>',<-1>,1:12]\n";
274278

275-
assertInstanceOf(ExecutedState.class, state);
279+
assertInstanceOf(ExecutedState.class, state, state.getErrorMessage());
276280

277281
assertEquals(expecting, ((ExecutedState) state).output);
278282
}
279283

280-
@Test void testActionTemplateSemanticPredicate(@TempDir Path tempDir) {
284+
@Test
285+
void testActionTemplateSemanticPredicate(@TempDir Path tempDir) {
281286
String actionTemplates = "pred() ::= <<true>>";
282287

283288
writeActionTemplatesFile(tempDir, actionTemplates);
284289

285290
String actionTemplatesFile = tempDir + FileSeparator + "Java.stg";
286291

287292
String grammar =
288-
"grammar P;\n"+
289-
"file : atom EOF ;\n"+
290-
"atom : scientific | { <pred()> }? variable ;\n"+
291-
"variable: VARIABLE ;\n"+
292-
"scientific: SCIENTIFIC_NUMBER ;\n"+
293+
"grammar P;\n" +
294+
"file : atom EOF ;\n" +
295+
"atom : scientific | { <pred()> }? variable ;\n" +
296+
"variable: VARIABLE ;\n" +
297+
"scientific: SCIENTIFIC_NUMBER ;\n" +
293298
"VARIABLE : VALID_ID_START VALID_ID_CHAR* ;\n" +
294-
"SCIENTIFIC_NUMBER : NUMBER (E SIGN? UNSIGNED_INTEGER)? ;\n"+
295-
"fragment VALID_ID_START : ('a' .. 'z') | ('A' .. 'Z') | '_' ;\n"+
296-
"fragment VALID_ID_CHAR : VALID_ID_START | ('0' .. '9') ;\n"+
297-
"fragment NUMBER : ('0' .. '9') + ('.' ('0' .. '9') +)? ;\n"+
298-
"fragment UNSIGNED_INTEGER : ('0' .. '9')+ ;\n"+
299-
"fragment E : 'E' | 'e' ;\n"+
300-
"fragment SIGN : ('+' | '-') ;\n"+
299+
"SCIENTIFIC_NUMBER : NUMBER (E SIGN? UNSIGNED_INTEGER)? ;\n" +
300+
"fragment VALID_ID_START : ('a' .. 'z') | ('A' .. 'Z') | '_' ;\n" +
301+
"fragment VALID_ID_CHAR : VALID_ID_START | ('0' .. '9') ;\n" +
302+
"fragment NUMBER : ('0' .. '9') + ('.' ('0' .. '9') +)? ;\n" +
303+
"fragment UNSIGNED_INTEGER : ('0' .. '9')+ ;\n" +
304+
"fragment E : 'E' | 'e' ;\n" +
305+
"fragment SIGN : ('+' | '-') ;\n" +
301306
"WS : (' '|'\\n') -> skip ;";
302307

303308
State state = execParser(grammar, "Bla", tempDir, "file", actionTemplatesFile);
304309

305-
assertInstanceOf(ExecutedState.class, state);
310+
assertInstanceOf(ExecutedState.class, state, state.getErrorMessage());
306311

307312
ExecutedState executedState = (ExecutedState) state;
308313

@@ -311,14 +316,40 @@ public class TestActionTemplates {
311316
assertEquals("", executedState.errors);
312317
}
313318

319+
@Test
320+
void testValidActionTemplateInLibDir(@TempDir Path tempDir, @TempDir Path libDir) {
321+
writeActionTemplatesFile(libDir, "writeln(s) ::= <<outStream.println(\"<s>\");>>");
322+
323+
String actionTemplates = "Java.stg";
324+
325+
String grammar =
326+
"lexer grammar L;\n" +
327+
"I : '0'..'9'+ {<writeln(\"I\")>} ;\n" +
328+
"WS : (' '|'\\n') -> skip ;";
329+
330+
State state = execLexer(grammar, "34 34", tempDir, libDir, actionTemplates);
331+
332+
// Should have identical output to TestLexerActions.testActionExecutedInDFA
333+
String expecting =
334+
"I\n" +
335+
"I\n" +
336+
"[@0,0:1='34',<1>,1:0]\n" +
337+
"[@1,3:4='34',<1>,1:3]\n" +
338+
"[@2,5:4='<EOF>',<-1>,1:5]\n";
339+
340+
assertInstanceOf(ExecutedState.class, state, state.getErrorMessage());
341+
342+
assertEquals(expecting, ((ExecutedState) state).output);
343+
}
344+
314345
void writeActionTemplatesFile(Path tempDir, String template) {
315346
writeFile(tempDir.toString(), "Java.stg", template);
316347
}
317348

318349
State execParser(String grammarStr, String input, Path tempDir, String startRule, String actionTemplates) {
319350
RunOptions runOptions = createOptionsForJavaToolTests("P.g4", grammarStr, "PParser", "PLexer",
320351
false, true, startRule, input,
321-
false, false, Stage.Execute, actionTemplates);
352+
false, false, Stage.Execute, null, actionTemplates);
322353
try (JavaRunner runner = new JavaRunner(tempDir, false)) {
323354
return runner.run(runOptions);
324355
}
@@ -327,7 +358,16 @@ State execParser(String grammarStr, String input, Path tempDir, String startRule
327358
State execLexer(String grammarStr, String input, Path tempDir, String actionTemplates) {
328359
RunOptions runOptions = createOptionsForJavaToolTests("L.g4", grammarStr, null, "L",
329360
false, true, null, input,
330-
false, false, Stage.Execute, actionTemplates);
361+
false, false, Stage.Execute, null, actionTemplates);
362+
try (JavaRunner runner = new JavaRunner(tempDir, false)) {
363+
return runner.run(runOptions);
364+
}
365+
}
366+
367+
State execLexer(String grammarStr, String input, Path tempDir, Path libDir, String actionTemplates) {
368+
RunOptions runOptions = createOptionsForJavaToolTests("L.g4", grammarStr, null, "L",
369+
false, true, null, input,
370+
false, false, Stage.Execute, libDir.toString(), actionTemplates);
331371
try (JavaRunner runner = new JavaRunner(tempDir, false)) {
332372
return runner.run(runOptions);
333373
}

tool-testsuite/test/org/antlr/v4/test/tool/TestCompositeGrammars.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ private static boolean compile(String grammarFileName, String grammarStr, String
727727
) {
728728
RunOptions runOptions = createOptionsForJavaToolTests(grammarFileName, grammarStr, parserName, null,
729729
false, false, startRuleName, null,
730-
false, false, Stage.Compile, null);
730+
false, false, Stage.Compile, null, null);
731731
try (JavaRunner runner = new JavaRunner(tempDirPath, false)) {
732732
JavaCompiledState compiledState = (JavaCompiledState) runner.run(runOptions);
733733
return !compiledState.containsErrors();

tool-testsuite/test/org/antlr/v4/test/tool/TestParseTreeMatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ private static ParseTreeMatch checkPatternMatch(String grammar, String startRule
392392
String lexerName = grammarName+"Lexer";
393393
RunOptions runOptions = createOptionsForJavaToolTests(grammarFileName, grammar, parserName, lexerName,
394394
false, false, startRule, input,
395-
false, false, Stage.Execute, null);
395+
false, false, Stage.Execute, null, null);
396396
try (JavaRunner runner = new JavaRunner()) {
397397
JavaExecutedState executedState = (JavaExecutedState)runner.run(runOptions);
398398
JavaCompiledState compiledState = (JavaCompiledState)executedState.previousState;
@@ -413,7 +413,7 @@ private static ParseTreePatternMatcher getPatternMatcher(
413413
) throws Exception {
414414
RunOptions runOptions = createOptionsForJavaToolTests(grammarFileName, grammar, parserName, lexerName,
415415
false, false, startRule, null,
416-
false, false, Stage.Compile, null);
416+
false, false, Stage.Compile, null, null);
417417
try (JavaRunner runner = new JavaRunner()) {
418418
JavaCompiledState compiledState = (JavaCompiledState) runner.run(runOptions);
419419

0 commit comments

Comments
 (0)