Class ForEachStmt

  • All Implemented Interfaces:
    ASTNode, Statement

    public final class ForEachStmt
    extends Object
    implements Statement
    An enhanced for loop over an iterable or array, with an optional label.
    • Constructor Detail

      • ForEachStmt

        public ForEachStmt​(VarDeclStmt variable,
                           Expression iterable,
                           Statement body,
                           String label,
                           SourceLocation location)
        Creates an enhanced for loop and parents its children to it.
        Parameters:
        variable - declaration of the loop variable
        iterable - the iterated expression
        body - the loop body
        label - loop label, or null for none
        location - source location, or null for unknown
        Throws:
        NullPointerException - if variable, iterable, or body is null
      • ForEachStmt

        public ForEachStmt​(VarDeclStmt variable,
                           Expression iterable,
                           Statement body)
        Creates an unlabeled enhanced for loop with an unknown location.
        Parameters:
        variable - declaration of the loop variable
        iterable - the iterated expression
        body - the loop body
        Throws:
        NullPointerException - if variable, iterable, or body is null
    • Method Detail

      • getVariable

        public VarDeclStmt getVariable()
        Returns:
        the variable
      • setVariable

        public void setVariable​(VarDeclStmt variable)
        Replaces the loop variable declaration, reparenting old and new nodes.
        Parameters:
        variable - the new loop variable declaration
      • getIterable

        public Expression getIterable()
        Returns:
        the iterable
      • setIterable

        public void setIterable​(Expression iterable)
        Replaces the iterated expression, reparenting old and new nodes.
        Parameters:
        iterable - the new iterated expression
      • getBody

        public Statement getBody()
        Returns:
        the loop body
      • setBody

        public void setBody​(Statement body)
        Replaces the loop body, reparenting old and new nodes.
        Parameters:
        body - the new body
      • setLabel

        public void setLabel​(String label)
        Sets the label used by labeled break and continue targeting this loop.
        Parameters:
        label - the label, or null to remove it
      • getLocation

        public SourceLocation getLocation()
        Description copied from interface: ASTNode
        Gets the source location of this node.
        Specified by:
        getLocation in interface ASTNode
        Returns:
        the location
      • getParent

        public ASTNode getParent()
        Description copied from interface: ASTNode
        Gets the parent node in the AST tree.
        Specified by:
        getParent in interface ASTNode
        Returns:
        the parent
      • setParent

        public void setParent​(ASTNode parent)
        Sets the enclosing AST node.
        Specified by:
        setParent in interface ASTNode
        Parameters:
        parent - the new parent node
      • getLabel

        public String getLabel()
        Description copied from interface: Statement
        Gets the label for this statement, if any.
        Specified by:
        getLabel in interface Statement
        Returns:
        the label, or null if not labeled
      • withVariable

        public ForEachStmt withVariable​(VarDeclStmt variable)
        Replaces the loop variable declaration, reparenting old and new nodes.
        Parameters:
        variable - the new loop variable declaration
        Returns:
        this statement
      • withIterable

        public ForEachStmt withIterable​(Expression iterable)
        Replaces the iterated expression, reparenting old and new nodes.
        Parameters:
        iterable - the new iterated expression
        Returns:
        this statement
      • withBody

        public ForEachStmt withBody​(Statement body)
        Replaces the loop body, reparenting old and new nodes.
        Parameters:
        body - the new body
        Returns:
        this statement
      • withLabel

        public ForEachStmt withLabel​(String label)
        Sets the loop label.
        Parameters:
        label - the new label, or null for none
        Returns:
        this statement
      • getChildren

        public List<ASTNode> getChildren()
        Description copied from interface: ASTNode
        Lists the direct children a node holds, in source order.
        Specified by:
        getChildren in interface ASTNode
        Returns:
        the direct children, empty for a leaf
      • accept

        public <T> T accept​(SourceVisitor<T> visitor)
        Description copied from interface: ASTNode
        Accepts a visitor for this node.
        Specified by:
        accept in interface ASTNode
        Type Parameters:
        T - the return type of the visitor
        Parameters:
        visitor - the visitor to accept
        Returns:
        the result from the visitor