rip out remainder of parselistener in code gen

This commit is contained in:
Terence Parr 2012-09-04 19:34:55 -07:00
parent 38e0ad238d
commit c277f445e7
13 changed files with 17 additions and 182 deletions

View File

@ -59,36 +59,6 @@ public class <file.grammarName>BaseListener implements <file.grammarName>Listene
}
>>
ParseListenerFile(file, header) ::= <<
<header>
import org.antlr.v4.runtime.tree.*;
import org.antlr.v4.runtime.*;
public interface <file.grammarName>ParseListener extends ParseListener\<<InputSymbolType()>\> {
<file.listenerEnterNames:{lname |
void enter<lname; format="cap">(ParserRuleContext\<<InputSymbolType()>\> ctx);}; separator="\n">
<file.listenerExitNames:{lname |
void exit<lname; format="cap">(<file.parserName>.<lname; format="cap">Context ctx);}; separator="\n">
}
>>
BaseParseListenerFile(file, header) ::= <<
<header>
import org.antlr.v4.runtime.*;
public class <file.grammarName>BaseParseListener implements <file.grammarName>ParseListener {
<file.listenerEnterNames:{lname |
@Override public void enter<lname; format="cap">(ParserRuleContext\<<InputSymbolType()>\> ctx) { \}}; separator="\n">
<file.listenerExitNames:{lname |
@Override public void exit<lname; format="cap">(<file.parserName>.<lname; format="cap">Context ctx) { \}}; separator="\n">
@Override public void enterNonLRRule(ParserRuleContext\<<InputSymbolType()>\> ctx) { }
@Override public void exitEveryRule(ParserRuleContext\<<InputSymbolType()>\> ctx) { }
@Override public void visitTerminal(ParserRuleContext\<<InputSymbolType()>\> ctx, <InputSymbolType()> symbol) { }
}
>>
VisitorFile(file, header) ::= <<
<header>
import org.antlr.v4.runtime.tree.*;
@ -648,13 +618,6 @@ public \<T> T accept(ParseTreeVisitor\<? extends T> visitor) {
}
>>
ParseListenerDispatchMethod(method) ::= <<
@Override
public void <if(method.isEnter)>enter<else>exit<endif>Rule(ParseListener\<<InputSymbolType()>\> listener) {
if ( listener instanceof <parser.grammarName>ParseListener ) ((<parser.grammarName>ParseListener)listener).<if(method.isEnter)>enter<else>exit<endif><struct.derivedFromName; format="cap">(this);
}
>>
AttributeDecl(d) ::= "public <d.decl>;"
/** If we don't know location of label def x, use this template */

View File

@ -122,7 +122,6 @@ public class Tool {
public boolean log = false;
public boolean verbose_dfa = false;
public boolean gen_listener = true;
public boolean gen_parse_listener = false;
public boolean gen_visitor = false;
public boolean abstract_recognizer = false;
@ -138,8 +137,6 @@ public class Tool {
new Option("msgFormat", "-message-format", OptionArgType.STRING, "specify output style for messages"),
new Option("gen_listener", "-listener", "generate parse tree listener (default)"),
new Option("gen_listener", "-no-listener", "don't generate parse tree listener"),
new Option("gen_parse_listener", "-parse-listener", "generate parse listener"),
new Option("gen_parse_listener", "-no-parse-listener", "don't generate parse listener (default)"),
new Option("gen_visitor", "-visitor", "generate parse tree visitor"),
new Option("gen_visitor", "-no-visitor", "don't generate parse tree visitor (default)"),
new Option("abstract_recognizer", "-abstract", "generate abstract recognizer classes"),

View File

@ -55,10 +55,6 @@ public class CodeGenPipeline {
gen.writeListener(gen.generateListener());
gen.writeBaseListener(gen.generateBaseListener());
}
if ( g.tool.gen_parse_listener ) {
gen.writeParseListener(gen.generateParseListener());
gen.writeBaseParseListener(gen.generateBaseParseListener());
}
if ( g.tool.gen_visitor ) {
gen.writeVisitor(gen.generateVisitor());
gen.writeBaseVisitor(gen.generateBaseVisitor());

View File

@ -33,13 +33,22 @@ import org.antlr.v4.Tool;
import org.antlr.v4.codegen.model.OutputModelObject;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.tool.*;
import org.stringtemplate.v4.*;
import org.antlr.v4.tool.ErrorType;
import org.antlr.v4.tool.Grammar;
import org.stringtemplate.v4.AutoIndentWriter;
import org.stringtemplate.v4.NumberRenderer;
import org.stringtemplate.v4.ST;
import org.stringtemplate.v4.STGroup;
import org.stringtemplate.v4.STGroupFile;
import org.stringtemplate.v4.STWriter;
import org.stringtemplate.v4.StringRenderer;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import org.antlr.v4.misc.Utils.Func1;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
/** General controller for code gen. Can instantiate sub generator(s).
*/
@ -132,8 +141,6 @@ public class CodeGenerator {
public ST generateParser() { return walk(createController().buildParserOutputModel()); }
public ST generateListener() { return walk(createController().buildListenerOutputModel()); }
public ST generateBaseListener() { return walk(createController().buildBaseListenerOutputModel()); }
public ST generateParseListener() { return walk(createController().buildParseListenerOutputModel()); }
public ST generateBaseParseListener() { return walk(createController().buildBaseParseListenerOutputModel()); }
public ST generateVisitor() { return walk(createController().buildVisitorOutputModel()); }
public ST generateBaseVisitor() { return walk(createController().buildBaseVisitorOutputModel()); }
@ -181,14 +188,6 @@ public class CodeGenerator {
target.genFile(g,outputFileST, getBaseListenerFileName());
}
public void writeParseListener(ST outputFileST) {
target.genFile(g,outputFileST, getParseListenerFileName());
}
public void writeBaseParseListener(ST outputFileST) {
target.genFile(g,outputFileST, getBaseParseListenerFileName());
}
public void writeVisitor(ST outputFileST) {
target.genFile(g,outputFileST, getVisitorFileName());
}
@ -296,20 +295,6 @@ public class CodeGenerator {
return listenerName+extST.render();
}
public String getParseListenerFileName() {
assert g.name != null;
ST extST = templates.getInstanceOf("codeFileExtension");
String listenerName = g.name + "ParseListener";
return listenerName+extST.render();
}
public String getBaseParseListenerFileName() {
assert g.name != null;
ST extST = templates.getInstanceOf("codeFileExtension");
String listenerName = g.name + "BaseParseListener";
return listenerName+extST.render();
}
/** A given grammar T, return a blank listener implementation
* such as TBaseListener.java, if we're using the Java target.
*/

View File

@ -34,7 +34,6 @@ import org.antlr.v4.analysis.LeftRecursiveRuleAltInfo;
import org.antlr.v4.codegen.model.Action;
import org.antlr.v4.codegen.model.AltBlock;
import org.antlr.v4.codegen.model.BaseListenerFile;
import org.antlr.v4.codegen.model.BaseParseListenerFile;
import org.antlr.v4.codegen.model.BaseVisitorFile;
import org.antlr.v4.codegen.model.Choice;
import org.antlr.v4.codegen.model.CodeBlockForAlt;
@ -45,7 +44,6 @@ import org.antlr.v4.codegen.model.Lexer;
import org.antlr.v4.codegen.model.LexerFile;
import org.antlr.v4.codegen.model.ListenerFile;
import org.antlr.v4.codegen.model.OutputModelObject;
import org.antlr.v4.codegen.model.ParseListenerFile;
import org.antlr.v4.codegen.model.Parser;
import org.antlr.v4.codegen.model.ParserFile;
import org.antlr.v4.codegen.model.RuleActionFunction;
@ -146,16 +144,6 @@ public class OutputModelController {
return new BaseListenerFile(delegate, gen.getBaseListenerFileName());
}
public OutputModelObject buildParseListenerOutputModel() {
CodeGenerator gen = delegate.getGenerator();
return new ParseListenerFile(delegate, gen.getParseListenerFileName());
}
public OutputModelObject buildBaseParseListenerOutputModel() {
CodeGenerator gen = delegate.getGenerator();
return new BaseParseListenerFile(delegate, gen.getBaseParseListenerFileName());
}
public OutputModelObject buildVisitorOutputModel() {
CodeGenerator gen = delegate.getGenerator();
return new VisitorFile(delegate, gen.getVisitorFileName());

View File

@ -1,9 +0,0 @@
package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.OutputModelFactory;
public class BaseParseListenerFile extends ParseListenerFile {
public BaseParseListenerFile(OutputModelFactory factory, String fileName) {
super(factory, fileName);
}
}

View File

@ -1,13 +0,0 @@
package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.OutputModelFactory;
public class ParseListenerDispatchMethod extends DispatchMethod {
public boolean isEnter;
public ParseListenerDispatchMethod(OutputModelFactory factory, boolean isEnter) {
super(factory);
this.isEnter = isEnter;
}
}

View File

@ -1,51 +0,0 @@
package org.antlr.v4.codegen.model;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.runtime.misc.Triple;
import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.LeftRecursiveRule;
import org.antlr.v4.tool.Rule;
import org.antlr.v4.tool.ast.ActionAST;
import org.antlr.v4.tool.ast.AltAST;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ParseListenerFile extends OutputFile {
public String grammarName;
public String parserName;
public Set<String> listenerEnterNames = new HashSet<String>();
public Set<String> listenerExitNames = new HashSet<String>();
@ModelElement public Action header;
public ParseListenerFile(OutputModelFactory factory, String fileName) {
super(factory, fileName);
Grammar g = factory.getGrammar();
parserName = g.getRecognizerName();
grammarName = g.name;
for (Rule r : g.rules.values()) {
List<Triple<Integer,AltAST,String>> labels = r.getAltLabels();
// EXIT RULES
if ( labels!=null ) {
// add exit rules for alt labels
for (Triple<Integer,AltAST,String> pair : labels) {
listenerExitNames.add(pair.c);
if ( !(r instanceof LeftRecursiveRule) ) {
listenerEnterNames.add(pair.c);
}
}
}
else {
// add exit rule if no labels
listenerExitNames.add(r.name);
if ( !(r instanceof LeftRecursiveRule) ) {
listenerEnterNames.add(r.name);
}
}
}
ActionAST ast = g.namedActions.get("header");
if ( ast!=null ) header = new Action(factory, ast);
}
}

View File

@ -32,9 +32,7 @@ package org.antlr.v4.codegen.model.decl;
import org.antlr.v4.codegen.OutputModelFactory;
import org.antlr.v4.codegen.model.DispatchMethod;
import org.antlr.v4.codegen.model.ListenerDispatchMethod;
import org.antlr.v4.codegen.model.ParseListenerDispatchMethod;
import org.antlr.v4.codegen.model.VisitorDispatchMethod;
import org.antlr.v4.tool.LeftRecursiveRule;
import org.antlr.v4.tool.Rule;
import java.util.ArrayList;
@ -62,13 +60,6 @@ public class AltLabelStructDecl extends StructDecl {
if ( factory.getGrammar().tool.gen_visitor ) {
dispatchMethods.add(new VisitorDispatchMethod(factory));
}
if ( factory.getGrammar().tool.gen_parse_listener ) {
if ( !(r instanceof LeftRecursiveRule) ) {
dispatchMethods.add(new ParseListenerDispatchMethod(factory, true));
}
dispatchMethods.add(new ParseListenerDispatchMethod(factory, false));
}
}
@Override

View File

@ -34,11 +34,9 @@ import org.antlr.v4.codegen.model.DispatchMethod;
import org.antlr.v4.codegen.model.ListenerDispatchMethod;
import org.antlr.v4.codegen.model.ModelElement;
import org.antlr.v4.codegen.model.OutputModelObject;
import org.antlr.v4.codegen.model.ParseListenerDispatchMethod;
import org.antlr.v4.codegen.model.VisitorDispatchMethod;
import org.antlr.v4.runtime.misc.OrderedHashSet;
import org.antlr.v4.tool.Attribute;
import org.antlr.v4.tool.LeftRecursiveRule;
import org.antlr.v4.tool.Rule;
import java.util.ArrayList;
@ -76,12 +74,6 @@ public class StructDecl extends Decl {
if ( factory.getGrammar().tool.gen_visitor ) {
dispatchMethods.add(new VisitorDispatchMethod(factory));
}
if ( factory.getGrammar().tool.gen_parse_listener ) {
if ( !(r instanceof LeftRecursiveRule) ) {
dispatchMethods.add(new ParseListenerDispatchMethod(factory, true));
}
dispatchMethods.add(new ParseListenerDispatchMethod(factory, false));
}
}
}

View File

@ -476,7 +476,6 @@ public abstract class BaseTest {
grammarStr,
parserName,
lexerName,
"-parse-listener",
"-visitor");
assertTrue(success);
writeFile(tmpdir, "input", input);
@ -523,9 +522,6 @@ public abstract class BaseTest {
if (optionsSet.contains("-visitor")) {
files.add(grammarFileName.substring(0, grammarFileName.lastIndexOf('.'))+"BaseVisitor.java");
}
if (optionsSet.contains("-parse-listener")) {
files.add(grammarFileName.substring(0, grammarFileName.lastIndexOf('.'))+"BaseParseListener.java");
}
}
allIsWell = compile(files.toArray(new String[files.size()]));
return allIsWell;

View File

@ -224,7 +224,7 @@ public class TestFullContextParsing extends BaseTest {
"prog: expr_or_assign*;\n" +
"expr_or_assign\n" +
" : expr '++' {System.out.println(\"fail.\");}\n" +
" | expr {System.out.println(\"pass.\");}\n" +
" | expr {System.out.println(\"pass: \"+$expr.text);}\n" +
" ;\n" +
"expr: expr_primary ('<-' ID)? ;\n" +
"expr_primary\n" +

View File

@ -9,7 +9,7 @@ public class TestListeners extends BaseTest {
"@header {import org.antlr.v4.runtime.tree.ParseTree;}\n"+
"@members {\n" +
"public static class LeafListener extends TBaseListener {\n" +
" public void visitTerminal(ParseTree.TerminalNode<Token> node) {\n" +
" public void visitTerminal(TerminalNode<Token> node) {\n" +
" System.out.println(node.getSymbol().getText());\n" +
" }\n" +
" }}\n" +