KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > parser > JJTPythonGrammarState


1 /* Generated By:JJTree: Do not edit this line. D:/jython/CVS.parser/org/python/parser\JJTPythonGrammarState.java */
2
3 // Modified by hand. The two closeNodeScope method have been rewritten
4
// completely and is used when building the AST tree bottom-up.
5

6 package org.python.parser;
7
8 class JJTPythonGrammarState {
9     private java.util.Stack JavaDoc nodes;
10     private IntStack marks;
11     private IntStack lines;
12     private IntStack columns;
13
14     private int sp; // number of nodes on stack
15
private int mk; // current mark
16
private boolean node_created;
17
18     private TreeBuilder builder;
19
20     JJTPythonGrammarState() {
21         nodes = new java.util.Stack JavaDoc();
22         marks = new IntStack();
23         lines = new IntStack();
24         columns = new IntStack();
25         sp = 0;
26         mk = 0;
27         builder = new TreeBuilder(this);
28     }
29
30     /* Determines whether the current node was actually closed and
31        pushed. This should only be called in the final user action of a
32        node scope. */

33     boolean nodeCreated() {
34         return node_created;
35     }
36
37     /* Call this to reinitialize the node stack. It is called
38        automatically by the parser's ReInit() method. */

39     void reset() {
40         nodes.removeAllElements();
41         marks.removeAllElements();
42         sp = 0;
43         mk = 0;
44     }
45
46     /* Returns the root node of the AST. It only makes sense to call
47        this after a successful parse. */

48     Node rootNode() {
49         return (Node)nodes.elementAt(0);
50     }
51
52     /* Pushes a node on to the stack. */
53     void pushNode(Node n) {
54         nodes.push(n);
55         ++sp;
56     }
57
58     /* Returns the node on the top of the stack, and remove it from the
59        stack. */

60     Node popNode() {
61         if (--sp < mk) {
62             mk = marks.pop();
63         }
64         return (Node)nodes.pop();
65     }
66
67     /* Returns the node currently on the top of the stack. */
68     Node peekNode() {
69         return (Node)nodes.peek();
70     }
71
72     /* Returns the number of children on the stack in the current node
73        scope. */

74     int nodeArity() {
75         return sp - mk;
76     }
77
78     void pushNodePos(int line, int col) {
79         lines.push(line);
80         columns.push(col);
81     }
82
83     void setNodePos() {
84         SimpleNode n = (SimpleNode) peekNode();
85         n.beginLine = lines.pop();
86         n.beginColumn = columns.pop();
87     }
88
89
90     void clearNodeScope(Node n) {
91         while (sp > mk) {
92             popNode();
93         }
94         mk = marks.pop();
95     }
96
97
98     void openNodeScope(Node n) {
99         marks.push(mk);
100         mk = sp;
101     }
102
103
104     /* A definite node is constructed from a specified number of
105        children. That number of nodes are popped from the stack and
106        made the children of the definite node. Then the definite node
107        is pushed on to the stack. */

108     void closeNodeScope(Node n, int num) throws ParseException {
109         SimpleNode sn = (SimpleNode) n;
110         mk = marks.pop();
111         SimpleNode newNode = null;
112         try {
113             newNode = builder.closeNode(sn, num);
114         } catch (ParseException exc) {
115             throw exc;
116         } catch (Exception JavaDoc exc) {
117             exc.printStackTrace();
118             throw new ParseException("Internal error:" + exc);
119         }
120         if (newNode == null) {
121             throw new ParseException("Internal AST builder error");
122         }
123         pushNode(newNode);
124         node_created = true;
125     }
126
127
128     /* A conditional node is constructed if its condition is true. All
129        the nodes that have been pushed since the node was opened are
130        made children of the the conditional node, which is then pushed
131        on to the stack. If the condition is false the node is not
132        constructed and they are left on the stack. */

133     void closeNodeScope(Node n, boolean condition) throws ParseException {
134         SimpleNode sn = (SimpleNode) n;
135         if (condition) {
136             SimpleNode newNode = null;
137             try {
138                 newNode = builder.closeNode(sn, nodeArity());
139             } catch (ParseException exc) {
140                 throw exc;
141             } catch (Exception JavaDoc exc) {
142                 exc.printStackTrace();
143                 throw new ParseException("Internal error:" + exc);
144             }
145             if (newNode == null) {
146                 throw new ParseException("Internal AST builder error");
147             }
148             mk = marks.pop();
149             pushNode(newNode);
150             node_created = true;
151         } else {
152             mk = marks.pop();
153             node_created = false;
154         }
155     }
156
157     public void dumpTop(String JavaDoc reason) {
158         int a = nodeArity();
159         System.out.println("dumpTop:" + reason);
160         System.out.println("arity:" + a);
161         for (int i = 0; i < a; i++) {
162             Node n = (Node) nodes.elementAt(nodes.size() - i-1);
163             System.out.println(" " + n);
164         }
165     }
166
167     public Node openNode(int id) {
168         return builder.openNode(id);
169     }
170
171     public void dump(String JavaDoc reason) {
172         int a = nodeArity();
173         System.out.println("dump:" + reason);
174         System.out.println(" mk:" + mk + " sp:" + sp);
175         for (int i = 0; i < nodes.size(); i++) {
176             Node n = (Node) nodes.elementAt(i);
177             System.out.println(" " + n);
178         }
179         for (int i = 0; i < marks.size(); i++) {
180             System.out.println(" " + marks.elementAt(i));
181         }
182     }
183 }
184
185
186 class IntStack {
187     int[] stack;
188     int sp = 0;
189
190     public IntStack() {
191         stack = new int[50];
192     }
193
194
195     public void removeAllElements() {
196         sp = 0;
197     }
198
199     public int size() {
200         return sp;
201     }
202
203     public int elementAt(int idx) {
204         return stack[idx];
205     }
206
207     public void push(int val) {
208         if (sp >= stack.length) {
209             int[] newstack = new int[sp*2];
210             System.arraycopy(stack, 0, newstack, 0, sp);
211             stack = newstack;
212         }
213         stack[sp++] = val;
214     }
215
216     public int pop() {
217         return stack[--sp];
218     }
219 }
220
Popular Tags