Added new error types for not recognized token, mode or channel name.

This commit is contained in:
Ivan Kochurkin 2016-12-04 03:47:28 +03:00
parent 9aab338021
commit 4e2f50aef8
4 changed files with 44 additions and 14 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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;
}
}

View File

@ -1011,6 +1011,30 @@ public enum ErrorType {
* <pre>C: 'test' '';</pre>
*/
EMPTY_STRINGS_NOT_ALLOWED(174, "string literals cannot be empty", ErrorSeverity.ERROR),
/**
* Compiler Error 175.
*
* <p><em>name</em> is not a recognized token name</p>
*
* <pre>TOKEN: 'a' -> type(CHANNEL1); // error 175</pre>
*/
CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_TOKEN_NAME(175, "<arg> is not a recognized token name", ErrorSeverity.ERROR),
/**
* Compiler Error 176.
*
* <p><em>name</em>is not a recognized mode name</p>
*
* <pre>TOKEN: 'a' -> channel(MODE1); // error 176</pre>
*/
CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_MODE_NAME(176, "<arg> is not a recognized mode name", ErrorSeverity.ERROR),
/**
* Compiler Error 177.
*
* <p><em>name</em> is not a recognized channel name</p>
*
* <pre>TOKEN: 'a' -> mode(TOKEN1); // error 177</pre>
*/
CONSTANT_VALUE_IS_NOT_A_RECOGNIZED_CHANNEL_NAME(177, "<arg> is not a recognized channel name", ErrorSeverity.ERROR),
/*
* Backward incompatibility errors