Add error 149: INVALID_LEXER_COMMAND (fixes #190)
This commit is contained in:
parent
05f4b76fab
commit
551100ea37
|
@ -143,6 +143,11 @@ public class LexerATNFactory extends ParserATNFactory {
|
|||
ST cmdST = codegenTemplates.getInstanceOf("Lexer" +
|
||||
CharSupport.capitalize(ID.getText())+
|
||||
"Command");
|
||||
if (cmdST == null) {
|
||||
g.tool.errMgr.grammarError(ErrorType.INVALID_LEXER_COMMAND, g.fileName, ID.token, ID.getText());
|
||||
return "";
|
||||
}
|
||||
|
||||
cmdST.add("arg", arg.getText());
|
||||
return cmdST.render();
|
||||
}
|
||||
|
@ -152,6 +157,11 @@ public class LexerATNFactory extends ParserATNFactory {
|
|||
ST cmdST = codegenTemplates.getInstanceOf("Lexer" +
|
||||
CharSupport.capitalize(ID.getText())+
|
||||
"Command");
|
||||
if (cmdST == null) {
|
||||
g.tool.errMgr.grammarError(ErrorType.INVALID_LEXER_COMMAND, g.fileName, ID.token, ID.getText());
|
||||
return "";
|
||||
}
|
||||
|
||||
return cmdST.render();
|
||||
}
|
||||
|
||||
|
|
|
@ -171,6 +171,19 @@ public enum ErrorType {
|
|||
* </pre>
|
||||
*/
|
||||
EPSILON_LR_FOLLOW(148, "left recursive rule '<arg>' contains a left recursive alternative which can be followed by the empty string", ErrorSeverity.ERROR),
|
||||
/**
|
||||
* Each lexer command requires an explicit implementation in the target
|
||||
* templates. This error indicates that the command was incorrectly written
|
||||
* or is not supported by the current target.
|
||||
* <p/>
|
||||
* The following rule produces this error.
|
||||
*
|
||||
* <pre>
|
||||
* X : 'foo' -> type(Foo); // ok
|
||||
* Y : 'foo' -> token(Foo); // error 149 (token is not a supported lexer command)
|
||||
* </pre>
|
||||
*/
|
||||
INVALID_LEXER_COMMAND(149, "lexer command '<arg>' does not exist or is not supported by the current target", ErrorSeverity.ERROR),
|
||||
|
||||
// Backward incompatibility errors
|
||||
V3_TREE_GRAMMAR(200, "tree grammars are not supported in ANTLR 4", ErrorSeverity.ERROR),
|
||||
|
|
|
@ -228,4 +228,23 @@ public class TestToolSyntaxErrors extends BaseTest {
|
|||
};
|
||||
super.testErrors(pair, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a regression test for antlr/antlr4#190
|
||||
* "NullPointerException building lexer grammar using bogus 'token' action"
|
||||
* https://github.com/antlr/antlr4/issues/190
|
||||
*/
|
||||
@Test public void testInvalidLexerCommand() {
|
||||
String[] pair = new String[] {
|
||||
"grammar A;\n" +
|
||||
"tokens{Foo}\n" +
|
||||
"b : Foo ;\n" +
|
||||
"X : 'foo' -> popmode;\n" + // "meant" to use -> popMode
|
||||
"Y : 'foo' -> token(Foo);", // "meant" to use -> type(Foo)
|
||||
|
||||
"error(" + ErrorType.INVALID_LEXER_COMMAND.code + "): A.g4:4:13: lexer command 'popmode' does not exist or is not supported by the current target\n" +
|
||||
"error(" + ErrorType.INVALID_LEXER_COMMAND.code + "): A.g4:5:13: lexer command 'token' does not exist or is not supported by the current target\n"
|
||||
};
|
||||
super.testErrors(pair, true);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue