Fixed non-informative NullPointerException. Empty strings now not allowed. fixed #959.

This commit is contained in:
Ivan Kochurkin 2015-10-12 16:33:32 +03:00
parent 78427bae79
commit 8bf480dcc5
3 changed files with 46 additions and 1 deletions

View File

@ -454,6 +454,33 @@ public class TestToolSyntaxErrors extends BaseTest {
super.testErrors(pair, true);
}
/**
* This is a regression test for antlr/antlr4#959 "NullPointerException".
* https://github.com/antlr/antlr4/issues/959
*/
@Test public void testNotAllowedEmptyStrings() {
String grammar =
"lexer grammar T;\n" +
"Error0: '''test''';\n" +
"Error1: '' 'test';\n" +
"Error2: 'test' '';\n" +
"Error3: '';\n" +
"NotError: ' ';";
String expected =
"error(" + ErrorType.EMPTY_STRINGS_NOT_ALLOWED.code + "): T.g4:2:8: empty strings not allowed\n" +
"error(" + ErrorType.EMPTY_STRINGS_NOT_ALLOWED.code + "): T.g4:2:16: empty strings not allowed\n" +
"error(" + ErrorType.EMPTY_STRINGS_NOT_ALLOWED.code + "): T.g4:3:8: empty strings not allowed\n" +
"error(" + ErrorType.EMPTY_STRINGS_NOT_ALLOWED.code + "): T.g4:4:15: empty strings not allowed\n" +
"error(" + ErrorType.EMPTY_STRINGS_NOT_ALLOWED.code + "): T.g4:5:8: empty strings not allowed\n";
String[] pair = new String[] {
grammar,
expected
};
super.testErrors(pair, true);
}
/**
* This test ensures the {@link ErrorType#UNRECOGNIZED_ASSOC_OPTION} warning
* is produced as described in the documentation.

View File

@ -491,6 +491,14 @@ public class BasicSemanticChecks extends GrammarTreeVisitor {
label.getText());
}
@Override
protected void enterTerminal(GrammarAST tree) {
String text = tree.getText();
if (text.equals("''")) {
g.tool.errMgr.grammarError(ErrorType.EMPTY_STRINGS_NOT_ALLOWED, g.fileName, tree.token);
}
}
/** Check option is appropriate for grammar, rule, subrule */
boolean checkOptions(GrammarAST parent,
Token optionID,

View File

@ -1001,6 +1001,16 @@ public enum ErrorType {
* <p>Can be used and can be declared: DEFAULT_MODE</p>
*/
MODE_CONFLICTS_WITH_COMMON_CONSTANTS(173, "can not use or declare mode with reserved name <arg>", ErrorSeverity.ERROR),
/**
* Compiler Error 174.
*
* <p>empty strings not allowed</p>
*
* <pre>A: '''test''';</pre>
* <pre>B: '';</pre>
* <pre>C: 'test' '';</pre>
*/
EMPTY_STRINGS_NOT_ALLOWED(174, "empty strings not allowed", ErrorSeverity.ERROR),
/*
* Backward incompatibility errors