1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 /* Generated By:JJTree: Do not edit this line. JJTMiniParserState.java */ 19 20 package Mini; 21 22 class JJTMiniParserState { 23 private java.util.Stack<Node> nodes; 24 private java.util.Stack<Integer> marks; 25 26 private int sp; // number of nodes on stack 27 private int mk; // current mark 28 private boolean node_created; 29 JJTMiniParserState()30 JJTMiniParserState() { 31 nodes = new java.util.Stack<Node>(); 32 marks = new java.util.Stack<Integer>(); 33 sp = 0; 34 mk = 0; 35 } 36 37 /* Determines whether the current node was actually closed and 38 pushed. This should only be called in the final user action of a 39 node scope. */ nodeCreated()40 boolean nodeCreated() { 41 return node_created; 42 } 43 44 /* Call this to reinitialize the node stack. It is called 45 automatically by the parser's ReInit() method. */ reset()46 void reset() { 47 nodes.removeAllElements(); 48 marks.removeAllElements(); 49 sp = 0; 50 mk = 0; 51 } 52 53 /* Returns the root node of the AST. It only makes sense to call 54 this after a successful parse. */ rootNode()55 Node rootNode() { 56 return nodes.elementAt(0); 57 } 58 59 /* Pushes a node on to the stack. */ pushNode(Node n)60 void pushNode(Node n) { 61 nodes.push(n); 62 ++sp; 63 } 64 65 /* Returns the node on the top of the stack, and remove it from the 66 stack. */ popNode()67 Node popNode() { 68 if (--sp < mk) { 69 mk = marks.pop().intValue(); 70 } 71 return nodes.pop(); 72 } 73 74 /* Returns the node currently on the top of the stack. */ peekNode()75 Node peekNode() { 76 return nodes.peek(); 77 } 78 79 /* Returns the number of children on the stack in the current node 80 scope. */ nodeArity()81 int nodeArity() { 82 return sp - mk; 83 } 84 85 clearNodeScope(Node n)86 void clearNodeScope(Node n) { 87 while (sp > mk) { 88 popNode(); 89 } 90 mk = marks.pop().intValue(); 91 } 92 93 openNodeScope(Node n)94 void openNodeScope(Node n) { 95 marks.push(new Integer(mk)); 96 mk = sp; 97 n.jjtOpen(); 98 } 99 100 101 /* A definite node is constructed from a specified number of 102 children. That number of nodes are popped from the stack and 103 made the children of the definite node. Then the definite node 104 is pushed on to the stack. */ closeNodeScope(Node n, int num)105 void closeNodeScope(Node n, int num) { 106 mk = marks.pop().intValue(); 107 while (num-- > 0) { 108 Node c = popNode(); 109 c.jjtSetParent(n); 110 n.jjtAddChild(c, num); 111 } 112 n.jjtClose(); 113 pushNode(n); 114 node_created = true; 115 } 116 117 118 /* A conditional node is constructed if its condition is true. All 119 the nodes that have been pushed since the node was opened are 120 made children of the the conditional node, which is then pushed 121 on to the stack. If the condition is false the node is not 122 constructed and they are left on the stack. */ closeNodeScope(Node n, boolean condition)123 void closeNodeScope(Node n, boolean condition) { 124 if (condition) { 125 int a = nodeArity(); 126 mk = marks.pop().intValue(); 127 while (a-- > 0) { 128 Node c = popNode(); 129 c.jjtSetParent(n); 130 n.jjtAddChild(c, a); 131 } 132 n.jjtClose(); 133 pushNode(n); 134 node_created = true; 135 } else { 136 mk = marks.pop().intValue(); 137 node_created = false; 138 } 139 } 140 } 141