Class Scope

All Implemented Interfaces:
Comparable<AstNode>, Iterable<Node>
Direct Known Subclasses:
ArrayComprehension, GeneratorExpression, LetNode, Loop, ScriptNode

public class Scope extends Jump
Represents a scope in the lexical scope chain. Base type for all AstNode implementations that can introduce a new scope.
  • Field Details

  • Constructor Details

    • Scope

      public Scope()
    • Scope

      public Scope(int pos)
    • Scope

      public Scope(int pos, int len)
  • Method Details

    • getParentScope

      public Scope getParentScope()
    • setParentScope

      public void setParentScope(Scope parentScope)
      Sets parent scope
    • clearParentScope

      public void clearParentScope()
      Used only for code generation.
    • getChildScopes

      public List<Scope> getChildScopes()
      Return a list of the scopes whose parent is this scope.
      Returns:
      the list of scopes we enclose, or null if none
    • addChildScope

      public void addChildScope(Scope child)
      Add a scope to our list of child scopes. Sets the child's parent scope to this scope.
      Throws:
      IllegalStateException - if the child's parent scope is non-null
    • replaceWith

      public void replaceWith(Scope newScope)
      Used by the parser; not intended for typical use. Changes the parent-scope links for this scope's child scopes to the specified new scope. Copies symbols from this scope into new scope.
      Parameters:
      newScope - the scope that will replace this one on the scope stack.
    • getTop

      public ScriptNode getTop()
      Returns current script or function scope
    • setTop

      public void setTop(ScriptNode top)
      Sets top current script or function scope
    • splitScope

      public static Scope splitScope(Scope scope)
      Creates a new scope node, moving symbol table information from "scope" to the new node, and making "scope" a nested scope contained by the new node. Useful for injecting a new scope in a scope chain.
    • joinScopes

      public static void joinScopes(Scope source, Scope dest)
      Copies all symbols from source scope to dest scope.
    • getDefiningScope

      public Scope getDefiningScope(String name)
      Returns the scope in which this name is defined
      Parameters:
      name - the symbol to look up
      Returns:
      this Scope, one of its parent scopes, or null if the name is not defined any this scope chain
    • getSymbol

      public Symbol getSymbol(String name)
      Looks up a symbol in this scope.
      Parameters:
      name - the symbol name
      Returns:
      the Symbol, or null if not found
    • putSymbol

      public void putSymbol(Symbol symbol)
      Enters a symbol into this scope.
    • getSymbolTable

      public Map<String,Symbol> getSymbolTable()
      Returns the symbol table for this scope.
      Returns:
      the symbol table. May be null.
    • setSymbolTable

      public void setSymbolTable(Map<String,Symbol> table)
      Sets the symbol table for this scope. May be null.
    • getStatements

      public List<AstNode> getStatements()
      Returns a copy of the child list, with each child cast to an AstNode.
      Throws:
      ClassCastException - if any non-AstNode objects are in the child list, e.g. if this method is called after the code generator begins the tree transformation.
    • toSource

      public String toSource(int depth)
      Description copied from class: AstNode
      Emits source code for this node. Callee is responsible for calling this function recursively on children, incrementing indent as appropriate.

      Note: if the parser was in error-recovery mode, some AST nodes may have null children that are expected to be non-null when no errors are present. In this situation, the behavior of the toSource method is undefined: toSource implementations may assume that the AST node is error-free, since it is intended to be invoked only at runtime after a successful parse.

      Overrides:
      toSource in class Jump
      Parameters:
      depth - the current recursion depth, typically beginning at 0 when called on the root node.
    • visit

      public void visit(NodeVisitor v)
      Description copied from class: Jump
      Jumps are only used directly during code generation, and do not support this interface.
      Overrides:
      visit in class Jump
      Parameters:
      v - the object to call with this node and its children