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

View File

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

View File

@ -375,4 +375,16 @@ public abstract class Target {
return result; 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; import java.util.List;
/** */ /** */
@SuppressWarnings("unused")
public class InvokeRule extends RuleElement implements LabeledOp { public class InvokeRule extends RuleElement implements LabeledOp {
public String name; public String name;
public OrderedHashSet<Decl> labels = new OrderedHashSet<Decl>(); // TODO: should need just 1 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; import java.util.List;
/** */ /** */
@SuppressWarnings("unused")
public class LL1StarBlockSingleAlt extends LL1Loop { public class LL1StarBlockSingleAlt extends LL1Loop {
public LL1StarBlockSingleAlt(OutputModelFactory factory, GrammarAST starRoot, List<CodeBlockForAlt> alts) { public LL1StarBlockSingleAlt(OutputModelFactory factory, GrammarAST starRoot, List<CodeBlockForAlt> alts) {
super(factory, starRoot, alts); super(factory, starRoot, alts);

View File

@ -41,6 +41,9 @@ public class LexerFile extends OutputFile {
public String genPackage; // from -package cmd-line public String genPackage; // from -package cmd-line
@ModelElement public Lexer lexer; @ModelElement public Lexer lexer;
@ModelElement public Map<String, Action> namedActions; @ModelElement public Map<String, Action> namedActions;
public Boolean genListener = false;
public Boolean genVisitor = false;
public String grammarName;
public LexerFile(OutputModelFactory factory, String fileName) { public LexerFile(OutputModelFactory factory, String fileName) {
super(factory, fileName); super(factory, fileName);
@ -51,5 +54,9 @@ public class LexerFile extends OutputFile {
namedActions.put(name, new Action(factory, ast)); namedActions.put(name, new Action(factory, ast));
} }
genPackage = factory.getGrammar().tool.genPackage; 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 grammarFileName;
public String grammarName; public String grammarName;
@ModelElement public ActionChunk superClass; @ModelElement public ActionChunk superClass;
public Boolean needsSuperClass = false;
public Map<String,Integer> tokens; public Map<String,Integer> tokens;
public String[] tokenNames; public String[] tokenNames;
public Set<String> ruleNames; public Set<String> ruleNames;
public Collection<Rule> rules; public Collection<Rule> rules;
public ParserFile file; public ParserFile file;
@ModelElement public List<RuleFunction> funcs = new ArrayList<RuleFunction>(); @ModelElement public List<RuleFunction> funcs = new ArrayList<RuleFunction>();
@ModelElement public SerializedATN atn; @ModelElement public SerializedATN atn;
@ModelElement public LinkedHashMap<Rule, RuleSempredFunction> sempredFuncs = @ModelElement public LinkedHashMap<Rule, RuleSempredFunction> sempredFuncs =
@ -96,6 +97,7 @@ public class Parser extends OutputModelObject {
atn = new SerializedATN(factory, g.atn); atn = new SerializedATN(factory, g.atn);
if (g.getOptionString("superClass") != null) { if (g.getOptionString("superClass") != null) {
superClass = new ActionText(null, g.getOptionString("superClass")); superClass = new ActionText(null, g.getOptionString("superClass"));
needsSuperClass = true;
} }
else { else {
superClass = new DefaultParserSuperClass(); superClass = new DefaultParserSuperClass();

View File

@ -42,7 +42,10 @@ public class ParserFile extends OutputFile {
public String genPackage; // from -package cmd-line public String genPackage; // from -package cmd-line
@ModelElement public Parser parser; @ModelElement public Parser parser;
@ModelElement public Map<String, Action> namedActions; @ModelElement public Map<String, Action> namedActions;
public Boolean genListener = false;
public Boolean genVisitor = false;
public String grammarName;
public ParserFile(OutputModelFactory factory, String fileName) { public ParserFile(OutputModelFactory factory, String fileName) {
super(factory, fileName); super(factory, fileName);
Grammar g = factory.getGrammar(); Grammar g = factory.getGrammar();
@ -51,6 +54,10 @@ public class ParserFile extends OutputFile {
ActionAST ast = g.namedActions.get(name); ActionAST ast = g.namedActions.get(name);
namedActions.put(name, new Action(factory, ast)); 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()); Rule rref = factory.getGrammar().getRule(t.getText());
String ctxName = factory.getGenerator().getTarget() String ctxName = factory.getGenerator().getTarget()
.getRuleFunctionContextStructName(rref); .getRuleFunctionContextStructName(rref);
if ( needList ) { if ( needList) {
decls.add( new ContextRuleListGetterDecl(factory, refLabelName, ctxName) ); if(factory.getGenerator().getTarget().supportsOverloadedMethods())
decls.add( new ContextRuleListGetterDecl(factory, refLabelName, ctxName) );
decls.add( new ContextRuleListIndexedGetterDecl(factory, refLabelName, ctxName) ); decls.add( new ContextRuleListIndexedGetterDecl(factory, refLabelName, ctxName) );
} }
else { else {
@ -257,7 +258,8 @@ public class RuleFunction extends OutputModelObject {
} }
else { else {
if ( needList ) { 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) ); decls.add( new ContextTokenListIndexedGetterDecl(factory, refLabelName) );
} }
else { else {