Skip to content

Commit

Permalink
Fix weird error if rule has tree name, fixes #1960
Browse files Browse the repository at this point in the history
Remove backward compatibility exception v3TreeGrammarException
  • Loading branch information
KvanTTT committed Dec 27, 2021
1 parent 13a0add commit 7b22bc5
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -839,4 +839,11 @@ public void AllErrorCodesDistinct() {

super.testErrors(pair, true);
}

@Test public void testRuleNamesAsTree() {
String grammar = "" +
"grammar T;\n" +
"tree : 'X';";
super.testErrors(new String[] { grammar, "" }, true);
}
}
22 changes: 8 additions & 14 deletions tool/src/org/antlr/v4/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.antlr.v4.parse.GrammarTreeVisitor;
import org.antlr.v4.parse.ToolANTLRLexer;
import org.antlr.v4.parse.ToolANTLRParser;
import org.antlr.v4.parse.v3TreeGrammarException;
import org.antlr.v4.runtime.RuntimeMetaData;
import org.antlr.v4.runtime.misc.LogManager;
import org.antlr.v4.runtime.misc.IntegerList;
Expand Down Expand Up @@ -657,20 +656,15 @@ public GrammarRootAST parse(String fileName, CharStream in) {
lexer.tokens = tokens;
ToolANTLRParser p = new ToolANTLRParser(tokens, this);
p.setTreeAdaptor(adaptor);
try {
ParserRuleReturnScope r = p.grammarSpec();
GrammarAST root = (GrammarAST)r.getTree();
if ( root instanceof GrammarRootAST) {
((GrammarRootAST)root).hasErrors = lexer.getNumberOfSyntaxErrors()>0 || p.getNumberOfSyntaxErrors()>0;
assert ((GrammarRootAST)root).tokenStream == tokens;
if ( grammarOptions!=null ) {
((GrammarRootAST)root).cmdLineOptions = grammarOptions;
}
return ((GrammarRootAST)root);
ParserRuleReturnScope r = p.grammarSpec();
GrammarAST root = (GrammarAST) r.getTree();
if (root instanceof GrammarRootAST) {
((GrammarRootAST) root).hasErrors = lexer.getNumberOfSyntaxErrors() > 0 || p.getNumberOfSyntaxErrors() > 0;
assert ((GrammarRootAST) root).tokenStream == tokens;
if (grammarOptions != null) {
((GrammarRootAST) root).cmdLineOptions = grammarOptions;
}
}
catch (v3TreeGrammarException e) {
errMgr.grammarError(ErrorType.V3_TREE_GRAMMAR, fileName, e.location);
return ((GrammarRootAST) root);
}
return null;
}
Expand Down
1 change: 0 additions & 1 deletion tool/src/org/antlr/v4/parse/ANTLRLexer.g
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,6 @@ FRAGMENT : 'fragment' ;
LEXER : 'lexer' ;
PARSER : 'parser' ;
GRAMMAR : 'grammar' ;
TREE_GRAMMAR : 'tree' WSNLCHARS* 'grammar' ;
PROTECTED : 'protected' ;
PUBLIC : 'public' ;
PRIVATE : 'private' ;
Expand Down
3 changes: 0 additions & 3 deletions tool/src/org/antlr/v4/parse/ANTLRParser.g
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ if ( options!=null ) {

grammarType
@after {
if ( $tg!=null ) throw new v3TreeGrammarException(tg);
if ( $t!=null ) ((GrammarRootAST)$tree).grammarType = $t.type;
else ((GrammarRootAST)$tree).grammarType=COMBINED;
}
Expand All @@ -207,8 +206,6 @@ grammarType

// A combined lexer and parser specification
| g=GRAMMAR -> GRAMMAR<GrammarRootAST>[$g, "COMBINED_GRAMMAR", getTokenStream()]
| tg=TREE_GRAMMAR

)
;

Expand Down
18 changes: 0 additions & 18 deletions tool/src/org/antlr/v4/parse/v3TreeGrammarException.java

This file was deleted.

1 change: 1 addition & 0 deletions tool/src/org/antlr/v4/tool/ErrorType.java
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,7 @@ public enum ErrorType {
* instead offers automatically generated parse tree listeners and visitors
* as a more maintainable alternative.</p>
*/
@Deprecated
V3_TREE_GRAMMAR(200, "tree grammars are not supported in ANTLR 4", ErrorSeverity.ERROR),
/**
* Compiler Warning 201.
Expand Down

0 comments on commit 7b22bc5

Please sign in to comment.