diff --git a/tool-testsuite/test/org/antlr/v4/test/tool/TestSymbolIssues.java b/tool-testsuite/test/org/antlr/v4/test/tool/TestSymbolIssues.java index e591a986b..f367459e2 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/TestSymbolIssues.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/TestSymbolIssues.java @@ -240,9 +240,9 @@ public class TestSymbolIssues extends BaseJavaToolTest { "mode MODE1;\n" + "MODE1_TOKEN: 'qwer';", - "warning(" + ErrorType.UNKNOWN_LEXER_CONSTANT.code + "): L.g4:4:22: rule TOKEN contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output\n" + - "warning(" + ErrorType.UNKNOWN_LEXER_CONSTANT.code + "): L.g4:4:41: rule TOKEN contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output\n" + - "warning(" + ErrorType.UNKNOWN_LEXER_CONSTANT.code + "): L.g4:4:54: rule TOKEN contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output\n" + "error(" + ErrorType.CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_TOKEN_NAME.code + "): L.g4:4:22: CHANNEL1 is not a recognized token name\n" + + "error(" + ErrorType.CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_CHANNEL_NAME.code + "): L.g4:4:41: MODE1 is not a recognized channel name\n" + + "error(" + ErrorType.CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_MODE_NAME.code + "): L.g4:4:54: TOKEN1 is not a recognized mode name\n" }; testErrors(test, false); 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 c22d170ac..220d748e1 100644 --- a/tool-testsuite/test/org/antlr/v4/test/tool/TestToolSyntaxErrors.java +++ b/tool-testsuite/test/org/antlr/v4/test/tool/TestToolSyntaxErrors.java @@ -657,8 +657,8 @@ public class TestToolSyntaxErrors extends BaseJavaToolTest { "WHITESPACE: [ \\t]+ -> channel(WHITESPACE_CHANNEL);\n"; String expected = - "warning(" + ErrorType.UNKNOWN_LEXER_CONSTANT.code + "): T.g4:10:35: rule COMMENT contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output\n" + - "warning(" + ErrorType.UNKNOWN_LEXER_CONSTANT.code + "): T.g4:11:35: rule WHITESPACE contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output\n" + + "error(" + ErrorType.CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_CHANNEL_NAME.code + "): T.g4:10:35: COMMENT_CHANNEL is not a recognized channel name\n" + + "error(" + ErrorType.CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_CHANNEL_NAME.code + "): T.g4:11:35: WHITESPACE_CHANNEL is not a recognized channel name\n" + "error(" + ErrorType.CHANNELS_BLOCK_IN_COMBINED_GRAMMAR.code + "): T.g4:3:0: custom channels are not supported in combined grammars\n"; String[] pair = { grammar, expected }; @@ -686,7 +686,7 @@ public class TestToolSyntaxErrors extends BaseJavaToolTest { // WHITESPACE_CHANNEL and COMMENT_CHANNEL are defined, but NEWLINE_CHANNEL is not String expected = - "warning(" + ErrorType.UNKNOWN_LEXER_CONSTANT.code + "): T.g4:10:34: rule NEWLINE contains a lexer command with an unrecognized constant value; lexer interpreters may produce incorrect output\n"; + "error(" + ErrorType.CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_CHANNEL_NAME.code + "): T.g4:10:34: NEWLINE_CHANNEL is not a recognized channel name\n"; String[] pair = { grammar, expected }; super.testErrors(pair, true); diff --git a/tool/src/org/antlr/v4/automata/LexerATNFactory.java b/tool/src/org/antlr/v4/automata/LexerATNFactory.java index 979d8d3ab..e1ce57436 100644 --- a/tool/src/org/antlr/v4/automata/LexerATNFactory.java +++ b/tool/src/org/antlr/v4/automata/LexerATNFactory.java @@ -470,7 +470,12 @@ public class LexerATNFactory extends ParserATNFactory { return null; } - return tryParseInt(modeName, token); + try { + return Integer.parseInt(modeName); + } catch (NumberFormatException ex) { + g.tool.errMgr.grammarError(ErrorType.CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_MODE_NAME, g.fileName, token, token.getText()); + return null; + } } private Integer getTokenConstantValue(String tokenName, Token token) { @@ -491,7 +496,12 @@ public class LexerATNFactory extends ParserATNFactory { return null; } - return tryParseInt(tokenName, token); + try { + return Integer.parseInt(tokenName); + } catch (NumberFormatException ex) { + g.tool.errMgr.grammarError(ErrorType.CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_TOKEN_NAME, g.fileName, token, token.getText()); + return null; + } } private Integer getChannelConstantValue(String channelName, Token token) { @@ -515,14 +525,10 @@ public class LexerATNFactory extends ParserATNFactory { return channelValue; } - return tryParseInt(channelName, token); - } - - private Integer tryParseInt(String name, Token token) { try { - return Integer.parseInt(name); + return Integer.parseInt(channelName); } catch (NumberFormatException ex) { - g.tool.errMgr.grammarError(ErrorType.UNKNOWN_LEXER_CONSTANT, g.fileName, token, currentRule.name, token != null ? token.getText() : null); + g.tool.errMgr.grammarError(ErrorType.CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_CHANNEL_NAME, g.fileName, token, token.getText()); return null; } } diff --git a/tool/src/org/antlr/v4/tool/ErrorType.java b/tool/src/org/antlr/v4/tool/ErrorType.java index 28f78f90f..c5db57763 100644 --- a/tool/src/org/antlr/v4/tool/ErrorType.java +++ b/tool/src/org/antlr/v4/tool/ErrorType.java @@ -1011,6 +1011,30 @@ public enum ErrorType { *
C: 'test' '';
*/ EMPTY_STRINGS_NOT_ALLOWED(174, "string literals cannot be empty", ErrorSeverity.ERROR), + /** + * Compiler Error 175. + * + *

name is not a recognized token name

+ * + *
TOKEN: 'a' -> type(CHANNEL1); // error 175
+ */ + CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_TOKEN_NAME(175, " is not a recognized token name", ErrorSeverity.ERROR), + /** + * Compiler Error 176. + * + *

nameis not a recognized mode name

+ * + *
TOKEN: 'a' -> channel(MODE1); // error 176
+ */ + CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_MODE_NAME(176, " is not a recognized mode name", ErrorSeverity.ERROR), + /** + * Compiler Error 177. + * + *

name is not a recognized channel name

+ * + *
TOKEN: 'a' -> mode(TOKEN1); // error 177
+ */ + CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_CHANNEL_NAME(177, " is not a recognized channel name", ErrorSeverity.ERROR), /* * Backward incompatibility errors