Move RuleNode, TerminalNode, ErrorNode, TerminalNodeImpl, and ErrorNodeImpl to top-level types

This commit is contained in:
Sam Harwell 2012-08-06 15:01:00 -05:00
parent 2947fe6a2a
commit cb09dd6d09
15 changed files with 281 additions and 113 deletions

View File

@ -33,8 +33,12 @@ import org.antlr.v4.runtime.atn.ATNState;
import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.misc.Nullable;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.ErrorNodeImpl;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeListener;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.tree.TerminalNodeImpl;
import java.util.ArrayList;
import java.util.Collections;

View File

@ -32,6 +32,7 @@ import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.misc.Nullable;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
import org.antlr.v4.runtime.tree.RuleNode;
import org.antlr.v4.runtime.tree.Trees;
import org.antlr.v4.runtime.tree.gui.TreeViewer;
@ -58,7 +59,7 @@ import java.io.IOException;
*
* @see ParserRuleContext
*/
public class RuleContext implements ParseTree.RuleNode {
public class RuleContext implements RuleNode {
/** What context invoked this rule? */
public RuleContext parent;

View File

@ -0,0 +1,35 @@
/*
[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.
*/
package org.antlr.v4.runtime.tree;
import org.antlr.v4.runtime.Token;
public interface ErrorNode<Symbol extends Token> extends TerminalNode<Symbol> {
}

View File

@ -0,0 +1,52 @@
/*
[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.
*/
package org.antlr.v4.runtime.tree;
import org.antlr.v4.runtime.Token;
/** Represents a token that was consumed during resynchronization
* rather than during a valid match operation. For example,
* we will create this kind of a node during single token insertion
* and deletion as well as during "consume until error recovery set"
* upon no viable alternative exceptions.
*/
public class ErrorNodeImpl<Symbol extends Token> extends
TerminalNodeImpl<Symbol>
implements ErrorNode<Symbol>
{
public ErrorNodeImpl(Symbol token) {
super(token);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
return visitor.visitErrorNode(this);
}
}

View File

@ -42,93 +42,6 @@ import org.antlr.v4.runtime.misc.Interval;
* The payload is either a token or a context object.
*/
public interface ParseTree extends SyntaxTree {
public interface RuleNode extends ParseTree {
RuleContext getRuleContext();
}
public interface TerminalNode<Symbol extends Token> extends ParseTree {
Symbol getSymbol();
}
public static class TerminalNodeImpl<Symbol extends Token> implements TerminalNode<Symbol> {
public Symbol symbol;
public ParseTree parent;
/** Which ATN node matched this token? */
public int s;
public TerminalNodeImpl(Symbol symbol) { this.symbol = symbol; }
@Override
public ParseTree getChild(int i) {return null;}
@Override
public Symbol getSymbol() {return symbol;}
@Override
public ParseTree getParent() { return parent; }
@Override
public Symbol getPayload() { return symbol; }
@Override
public Interval getSourceInterval() {
if ( symbol ==null ) return Interval.INVALID;
return new Interval(symbol.getStartIndex(), symbol.getStopIndex());
}
@Override
public int getChildCount() { return 0; }
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
return visitor.visitTerminal(this);
}
@Override
public String getText() { return symbol.getText(); }
@Override
public String toStringTree(Parser parser) {
return toString();
}
public boolean isErrorNode() { return this instanceof ErrorNode; }
@Override
public String toString() {
if ( symbol.getType() == Token.EOF ) return "<EOF>";
return symbol.getText();
}
@Override
public String toStringTree() {
return toString();
}
}
public interface ErrorNode<Symbol extends Token> extends TerminalNode<Symbol> {
}
/** Represents a token that was consumed during resynchronization
* rather than during a valid match operation. For example,
* we will create this kind of a node during single token insertion
* and deletion as well as during "consume until error recovery set"
* upon no viable alternative exceptions.
*/
public static class ErrorNodeImpl<Symbol extends Token> extends
TerminalNodeImpl<Symbol>
implements ErrorNode<Symbol>
{
public ErrorNodeImpl(Symbol token) {
super(token);
}
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
return visitor.visitErrorNode(this);
}
}
// the following methods narrow the return type; they are not additional methods
@Override
ParseTree getParent();

View File

@ -33,8 +33,8 @@ import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
public interface ParseTreeListener<Symbol extends Token> {
void visitTerminal(ParseTree.TerminalNode<Symbol> node);
void visitErrorNode(ParseTree.ErrorNode<Symbol> node);
void visitTerminal(TerminalNode<Symbol> node);
void visitErrorNode(ErrorNode<Symbol> node);
void enterEveryRule(ParserRuleContext<Symbol> ctx);
void exitEveryRule(ParserRuleContext<Symbol> ctx);
}

View File

@ -17,7 +17,7 @@ public class ParseTreeVisitor<T> {
* care about some nodes. The {@link ParserRuleContext#accept} method
* walks all children by default; i.e., calls this method.
*/
public T visitChildren(ParseTree.RuleNode node) {
public T visitChildren(RuleNode node) {
T result = null;
int n = node.getChildCount();
for (int i=0; i<n; i++) {
@ -27,6 +27,6 @@ public class ParseTreeVisitor<T> {
return result;
}
public T visitTerminal(ParseTree.TerminalNode<? extends Token> node) { return null; }
public T visitErrorNode(ParseTree.ErrorNode<? extends Token> node) { return null; }
public T visitTerminal(TerminalNode<? extends Token> node) { return null; }
public T visitErrorNode(ErrorNode<? extends Token> node) { return null; }
}

View File

@ -37,15 +37,15 @@ public class ParseTreeWalker {
@SuppressWarnings("unchecked")
public <Symbol extends Token> void walk(ParseTreeListener<Symbol> listener, ParseTree t) {
if ( t instanceof ParseTree.ErrorNodeImpl) {
listener.visitErrorNode((ParseTree.ErrorNode<Symbol>)t);
if ( t instanceof ErrorNodeImpl) {
listener.visitErrorNode((ErrorNode<Symbol>)t);
return;
}
else if ( t instanceof ParseTree.TerminalNode) {
listener.visitTerminal((ParseTree.TerminalNode<Symbol>)t);
else if ( t instanceof TerminalNode) {
listener.visitTerminal((TerminalNode<Symbol>)t);
return;
}
ParseTree.RuleNode r = (ParseTree.RuleNode)t;
RuleNode r = (RuleNode)t;
enterRule(listener, r);
int n = r.getChildCount();
for (int i = 0; i<n; i++) {
@ -59,14 +59,14 @@ public class ParseTreeWalker {
* First we trigger the generic and then the rule specific.
* We to them in reverse order upon finishing the node.
*/
protected <Symbol extends Token> void enterRule(ParseTreeListener<Symbol> listener, ParseTree.RuleNode r) {
protected <Symbol extends Token> void enterRule(ParseTreeListener<Symbol> listener, RuleNode r) {
@SuppressWarnings("unchecked")
ParserRuleContext<Symbol> ctx = (ParserRuleContext<Symbol>)r.getRuleContext();
listener.enterEveryRule(ctx);
ctx.enterRule(listener);
}
protected <Symbol extends Token> void exitRule(ParseTreeListener<Symbol> listener, ParseTree.RuleNode r) {
protected <Symbol extends Token> void exitRule(ParseTreeListener<Symbol> listener, RuleNode r) {
@SuppressWarnings("unchecked")
ParserRuleContext<Symbol> ctx = (ParserRuleContext<Symbol>)r.getRuleContext();
ctx.exitRule(listener);

View File

@ -0,0 +1,36 @@
/*
[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.
*/
package org.antlr.v4.runtime.tree;
import org.antlr.v4.runtime.RuleContext;
public interface RuleNode extends ParseTree {
RuleContext getRuleContext();
}

View File

@ -0,0 +1,36 @@
/*
[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.
*/
package org.antlr.v4.runtime.tree;
import org.antlr.v4.runtime.Token;
public interface TerminalNode<Symbol extends Token> extends ParseTree {
Symbol getSymbol();
}

View File

@ -0,0 +1,90 @@
/*
[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.
*/
package org.antlr.v4.runtime.tree;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.Interval;
public class TerminalNodeImpl<Symbol extends Token> implements TerminalNode<Symbol> {
public Symbol symbol;
public ParseTree parent;
/** Which ATN node matched this token? */
public int s;
public TerminalNodeImpl(Symbol symbol) { this.symbol = symbol; }
@Override
public ParseTree getChild(int i) {return null;}
@Override
public Symbol getSymbol() {return symbol;}
@Override
public ParseTree getParent() { return parent; }
@Override
public Symbol getPayload() { return symbol; }
@Override
public Interval getSourceInterval() {
if ( symbol ==null ) return Interval.INVALID;
return new Interval(symbol.getStartIndex(), symbol.getStopIndex());
}
@Override
public int getChildCount() { return 0; }
@Override
public <T> T accept(ParseTreeVisitor<? extends T> visitor) {
return visitor.visitTerminal(this);
}
@Override
public String getText() { return symbol.getText(); }
@Override
public String toStringTree(Parser parser) {
return toString();
}
public boolean isErrorNode() { return this instanceof ErrorNode; }
@Override
public String toString() {
if ( symbol.getType() == Token.EOF ) return "<EOF>";
return symbol.getText();
}
@Override
public String toStringTree() {
return toString();
}
}

View File

@ -97,16 +97,16 @@ public class Trees {
public static String getNodeText(Tree t, Parser recog) {
if ( recog!=null ) {
if ( t instanceof ParseTree.RuleNode ) {
int ruleIndex = ((ParseTree.RuleNode)t).getRuleContext().getRuleIndex();
if ( t instanceof RuleNode ) {
int ruleIndex = ((RuleNode)t).getRuleContext().getRuleIndex();
String ruleName = recog.getRuleNames()[ruleIndex];
return ruleName;
}
else if ( t instanceof ParseTree.ErrorNodeImpl) {
else if ( t instanceof ErrorNodeImpl) {
return t.toString();
}
else if ( t instanceof ParseTree.TerminalNode) {
Object symbol = ((ParseTree.TerminalNode<?>)t).getSymbol();
else if ( t instanceof TerminalNode) {
Object symbol = ((TerminalNode<?>)t).getSymbol();
if (symbol instanceof Token) {
String s = ((Token)symbol).getText();
return s;

View File

@ -36,7 +36,7 @@ import org.abego.treelayout.TreeLayout;
import org.abego.treelayout.util.DefaultConfiguration;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.misc.Utils;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.Tree;
import java.awt.*;
@ -129,7 +129,7 @@ public class TreePostScriptGenerator {
// for debugging, turn this on to see boundingbox of nodes
//doc.rect(box.x, box.y, box.width, box.height);
// make error nodes from parse tree red by default
if ( t instanceof ParseTree.ErrorNode ) {
if ( t instanceof ErrorNode ) {
doc.highlight(box.x, box.y, box.width, box.height);
}
double x = box.x+nodeWidthPadding;

View File

@ -36,7 +36,7 @@ import org.abego.treelayout.util.DefaultConfiguration;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.misc.GraphicsSupport;
import org.antlr.v4.runtime.misc.Utils;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.Tree;
import org.antlr.v4.runtime.tree.Trees;
@ -186,10 +186,10 @@ public class TreeViewer extends JComponent {
Rectangle2D.Double box = getBoundsOfNode(tree);
// draw the box in the background
if ( isHighlighted(tree) || boxColor!=null ||
tree instanceof ParseTree.ErrorNode )
tree instanceof ErrorNode )
{
if ( isHighlighted(tree) ) g.setColor(highlightedBoxColor);
else if ( tree instanceof ParseTree.ErrorNode ) g.setColor(LIGHT_RED);
else if ( tree instanceof ErrorNode ) g.setColor(LIGHT_RED);
else g.setColor(boxColor);
g.fillRoundRect((int) box.x, (int) box.y, (int) box.width - 1,
(int) box.height - 1, arcSize, arcSize);

View File

@ -44,7 +44,8 @@ BaseListenerFile(file, header) ::= <<
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.tree.ErrorNode;
public class <file.grammarName>BaseListener implements <file.grammarName>Listener {
<file.listenerNames:{lname |
@ -53,8 +54,8 @@ public class <file.grammarName>BaseListener implements <file.grammarName>Listene
@Override public void enterEveryRule(ParserRuleContext\<<InputSymbolType()>\> ctx) { }
@Override public void exitEveryRule(ParserRuleContext\<<InputSymbolType()>\> ctx) { }
@Override public void visitTerminal(ParseTree.TerminalNode\<<InputSymbolType()>\> node) { }
@Override public void visitErrorNode(ParseTree.ErrorNode\<<InputSymbolType()>\> node) { }
@Override public void visitTerminal(TerminalNode\<<InputSymbolType()>\> node) { }
@Override public void visitErrorNode(ErrorNode\<<InputSymbolType()>\> node) { }
}
>>