KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jep > JJTParserState


1 /*****************************************************************************
2
3 JEP - Java Math Expression Parser 2.3.0
4       October 3 2004
5       (c) Copyright 2004, Nathan Funk and Richard Morris
6       See LICENSE.txt for license information.
7
8 *****************************************************************************/

9 /* Generated By:JJTree: Do not edit this line. JJTParserState.java */
10
11 package org.nfunk.jep;
12
13 class JJTParserState {
14   private java.util.Stack JavaDoc nodes;
15   private java.util.Stack JavaDoc marks;
16
17   private int sp; // number of nodes on stack
18
private int mk; // current mark
19
private boolean node_created;
20
21   JJTParserState() {
22     nodes = new java.util.Stack JavaDoc();
23     marks = new java.util.Stack JavaDoc();
24     sp = 0;
25     mk = 0;
26   }
27
28   /* Determines whether the current node was actually closed and
29      pushed. This should only be called in the final user action of a
30      node scope. */

31   boolean nodeCreated() {
32     return node_created;
33   }
34
35   /* Call this to reinitialize the node stack. It is called
36      automatically by the parser's ReInit() method. */

37   void reset() {
38     nodes.removeAllElements();
39     marks.removeAllElements();
40     sp = 0;
41     mk = 0;
42   }
43
44   /* Returns the root node of the AST. It only makes sense to call
45      this after a successful parse. */

46   Node rootNode() {
47     return (Node)nodes.elementAt(0);
48   }
49
50   /* Pushes a node on to the stack. */
51   void pushNode(Node n) {
52     nodes.push(n);
53     ++sp;
54   }
55
56   /* Returns the node on the top of the stack, and remove it from the
57      stack. */

58   Node popNode() {
59     if (--sp < mk) {
60       mk = ((Integer JavaDoc)marks.pop()).intValue();
61     }
62     return (Node)nodes.pop();
63   }
64
65   /* Returns the node currently on the top of the stack. */
66   Node peekNode() {
67     return (Node)nodes.peek();
68   }
69
70   /* Returns the number of children on the stack in the current node
71      scope. */

72   int nodeArity() {
73     return sp - mk;
74   }
75
76
77   void clearNodeScope(Node n) {
78     while (sp > mk) {
79       popNode();
80     }
81     mk = ((Integer JavaDoc)marks.pop()).intValue();
82   }
83
84
85   void openNodeScope(Node n) {
86     marks.push(new Integer JavaDoc(mk));
87     mk = sp;
88     n.jjtOpen();
89   }
90
91
92   /* A definite node is constructed from a specified number of
93      children. That number of nodes are popped from the stack and
94      made the children of the definite node. Then the definite node
95      is pushed on to the stack. */

96   void closeNodeScope(Node n, int num) {
97     mk = ((Integer JavaDoc)marks.pop()).intValue();
98     while (num-- > 0) {
99       Node c = popNode();
100       c.jjtSetParent(n);
101       n.jjtAddChild(c, num);
102     }
103     n.jjtClose();
104     pushNode(n);
105     node_created = true;
106   }
107
108
109   /* A conditional node is constructed if its condition is true. All
110      the nodes that have been pushed since the node was opened are
111      made children of the the conditional node, which is then pushed
112      on to the stack. If the condition is false the node is not
113      constructed and they are left on the stack. */

114   void closeNodeScope(Node n, boolean condition) {
115     if (condition) {
116       int a = nodeArity();
117       mk = ((Integer JavaDoc)marks.pop()).intValue();
118       while (a-- > 0) {
119     Node c = popNode();
120     c.jjtSetParent(n);
121     n.jjtAddChild(c, a);
122       }
123       n.jjtClose();
124       pushNode(n);
125       node_created = true;
126     } else {
127       mk = ((Integer JavaDoc)marks.pop()).intValue();
128       node_created = false;
129     }
130   }
131 }
132
Popular Tags