track finally action separately now

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 6843]
This commit is contained in:
parrt 2010-05-06 11:55:49 -08:00
parent 9913972caa
commit fbaeb50e5e
7 changed files with 52 additions and 9 deletions

View File

@ -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<String> modifiers;
public List<Attribute> args;
public List<Attribute> retvals;
public Collection<Attribute> args;
public Collection<Attribute> retvals;
public Collection<Attribute> ruleScopeDecls;
public List<String> globalScopesUsed;
public Collection<String> ruleLabels;
public Collection<String> tokenLabels;
public List<String> elementsReferencedInRewrite;
public List<String> exceptions;
public String finallyAction;
public CodeBlock code;
public RuleFunction(Rule r) {
@ -21,5 +31,14 @@ public class RuleFunction {
this.modifiers = new ArrayList<String>();
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();
}
}

View File

@ -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 <T extends GrammarAST> List<String> nodesToStrings(List<T> nodes) {
List<String> a = new ArrayList<String>();
for (T t : nodes) a.add(t.getText());
return a;
}
}

View File

@ -49,6 +49,11 @@ public class AttributeChecks implements ActionSplitterListener {
AttributeChecks checker = new AttributeChecks(g, r, null, a, a.token);
checker.examineAction();
}
if ( r.finallyAction!=null ) {
AttributeChecks checker =
new AttributeChecks(g, r, null, r.finallyAction, r.finallyAction.token);
checker.examineAction();
}
}
}

View File

@ -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"]

View File

@ -202,7 +202,7 @@ exceptionHandler
finallyClause
: ^(FINALLY ACTION)
{
currentRule.exceptionActions.add((ActionAST)$ACTION);
currentRule.finallyAction = (ActionAST)$ACTION;
((ActionAST)$ACTION).resolver = currentRule;
}
;

View File

@ -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;
}

View File

@ -63,9 +63,13 @@ public class Rule implements AttributeResolver {
public Map<String, ActionAST> namedActions =
new HashMap<String, ActionAST>();
/** Track exception handlers, finally action */
/** Track exception handler actions (exception type is prev child);
* don't track finally action
*/
public List<ActionAST> exceptionActions = new ArrayList<ActionAST>();
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<String> getRuleRefs() {
Set<String> refs = new HashSet<String>();
for (Alternative a : alt) refs.addAll(a.ruleRefs.keySet());
return refs;
}
public Set<String> getTokenRefs() {
Set<String> refs = new HashSet<String>();
for (Alternative a : alt) refs.addAll(a.tokenRefs.keySet());
return refs;
}
public Set<String> getLabelNames() {
Set<String> refs = new HashSet<String>();
for (int i=1; i<=numberOfAlts; i++) {