Class Scope
- java.lang.Object
-
- org.python.indexer.Scope
-
public class Scope extends java.lang.ObjectSymbol table.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class and Description static classScope.Type
-
Constructor Summary
Constructors Constructor and Description Scope(Scope parent, Scope.Type type)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidaddGlobalName(java.lang.String name)Mark a name as being global (i.e.voidaddSuper(Scope sup)voidclear()Dismantles all resources allocated by this scope.Scopecopy(Scope.Type tableType)Create a copy of the symbol table but without the links to parent, supers and children.java.util.Set<java.util.Map.Entry<java.lang.String,NBinding>>entrySet()java.lang.StringextendPath(java.lang.String name)Constructs a qualified name by appendingnameto this scope's qname.java.lang.StringextendPathForParam(java.lang.String name)Generates a qname for a parameter of a function or method.ScopegetEnclosingLexicalScope()Returns the containing lexical scope (which may be this scope) for lexical name lookups.ScopegetGlobalTable()Returns the global scope (i.e.ScopegetParent()java.lang.StringgetPath()ScopegetScopeSymtab()Find the enclosing scope-defining symbol table.Scope.TypegetScopeType()java.util.List<Scope>getSupers()ScopegetSymtabOfType(Scope.Type type)Find a symbol table of a certain type in the enclosing scopes.java.util.Map<java.lang.String,NBinding>getTable()Returns an immutable view of the table.booleanisEmpty()booleanisFunctionScope()booleanisGlobalName(java.lang.String name)Returnstrueifnameappears in aglobalstatement in this scope or any enclosing scope.booleanisNameBindingPhase()Name binding occurs in a separate pass before the name resolution pass, building out the scope tree and binding names in the correct scopes.booleanisScope()Returnstrueif this is a scope in which names may be bound.java.util.Set<java.lang.String>keySet()NBindinglookup(NNode n)Specialized version for the convenience of looking upNames.NBindinglookup(java.lang.String name)Look up a name (String) in the current symbol table.NBindinglookupAttr(java.lang.String name)Look up an attribute in the local scope and superclass scopes.NBindinglookupAttr(java.lang.String name, boolean supersOnly)Look up an attribute in the type hierarchy.NBindinglookupBounded(java.lang.String name, Scope.Type typebound)Look up a name, but the search is bounded by a type and will not proceed to an outer scope when reaching a certain type of symbol table.NBindinglookupLocal(java.lang.String name)Look up a name, but only in the current scope.NBindinglookupScope(java.lang.String name)Look up a name, but bounded by a scope defining construct.NTypelookupType(java.lang.String name)Look up the scope chain for a binding namednameand if found, return its type.NTypelookupType(java.lang.String name, boolean localOnly)Look for a binding namednameand if found, return its type.NTypelookupTypeAttr(java.lang.String name)voidmerge(Scope other)Merge all records from another symbol table.java.lang.StringnewLambdaName()voidput(java.lang.String id, NBinding b)Directly assigns a binding to a name in this table.NBindingput(java.lang.String id, NNode loc, NType type, NBinding.Kind kind)Adds a definition and/or reference to the table.NBindingputAttr(java.lang.String id, NNode loc, NType type, NBinding.Kind kind)Same asput(java.lang.String, org.python.indexer.NBinding), but adds the name as an attribute of this scope.voidremove(java.lang.String id)voidsetNameBindingPhase(boolean isBindingPhase)voidsetParent(Scope parent)voidsetPath(java.lang.String path)voidsetPath(java.lang.String a, java.lang.String b)voidsetScopeType(Scope.Type type)voidsetSupers(java.util.List<Scope> supers)voidsetTable(java.util.Map<java.lang.String,NBinding> table)java.lang.StringtoShortString()java.lang.StringtoString()NBindingupdate(java.lang.String id, Def loc, NType type, NBinding.Kind kind)Adds a new binding forid.NBindingupdate(java.lang.String id, NNode loc, NType type, NBinding.Kind kind)Adds a new binding forid.java.util.Collection<NBinding>values()
-
-
-
Constructor Detail
-
Scope
public Scope(Scope parent, Scope.Type type)
-
-
Method Detail
-
setTable
public void setTable(java.util.Map<java.lang.String,NBinding> table)
-
getTable
public java.util.Map<java.lang.String,NBinding> getTable()
Returns an immutable view of the table.
-
setParent
public void setParent(Scope parent)
-
getParent
public Scope getParent()
-
addSuper
public void addSuper(Scope sup)
-
setSupers
public void setSupers(java.util.List<Scope> supers)
-
getSupers
public java.util.List<Scope> getSupers()
-
setScopeType
public void setScopeType(Scope.Type type)
-
getScopeType
public Scope.Type getScopeType()
-
isFunctionScope
public boolean isFunctionScope()
-
addGlobalName
public void addGlobalName(java.lang.String name)
Mark a name as being global (i.e. module scoped) for name-binding and name-lookup operations in this code block and any nested scopes.
-
isGlobalName
public boolean isGlobalName(java.lang.String name)
Returnstrueifnameappears in aglobalstatement in this scope or any enclosing scope.
-
put
public void put(java.lang.String id, NBinding b)Directly assigns a binding to a name in this table. Does not add a new definition or reference to the binding. This form ofputis often followed by a call toIndexer.putLocation(org.python.indexer.ast.NNode, org.python.indexer.NBinding)to create a reference to the binding. When there is no code location associated withid, or it is otherwise undesirable to create a reference, theIndexer.putLocation(org.python.indexer.ast.NNode, org.python.indexer.NBinding)call is omitted.
-
put
public NBinding put(java.lang.String id, NNode loc, NType type, NBinding.Kind kind)
Adds a definition and/or reference to the table. If there is no binding forid, creates one and gives ittypeandkind.If a binding already exists, then add either a definition or a reference at
locto the binding. By convention we consider it a definition if the type changes. If the passed type is different from the binding's current type, set the binding's type to the union of the old and new types, and add a definition. If the new type is the same, just add a reference.If the binding already exists,
kindis only updated if a definition was added and the binding's type was previously the unknown type.
-
putAttr
public NBinding putAttr(java.lang.String id, NNode loc, NType type, NBinding.Kind kind)
Same asput(java.lang.String, org.python.indexer.NBinding), but adds the name as an attribute of this scope. Looks up the superclass chain to see if the attribute exists, rather than looking in the lexical scope chain.- Returns:
- the new binding, or
nullif the current scope does not have a properly initialized path.
-
update
public NBinding update(java.lang.String id, NNode loc, NType type, NBinding.Kind kind)
Adds a new binding forid. If a binding already existed, replaces its previous definitions, if any, withloc. Sets the binding's type totype(not a union with the previous type).
-
update
public NBinding update(java.lang.String id, Def loc, NType type, NBinding.Kind kind)
Adds a new binding forid. If a binding already existed, replaces its previous definitions, if any, withloc. Sets the binding's type totype(not a union with the previous type).
-
remove
public void remove(java.lang.String id)
-
copy
public Scope copy(Scope.Type tableType)
Create a copy of the symbol table but without the links to parent, supers and children. Useful for creating instances.- Returns:
- the symbol table for use by the instance.
-
setPath
public void setPath(java.lang.String path)
-
getPath
public java.lang.String getPath()
-
setPath
public void setPath(java.lang.String a, java.lang.String b)
-
lookup
public NBinding lookup(java.lang.String name)
Look up a name (String) in the current symbol table. If not found, recurse on the parent table.
-
lookup
public NBinding lookup(NNode n)
Specialized version for the convenience of looking upNames. For all other types returnnull.
-
lookupLocal
public NBinding lookupLocal(java.lang.String name)
Look up a name, but only in the current scope.- Returns:
- the local binding for
name, ornull.
-
lookupAttr
public NBinding lookupAttr(java.lang.String name, boolean supersOnly)
Look up an attribute in the type hierarchy. Don't look at parent link, because the enclosing scope may not be a super class. The search is "depth first, left to right" as in Python's (old) multiple inheritance rule. The new MRO can be implemented, but will probably not introduce much difference.- Parameters:
supersOnly- search only in the supers' scopes, not in local table.
-
lookupAttr
public NBinding lookupAttr(java.lang.String name)
Look up an attribute in the local scope and superclass scopes.- See Also:
lookupAttr(String,boolean)
-
lookupType
public NType lookupType(java.lang.String name)
Look up the scope chain for a binding namednameand if found, return its type.
-
lookupType
public NType lookupType(java.lang.String name, boolean localOnly)
Look for a binding namednameand if found, return its type.- Parameters:
localOnly-trueto look only in the current scope; iffalse, follows the scope chain.
-
lookupTypeAttr
public NType lookupTypeAttr(java.lang.String name)
-
lookupBounded
public NBinding lookupBounded(java.lang.String name, Scope.Type typebound)
Look up a name, but the search is bounded by a type and will not proceed to an outer scope when reaching a certain type of symbol table.- Parameters:
name- the name to be looked uptypebound- the type we wish the search to be bounded at- Returns:
- a binding, or
nullif not found
-
isScope
public boolean isScope()
Returnstrueif this is a scope in which names may be bound.
-
getScopeSymtab
public Scope getScopeSymtab()
Find the enclosing scope-defining symbol table.More precisely, if a form introduces a new name in the "current scope", resolving the form needs to search up the symbol-table chain until it finds the table representing the scope to which the name should be added. Used by
NameBinderto create new name bindings in the appropriate enclosing table with the appropriate binding type.
-
lookupScope
public NBinding lookupScope(java.lang.String name)
Look up a name, but bounded by a scope defining construct. Those scopes are of type module, class, instance or function. This is used in determining the locations of a variable's definition.
-
getSymtabOfType
public Scope getSymtabOfType(Scope.Type type)
Find a symbol table of a certain type in the enclosing scopes.
-
getGlobalTable
public Scope getGlobalTable()
Returns the global scope (i.e. the module scope for the current module).
-
getEnclosingLexicalScope
public Scope getEnclosingLexicalScope()
Returns the containing lexical scope (which may be this scope) for lexical name lookups. In particular, it skips class scopes.
-
isNameBindingPhase
public boolean isNameBindingPhase()
Name binding occurs in a separate pass before the name resolution pass, building out the scope tree and binding names in the correct scopes. In this pass, the name binding and lookup rules are slightly different. This condition is transient: no scopes will be in the name-binding phase in a completed index (or module).
-
setNameBindingPhase
public void setNameBindingPhase(boolean isBindingPhase)
-
merge
public void merge(Scope other)
Merge all records from another symbol table. Used byimport from *.
-
keySet
public java.util.Set<java.lang.String> keySet()
-
values
public java.util.Collection<NBinding> values()
-
entrySet
public java.util.Set<java.util.Map.Entry<java.lang.String,NBinding>> entrySet()
-
isEmpty
public boolean isEmpty()
-
clear
public void clear()
Dismantles all resources allocated by this scope.
-
newLambdaName
public java.lang.String newLambdaName()
-
extendPathForParam
public java.lang.String extendPathForParam(java.lang.String name)
Generates a qname for a parameter of a function or method. There is not enough context forextendPath(java.lang.String)to differentiate params from locals, so callers must use this method when the name is known to be a parameter name.
-
extendPath
public java.lang.String extendPath(java.lang.String name)
Constructs a qualified name by appendingnameto this scope's qname.The indexer uses globally unique fully qualified names to address identifier definition sites. Many Python identifiers are already globally addressable using dot-separated package, class and attribute names.
Function variables and parameters are not globally addressable in the language, so the indexer uses a special path syntax for creating globally unique qualified names for them. By convention the syntax is "@" for parameters and "&" for local variables.
- Parameters:
name- a name to append to the current qname- Returns:
- the qname for
name. Does not change this scope's path.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
toShortString
public java.lang.String toShortString()
-
-
DMelt 3.0 © DataMelt by jWork.ORG