Skip to content

Commit

Permalink
Added visitor gen mechanism, runtime support
Browse files Browse the repository at this point in the history
  • Loading branch information
parrt committed Feb 17, 2012
1 parent 66e7e0f commit 725b105
Show file tree
Hide file tree
Showing 22 changed files with 399 additions and 93 deletions.
16 changes: 6 additions & 10 deletions runtime/Java/src/org/antlr/v4/runtime/ParserRuleContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,11 @@ INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
*/
package org.antlr.v4.runtime;

import org.antlr.v4.runtime.atn.ATN;
import org.antlr.v4.runtime.atn.ATNState;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeListener;
import org.antlr.v4.runtime.atn.*;
import org.antlr.v4.runtime.misc.*;
import org.antlr.v4.runtime.tree.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;

/** A rule invocation record for parsing and tree parsing.
*
Expand Down Expand Up @@ -131,10 +126,11 @@ public ParserRuleContext(@Nullable ParserRuleContext<Symbol> parent, int stateNu
this(parent, parent!=null ? parent.s : -1 /* invoking state */, stateNumber);
}

// Double dispatch methods
// Double dispatch methods for listeners and visitors

public void enterRule(ParseTreeListener<Symbol> listener) { }
public void exitRule(ParseTreeListener<Symbol> listener) { }
public <T> T accept(ParseTreeVisitor<? extends T> visitor) { visitor.visitChildren(this); return null; }

/** Does not set parent link; other add methods do */
public void addChild(TerminalNode<Symbol> t) {
Expand Down
26 changes: 26 additions & 0 deletions runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreeVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.antlr.v4.runtime.tree;

import org.antlr.v4.runtime.ParserRuleContext;

/** T is return type of visit methods. Use T=Void for no return type. */
public class ParseTreeVisitor<T> {
public T visit(ParserRuleContext<?> ctx) {
return ctx.accept(this);
}

/** Visit all rule, nonleaf children. Not useful if you are using T as
* non-Void. This returns nothing, losing all computations from below.
* But handy if you are just walking the tree with a visitor and only
* care about some nodes. The ParserRuleContext.accept() method
* walks all children by default; i.e., calls this method.
*/
public <Symbol> void visitChildren(ParserRuleContext<Symbol> ctx) {
for (ParseTree c : ctx.children) {
if ( c instanceof ParseTree.RuleNode) {
ParseTree.RuleNode r = (ParseTree.RuleNode)c;
ParserRuleContext<Symbol> rctx = (ParserRuleContext<Symbol>)r.getRuleContext();
visit(rctx);
}
}
}
}
10 changes: 10 additions & 0 deletions tool/playground/AVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import org.antlr.v4.runtime.tree.*;
import org.antlr.v4.runtime.Token;

public interface AVisitor<T> {
T visit(AParser.MultContext ctx);
T visit(AParser.ParensContext ctx);
T visit(AParser.sContext ctx);
T visit(AParser.AddContext ctx);
T visit(AParser.IntContext ctx);
}
2 changes: 1 addition & 1 deletion tool/playground/TestE.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class TestE {
public static void main(String[] args) throws Exception {
CharStream input = new ANTLRFileStream(args[0]);
E lex = new E(input);
ELexer lex = new ELexer(input);
CommonTokenStream tokens = new CommonTokenStream(lex);
tokens.fill();
for (Object t : tokens.getTokens()) System.out.println(t);
Expand Down
76 changes: 76 additions & 0 deletions tool/playground/TestVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
[The "BSD license"]
Copyright (c) 2011 Terence Parr
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;

public class TestVisitor {
public static class MyVisitor extends ABaseVisitor<Integer> implements AVisitor<Integer> {
@Override
public Integer visit(AParser.AddContext ctx) {
return ctx.e(0).accept(this) + ctx.e(1).accept(this);
}

@Override
public Integer visit(AParser.IntContext ctx) {
return Integer.valueOf(ctx.INT().getText());
}

@Override
public Integer visit(AParser.MultContext ctx) {
// return ctx.e(0).accept(this) * ctx.e(1).accept(this);
return visit(ctx.e(0)) * visit(ctx.e(1));
}

@Override
public Integer visit(AParser.ParensContext ctx) {
return ctx.e().accept(this);
}

@Override
public Integer visit(AParser.sContext ctx) {
return visit(ctx.e());
//return ctx.e().accept(this);
}
}

public static void main(String[] args) throws Exception {
ALexer lexer = new ALexer(new ANTLRFileStream(args[0]));
CommonTokenStream tokens = new CommonTokenStream(lexer);
AParser p = new AParser(tokens);
p.setBuildParseTree(true);
ParserRuleContext<Token> t = p.s();
System.out.println("tree = "+t.toStringTree(p));

MyVisitor visitor = new MyVisitor();
Integer result = visitor.visit(t);
// Integer result = t.accept(visitor);
System.out.println("result from tree walk = " + result);
}
}
40 changes: 35 additions & 5 deletions tool/resources/org/antlr/v4/tool/templates/codegen/Java/Java.stg
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ public class <file.grammarName>BaseListener implements <file.grammarName>Listene
}
>>

VisitorFile(file, header) ::= <<
<header>
import org.antlr.v4.runtime.tree.*;
import org.antlr.v4.runtime.Token;

public interface <file.grammarName>Visitor\<T> {
<file.visitorNames:{lname |
T visit(<file.parserName>.<lname>Context ctx);}; separator="\n">
}
>>

BaseVisitorFile(file, header) ::= <<
<header>
import org.antlr.v4.runtime.tree.*;
import org.antlr.v4.runtime.Token;

public class <file.grammarName>BaseVisitor\<T> extends ParseTreeVisitor\<T> implements <file.grammarName>Visitor\<T> {
<file.visitorNames:{lname |
public T visit(<file.parserName>.<lname>Context ctx) { visitChildren(ctx); return null; \}}; separator="\n">
}
>>

Parser(parser, funcs, atn, sempredFuncs, superclass) ::= <<
<Parser_(ctor="parser_ctor", ...)>
>>
Expand Down Expand Up @@ -533,7 +555,7 @@ ListLabelName(label) ::= "<label>"
CaptureNextToken(d) ::= "<d.varName> = _input.LT(1);"
CaptureNextTokenType(d) ::= "<d.varName> = _input.LA(1);"

StructDecl(s,attrs,getters,visitorDispatchMethods,interfaces,extensionMembers,
StructDecl(s,attrs,getters,dispatchMethods,interfaces,extensionMembers,
superClass={ParserRuleContext\<<InputSymbolType()>>}) ::= <<
public static class <s.name> extends <superClass><if(interfaces)> implements <interfaces; separator=", "><endif> {
<attrs:{a | <a>}; separator="\n">
Expand All @@ -550,27 +572,35 @@ public static class <s.name> extends <superClass><if(interfaces)> implements <in
<s.attrs:{a | this.<a.name> = ctx.<a.name>;}; separator="\n">
}
<endif>
<visitorDispatchMethods; separator="\n">
<dispatchMethods; separator="\n">
<extensionMembers; separator="\n">
}
>>

AltLabelStructDecl(s,attrs,getters,visitorDispatchMethods) ::= <<
AltLabelStructDecl(s,attrs,getters,dispatchMethods) ::= <<
public static class <s.name> extends <currentRule.name>Context {
<attrs:{a | <a>}; separator="\n">
<getters:{g | <g>}; separator="\n">
public <s.name>(<currentRule.name>Context ctx) { copyFrom(ctx); }
<visitorDispatchMethods; separator="\n">
<dispatchMethods; separator="\n">
}
>>

VisitorDispatchMethod(method) ::= <<
ListenerDispatchMethod(method) ::= <<
@Override
public void <if(method.isEnter)>enter<else>exit<endif>Rule(ParseTreeListener\<<InputSymbolType()>\> listener) {
if ( listener instanceof <parser.grammarName>Listener ) ((<parser.grammarName>Listener)listener).<if(method.isEnter)>enter<else>exit<endif>(this);
}
>>

VisitorDispatchMethod(method) ::= <<
@Override
public \<T> T accept(ParseTreeVisitor\<? extends T> visitor) {
if ( visitor instanceof <parser.grammarName>Visitor ) return ((<parser.grammarName>Visitor\<T>)visitor).visit(this);
else return null;
}
>>

AttributeDecl(d) ::= "public <d.decl>;"

/** If we don't know location of label def x, use this template */
Expand Down
7 changes: 6 additions & 1 deletion tool/src/org/antlr/v4/Tool.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,10 @@ protected void handleArgs() {
grammarFiles.add(arg);
continue;
}
boolean found = false;
for (Option o : optionDefs) {
if ( arg.equals(o.name) ) {
found = true;
String argValue = null;
if ( o.argType==OptionArgType.STRING ) {
argValue = args[i];
Expand All @@ -198,7 +200,7 @@ protected void handleArgs() {
try {
Field f = c.getField(o.fieldName);
if ( argValue==null ) {
if ( o.fieldName.startsWith("-no-") ) f.setBoolean(this, false);
if ( arg.startsWith("-no-") ) f.setBoolean(this, false);
else f.setBoolean(this, true);
}
else f.set(this, argValue);
Expand All @@ -208,6 +210,9 @@ protected void handleArgs() {
}
}
}
if ( !found ) {
errMgr.toolError(ErrorType.INVALID_CMDLINE_ARG, arg);
}
}
if ( outputDirectory!=null ) {
if (outputDirectory.endsWith("/") ||
Expand Down
8 changes: 6 additions & 2 deletions tool/src/org/antlr/v4/codegen/CodeGenPipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ public void process() {
}
else {
gen.writeRecognizer(gen.generateParser());
if ( g.tool.gen_listener) {
if ( g.tool.gen_listener ) {
gen.writeListener(gen.generateListener());
gen.writeBlankListener(gen.generateBlankListener());
gen.writeBaseListener(gen.generateBaseListener());
}
if ( g.tool.gen_visitor ) {
gen.writeVisitor(gen.generateVisitor());
gen.writeBaseVisitor(gen.generateBaseVisitor());
}
gen.writeHeaderFile();
}
Expand Down
Loading

0 comments on commit 725b105

Please sign in to comment.