it.twenfir.antlr.ast.WalkerIterator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of twenfir-antlr Show documentation
Show all versions of twenfir-antlr Show documentation
Add-ons for the ANTLR parser generator
package it.twenfir.antlr.ast;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Stack;
/**
* An iterator that traverses an AST
*/
public class WalkerIterator implements Iterator {
private Stack> stack = new Stack<>();
private AstNode current;
/**
* Constructor.
*
* @param node the root of the AST subtree to be traversed
*/
public WalkerIterator(AstNode node) {
stack.push(node.getChildren());
}
private void advance() {
while ( current == null && ! stack.empty() ) {
if ( stack.peek().hasNext() ) {
current = stack.peek().next();
stack.push(current.getChildren());
}
else {
stack.pop();
}
}
}
@Override
public boolean hasNext() {
advance();
return current != null;
}
@Override
public AstNode next() {
advance();
if ( current == null ) {
throw new NoSuchElementException();
}
AstNode value = current;
current = null;
return value;
}
}