v4: check for invalid block and rule options

fix error for hetero AST usage outside of rewrite in alts with rewrites

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9347]
This commit is contained in:
sharwell 2011-11-17 17:00:23 -08:00
parent 062210b54a
commit 32976eefdb
1 changed files with 13 additions and 2 deletions

View File

@ -30,6 +30,7 @@
package org.antlr.v4.semantics;
import org.antlr.runtime.Token;
import org.antlr.runtime.tree.Tree;
import org.antlr.v4.misc.Utils;
import org.antlr.v4.parse.*;
import org.antlr.v4.tool.*;
@ -223,6 +224,16 @@ public class BasicSemanticChecks extends GrammarTreeVisitor {
checkInvalidRuleRef(ref.token);
}
@Override
public void ruleOption(GrammarAST ID, GrammarAST valueAST) {
checkOptions(ID.getAncestor(RULE), ID.token, valueAST);
}
@Override
public void blockOption(GrammarAST ID, GrammarAST valueAST) {
checkOptions(ID.getAncestor(BLOCK), ID.token, valueAST);
}
@Override
public void grammarOption(GrammarAST ID, GrammarAST valueAST) {
boolean ok = checkOptions(g.ast, ID.token, valueAST);
@ -370,7 +381,7 @@ public class BasicSemanticChecks extends GrammarTreeVisitor {
}
/** Check option is appropriate for grammar, rule, subrule */
boolean checkOptions(GrammarAST parent,
boolean checkOptions(Tree parent,
Token optionID, GrammarAST valueAST)
{
boolean ok = true;
@ -441,7 +452,7 @@ public class BasicSemanticChecks extends GrammarTreeVisitor {
return false;
}
// example (ALT_REWRITE (ALT (ID (ELEMENT_OPTIONS Foo))) (-> (ALT ID))
if ( inRewrite ) {
if ( !inRewrite && this.currentOuterAltHasRewrite ) {
g.tool.errMgr.grammarError(ErrorType.HETERO_ILLEGAL_IN_REWRITE_ALT,
fileName,
optionID);