KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > annotation > factory > ast > JJTAnnotationParserState


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22
23 package org.jboss.annotation.factory.ast;
24
25 class JJTAnnotationParserState {
26   private java.util.Stack JavaDoc nodes;
27   private java.util.Stack JavaDoc marks;
28
29   private int sp; // number of nodes on stack
30
private int mk; // current mark
31
private boolean node_created;
32
33   JJTAnnotationParserState() {
34     nodes = new java.util.Stack JavaDoc();
35     marks = new java.util.Stack JavaDoc();
36     sp = 0;
37     mk = 0;
38   }
39
40   /* Determines whether the current node was actually closed and
41      pushed. This should only be called in the final user action of a
42      node scope. */

43   boolean nodeCreated() {
44     return node_created;
45   }
46
47   /* Call this to reinitialize the node stack. It is called
48      automatically by the parser's ReInit() method. */

49   void reset() {
50     nodes.removeAllElements();
51     marks.removeAllElements();
52     sp = 0;
53     mk = 0;
54   }
55
56   /* Returns the root node of the AST. It only makes sense to call
57      this after a successful parse. */

58   Node rootNode() {
59     return (Node)nodes.elementAt(0);
60   }
61
62   /* Pushes a node on to the stack. */
63   void pushNode(Node n) {
64     nodes.push(n);
65     ++sp;
66   }
67
68   /* Returns the node on the top of the stack, and remove it from the
69      stack. */

70   Node popNode() {
71     if (--sp < mk) {
72       mk = ((Integer JavaDoc)marks.pop()).intValue();
73     }
74     return (Node)nodes.pop();
75   }
76
77   /* Returns the node currently on the top of the stack. */
78   Node peekNode() {
79     return (Node)nodes.peek();
80   }
81
82   /* Returns the number of children on the stack in the current node
83      scope. */

84   int nodeArity() {
85     return sp - mk;
86   }
87
88
89   void clearNodeScope(Node n) {
90     while (sp > mk) {
91       popNode();
92     }
93     mk = ((Integer JavaDoc)marks.pop()).intValue();
94   }
95
96
97   void openNodeScope(Node n) {
98     marks.push(new Integer JavaDoc(mk));
99     mk = sp;
100     n.jjtOpen();
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) {
109     mk = ((Integer JavaDoc)marks.pop()).intValue();
110     while (num-- > 0) {
111       Node c = popNode();
112       c.jjtSetParent(n);
113       n.jjtAddChild(c, num);
114     }
115     n.jjtClose();
116     pushNode(n);
117     node_created = true;
118   }
119
120
121   /* A conditional node is constructed if its condition is true. All
122      the nodes that have been pushed since the node was opened are
123      made children of the the conditional node, which is then pushed
124      on to the stack. If the condition is false the node is not
125      constructed and they are left on the stack. */

126   void closeNodeScope(Node n, boolean condition) {
127     if (condition) {
128       int a = nodeArity();
129       mk = ((Integer JavaDoc)marks.pop()).intValue();
130       while (a-- > 0) {
131     Node c = popNode();
132     c.jjtSetParent(n);
133     n.jjtAddChild(c, a);
134       }
135       n.jjtClose();
136       pushNode(n);
137       node_created = true;
138     } else {
139       mk = ((Integer JavaDoc)marks.pop()).intValue();
140       node_created = false;
141     }
142   }
143 }
144
Popular Tags