KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jruby > parser > StaticScope


1 /***** BEGIN LICENSE BLOCK *****
2  * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Common Public
5  * License Version 1.0 (the "License"); you may not use this file
6  * except in compliance with the License. You may obtain a copy of
7  * the License at http://www.eclipse.org/legal/cpl-v10.html
8  *
9  * Software distributed under the License is distributed on an "AS
10  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11  * implied. See the License for the specific language governing
12  * rights and limitations under the License.
13  *
14  * Copyright (C) 2006 Thomas E Enebo <enebo@acm.org>
15  *
16  * Alternatively, the contents of this file may be used under the terms of
17  * either of the GNU General Public License Version 2 or later (the "GPL"),
18  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19  * in which case the provisions of the GPL or the LGPL are applicable instead
20  * of those above. If you wish to allow use of your version of this file only
21  * under the terms of either the GPL or the LGPL, and not to allow others to
22  * use your version of this file under the terms of the CPL, indicate your
23  * decision by deleting the provisions above and replace them with the notice
24  * and other provisions required by the GPL or the LGPL. If you do not delete
25  * the provisions above, a recipient may use your version of this file under
26  * the terms of any one of the CPL, the GPL or the LGPL.
27  ***** END LICENSE BLOCK *****/

28 package org.jruby.parser;
29
30 import java.io.Serializable JavaDoc;
31
32 import org.jruby.ast.AssignableNode;
33 import org.jruby.ast.Node;
34 import org.jruby.lexer.yacc.ISourcePosition;
35
36 public abstract class StaticScope implements Serializable JavaDoc {
37     private static final long serialVersionUID = 4843861446986961013L;
38     
39     private StaticScope enclosingScope;
40     
41     // Our name holder (offsets are assigned as variables are added
42
private String JavaDoc[] variableNames;
43     
44     protected StaticScope(StaticScope enclosingScope) {
45         this.enclosingScope = enclosingScope;
46     }
47     
48     public int addVariable(String JavaDoc name) {
49         int slot = isDefined(name);
50
51         if (slot >= 0) {
52             return slot;
53         }
54             
55         // This is perhaps innefficient timewise? Optimal spacewise
56
if (variableNames == null) {
57             variableNames = new String JavaDoc[1];
58             variableNames[0] = name;
59         } else {
60             String JavaDoc[] newVariableNames = new String JavaDoc[variableNames.length + 1];
61             System.arraycopy(variableNames, 0, newVariableNames, 0, variableNames.length);
62             variableNames = newVariableNames;
63             variableNames[variableNames.length - 1] = name;
64         }
65         
66         // Returns slot of variable
67
return variableNames.length - 1;
68     }
69     
70     public String JavaDoc[] getVariables() {
71         return variableNames;
72     }
73     
74     public int getNumberOfVariables() {
75         return variableNames == null ? 0 : variableNames.length;
76     }
77     
78     public void setVariables(String JavaDoc[] names) {
79         if (names == null) {
80             return;
81         }
82         
83         variableNames = new String JavaDoc[names.length];
84         System.arraycopy(names, 0, variableNames, 0, names.length);
85     }
86     
87     /**
88      * Next outer most scope in list of scopes. An enclosing scope may have no direct scoping
89      * relationship to its child. If I am in a localScope and then I enter something which
90      * creates another localScope the enclosing scope will be the first scope, but there are
91      * no valid scoping relationships between the two. Methods which walk the enclosing scopes
92      * are responsible for enforcing appropriate scoping relationships.
93      *
94      * @return the parent scope
95      */

96     public StaticScope getEnclosingScope() {
97         return enclosingScope;
98     }
99     
100     /**
101      * Does the variable exist?
102      *
103      * @param name of the variable to find
104      * @return index of variable or -1 if it does not exist
105      */

106     public int exists(String JavaDoc name) {
107         if (variableNames != null) {
108             for (int i = 0; i < variableNames.length; i++) {
109                 if (name == variableNames[i] || name.equals(variableNames[i])) {
110                     return i;
111                 }
112             }
113         }
114         
115         return -1;
116     }
117     
118     /**
119      * Is this name in the visible to the current scope
120      *
121      * @param name to be looked for
122      * @return a location where the left-most 16 bits of number of scopes down it is and the
123      * right-most 16 bits represents its index in that scope
124      */

125     public int isDefined(String JavaDoc name) {
126         return isDefined(name, 0);
127     }
128     
129     /**
130      * Make a DASgn or LocalAsgn node based on scope logic
131      *
132      * @param position
133      * @param name
134      * @param value
135      * @return
136      */

137     public AssignableNode assign(ISourcePosition position, String JavaDoc name, Node value) {
138         return assign(position, name, value, this, 0);
139     }
140     
141     /**
142      * Get all visible variables that we can see from this scope
143      *
144      * @return a list of all names (sans $~ and $_ which are special names)
145      */

146     public abstract String JavaDoc[] getAllNamesInScope();
147     
148     protected abstract int isDefined(String JavaDoc name, int depth);
149     protected abstract AssignableNode assign(ISourcePosition position, String JavaDoc name, Node value,
150             StaticScope topScope, int depth);
151     protected abstract Node declare(ISourcePosition position, String JavaDoc name, int depth);
152     
153     /**
154      * Make a DVar or LocalVar node based on scoping logic
155      *
156      * @param position the location that in the source that the new node will come from
157      * @param name of the variable to be created is named
158      * @return a DVarNode or LocalVarNode
159      */

160     public Node declare(ISourcePosition position, String JavaDoc name) {
161         return declare(position, name, 0);
162     }
163
164     /**
165      * Gets the Local Scope relative to the current Scope. For LocalScopes this will be itself.
166      * Blocks will contain the LocalScope it contains.
167      *
168      * @return localScope
169      */

170     public abstract StaticScope getLocalScope();
171 }
172
Popular Tags