Class ArrayLiteral

All Implemented Interfaces:
Comparable<AstNode>, Iterable<Node>, DestructuringForm

public class ArrayLiteral extends AstNode implements DestructuringForm
AST node for an Array literal. The elements list will always be non-null, although the list will have no elements if the Array literal is empty.

Node type is Token.ARRAYLIT.

ArrayLiteral :
        [ Elisionopt ]
        [ ElementList ]
        [ ElementList , Elisionopt ]
 ElementList :
        Elisionopt AssignmentExpression
        ElementList , Elisionopt AssignmentExpression
 Elision :
        ,
        Elision ,
  • Constructor Details

    • ArrayLiteral

      public ArrayLiteral()
    • ArrayLiteral

      public ArrayLiteral(int pos)
    • ArrayLiteral

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

    • getElements

      public List<AstNode> getElements()
      Returns the element list
      Returns:
      the element list. If there are no elements, returns an immutable empty list. Elisions are represented as EmptyExpression nodes.
    • setElements

      public void setElements(List<AstNode> elements)
      Sets the element list, and sets each element's parent to this node.
      Parameters:
      elements - the element list. Can be null.
    • addElement

      public void addElement(AstNode element)
      Adds an element to the list, and sets its parent to this node.
      Parameters:
      element - the element to add
      Throws:
      IllegalArgumentException - if element is null. To indicate an empty element, use an EmptyExpression node.
    • getSize

      public int getSize()
      Returns the number of elements in this Array literal, including empty elements.
    • getElement

      public AstNode getElement(int index)
      Returns element at specified index.
      Parameters:
      index - the index of the element to retrieve
      Returns:
      the element
      Throws:
      IndexOutOfBoundsException - if the index is invalid
    • getDestructuringLength

      public int getDestructuringLength()
      Returns destructuring length
    • setDestructuringLength

      public void setDestructuringLength(int destructuringLength)
      Sets destructuring length. This is set by the parser and used by the code generator. for ([a,] in obj) is legal, but for ([a] in obj) is not since we have both key and value supplied. The difference is only meaningful in array literals used in destructuring-assignment contexts.
    • getSkipCount

      public int getSkipCount()
      Used by code generator.
      Returns:
      the number of empty elements
    • setSkipCount

      public void setSkipCount(int count)
      Used by code generator.
      Parameters:
      count - the count of empty elements
    • setIsDestructuring

      public void setIsDestructuring(boolean destructuring)
      Marks this node as being a destructuring form - that is, appearing in a context such as for ([a, b] in ...) where it's the target of a destructuring assignment.
      Specified by:
      setIsDestructuring in interface DestructuringForm
    • isDestructuring

      public boolean isDestructuring()
      Returns true if this node is in a destructuring position: a function parameter, the target of a variable initializer, the iterator of a for..in loop, etc.
      Specified by:
      isDestructuring in interface DestructuringForm
    • 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.

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

      public void visit(NodeVisitor v)
      Visits this node, then visits its element expressions in order. Any empty elements are represented by EmptyExpression objects, so the callback will never be passed null.
      Specified by:
      visit in class AstNode
      Parameters:
      v - the object to call with this node and its children