Preliminary changes for Python

This commit is contained in:
ericvergnaud 2014-06-06 03:13:09 +08:00
parent bc566605b1
commit b8a91b9e3f
9 changed files with 45 additions and 8 deletions

View File

@ -175,6 +175,7 @@ public class ActionTranslator implements ActionSplitterListener {
case RET: chunks.add(new RetValueRef(rf.ruleCtx, x.getText())); break;
case LOCAL: chunks.add(new LocalRef(nodeContext,x.getText())); break;
case PREDEFINED_RULE: chunks.add(getRulePropertyRef(x)); break;
default: // avoid warning
}
}
if ( node.resolver.resolvesToToken(x.getText(), node) ) {
@ -229,6 +230,8 @@ public class ActionTranslator implements ActionSplitterListener {
case TOKEN:
chunks.add(getTokenPropertyRef(x, y));
break;
default:
// avoid warning
}
}

View File

@ -74,11 +74,13 @@ public class CodeGenPipeline {
writeRecognizer(parser, gen);
if ( g.tool.gen_listener ) {
gen.writeListener(gen.generateListener());
gen.writeBaseListener(gen.generateBaseListener());
if (gen.getTarget().wantsBaseListener())
gen.writeBaseListener(gen.generateBaseListener());
}
if ( g.tool.gen_visitor ) {
gen.writeVisitor(gen.generateVisitor());
gen.writeBaseVisitor(gen.generateBaseVisitor());
if (gen.getTarget().wantsBaseVisitor())
gen.writeBaseVisitor(gen.generateBaseVisitor());
}
gen.writeHeaderFile();
}

View File

@ -375,4 +375,16 @@ public abstract class Target {
return result;
}
public boolean wantsBaseListener() {
return true;
}
public boolean wantsBaseVisitor() {
return true;
}
public boolean supportsOverloadedMethods() {
return true;
}
}

View File

@ -47,6 +47,7 @@ import org.antlr.v4.tool.ast.GrammarAST;
import java.util.List;
/** */
@SuppressWarnings("unused")
public class InvokeRule extends RuleElement implements LabeledOp {
public String name;
public OrderedHashSet<Decl> labels = new OrderedHashSet<Decl>(); // TODO: should need just 1

View File

@ -38,6 +38,7 @@ import org.antlr.v4.tool.ast.GrammarAST;
import java.util.List;
/** */
@SuppressWarnings("unused")
public class LL1StarBlockSingleAlt extends LL1Loop {
public LL1StarBlockSingleAlt(OutputModelFactory factory, GrammarAST starRoot, List<CodeBlockForAlt> alts) {
super(factory, starRoot, alts);

View File

@ -41,6 +41,9 @@ public class LexerFile extends OutputFile {
public String genPackage; // from -package cmd-line
@ModelElement public Lexer lexer;
@ModelElement public Map<String, Action> namedActions;
public Boolean genListener = false;
public Boolean genVisitor = false;
public String grammarName;
public LexerFile(OutputModelFactory factory, String fileName) {
super(factory, fileName);
@ -51,5 +54,9 @@ public class LexerFile extends OutputFile {
namedActions.put(name, new Action(factory, ast));
}
genPackage = factory.getGrammar().tool.genPackage;
// need the below members in the ST for Python
genListener = g.tool.gen_listener;
genVisitor = g.tool.gen_visitor;
grammarName = g.originalGrammar==null ? g.name : g.originalGrammar.name;
}
}

View File

@ -52,12 +52,13 @@ public class Parser extends OutputModelObject {
public String grammarFileName;
public String grammarName;
@ModelElement public ActionChunk superClass;
public Boolean needsSuperClass = false;
public Map<String,Integer> tokens;
public String[] tokenNames;
public Set<String> ruleNames;
public Collection<Rule> rules;
public ParserFile file;
@ModelElement public List<RuleFunction> funcs = new ArrayList<RuleFunction>();
@ModelElement public SerializedATN atn;
@ModelElement public LinkedHashMap<Rule, RuleSempredFunction> sempredFuncs =
@ -96,6 +97,7 @@ public class Parser extends OutputModelObject {
atn = new SerializedATN(factory, g.atn);
if (g.getOptionString("superClass") != null) {
superClass = new ActionText(null, g.getOptionString("superClass"));
needsSuperClass = true;
}
else {
superClass = new DefaultParserSuperClass();

View File

@ -42,7 +42,10 @@ public class ParserFile extends OutputFile {
public String genPackage; // from -package cmd-line
@ModelElement public Parser parser;
@ModelElement public Map<String, Action> namedActions;
public Boolean genListener = false;
public Boolean genVisitor = false;
public String grammarName;
public ParserFile(OutputModelFactory factory, String fileName) {
super(factory, fileName);
Grammar g = factory.getGrammar();
@ -51,6 +54,10 @@ public class ParserFile extends OutputFile {
ActionAST ast = g.namedActions.get(name);
namedActions.put(name, new Action(factory, ast));
}
genPackage = factory.getGrammar().tool.genPackage;
genPackage = g.tool.genPackage;
// need the below members in the ST for Python
genListener = g.tool.gen_listener;
genVisitor = g.tool.gen_visitor;
grammarName = g.name;
}
}

View File

@ -247,8 +247,9 @@ public class RuleFunction extends OutputModelObject {
Rule rref = factory.getGrammar().getRule(t.getText());
String ctxName = factory.getGenerator().getTarget()
.getRuleFunctionContextStructName(rref);
if ( needList ) {
decls.add( new ContextRuleListGetterDecl(factory, refLabelName, ctxName) );
if ( needList) {
if(factory.getGenerator().getTarget().supportsOverloadedMethods())
decls.add( new ContextRuleListGetterDecl(factory, refLabelName, ctxName) );
decls.add( new ContextRuleListIndexedGetterDecl(factory, refLabelName, ctxName) );
}
else {
@ -257,7 +258,8 @@ public class RuleFunction extends OutputModelObject {
}
else {
if ( needList ) {
decls.add( new ContextTokenListGetterDecl(factory, refLabelName) );
if(factory.getGenerator().getTarget().supportsOverloadedMethods())
decls.add( new ContextTokenListGetterDecl(factory, refLabelName) );
decls.add( new ContextTokenListIndexedGetterDecl(factory, refLabelName) );
}
else {