From b8a91b9e3f5078a4749997d81a2a9288a132b0ba Mon Sep 17 00:00:00 2001 From: ericvergnaud Date: Fri, 6 Jun 2014 03:13:09 +0800 Subject: [PATCH] Preliminary changes for Python --- tool/src/org/antlr/v4/codegen/ActionTranslator.java | 3 +++ tool/src/org/antlr/v4/codegen/CodeGenPipeline.java | 6 ++++-- tool/src/org/antlr/v4/codegen/Target.java | 12 ++++++++++++ tool/src/org/antlr/v4/codegen/model/InvokeRule.java | 1 + .../v4/codegen/model/LL1StarBlockSingleAlt.java | 1 + tool/src/org/antlr/v4/codegen/model/LexerFile.java | 7 +++++++ tool/src/org/antlr/v4/codegen/model/Parser.java | 4 +++- tool/src/org/antlr/v4/codegen/model/ParserFile.java | 11 +++++++++-- .../src/org/antlr/v4/codegen/model/RuleFunction.java | 8 +++++--- 9 files changed, 45 insertions(+), 8 deletions(-) diff --git a/tool/src/org/antlr/v4/codegen/ActionTranslator.java b/tool/src/org/antlr/v4/codegen/ActionTranslator.java index 6647bc40b..ef4731353 100644 --- a/tool/src/org/antlr/v4/codegen/ActionTranslator.java +++ b/tool/src/org/antlr/v4/codegen/ActionTranslator.java @@ -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 } } diff --git a/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java b/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java index d54e7fb9a..f0b97984b 100644 --- a/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java +++ b/tool/src/org/antlr/v4/codegen/CodeGenPipeline.java @@ -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(); } diff --git a/tool/src/org/antlr/v4/codegen/Target.java b/tool/src/org/antlr/v4/codegen/Target.java index 69af70e8b..ae1930c75 100644 --- a/tool/src/org/antlr/v4/codegen/Target.java +++ b/tool/src/org/antlr/v4/codegen/Target.java @@ -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; + } } diff --git a/tool/src/org/antlr/v4/codegen/model/InvokeRule.java b/tool/src/org/antlr/v4/codegen/model/InvokeRule.java index 70c9f2975..a89287701 100644 --- a/tool/src/org/antlr/v4/codegen/model/InvokeRule.java +++ b/tool/src/org/antlr/v4/codegen/model/InvokeRule.java @@ -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 labels = new OrderedHashSet(); // TODO: should need just 1 diff --git a/tool/src/org/antlr/v4/codegen/model/LL1StarBlockSingleAlt.java b/tool/src/org/antlr/v4/codegen/model/LL1StarBlockSingleAlt.java index 4c1bf948d..a97f99782 100644 --- a/tool/src/org/antlr/v4/codegen/model/LL1StarBlockSingleAlt.java +++ b/tool/src/org/antlr/v4/codegen/model/LL1StarBlockSingleAlt.java @@ -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 alts) { super(factory, starRoot, alts); diff --git a/tool/src/org/antlr/v4/codegen/model/LexerFile.java b/tool/src/org/antlr/v4/codegen/model/LexerFile.java index 8bec073f4..815b6ec01 100644 --- a/tool/src/org/antlr/v4/codegen/model/LexerFile.java +++ b/tool/src/org/antlr/v4/codegen/model/LexerFile.java @@ -41,6 +41,9 @@ public class LexerFile extends OutputFile { public String genPackage; // from -package cmd-line @ModelElement public Lexer lexer; @ModelElement public Map 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; } } diff --git a/tool/src/org/antlr/v4/codegen/model/Parser.java b/tool/src/org/antlr/v4/codegen/model/Parser.java index 839ceaca5..16992b3da 100644 --- a/tool/src/org/antlr/v4/codegen/model/Parser.java +++ b/tool/src/org/antlr/v4/codegen/model/Parser.java @@ -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 tokens; public String[] tokenNames; public Set ruleNames; public Collection rules; public ParserFile file; - + @ModelElement public List funcs = new ArrayList(); @ModelElement public SerializedATN atn; @ModelElement public LinkedHashMap 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(); diff --git a/tool/src/org/antlr/v4/codegen/model/ParserFile.java b/tool/src/org/antlr/v4/codegen/model/ParserFile.java index e9200b2ce..28ef20054 100644 --- a/tool/src/org/antlr/v4/codegen/model/ParserFile.java +++ b/tool/src/org/antlr/v4/codegen/model/ParserFile.java @@ -42,7 +42,10 @@ public class ParserFile extends OutputFile { public String genPackage; // from -package cmd-line @ModelElement public Parser parser; @ModelElement public Map 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; } } diff --git a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java index caa05a41d..fc1182d1c 100644 --- a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java +++ b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java @@ -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 {