KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > query > util > scope > SymbolTable


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * SymbolTable.java
26  *
27  * Created on March 8, 2000
28  */

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.query.util.scope;
31
32 import java.util.Hashtable JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Stack JavaDoc;
35
36 /**
37  * The symbol table handling declared identifies.
38  *
39  * @author Michael Bouschen
40  * @version 0.1
41  */

42 public class SymbolTable
43 {
44     /**
45      * The actual scope level.
46      */

47     protected int actualScope = 0;
48     
49     /**
50      * Stack of old definitions.
51      */

52     protected Stack JavaDoc nestings = new Stack JavaDoc();
53     
54     /**
55      * The table of declared identifier (symbols).
56      */

57     protected Hashtable JavaDoc symbols = new Hashtable JavaDoc();
58
59     /**
60      * Opens a new scope.
61      * Prepare everything to handle old definitions when
62      * a identifier declaration is hidden.
63      */

64     public void enterScope()
65     {
66         actualScope++;
67         nestings.push(new Nesting());
68     }
69
70     /**
71      * Closes the actual scope.
72      * Hidden definitions are reinstalled.
73      */

74     public void leaveScope()
75     {
76         forgetNesting((Nesting)nestings.pop());
77         actualScope--;
78     }
79
80     /**
81      * Returns the level of the actual scope.
82      * @return actual scope level.
83      */

84     public int getActualScope()
85     {
86         return actualScope;
87     }
88     
89     /**
90      * Add identifier to the actual scope.
91      * If the identifier was already declared in the actual
92      * scope the symbol table is NOT changed and the old definition
93      * is returned. Otherwise a possible definition of a lower
94      * level scope is saved in the actual nesting and the new definition
95      * is stored in the symbol table. This allows to reinstall the old
96      * definition when the sctaul scope is closed.
97      * @param ident identifier to be declared
98      * @param definition new definition of identifier
99      * @return the old definition if the identifier was already declared
100      * in the actual scope; null otherwise
101      */

102     public Definition declare(String JavaDoc ident, Definition def)
103     {
104         Definition old = (Definition)symbols.get(ident);
105         def.setScope(actualScope);
106         if ((old == null) || (old.getScope() < actualScope))
107         {
108             Nesting nest = (Nesting)nestings.peek();
109             nest.add(ident, old); // save old definition in nesting
110
symbols.put(ident, def); // install new definition as actual definition
111
return null;
112         }
113         else
114         {
115             return old;
116         }
117     }
118
119     /**
120      * Checks whether the specified identifier is declared.
121      * @param ident the name of identifier to be tested
122      * @return true if the identifier is declared;
123      * false otherwise.
124      */

125     public boolean isDeclared(String JavaDoc ident)
126     {
127         return (getDefinition(ident) != null);
128     }
129
130     /**
131      * Checks the symbol table for the actual definition
132      * of the specified identifier. If the identifier is
133      * declared the definition is returned, otherwise null.
134      * @param ident the name of identifier
135      * @return the actual definition of ident is declared;
136      * null otherise.
137      */

138     public Definition getDefinition(String JavaDoc ident)
139     {
140         return (Definition)symbols.get(ident);
141     }
142     
143     /**
144      * Internal method to reinstall the old definitions.
145      * The method is called when a scope is closed.
146      * For all identifier that were declared in the
147      * closed scope their former definition (that was hidden)
148      * is reinstalled.
149      * @param nesting list of hidden definitions
150      */

151     protected void forgetNesting(Nesting nesting)
152     {
153         String JavaDoc ident = null;
154         Definition hidden = null;
155
156         Iterator JavaDoc idents = nesting.getIdents();
157         Iterator JavaDoc hiddenDefs = nesting.getHiddenDefinitions();
158
159         while (idents.hasNext())
160         {
161             ident = (String JavaDoc) idents.next();
162             hidden = (Definition) hiddenDefs.next();
163             if (hidden == null)
164             {
165                 symbols.remove(ident);
166             }
167             else
168             {
169                 symbols.put(ident, hidden);
170             }
171         }
172     }
173 }
174
Popular Tags