Example usage for org.antlr.v4.runtime.misc IntegerStack peek

List of usage examples for org.antlr.v4.runtime.misc IntegerStack peek

Introduction

In this page you can find the example usage for org.antlr.v4.runtime.misc IntegerStack peek.

Prototype

public final int peek() 

Source Link

Usage

From source file:com.xiaomi.linden.bql.BQLCompiler.java

License:Apache License

private static TerminalNode getStartNode(ParseTree tree) {
    if (tree instanceof TerminalNode) {
        return (TerminalNode) tree;
    }//from  w w  w . j a  va 2s.  com

    Deque<ParseTree> workList = new ArrayDeque<ParseTree>();
    IntegerStack workIndexStack = new IntegerStack();
    workList.push(tree);
    workIndexStack.push(0);
    while (!workList.isEmpty()) {
        ParseTree currentTree = workList.peek();
        int currentIndex = workIndexStack.peek();
        if (currentIndex == currentTree.getChildCount()) {
            workList.pop();
            workIndexStack.pop();
            continue;
        }

        // move work list to next child
        workIndexStack.push(workIndexStack.pop() + 1);

        // process the current child
        ParseTree child = currentTree.getChild(currentIndex);
        if (child instanceof TerminalNode) {
            return (TerminalNode) child;
        }

        workList.push(child);
        workIndexStack.push(0);
    }

    return null;
}