Merge pull request #1721 from KvanTTT/invalid-charset

Added invalid charset error (for ranges without start or end)
This commit is contained in:
Terence Parr 2017-03-03 09:39:25 -08:00 committed by GitHub
commit d3ce604df1
5 changed files with 22 additions and 29 deletions

View File

@ -206,30 +206,6 @@ public class LexerExecDescriptors {
}
public static class CharSetWithMissingEndRange extends BaseLexerTestDescriptor {
public String input = "00\n";
/**
I
[@0,0:1='00',<1>,1:0]
[@1,3:2='<EOF>',<-1>,2:0]
*/
@CommentHasStringValue
public String output;
public String errors = null;
public String startRule = "";
public String grammarName = "L";
/**
lexer grammar L;
I : [0-]+ {<writeln("\"I\"")>} ;
WS : [ \n\\u000D]+ -> skip ;
*/ // needs escape on u000D otherwise java converts even in comment
@CommentHasStringValue
public String grammar;
}
public static class CharSetWithMissingEscapeChar extends BaseLexerTestDescriptor {
public String input = "34 ";
/**

View File

@ -1140,7 +1140,7 @@ SignedInteger
fragment
Sign
: [+-]
: [+\-]
;
fragment

View File

@ -509,6 +509,7 @@ public class TestToolSyntaxErrors extends BaseJavaToolTest {
"INVALID_CHAR_SET: [\\u24\\uA2][\\{];\n" + //https://github.com/antlr/antlr4/issues/1077
"EMPTY_STRING_LITERAL_RANGE: 'F'..'A' | 'Z';\n" +
"EMPTY_CHAR_SET: [f-az][];\n" +
"INVALID_RANGE_IN_CHAR_SET: [-z] | [a-] | [-];\n" +
"VALID_STRING_LITERALS: '\\u1234' | '\\t' | '\\'';\n" +
"VALID_CHAR_SET: [`\\-=\\]];";
@ -522,7 +523,10 @@ public class TestToolSyntaxErrors extends BaseJavaToolTest {
"warning(" + ErrorType.INVALID_ESCAPE_SEQUENCE.code + "): Test.g4:4:40: invalid escape sequence\n" +
"error(" + ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED.code + "): Test.g4:5:33: string literals and sets cannot be empty: 'F'..'A'\n" +
"error(" + ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED.code + "): Test.g4:6:30: string literals and sets cannot be empty: [f-a]\n" +
"error(" + ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED.code + "): Test.g4:6:36: string literals and sets cannot be empty: []\n";
"error(" + ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED.code + "): Test.g4:6:36: string literals and sets cannot be empty: []\n" +
"error(" + ErrorType.INVALID_CHAR_SET.code + "): Test.g4:7:30: invalid charset (range without start or end): [-z]\n" +
"error(" + ErrorType.INVALID_CHAR_SET.code + "): Test.g4:7:37: invalid charset (range without start or end): [a-]\n" +
"error(" + ErrorType.INVALID_CHAR_SET.code + "): Test.g4:7:44: invalid charset (range without start or end): [-]\n";
String[] pair = new String[] {
grammar,

View File

@ -476,9 +476,11 @@ public class LexerATNFactory extends ParserATNFactory {
offset = escapeParseResult.parseLength;
}
else if (c == '-' && !state.inRange) {
if (state.mode == CharSetParseState.Mode.PREV_PROPERTY) {
g.tool.errMgr.grammarError(ErrorType.UNICODE_PROPERTY_NOT_ALLOWED_IN_RANGE,
g.fileName, charSetAST.getToken(), charSetAST.getText());
if (state.mode == CharSetParseState.Mode.PREV_PROPERTY || i == 0 || i == n - 1) {
ErrorType errorType = state.mode == CharSetParseState.Mode.PREV_PROPERTY
? ErrorType.UNICODE_PROPERTY_NOT_ALLOWED_IN_RANGE
: ErrorType.INVALID_CHAR_SET;
g.tool.errMgr.grammarError(errorType, g.fileName, charSetAST.getToken(), charSetAST.getText());
state = CharSetParseState.ERROR;
}
else {

View File

@ -931,6 +931,17 @@ public enum ErrorType {
* <p>custom channels are not supported in combined grammars</p>
*/
CHANNELS_BLOCK_IN_COMBINED_GRAMMAR(164, "custom channels are not supported in combined grammars", ErrorSeverity.ERROR),
/**
* Compiler Error 165.
*
* <p>charset range should have start and end</p>
*
* <pre>
* INVALID_RANGE_IN_CHAR_SET: [-z] | [a-] | [-]; // error 165
* </pre>
*
* */
INVALID_CHAR_SET(165, "invalid charset (range without start or end): <arg>", ErrorSeverity.ERROR),
NONCONFORMING_LR_RULE(169, "rule <arg> is left recursive but doesn't conform to a pattern ANTLR can handle", ErrorSeverity.ERROR),
/**