001/* Generated By:JavaCC: Do not edit this line. JJTMatrixParserState.java Version 5.0 */
002package ptolemy.data.expr;
003
004public class JJTMatrixParserState {
005    private java.util.List<Node> nodes;
006    private java.util.List<Integer> marks;
007
008    private int sp; // number of nodes on stack
009    private int mk; // current mark
010    private boolean node_created;
011
012    public JJTMatrixParserState() {
013        nodes = new java.util.ArrayList<Node>();
014        marks = new java.util.ArrayList<Integer>();
015        sp = 0;
016        mk = 0;
017    }
018
019    /* Determines whether the current node was actually closed and
020       pushed.  This should only be called in the final user action of a
021       node scope.  */
022    public boolean nodeCreated() {
023        return node_created;
024    }
025
026    /* Call this to reinitialize the node stack.  It is called
027       automatically by the parser's ReInit() method. */
028    public void reset() {
029        nodes.clear();
030        marks.clear();
031        sp = 0;
032        mk = 0;
033    }
034
035    /* Returns the root node of the AST.  It only makes sense to call
036       this after a successful parse. */
037    public Node rootNode() {
038        return nodes.get(0);
039    }
040
041    /* Pushes a node on to the stack. */
042    public void pushNode(Node n) {
043        nodes.add(n);
044        ++sp;
045    }
046
047    /* Returns the node on the top of the stack, and remove it from the
048       stack.  */
049    public Node popNode() {
050        if (--sp < mk) {
051            mk = marks.remove(marks.size() - 1);
052        }
053        return nodes.remove(nodes.size() - 1);
054    }
055
056    /* Returns the node currently on the top of the stack. */
057    public Node peekNode() {
058        return nodes.get(nodes.size() - 1);
059    }
060
061    /* Returns the number of children on the stack in the current node
062       scope. */
063    public int nodeArity() {
064        return sp - mk;
065    }
066
067    public void clearNodeScope(Node n) {
068        while (sp > mk) {
069            popNode();
070        }
071        mk = marks.remove(marks.size() - 1);
072    }
073
074    public void openNodeScope(Node n) {
075        marks.add(mk);
076        mk = sp;
077        n.jjtOpen();
078    }
079
080    /* A definite node is constructed from a specified number of
081       children.  That number of nodes are popped from the stack and
082       made the children of the definite node.  Then the definite node
083       is pushed on to the stack. */
084    public void closeNodeScope(Node n, int num) {
085        mk = marks.remove(marks.size() - 1);
086        while (num-- > 0) {
087            Node c = popNode();
088            c.jjtSetParent(n);
089            n.jjtAddChild(c, num);
090        }
091        n.jjtClose();
092        pushNode(n);
093        node_created = true;
094    }
095
096    /* A conditional node is constructed if its condition is true.  All
097       the nodes that have been pushed since the node was opened are
098       made children of the conditional node, which is then pushed
099       on to the stack.  If the condition is false the node is not
100       constructed and they are left on the stack. */
101    public void closeNodeScope(Node n, boolean condition) {
102        if (condition) {
103            int a = nodeArity();
104            mk = marks.remove(marks.size() - 1);
105            while (a-- > 0) {
106                Node c = popNode();
107                c.jjtSetParent(n);
108                n.jjtAddChild(c, a);
109            }
110            n.jjtClose();
111            pushNode(n);
112            node_created = true;
113        } else {
114            mk = marks.remove(marks.size() - 1);
115            node_created = false;
116        }
117    }
118}
119/* JavaCC - OriginalChecksum=629fcd3b50264b1bf0a93f9fdb22246c (do not edit this line) */