View Javadoc

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