1 28 package org.jruby.parser; 29 30 import java.io.Serializable ; 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 { 37 private static final long serialVersionUID = 4843861446986961013L; 38 39 private StaticScope enclosingScope; 40 41 private String [] variableNames; 43 44 protected StaticScope(StaticScope enclosingScope) { 45 this.enclosingScope = enclosingScope; 46 } 47 48 public int addVariable(String name) { 49 int slot = isDefined(name); 50 51 if (slot >= 0) { 52 return slot; 53 } 54 55 if (variableNames == null) { 57 variableNames = new String [1]; 58 variableNames[0] = name; 59 } else { 60 String [] newVariableNames = new String [variableNames.length + 1]; 61 System.arraycopy(variableNames, 0, newVariableNames, 0, variableNames.length); 62 variableNames = newVariableNames; 63 variableNames[variableNames.length - 1] = name; 64 } 65 66 return variableNames.length - 1; 68 } 69 70 public String [] getVariables() { 71 return variableNames; 72 } 73 74 public int getNumberOfVariables() { 75 return variableNames == null ? 0 : variableNames.length; 76 } 77 78 public void setVariables(String [] names) { 79 if (names == null) { 80 return; 81 } 82 83 variableNames = new String [names.length]; 84 System.arraycopy(names, 0, variableNames, 0, names.length); 85 } 86 87 96 public StaticScope getEnclosingScope() { 97 return enclosingScope; 98 } 99 100 106 public int exists(String 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 125 public int isDefined(String name) { 126 return isDefined(name, 0); 127 } 128 129 137 public AssignableNode assign(ISourcePosition position, String name, Node value) { 138 return assign(position, name, value, this, 0); 139 } 140 141 146 public abstract String [] getAllNamesInScope(); 147 148 protected abstract int isDefined(String name, int depth); 149 protected abstract AssignableNode assign(ISourcePosition position, String name, Node value, 150 StaticScope topScope, int depth); 151 protected abstract Node declare(ISourcePosition position, String name, int depth); 152 153 160 public Node declare(ISourcePosition position, String name) { 161 return declare(position, name, 0); 162 } 163 164 170 public abstract StaticScope getLocalScope(); 171 } 172 | Popular Tags |