View Javadoc

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