View Javadoc

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.Node;
6   
7   
8   class JJTParserState {
9     private java.util.Stack nodes;
10    private java.util.Stack marks;
11  
12    private int sp;		// number of nodes on stack
13    private int mk;		// current mark
14    private boolean node_created;
15  
16    JJTParserState() {
17      nodes = new java.util.Stack();
18      marks = new java.util.Stack();
19      sp = 0;
20      mk = 0;
21    }
22  
23    /* Determines whether the current node was actually closed and
24       pushed.  This should only be called in the final user action of a
25       node scope.  */
26    boolean nodeCreated() {
27      return node_created;
28    }
29  
30    /* Call this to reinitialize the node stack.  It is called
31       automatically by the parser's ReInit() method. */
32    void reset() {
33      nodes.removeAllElements();
34      marks.removeAllElements();
35      sp = 0;
36      mk = 0;
37    }
38  
39    /* Returns the root node of the AST.  It only makes sense to call
40       this after a successful parse. */
41    Node rootNode() {
42      return (Node)nodes.elementAt(0);
43    }
44  
45    /* Pushes a node on to the stack. */
46    void pushNode(Node n) {
47      nodes.push(n);
48      ++sp;
49    }
50  
51    /* Returns the node on the top of the stack, and remove it from the
52       stack.  */
53    Node popNode() {
54      if (--sp < mk) {
55        mk = ((Integer)marks.pop()).intValue();
56      }
57      return (Node)nodes.pop();
58    }
59  
60    /* Returns the node currently on the top of the stack. */
61    Node peekNode() {
62      return (Node)nodes.peek();
63    }
64  
65    /* Returns the number of children on the stack in the current node
66       scope. */
67    int nodeArity() {
68      return sp - mk;
69    }
70  
71  
72    void clearNodeScope(Node n) {
73      while (sp > mk) {
74        popNode();
75      }
76      mk = ((Integer)marks.pop()).intValue();
77    }
78  
79  
80    void openNodeScope(Node n) {
81      marks.push(new Integer(mk));
82      mk = sp;
83      n.jjtOpen();
84    }
85  
86  
87    /* A definite node is constructed from a specified number of
88       children.  That number of nodes are popped from the stack and
89       made the children of the definite node.  Then the definite node
90       is pushed on to the stack. */
91    void closeNodeScope(Node n, int num) {
92      mk = ((Integer)marks.pop()).intValue();
93      while (num-- > 0) {
94        Node c = popNode();
95        c.jjtSetParent(n);
96        n.jjtAddChild(c, num);
97      }
98      n.jjtClose();
99      pushNode(n);
100     node_created = true;
101   }
102 
103 
104   /* A conditional node is constructed if its condition is true.  All
105      the nodes that have been pushed since the node was opened are
106      made children of the the conditional node, which is then pushed
107      on to the stack.  If the condition is false the node is not
108      constructed and they are left on the stack. */
109   void closeNodeScope(Node n, boolean condition) {
110     if (condition) {
111       int a = nodeArity();
112       mk = ((Integer)marks.pop()).intValue();
113       while (a-- > 0) {
114 	Node c = popNode();
115 	c.jjtSetParent(n);
116 	n.jjtAddChild(c, a);
117       }
118       n.jjtClose();
119       pushNode(n);
120       node_created = true;
121     } else {
122       mk = ((Integer)marks.pop()).intValue();
123       node_created = false;
124     }
125   }
126 }