1 package org.apache.commons.ognl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27
28
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
52
53
54 public boolean nodeCreated()
55 {
56 return nodeCreated;
57 }
58
59
60
61
62 public void reset()
63 {
64 nodes.clear();
65 marks.clear();
66 numNodesOnStack = 0;
67 currentMark = 0;
68 }
69
70
71
72
73 public Node rootNode()
74 {
75 return nodes.get( 0 );
76 }
77
78
79 public void pushNode( Node node )
80 {
81 nodes.add( node );
82 ++numNodesOnStack;
83 }
84
85
86
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
98 public Node peekNode()
99 {
100 return nodes.get( nodes.size() - 1 );
101 }
102
103
104
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
129
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
147
148
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