forked from jasder/antlr
stash; got lost of visitor stuff working
This commit is contained in:
parent
f426e8781b
commit
8013bb868c
|
@ -34,6 +34,7 @@ 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.tree.ParseTreeVisitor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
|
@ -131,10 +132,11 @@ public class ParserRuleContext<Symbol> extends RuleContext {
|
|||
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,V extends ParseTreeVisitor<T>> T dispatch(V visitor) { visitor.visitChildren(this); return null; }
|
||||
|
||||
/** Does not set parent link; other add methods do */
|
||||
public void addChild(TerminalNode<Symbol> t) {
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
package org.antlr.v4.runtime.tree;
|
||||
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
|
||||
/** 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.dispatch(this);
|
||||
}
|
||||
|
||||
/** Visit all rule, nonleaf children */
|
||||
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();
|
||||
rctx.dispatch(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Don't need to visit tokens. each visit() method deals with it's leaf children
|
||||
// public T visit(Token t) {
|
||||
// return null;
|
||||
// }
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
import org.antlr.v4.runtime.Token;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||
|
||||
public class ABaseVisitor<T> extends ParseTreeVisitor<T> implements AVisitor<T> {
|
||||
public T visit(AParser.MultContext ctx) { return null; }
|
||||
public T visit(AParser.ParensContext ctx) { return null; }
|
||||
public T visit(AParser.eContext ctx) { return null; }
|
||||
public T visit(AParser.sContext ctx) { return null; }
|
||||
public T visit(AParser.AddContext ctx) { return null; }
|
||||
public T visit(AParser.IntContext ctx) { return null; }
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
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);
|
||||
// T visit(Token t);
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
/*
|
||||
[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.ANTLRFileStream;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeWalker;
|
||||
|
||||
public class TestVisitor {
|
||||
public static class MyVisitor extends ParseTreeVisitor<Integer> implements AVisitor<Integer> {
|
||||
@Override
|
||||
public Integer visit(AParser.AddContext ctx) {
|
||||
return ctx.e(0).dispatch(this) + ctx.e(1).dispatch(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).dispatch(this) * ctx.e(1).dispatch(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer visit(AParser.ParensContext ctx) {
|
||||
return ctx.e().dispatch(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer visit(AParser.sContext ctx) {
|
||||
return ctx.e().dispatch(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.dispatch(visitor);
|
||||
System.out.println("result from tree walk = " + result);
|
||||
}
|
||||
}
|
||||
|
|
@ -551,6 +551,7 @@ public static class <s.name> extends <superClass><if(interfaces)> implements <in
|
|||
}
|
||||
<endif>
|
||||
<visitorDispatchMethods; separator="\n">
|
||||
public \<T, V extends <parser.grammarName>Visitor\<T>\> T dispatch(V visitor) { return visitor.visit(this); }
|
||||
<extensionMembers; separator="\n">
|
||||
}
|
||||
>>
|
||||
|
@ -561,6 +562,7 @@ public static class <s.name> extends <currentRule.name>Context {
|
|||
<getters:{g | <g>}; separator="\n">
|
||||
public <s.name>(<currentRule.name>Context ctx) { copyFrom(ctx); }
|
||||
<visitorDispatchMethods; separator="\n">
|
||||
public \<T, V extends <parser.grammarName>Visitor\<T>\> T dispatch(V visitor) { return visitor.visit(this); }
|
||||
}
|
||||
>>
|
||||
|
||||
|
|
Loading…
Reference in New Issue