From 32976eefdb8c5ae0b71fc8842831ae99b5881bda Mon Sep 17 00:00:00 2001 From: sharwell Date: Thu, 17 Nov 2011 17:00:23 -0800 Subject: [PATCH] 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] --- .../antlr/v4/semantics/BasicSemanticChecks.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java b/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java index 558c0d3cb..36f591787 100644 --- a/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java +++ b/tool/src/org/antlr/v4/semantics/BasicSemanticChecks.java @@ -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);