```
beast:/tmp $ a4.6 T.g4
org/antlr/v4/parse/GrammarTreeVisitor.g: node from line 2:7 no viable alternative at input '..'
org/antlr/v4/parse/GrammarTreeVisitor.g: node from line 2:7 no viable alternative at input '..'
org/antlr/v4/parse/GrammarTreeVisitor.g: node from line 2:7 no viable alternative at input '..'
org/antlr/v4/parse/GrammarTreeVisitor.g: node from line 2:7 no viable alternative at input '..'
org/antlr/v4/parse/GrammarTreeVisitor.g: node from line 2:7 no viable alternative at input '..'
context [/report INTERNAL_ERROR] 1:17 attribute arg isn't defined
error(20):  internal error:
beast:/tmp $ a4.6.1 T.g4
error(181): T.g4:2:4: token ranges not allowed in parser: 'A'..'Z'
```
This commit is contained in:
parrt 2016-12-28 11:47:48 -08:00
parent b57843d983
commit 36ee17449f
4 changed files with 38 additions and 2 deletions

View File

@ -729,4 +729,20 @@ public class TestToolSyntaxErrors extends BaseJavaToolTest {
String[] pair = { grammar, expected };
super.testErrors(pair, true);
}
// Test for https://github.com/antlr/antlr4/issues/1556
@Test public void testRangeInParserGrammar() {
String grammar =
"grammar T;\n"+
"a: 'A'..'Z' ;\n";
String expected =
"error(" + ErrorType.TOKEN_RANGE_IN_PARSER.code + "): T.g4:2:4: token ranges not allowed in parser: 'A'..'Z'\n";
String[] pair = new String[] {
grammar,
expected
};
super.testErrors(pair, true);
}
}

View File

@ -233,7 +233,12 @@ public class ParserATNFactory implements ATNFactory {
@Override
public Handle range(GrammarAST a, GrammarAST b) {
throw new UnsupportedOperationException("This construct is not valid in parsers.");
g.tool.errMgr.grammarError(ErrorType.TOKEN_RANGE_IN_PARSER, g.fileName,
a.getToken(),
a.getToken().getText(),
b.getToken().getText());
// From a..b, yield ATN for just a.
return tokenRef((TerminalAST)a);
}
protected int getTokenType(GrammarAST atom) {

View File

@ -844,7 +844,7 @@ element
| SEMPRED {sempredInAlt((PredAST)$SEMPRED);}
| ^(ACTION elementOptions) {actionInAlt((ActionAST)$ACTION);}
| ^(SEMPRED elementOptions) {sempredInAlt((PredAST)$SEMPRED);}
| range
| ^(NOT blockSet)
| ^(NOT block)
;

View File

@ -1045,6 +1045,21 @@ public enum ErrorType {
*/
CHARACTERS_COLLISION_IN_SET(180, "chars \"<arg>\" used multiple times in set <arg2>", ErrorSeverity.WARNING),
/**
* Compiler Warning 181
*
* <p>The token range operator makes no sense in the parser as token types
* are not ordered (except in implementation).
* </p>
*
* <pre>
* grammar T;
* a : 'A'..'Z' ;
* </pre>
*
*/
TOKEN_RANGE_IN_PARSER(181, "token ranges not allowed in parser: <arg>..<arg2>", ErrorSeverity.ERROR),
/*
* Backward incompatibility errors
*/