Skip to content

Commit

Permalink
Fix a bug in passing the path to the StringTemplate group file, add a…
Browse files Browse the repository at this point in the history
… unit test

Signed-off-by: David Gregory <[email protected]>
  • Loading branch information
DavidGregory084 committed Apr 15, 2024
1 parent c7dc4c8 commit 0ae16da
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class RunOptions {
public final boolean showDFA;
public final Stage endStage;
public final String superClass;
public final String libDir;
public final String actionTemplates;
public final PredictionMode predictionMode;
public final boolean buildParseTree;
Expand All @@ -32,7 +33,7 @@ public RunOptions(String grammarFileName, String grammarStr, String parserName,
boolean useListener, boolean useVisitor, String startRuleName,
String input, boolean profile, boolean showDiagnosticErrors,
boolean traceATN, boolean showDFA, Stage endStage,
String language, String superClass, String actionTemplates, PredictionMode predictionMode, boolean buildParseTree) {
String language, String superClass, String libDir, String actionTemplates, PredictionMode predictionMode, boolean buildParseTree) {
this.grammarFileName = grammarFileName;
this.grammarStr = grammarStr;
this.parserName = parserName;
Expand Down Expand Up @@ -70,6 +71,7 @@ else if (lexerName != null) {
this.showDFA = showDFA;
this.endStage = endStage;
this.superClass = superClass;
this.libDir = libDir;
this.actionTemplates = actionTemplates;
this.predictionMode = predictionMode;
this.buildParseTree = buildParseTree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ public State run(RunOptions runOptions) {
if (runOptions.useVisitor) {
options.add("-visitor");
}

if (runOptions.libDir != null && runOptions.libDir.length() > 0) {
options.add("-lib");
options.add(runOptions.libDir);
}

if (runOptions.superClass != null && runOptions.superClass.length() > 0) {
options.add("-DsuperClass=" + runOptions.superClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ private static String test(RuntimeTestDescriptor descriptor, RuntimeRunner runne
targetName,
superClass,
null,
null,
descriptor.predictionMode,
descriptor.buildParseTree
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ public String test(RuntimeTestDescriptor descriptor, RuntimeRunner runner, Strin
targetName,
superClass,
null,
null,
PredictionMode.LL,
true
);
Expand Down
154 changes: 97 additions & 57 deletions tool-testsuite/test/org/antlr/v4/test/tool/TestActionTemplates.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ public class TestActionTemplates {
String actionTemplates = tempDir + FileSeparator + "Java.st";

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

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

assertInstanceOf(GeneratedState.class, state);
assertInstanceOf(GeneratedState.class, state, state.getErrorMessage());

GeneratedState generated = (GeneratedState) state;

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

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

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

assertInstanceOf(GeneratedState.class, state);
assertInstanceOf(GeneratedState.class, state, state.getErrorMessage());

GeneratedState generated = (GeneratedState) state;

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

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

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

assertInstanceOf(GeneratedState.class, state);
assertInstanceOf(GeneratedState.class, state, state.getErrorMessage());

GeneratedState generated = (GeneratedState) state;

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

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

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

assertInstanceOf(GeneratedState.class, state);
assertInstanceOf(GeneratedState.class, state, state.getErrorMessage());

GeneratedState generated = (GeneratedState) state;

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

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

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

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

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

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

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

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

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

@Test void testValidActionTemplate(@TempDir Path tempDir) {
@Test
void testValidActionTemplate(@TempDir Path tempDir) {
writeActionTemplatesFile(tempDir, "writeln(s) ::= <<outStream.println(\"<s>\");>>");

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

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

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

assertInstanceOf(ExecutedState.class, state);
assertInstanceOf(ExecutedState.class, state, state.getErrorMessage());

assertEquals(expecting, ((ExecutedState) state).output);
}

@Test void testValidMultilineActionTemplate(@TempDir Path tempDir) {
@Test
void testValidMultilineActionTemplate(@TempDir Path tempDir) {
writeActionTemplatesFile(tempDir, "writeln(s) ::= <<outStream.println(\"<s>\");>>");

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

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

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

assertInstanceOf(ExecutedState.class, state);
assertInstanceOf(ExecutedState.class, state, state.getErrorMessage());

assertEquals(expecting, ((ExecutedState) state).output);
}

@Test void testActionTemplateHeader(@TempDir Path tempDir) {
@Test
void testActionTemplateHeader(@TempDir Path tempDir) {
String actionTemplates =
"normalizerImports() ::= <<\n" +
"import java.text.Normalizer;\n" +
Expand All @@ -255,54 +259,55 @@ public class TestActionTemplates {
String actionTemplatesFile = tempDir + FileSeparator + "Java.stg";

String grammar =
"lexer grammar L;\n"+
"@lexer::header {\n"+
"<normalizerImports()>\n"+
"}\n"+
"ID : (ID_START ID_CONTINUE* | '_' ID_CONTINUE+) { <setText(normalize(getText()))> } ;\n"+
"ID_START : [\\p{XID_Start}] ;\n"+
"ID_CONTINUE: [\\p{XID_Continue}] ;\n"+
"lexer grammar L;\n" +
"@lexer::header {\n" +
"<normalizerImports()>\n" +
"}\n" +
"ID : (ID_START ID_CONTINUE* | '_' ID_CONTINUE+) { <setText(normalize(getText()))> } ;\n" +
"ID_START : [\\p{XID_Start}] ;\n" +
"ID_CONTINUE: [\\p{XID_Continue}] ;\n" +
"WS : (' '|'\\n') -> skip ;";

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

String expecting =
"[@0,0:3='This',<1>,1:0]\n"+
"[@1,5:7='_is',<1>,1:5]\n"+
"[@2,9:11='fine',<1>,1:9]\n"+
"[@0,0:3='This',<1>,1:0]\n" +
"[@1,5:7='_is',<1>,1:5]\n" +
"[@2,9:11='fine',<1>,1:9]\n" +
"[@3,12:11='<EOF>',<-1>,1:12]\n";

assertInstanceOf(ExecutedState.class, state);
assertInstanceOf(ExecutedState.class, state, state.getErrorMessage());

assertEquals(expecting, ((ExecutedState) state).output);
}

@Test void testActionTemplateSemanticPredicate(@TempDir Path tempDir) {
@Test
void testActionTemplateSemanticPredicate(@TempDir Path tempDir) {
String actionTemplates = "pred() ::= <<true>>";

writeActionTemplatesFile(tempDir, actionTemplates);

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

String grammar =
"grammar P;\n"+
"file : atom EOF ;\n"+
"atom : scientific | { <pred()> }? variable ;\n"+
"variable: VARIABLE ;\n"+
"scientific: SCIENTIFIC_NUMBER ;\n"+
"grammar P;\n" +
"file : atom EOF ;\n" +
"atom : scientific | { <pred()> }? variable ;\n" +
"variable: VARIABLE ;\n" +
"scientific: SCIENTIFIC_NUMBER ;\n" +
"VARIABLE : VALID_ID_START VALID_ID_CHAR* ;\n" +
"SCIENTIFIC_NUMBER : NUMBER (E SIGN? UNSIGNED_INTEGER)? ;\n"+
"fragment VALID_ID_START : ('a' .. 'z') | ('A' .. 'Z') | '_' ;\n"+
"fragment VALID_ID_CHAR : VALID_ID_START | ('0' .. '9') ;\n"+
"fragment NUMBER : ('0' .. '9') + ('.' ('0' .. '9') +)? ;\n"+
"fragment UNSIGNED_INTEGER : ('0' .. '9')+ ;\n"+
"fragment E : 'E' | 'e' ;\n"+
"fragment SIGN : ('+' | '-') ;\n"+
"SCIENTIFIC_NUMBER : NUMBER (E SIGN? UNSIGNED_INTEGER)? ;\n" +
"fragment VALID_ID_START : ('a' .. 'z') | ('A' .. 'Z') | '_' ;\n" +
"fragment VALID_ID_CHAR : VALID_ID_START | ('0' .. '9') ;\n" +
"fragment NUMBER : ('0' .. '9') + ('.' ('0' .. '9') +)? ;\n" +
"fragment UNSIGNED_INTEGER : ('0' .. '9')+ ;\n" +
"fragment E : 'E' | 'e' ;\n" +
"fragment SIGN : ('+' | '-') ;\n" +
"WS : (' '|'\\n') -> skip ;";

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

assertInstanceOf(ExecutedState.class, state);
assertInstanceOf(ExecutedState.class, state, state.getErrorMessage());

ExecutedState executedState = (ExecutedState) state;

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

@Test
void testValidActionTemplateInLibDir(@TempDir Path tempDir, @TempDir Path libDir) {
writeActionTemplatesFile(libDir, "writeln(s) ::= <<outStream.println(\"<s>\");>>");

String actionTemplates = "Java.stg";

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

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

// Should have identical output to TestLexerActions.testActionExecutedInDFA
String expecting =
"I\n" +
"I\n" +
"[@0,0:1='34',<1>,1:0]\n" +
"[@1,3:4='34',<1>,1:3]\n" +
"[@2,5:4='<EOF>',<-1>,1:5]\n";

assertInstanceOf(ExecutedState.class, state, state.getErrorMessage());

assertEquals(expecting, ((ExecutedState) state).output);
}

void writeActionTemplatesFile(Path tempDir, String template) {
writeFile(tempDir.toString(), "Java.stg", template);
}

State execParser(String grammarStr, String input, Path tempDir, String startRule, String actionTemplates) {
RunOptions runOptions = createOptionsForJavaToolTests("P.g4", grammarStr, "PParser", "PLexer",
false, true, startRule, input,
false, false, Stage.Execute, actionTemplates);
false, false, Stage.Execute, null, actionTemplates);
try (JavaRunner runner = new JavaRunner(tempDir, false)) {
return runner.run(runOptions);
}
Expand All @@ -327,7 +358,16 @@ State execParser(String grammarStr, String input, Path tempDir, String startRule
State execLexer(String grammarStr, String input, Path tempDir, String actionTemplates) {
RunOptions runOptions = createOptionsForJavaToolTests("L.g4", grammarStr, null, "L",
false, true, null, input,
false, false, Stage.Execute, actionTemplates);
false, false, Stage.Execute, null, actionTemplates);
try (JavaRunner runner = new JavaRunner(tempDir, false)) {
return runner.run(runOptions);
}
}

State execLexer(String grammarStr, String input, Path tempDir, Path libDir, String actionTemplates) {
RunOptions runOptions = createOptionsForJavaToolTests("L.g4", grammarStr, null, "L",
false, true, null, input,
false, false, Stage.Execute, libDir.toString(), actionTemplates);
try (JavaRunner runner = new JavaRunner(tempDir, false)) {
return runner.run(runOptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ private static boolean compile(String grammarFileName, String grammarStr, String
) {
RunOptions runOptions = createOptionsForJavaToolTests(grammarFileName, grammarStr, parserName, null,
false, false, startRuleName, null,
false, false, Stage.Compile, null);
false, false, Stage.Compile, null, null);
try (JavaRunner runner = new JavaRunner(tempDirPath, false)) {
JavaCompiledState compiledState = (JavaCompiledState) runner.run(runOptions);
return !compiledState.containsErrors();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ private static ParseTreeMatch checkPatternMatch(String grammar, String startRule
String lexerName = grammarName+"Lexer";
RunOptions runOptions = createOptionsForJavaToolTests(grammarFileName, grammar, parserName, lexerName,
false, false, startRule, input,
false, false, Stage.Execute, null);
false, false, Stage.Execute, null, null);
try (JavaRunner runner = new JavaRunner()) {
JavaExecutedState executedState = (JavaExecutedState)runner.run(runOptions);
JavaCompiledState compiledState = (JavaCompiledState)executedState.previousState;
Expand All @@ -413,7 +413,7 @@ private static ParseTreePatternMatcher getPatternMatcher(
) throws Exception {
RunOptions runOptions = createOptionsForJavaToolTests(grammarFileName, grammar, parserName, lexerName,
false, false, startRule, null,
false, false, Stage.Compile, null);
false, false, Stage.Compile, null, null);
try (JavaRunner runner = new JavaRunner()) {
JavaCompiledState compiledState = (JavaCompiledState) runner.run(runOptions);

Expand Down
Loading

0 comments on commit 0ae16da

Please sign in to comment.