Class LexicalContext

java.lang.Object
com.oracle.js.parser.ir.LexicalContext

public class LexicalContext extends Object
A class that tracks the current lexical context of node visitation as a stack of Block nodes. Has special methods to retrieve useful subsets of the context. This is implemented with a primitive array and a stack pointer, because it really makes a difference performance-wise. None of the collection classes were optimal.
  • Constructor Details

    • LexicalContext

      public LexicalContext()
      Creates a new empty lexical context.
  • Method Details

    • getAllNodes

      public Iterator<LexicalContextNode> getAllNodes()
      Returns:
      all nodes in the LexicalContext.
    • push

      public <T extends LexicalContextNode> T push(T node)
      Pushes a new block on top of the context, making it the innermost open block.
      Type Parameters:
      T - the type of the new node
      Parameters:
      node - the new node
      Returns:
      the node that was pushed
    • isEmpty

      public boolean isEmpty()
      Is the context empty?
      Returns:
      true if empty
    • pop

      public <T extends LexicalContextNode> T pop(T node)
      Pops the innermost block off the context and all nodes that has been contributed since it was put there.
      Type Parameters:
      T - the type of the node to be popped
      Parameters:
      node - the node expected to be popped, used to detect unbalanced pushes/pops
      Returns:
      the node that was popped
    • contains

      public boolean contains(LexicalContextNode node)
      Check if a node is in the lexical context.
      Parameters:
      node - node to check for
      Returns:
      true if in the context
    • replace

      public LexicalContextNode replace(LexicalContextNode oldNode, LexicalContextNode newNode)
      Replace a node on the lexical context with a new one. Normally you should try to engineer IR traversals so this isn't needed
      Parameters:
      oldNode - old node
      newNode - new node
      Returns:
      the new node
    • getBlocks

      public Iterator<Block> getBlocks()
      Returns an iterator over all blocks in the context, with the top block (innermost lexical context) first.
      Returns:
      an iterator over all blocks in the context.
    • getFunctions

      public Iterator<FunctionNode> getFunctions()
      Returns an iterator over all functions in the context, with the top (innermost open) function first.
      Returns:
      an iterator over all functions in the context.
    • getCurrentBlock

      public Block getCurrentBlock()
      Returns:
      the innermost block in the context.
    • getCurrentFunction

      public FunctionNode getCurrentFunction()
      Returns:
      the innermost function in the context.
    • getCurrentNonArrowFunction

      public FunctionNode getCurrentNonArrowFunction()
    • getCurrentScope

      public Scope getCurrentScope()
      Returns the innermost scope in the context.
    • getCurrentClass

      public ClassNode getCurrentClass()
      Returns:
      the innermost class in the context.
    • inModule

      public boolean inModule()
    • copy

      public LexicalContext copy()
    • toString

      public String toString()
      Overrides:
      toString in class Object