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