diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/descriptors/LexerExecDescriptors.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/descriptors/LexerExecDescriptors.java index 4b76f4286..ac89fc7ee 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/descriptors/LexerExecDescriptors.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/descriptors/LexerExecDescriptors.java @@ -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='',<-1>,2:0] - */ - @CommentHasStringValue - public String output; - - public String errors = null; - public String startRule = ""; - public String grammarName = "L"; - - /** - lexer grammar L; - I : [0-]+ {} ; - 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 "; /** diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/Java.g4 b/tool-testsuite/test/org/antlr/v4/test/tool/Java.g4 index e3e39f679..7db38ef3d 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/Java.g4 +++ b/tool-testsuite/test/org/antlr/v4/test/tool/Java.g4 @@ -1140,7 +1140,7 @@ SignedInteger fragment Sign - : [+-] + : [+\-] ; fragment diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/TestToolSyntaxErrors.java b/tool-testsuite/test/org/antlr/v4/test/tool/TestToolSyntaxErrors.java index c0785cc18..abc5eae47 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/TestToolSyntaxErrors.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/TestToolSyntaxErrors.java @@ -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, diff --git a/tool/src/org/antlr/v4/automata/LexerATNFactory.java b/tool/src/org/antlr/v4/automata/LexerATNFactory.java index 9c10c8310..e9839872f 100644 --- a/tool/src/org/antlr/v4/automata/LexerATNFactory.java +++ b/tool/src/org/antlr/v4/automata/LexerATNFactory.java @@ -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 { diff --git a/tool/src/org/antlr/v4/tool/ErrorType.java b/tool/src/org/antlr/v4/tool/ErrorType.java index 7f6dfbac5..92038efd8 100644 --- a/tool/src/org/antlr/v4/tool/ErrorType.java +++ b/tool/src/org/antlr/v4/tool/ErrorType.java @@ -931,6 +931,17 @@ public enum ErrorType { *

custom channels are not supported in combined grammars

*/ CHANNELS_BLOCK_IN_COMBINED_GRAMMAR(164, "custom channels are not supported in combined grammars", ErrorSeverity.ERROR), + /** + * Compiler Error 165. + * + *

charset range should have start and end

+ * + *
+	 * INVALID_RANGE_IN_CHAR_SET:    [-z] | [a-] | [-]; // error 165
+	 * 
+ * + * */ + INVALID_CHAR_SET(165, "invalid charset (range without start or end): ", ErrorSeverity.ERROR), NONCONFORMING_LR_RULE(169, "rule is left recursive but doesn't conform to a pattern ANTLR can handle", ErrorSeverity.ERROR), /**