org.jrubyparser
Class StaticScope

java.lang.Object
  extended by org.jrubyparser.StaticScope
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
BlockStaticScope, LocalStaticScope

public abstract class StaticScope
extends Object
implements Serializable

StaticScope represents lexical scoping of variables and module/class constants. At a very high level every scopes enclosing scope contains variables in the next outer lexical layer. The enclosing scopes variables may or may not be reachable depending on the scoping rules for variables (governed by BlockStaticScope and LocalStaticScope). StaticScope also keeps track of current module/class that is in scope. previousCRefScope will point to the previous scope of the enclosing module/class (cref).

See Also:
Serialized Form

Field Summary
protected  StaticScope enclosingScope
           
 
Constructor Summary
protected StaticScope(StaticScope enclosingScope, String[] names)
           
 
Method Summary
 int addVariable(String name)
           
 int addVariableThisScope(String name)
          Add a new variable to this (current) scope unless it is already defined in the current scope.
 AssignableNode assign(SourcePosition position, String name, Node value)
          Make a DASgn or LocalAsgn node based on scope logic
protected abstract  AssignableNode assign(SourcePosition position, String name, Node value, StaticScope topScope, int depth)
           
 void capture(int index)
           
 Node declare(SourcePosition position, String name)
          Make a DVar or LocalVar node based on scoping logic
protected abstract  Node declare(SourcePosition position, String name, int depth)
           
 int exists(String name)
          Does the variable exist?
abstract  String[] getAllNamesInScope()
          Get all visible variables that we can see from this scope that have been assigned (e.g.
 StaticScope getEnclosingScope()
          Next outer most scope in list of scopes.
abstract  StaticScope getLocalScope()
          Gets the Local Scope relative to the current Scope.
 int getNumberOfVariables()
           
 StaticScope getPreviousCRefScope()
           
 String[] getVariables()
           
 boolean isArgumentScope()
           
 boolean isCaptured(int index)
           
 int isDefined(String name)
          Is this name in the visible to the current scope
protected abstract  int isDefined(String name, int depth)
           
 void setArgumentScope(boolean isArgumentScope)
           
 void setVariables(String[] names)
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

enclosingScope

protected final StaticScope enclosingScope
Constructor Detail

StaticScope

protected StaticScope(StaticScope enclosingScope,
                      String[] names)
Method Detail

addVariableThisScope

public int addVariableThisScope(String name)
Add a new variable to this (current) scope unless it is already defined in the current scope.

Parameters:
name - of new variable
Returns:
index+depth merged location of scope

addVariable

public int addVariable(String name)

getVariables

public String[] getVariables()

getNumberOfVariables

public int getNumberOfVariables()

setVariables

public void setVariables(String[] names)

getEnclosingScope

public StaticScope getEnclosingScope()
Next outer most scope in list of scopes. An enclosing scope may have no direct scoping relationship to its child. If I am in a localScope and then I enter something which creates another localScope the enclosing scope will be the first scope, but there are no valid scoping relationships between the two. Methods which walk the enclosing scopes are responsible for enforcing appropriate scoping relationships.

Returns:
the parent scope

exists

public int exists(String name)
Does the variable exist?

Parameters:
name - of the variable to find
Returns:
index of variable or -1 if it does not exist

isDefined

public int isDefined(String name)
Is this name in the visible to the current scope

Parameters:
name - to be looked for
Returns:
a location where the left-most 16 bits of number of scopes down it is and the right-most 16 bits represents its index in that scope

assign

public AssignableNode assign(SourcePosition position,
                             String name,
                             Node value)
Make a DASgn or LocalAsgn node based on scope logic

Parameters:
position -
name -
value -
Returns:

getAllNamesInScope

public abstract String[] getAllNamesInScope()
Get all visible variables that we can see from this scope that have been assigned (e.g. seen so far)

Returns:
a list of all names (sans $~ and $_ which are special names)

isDefined

protected abstract int isDefined(String name,
                                 int depth)

assign

protected abstract AssignableNode assign(SourcePosition position,
                                         String name,
                                         Node value,
                                         StaticScope topScope,
                                         int depth)

declare

protected abstract Node declare(SourcePosition position,
                                String name,
                                int depth)

declare

public Node declare(SourcePosition position,
                    String name)
Make a DVar or LocalVar node based on scoping logic

Parameters:
position - the location that in the source that the new node will come from
name - of the variable to be created is named
Returns:
a DVarNode or LocalVarNode

capture

public void capture(int index)

isCaptured

public boolean isCaptured(int index)

getLocalScope

public abstract StaticScope getLocalScope()
Gets the Local Scope relative to the current Scope. For LocalScopes this will be itself. Blocks will contain the LocalScope it contains.

Returns:
localScope

getPreviousCRefScope

public StaticScope getPreviousCRefScope()

isArgumentScope

public boolean isArgumentScope()

setArgumentScope

public void setArgumentScope(boolean isArgumentScope)

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2013. All Rights Reserved.