reuse code to builld named actions. all file model objects have it now.

This commit is contained in:
parrt 2016-11-07 11:07:23 -08:00
parent 51d38110be
commit 3d0678508b
6 changed files with 17 additions and 34 deletions

View File

@ -71,9 +71,7 @@ import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
/** This receives events from SourceGenTriggers.g and asks factory to do work.
@ -363,15 +361,6 @@ public class OutputModelController {
return ops;
}
public Map<String, Action> buildNamedActions(Grammar g) {
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(delegate, ast));
}
return namedActions;
}
public Grammar getGrammar() { return delegate.getGrammar(); }
public CodeGenerator getGenerator() { return delegate.getGenerator(); }

View File

@ -31,10 +31,7 @@
package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.ast.ActionAST;
import java.util.HashMap;
import java.util.Map;
public class LexerFile extends OutputFile {
@ -46,12 +43,7 @@ public class LexerFile extends OutputFile {
public LexerFile(OutputModelFactory factory, String fileName) {
super(factory, fileName);
namedActions = new HashMap<String, Action>();
Grammar g = factory.getGrammar();
for (String name : g.namedActions.keySet()) {
ActionAST ast = g.namedActions.get(name);
namedActions.put(name, new Action(factory, ast));
}
namedActions = buildNamedActions(factory.getGrammar());
genPackage = factory.getGrammar().tool.genPackage;
genListener = factory.getGrammar().tool.gen_listener;
genVisitor = factory.getGrammar().tool.gen_visitor;

View File

@ -36,7 +36,6 @@ import org.antlr.v4.tool.Rule;
import org.antlr.v4.tool.ast.ActionAST;
import org.antlr.v4.tool.ast.AltAST;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@ -69,11 +68,7 @@ public class ListenerFile extends OutputFile {
Grammar g = factory.getGrammar();
parserName = g.getRecognizerName();
grammarName = g.name;
namedActions = new HashMap<String, Action>();
for (String name : g.namedActions.keySet()) {
ActionAST ast = g.namedActions.get(name);
namedActions.put(name, new Action(factory, ast));
}
namedActions = buildNamedActions(factory.getGrammar());
for (Rule r : g.rules.values()) {
Map<String, List<Pair<Integer,AltAST>>> labels = r.getAltLabels();
if ( labels!=null ) {

View File

@ -33,6 +33,10 @@ package org.antlr.v4.codegen.model;
import org.antlr.v4.Tool;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.ast.ActionAST;
import java.util.HashMap;
import java.util.Map;
public abstract class OutputFile extends OutputModelObject {
public final String fileName;
@ -50,4 +54,13 @@ public abstract class OutputFile extends OutputModelObject {
TokenLabelType = g.getOptionString("TokenLabelType");
InputSymbolType = TokenLabelType;
}
public Map<String, Action> buildNamedActions(Grammar g) {
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));
}
return namedActions;
}
}

View File

@ -30,7 +30,6 @@
package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.OutputModelController;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.codegen.model.chunk.ActionChunk;
import org.antlr.v4.codegen.model.chunk.ActionText;
@ -51,7 +50,7 @@ public class ParserFile extends OutputFile {
public ParserFile(OutputModelFactory factory, String fileName) {
super(factory, fileName);
Grammar g = factory.getGrammar();
namedActions = factory.getController().buildNamedActions(g);
namedActions = buildNamedActions(factory.getGrammar());
genPackage = g.tool.genPackage;
// need the below members in the ST for Python, C++
genListener = g.tool.gen_listener;

View File

@ -36,7 +36,6 @@ import org.antlr.v4.tool.Rule;
import org.antlr.v4.tool.ast.ActionAST;
import org.antlr.v4.tool.ast.AltAST;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@ -64,11 +63,7 @@ public class VisitorFile extends OutputFile {
public VisitorFile(OutputModelFactory factory, String fileName) {
super(factory, fileName);
Grammar g = factory.getGrammar();
namedActions = new HashMap<String, Action>();
for (String name : g.namedActions.keySet()) {
ActionAST ast = g.namedActions.get(name);
namedActions.put(name, new Action(factory, ast));
}
namedActions = buildNamedActions(g);
parserName = g.getRecognizerName();
grammarName = g.name;
for (Rule r : g.rules.values()) {