Use two queues instead of a custom linked memory structure

This commit is contained in:
Tom Wieczorek 2016-07-20 09:17:50 +02:00
parent 53f2a67b9d
commit 7ca7acd084
No known key found for this signature in database
GPG Key ID: 97132D3CD100563E
1 changed files with 54 additions and 102 deletions

View File

@ -30,122 +30,74 @@
package org.antlr.v4.runtime.tree; package org.antlr.v4.runtime.tree;
import java.util.ArrayDeque;
import java.util.Deque;
import org.antlr.v4.runtime.misc.IntegerStack;
/** /**
* An iterative (read: non-recursive) pre-order and post-order tree walker that * An iterative (read: non-recursive) pre-order and post-order tree walker that
* doesn't use the thread stack but it's own heap-based stack using linked data * doesn't use the thread stack but heap-based stacks. Makes it possible to
* structures. Makes it possible to process deeply nested parse trees. * process deeply nested parse trees.
*/ */
public class IterativeParseTreeWalker extends ParseTreeWalker { public class IterativeParseTreeWalker extends ParseTreeWalker {
public static final ParseTreeWalker DEFAULT = new IterativeParseTreeWalker();
@Override @Override
public void walk(final ParseTreeListener listener, final ParseTree t) { public void walk(ParseTreeListener listener, ParseTree t) {
for (WalkerNode<?> currentNode = createNode(null, t, 0); currentNode != null;) {
currentNode.enter(listener); // Pre order visit
// Move down to first child final Deque<ParseTree> nodeStack = new ArrayDeque<ParseTree>();
WalkerNode<?> nextNode = currentNode.getFirstChild(); final IntegerStack indexStack = new IntegerStack();
if (nextNode != null) {
currentNode = nextNode;
continue;
}
// No child nodes, so walk tree ParseTree currentNode = t;
while (currentNode != null) { int currentIndex = 0;
currentNode.exit(listener); // Post order visit
// Move to sibling if possible while (currentNode != null) {
nextNode = currentNode.getNextSibling();
if (nextNode != null) {
currentNode = nextNode;
break;
}
// Move up // pre-order visit
currentNode = currentNode.getParentNode(); if (currentNode instanceof ErrorNode) {
} listener.visitErrorNode((ErrorNode) currentNode);
} } else if (currentNode instanceof TerminalNode) {
} listener.visitTerminal((TerminalNode) currentNode);
} else {
final RuleNode r = (RuleNode) currentNode;
enterRule(listener, r);
}
protected WalkerNode<?> createNode(final WalkerNode<?> parent, final ParseTree self, final int pos) { // Move down to first child, if exists
if (self instanceof ErrorNode) { if (currentNode.getChildCount() > 0) {
return new ErrorWalkerNode(parent, (ErrorNode) self, pos); nodeStack.push(currentNode);
} else if (self instanceof TerminalNode) { indexStack.push(currentIndex);
return new TerminalWalkerNode(parent, (TerminalNode) self, pos); currentIndex = 0;
} else { currentNode = currentNode.getChild(0);
return new RuleWalkerNode(parent, (RuleNode) self, pos); continue;
} }
}
protected abstract class WalkerNode<N extends ParseTree> { // No child nodes, so walk tree
private final WalkerNode<?> parent; do {
protected final N self;
private final int pos;
protected WalkerNode(final WalkerNode<?> parent, final N self, final int pos) { // post-order visit
this.parent = parent; if (currentNode instanceof RuleNode) {
this.self = self; exitRule(listener, (RuleNode) currentNode);
this.pos = pos; }
}
public WalkerNode<?> getFirstChild() { // No parent, so no siblings
return self.getChildCount() > 0 ? createChildNode(0) : null; if (nodeStack.isEmpty()) {
} currentNode = null;
currentIndex = 0;
break;
}
public WalkerNode<?> getNextSibling() { // Move to next sibling if possible
return parent != null && parent.self.getChildCount() > pos + 1 ? parent.createChildNode(pos + 1) : null; currentNode = nodeStack.peek().getChild(++currentIndex);
} if (currentNode != null) {
break;
}
public WalkerNode<?> getParentNode() { // No next, sibling, so move up
return parent; currentNode = nodeStack.pop();
} currentIndex = indexStack.pop();
public abstract void enter(ParseTreeListener listener); } while (currentNode != null);
}
public void exit(final ParseTreeListener listener) { }
// defaults to do nothing
}
protected WalkerNode<?> createChildNode(final int pos) {
return createNode(this, self.getChild(pos), pos);
}
}
protected class RuleWalkerNode extends WalkerNode<RuleNode> {
public RuleWalkerNode(final WalkerNode<?> parent, final RuleNode self, final int pos) {
super(parent, self, pos);
}
@Override
public void enter(final ParseTreeListener listener) {
enterRule(listener, self);
}
@Override
public void exit(final ParseTreeListener listener) {
exitRule(listener, self);
}
}
protected class ErrorWalkerNode extends WalkerNode<ErrorNode> {
public ErrorWalkerNode(final WalkerNode<?> parent, final ErrorNode self, final int pos) {
super(parent, self, pos);
}
@Override
public void enter(final ParseTreeListener listener) {
listener.visitErrorNode(self);
}
}
protected class TerminalWalkerNode extends WalkerNode<TerminalNode> {
public TerminalWalkerNode(final WalkerNode<?> parent, final TerminalNode self, final int pos) {
super(parent, self, pos);
}
@Override
public void enter(final ParseTreeListener listener) {
listener.visitTerminal(self);
}
}
} }