Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tool/src/org/antlr/v4/codegen/model/ListenerFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ListenerFile(OutputModelFactory factory, String fileName) {
Grammar g = factory.getGrammar();
parserName = g.getRecognizerName();
grammarName = g.name;
namedActions = buildNamedActions(factory.getGrammar());
namedActions = buildNamedActions(factory.getGrammar(), ast -> ast.getScope() == null);
for (Rule r : g.rules.values()) {
Map<String, List<Pair<Integer,AltAST>>> labels = r.getAltLabels();
if ( labels!=null ) {
Expand Down
8 changes: 7 additions & 1 deletion tool/src/org/antlr/v4/codegen/model/OutputFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;

public abstract class OutputFile extends OutputModelObject {
public final String fileName;
Expand All @@ -32,10 +33,15 @@ public OutputFile(OutputModelFactory factory, String fileName) {
}

public Map<String, Action> buildNamedActions(Grammar g) {
return buildNamedActions(g, null);
}

public Map<String, Action> buildNamedActions(Grammar g, Predicate<ActionAST> filter) {
Map<String, Action> namedActions = new HashMap<String, Action>();
for (String name : g.namedActions.keySet()) {
ActionAST ast = g.namedActions.get(name);
namedActions.put(name, new Action(factory, ast));
if(filter==null || filter.test(ast))
namedActions.put(name, new Action(factory, ast));
}
return namedActions;
}
Expand Down
3 changes: 2 additions & 1 deletion tool/src/org/antlr/v4/codegen/model/VisitorFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public VisitorFile(OutputModelFactory factory, String fileName) {
}
}
ActionAST ast = g.namedActions.get("header");
if ( ast!=null ) header = new Action(factory, ast);
if ( ast!=null && ast.getScope()==null)
header = new Action(factory, ast);
genPackage = g.tool.genPackage;
accessLevel = g.getOptionString("accessLevel");
exportMacro = g.getOptionString("exportMacro");
Expand Down
1 change: 1 addition & 0 deletions tool/src/org/antlr/v4/semantics/SymbolCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public SymbolCollector(Grammar g) {

@Override
public void globalNamedAction(GrammarAST scope, GrammarAST ID, ActionAST action) {
action.setScope(scope);
namedActions.add((GrammarAST)ID.getParent());
action.resolver = g;
}
Expand Down
13 changes: 12 additions & 1 deletion tool/src/org/antlr/v4/tool/ast/ActionAST.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

public class ActionAST extends GrammarASTWithOptions implements RuleElementAST {
// Alt, rule, grammar space
public AttributeResolver resolver;
GrammarAST scope = null;
public AttributeResolver resolver;
public List<Token> chunks; // useful for ANTLR IDE developers

public ActionAST(ActionAST node) {
Expand All @@ -31,4 +32,14 @@ public ActionAST(ActionAST node) {

@Override
public Object visit(GrammarASTVisitor v) { return v.visit(this); }

public void setScope(GrammarAST scope) {
this.scope = scope;
}

public GrammarAST getScope() {
return scope;
}


}