KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > runtime > parser > JJTParserState


1 /* Generated By:JJTree: Do not edit this line. JJTParserState.java */
2
3 package org.apache.velocity.runtime.parser;
4
5 import org.apache.velocity.runtime.parser.node.*;
6
7 class JJTParserState {
8   private java.util.Stack JavaDoc nodes;
9   private java.util.Stack JavaDoc marks;
10
11   private int sp; // number of nodes on stack
12
private int mk; // current mark
13
private boolean node_created;
14
15   JJTParserState() {
16     nodes = new java.util.Stack JavaDoc();
17     marks = new java.util.Stack JavaDoc();
18     sp = 0;
19     mk = 0;
20   }
21
22   /* Determines whether the current node was actually closed and
23      pushed. This should only be called in the final user action of a
24      node scope. */

25   boolean nodeCreated() {
26     return node_created;
27   }
28
29   /* Call this to reinitialize the node stack. It is called
30      automatically by the parser's ReInit() method. */

31   void reset() {
32     nodes.removeAllElements();
33     marks.removeAllElements();
34     sp = 0;
35     mk = 0;
36   }
37
38   /* Returns the root node of the AST. It only makes sense to call
39      this after a successful parse. */

40   Node rootNode() {
41     return (Node)nodes.elementAt(0);
42   }
43
44   /* Pushes a node on to the stack. */
45   void pushNode(Node n) {
46     nodes.push(n);
47     ++sp;
48   }
49
50   /* Returns the node on the top of the stack, and remove it from the
51      stack. */

52   Node popNode() {
53     if (--sp < mk) {
54       mk = ((Integer JavaDoc)marks.pop()).intValue();
55     }
56     return (Node)nodes.pop();
57   }
58
59   /* Returns the node currently on the top of the stack. */
60   Node peekNode() {
61     return (Node)nodes.peek();
62   }
63
64   /* Returns the number of children on the stack in the current node
65      scope. */

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

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

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