diff --git a/tool/src/org/antlr/v4/codegen/src/RuleFunction.java b/tool/src/org/antlr/v4/codegen/src/RuleFunction.java index 3ded4a148..0513ee53c 100644 --- a/tool/src/org/antlr/v4/codegen/src/RuleFunction.java +++ b/tool/src/org/antlr/v4/codegen/src/RuleFunction.java @@ -1,18 +1,28 @@ package org.antlr.v4.codegen.src; +import org.antlr.v4.misc.Utils; import org.antlr.v4.tool.Attribute; import org.antlr.v4.tool.GrammarAST; import org.antlr.v4.tool.Rule; import java.util.ArrayList; +import java.util.Collection; import java.util.List; /** */ public class RuleFunction { public String name; public List modifiers; - public List args; - public List retvals; + public Collection args; + public Collection retvals; + public Collection ruleScopeDecls; + public List globalScopesUsed; + public Collection ruleLabels; + public Collection tokenLabels; + public List elementsReferencedInRewrite; + public List exceptions; + public String finallyAction; + public CodeBlock code; public RuleFunction(Rule r) { @@ -21,5 +31,14 @@ public class RuleFunction { this.modifiers = new ArrayList(); for (GrammarAST t : r.modifiers) modifiers.add(t.getText()); } + modifiers = Utils.nodesToStrings(r.modifiers); + + args = r.args.attributes.values(); + retvals = r.retvals.attributes.values(); + ruleScopeDecls = r.scope.attributes.values(); + ruleLabels = r.getLabelNames(); + tokenLabels = r.getTokenRefs(); + exceptions = Utils.nodesToStrings(r.exceptionActions); + finallyAction = r.finallyAction.getText(); } } diff --git a/tool/src/org/antlr/v4/misc/Utils.java b/tool/src/org/antlr/v4/misc/Utils.java index 2d00d69a4..68c0ab32d 100644 --- a/tool/src/org/antlr/v4/misc/Utils.java +++ b/tool/src/org/antlr/v4/misc/Utils.java @@ -1,5 +1,8 @@ package org.antlr.v4.misc; +import org.antlr.v4.tool.GrammarAST; + +import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; @@ -92,4 +95,9 @@ public class Utils { return buf.toString(); } + public static List nodesToStrings(List nodes) { + List a = new ArrayList(); + for (T t : nodes) a.add(t.getText()); + return a; + } } diff --git a/tool/src/org/antlr/v4/semantics/AttributeChecks.java b/tool/src/org/antlr/v4/semantics/AttributeChecks.java index 0168901bc..a8d2c725c 100644 --- a/tool/src/org/antlr/v4/semantics/AttributeChecks.java +++ b/tool/src/org/antlr/v4/semantics/AttributeChecks.java @@ -47,8 +47,13 @@ public class AttributeChecks implements ActionSplitterListener { } for (ActionAST a : r.exceptionActions) { AttributeChecks checker = new AttributeChecks(g, r, null, a, a.token); - checker.examineAction(); - } + checker.examineAction(); + } + if ( r.finallyAction!=null ) { + AttributeChecks checker = + new AttributeChecks(g, r, null, r.finallyAction, r.finallyAction.token); + checker.examineAction(); + } } } diff --git a/tool/src/org/antlr/v4/semantics/BasicSemanticTriggers.java b/tool/src/org/antlr/v4/semantics/BasicSemanticTriggers.java index f9bfa6515..0756ff70d 100644 --- a/tool/src/org/antlr/v4/semantics/BasicSemanticTriggers.java +++ b/tool/src/org/antlr/v4/semantics/BasicSemanticTriggers.java @@ -1,4 +1,4 @@ -// $ANTLR ${project.version} ${buildNumber} BasicSemanticTriggers.g 2010-04-19 16:22:28 +// $ANTLR ${project.version} ${buildNumber} BasicSemanticTriggers.g 2010-05-06 12:54:52 /* [The "BSD license"] diff --git a/tool/src/org/antlr/v4/semantics/CollectSymbols.g b/tool/src/org/antlr/v4/semantics/CollectSymbols.g index 1b61e015d..a2aa6c186 100644 --- a/tool/src/org/antlr/v4/semantics/CollectSymbols.g +++ b/tool/src/org/antlr/v4/semantics/CollectSymbols.g @@ -202,7 +202,7 @@ exceptionHandler finallyClause : ^(FINALLY ACTION) { - currentRule.exceptionActions.add((ActionAST)$ACTION); + currentRule.finallyAction = (ActionAST)$ACTION; ((ActionAST)$ACTION).resolver = currentRule; } ; diff --git a/tool/src/org/antlr/v4/semantics/CollectSymbols.java b/tool/src/org/antlr/v4/semantics/CollectSymbols.java index f7426085e..a1f38f771 100644 --- a/tool/src/org/antlr/v4/semantics/CollectSymbols.java +++ b/tool/src/org/antlr/v4/semantics/CollectSymbols.java @@ -1,4 +1,4 @@ -// $ANTLR ${project.version} ${buildNumber} CollectSymbols.g 2010-04-28 18:42:02 +// $ANTLR ${project.version} ${buildNumber} CollectSymbols.g 2010-05-06 12:54:51 /* [The "BSD license"] @@ -1088,7 +1088,7 @@ public class CollectSymbols extends org.antlr.v4.runtime.tree.TreeFilter { match(input, Token.UP, null); if (state.failed) return ; if ( state.backtracking==1 ) { - currentRule.exceptionActions.add((ActionAST)ACTION13); + currentRule.finallyAction = (ActionAST)ACTION13; ((ActionAST)ACTION13).resolver = currentRule; } diff --git a/tool/src/org/antlr/v4/tool/Rule.java b/tool/src/org/antlr/v4/tool/Rule.java index 07eff0c6e..c299a4bb7 100644 --- a/tool/src/org/antlr/v4/tool/Rule.java +++ b/tool/src/org/antlr/v4/tool/Rule.java @@ -63,9 +63,13 @@ public class Rule implements AttributeResolver { public Map namedActions = new HashMap(); - /** Track exception handlers, finally action */ + /** Track exception handler actions (exception type is prev child); + * don't track finally action + */ public List exceptionActions = new ArrayList(); + public ActionAST finallyAction; + public int numberOfAlts; /** Labels are visible to all alts in a rule. Record all defs here. @@ -118,12 +122,19 @@ public class Rule implements AttributeResolver { return d.get(y); } + // TODO: move to code gen InvokeRule function? is only place we ref? public Set getRuleRefs() { Set refs = new HashSet(); for (Alternative a : alt) refs.addAll(a.ruleRefs.keySet()); return refs; } + public Set getTokenRefs() { + Set refs = new HashSet(); + for (Alternative a : alt) refs.addAll(a.tokenRefs.keySet()); + return refs; + } + public Set getLabelNames() { Set refs = new HashSet(); for (int i=1; i<=numberOfAlts; i++) {