-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added visitor gen mechanism, runtime support
- Loading branch information
Showing
22 changed files
with
399 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
runtime/Java/src/org/antlr/v4/runtime/tree/ParseTreeVisitor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.