All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.twenfir.antlr.ast.WalkerIterator Maven / Gradle / Ivy

There is a newer version: 0.1.3
Show newest version
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;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy