Skip to content

Commit

Permalink
Integrate ST error reporting with ANTLR's grammar error reporting
Browse files Browse the repository at this point in the history
Signed-off-by: David Gregory <2992938+DavidGregory084@users.noreply.github.com>
  • Loading branch information
DavidGregory084 committed Jul 5, 2023
1 parent 616d764 commit f89c520
Show file tree
Hide file tree
Showing 3 changed files with 472 additions and 29 deletions.
163 changes: 161 additions & 2 deletions tool-testsuite/test/org/antlr/v4/test/tool/TestActionTemplates.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ public class TestActionTemplates {
State state = execLexer(grammar, "34 34", tempDir, actionTemplates);

assertInstanceOf(GeneratedState.class, state);

GeneratedState generated = (GeneratedState) state;

assertTrue(generated.containsErrors());

assertEquals(
"State: Generate; \n" +
"error(207): error reading action templates file " + actionTemplates + ": " +
"error(208): error reading action templates file " + actionTemplates + ": " +
"Group file names must end in .stg: " + actionTemplates + "\n",
generated.getErrorMessage());
}
Expand All @@ -46,15 +49,144 @@ public class TestActionTemplates {
State state = execLexer(grammar, "34 34", tempDir, actionTemplates);

assertInstanceOf(GeneratedState.class, state);

GeneratedState generated = (GeneratedState) state;

assertTrue(generated.containsErrors());

assertEquals(
"State: Generate; \n" +
"error(206): cannot find action templates file " + actionTemplates + " given for L\n",
generated.getErrorMessage());
}

@Test void testActionTemplateLexerAction(@TempDir Path tempDir) {
@Test void testUnlexableActionTemplate(@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"+
"WS : (' '|'\\n') -> skip ;";

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

assertInstanceOf(GeneratedState.class, state);

GeneratedState generated = (GeneratedState) state;

assertTrue(generated.containsErrors());

assertEquals(
"State: Generate; \n" +
"error(211): " + grammarFile + ":2:14: error compiling action template: 2:16: invalid character '¢'\n" +
"error(211): " + grammarFile + ":2:14: error compiling action template: 2:14: this doesn't look like a template: \" <¢> \"\n",
generated.getErrorMessage());
}

@Test void testUnlexableMultilineActionTemplate(@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" +
" <¢>\n" +
"};\n"+
"WS : (' '|'\\n') -> skip ;";

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

assertInstanceOf(GeneratedState.class, state);

GeneratedState generated = (GeneratedState) state;

assertTrue(generated.containsErrors());

assertEquals(
"State: Generate; \n" +
"error(211): " + grammarFile + ":2:14: error compiling action template: 3:3: invalid character '¢'\n" +
"error(211): " + grammarFile + ":2:14: error compiling action template: 3:0: mismatched input ' ' expecting EOF\n",
generated.getErrorMessage());
}

@Test void testInvalidActionTemplateGroup(@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'+ {<writeln(\"I\")>} ;\n"+
"WS : (' '|'\\n') -> skip ;";

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

GeneratedState generated = (GeneratedState) state;

assertTrue(generated.containsErrors());

assertEquals(
"State: Generate; \n" +
"error(210): error rendering action template file " + actionTemplates + ": Java.stg 1:11: mismatched input ':' expecting '::='\n" +
"error(212): " + grammarFile + ":2:14: error rendering action template: 2:16: no such template: /writeln\n",
generated.getErrorMessage());
}

@Test void testInvalidActionTemplate(@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'+ {<writeln(\"I\")} ;\n"+
"WS : (' '|'\\n') -> skip ;";

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

GeneratedState generated = (GeneratedState) state;

assertTrue(generated.containsErrors());

assertEquals(
"State: Generate; \n" +
"error(211): " + grammarFile + ":2:14: error compiling action template: 2:29: premature EOF\n",
generated.getErrorMessage());
}

@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"+
"WS : (' '|'\\n') -> skip ;";

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

GeneratedState generated = (GeneratedState) state;

assertTrue(generated.containsErrors());

assertEquals(
"State: Generate; \n" +
"error(211): " + grammarFile + ":2:14: error compiling action template: 4:1: premature EOF\n",
generated.getErrorMessage());
}

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

String actionTemplates = tempDir + FileSeparator + "Java.stg";
Expand All @@ -79,6 +211,33 @@ public class TestActionTemplates {
assertEquals(expecting, ((ExecutedState) state).output);
}

@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"+
"WS : (' '|'\\n') -> skip ;";

State state = execLexer(grammar, "34 34", tempDir, 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);

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

@Test void testActionTemplateHeader(@TempDir Path tempDir) {
String actionTemplates =
"normalizerImports() ::= <<\n" +
Expand Down
32 changes: 31 additions & 1 deletion tool/src/org/antlr/v4/tool/ErrorType.java
Original file line number Diff line number Diff line change
Expand Up @@ -1244,9 +1244,39 @@ public enum ErrorType {
/**
* Compiler Error 207.
*
* <p>cannot find action tempaltes file <em>filename</em></p>
*/
CANNOT_FIND_ACTION_TEMPLATES_FILE_REFD_IN_GRAMMAR(207, "cannot find action templates file <arg>", ErrorSeverity.ERROR),
/**
* Compiler Error 208.
*
* <p>error reading action templates file <em>filename</em>: <em>reason</em></p>
*/
ERROR_READING_ACTION_TEMPLATES_FILE(207, "error reading action templates file <arg>: <arg2>", ErrorSeverity.ERROR),
ERROR_READING_ACTION_TEMPLATES_FILE(208, "error reading action templates file <arg>: <arg2>", ErrorSeverity.ERROR),
/**
* Compiler Error 209.
*
* <p>error compiling action template file <em>filename</em>: <em>reason</em></p>
*/
ERROR_COMPILING_ACTION_TEMPLATE_FILE(209, "error compiling action templates file <arg>: <arg2>", ErrorSeverity.ERROR),
/**
* Compiler Error 210.
*
* <p>error rendering action template file <em>filename</em>: <em>reason</em></p>
*/
ERROR_RENDERING_ACTION_TEMPLATE_FILE(210, "error rendering action template file <arg>: <arg2>", ErrorSeverity.ERROR),
/**
* Compiler Error 211.
*
* <p>error compiling action template: <em>reason</em></p>
*/
ERROR_COMPILING_ACTION_TEMPLATE(211, "error compiling action template: <arg>", ErrorSeverity.ERROR),
/**
* Compiler Error 212.
*
* <p>error rendering action template: <em>reason</em></p>
*/
ERROR_RENDERING_ACTION_TEMPLATE(212, "error rendering action template: <arg>", ErrorSeverity.ERROR),

// Dependency sorting errors

Expand Down
Loading

0 comments on commit f89c520

Please sign in to comment.