Class TreeCursor

  • All Implemented Interfaces:
    AutoCloseable, Cloneable
    Direct Known Subclasses:
    OffsetTreeCursor

    public class TreeCursor
    extends Object
    implements Cloneable
    A tree cursor is a stateful object that allows you to walk a syntax tree with maximum efficiency. It allows you to walk a syntax tree more efficiently than is possible using the Node traversal functions. It is always on a certain syntax node, and can be moved imperatively to different nodes.
    Since:
    1.0.0
    Author:
    Tommy MacWilliam, Ozren Dabić
    • Method Detail

      • getCurrentDepth

        public int getCurrentDepth()
        Get the depth of the cursor's current Node relative to the original Node that the cursor was constructed from.
        Returns:
        the relative depth of the tree cursor's current node
        Since:
        1.11.0
      • getCurrentNode

        public Node getCurrentNode()
        Get the Node that the cursor is currently pointing to.
        Returns:
        the tree cursor's current node
      • getCurrentFieldName

        public String getCurrentFieldName()
        Get the field name of the Node that the cursor is currently pointing to.
        Returns:
        the field name of the tree cursor's current node, null if the current node doesn't have a field
      • getCurrentTreeCursorNode

        public TreeCursorNode getCurrentTreeCursorNode()
        Get the TreeCursorNode representation of the Node that the cursor is currently pointing to.
        Returns:
        the tree cursor's current node
      • gotoFirstChild

        public boolean gotoFirstChild()
        Move the cursor to the first child of its current node.
        Returns:
        true if the cursor successfully moved, and false if there were no children
      • gotoFirstChild

        public boolean gotoFirstChild​(int offset)
        Move the cursor to the first child of its current node that extends beyond the given byte offset.
        Parameters:
        offset - the starting byte of the child
        Returns:
        true if the cursor successfully moved, and false if no such child was found
        Throws:
        IllegalArgumentException - if offset is negative
        ByteOffsetOutOfBoundsException - if offset is outside the current node's byte range
        Since:
        1.7.0
      • gotoFirstChild

        public boolean gotoFirstChild​(@NotNull
                                      @NotNull Point point)
        Move the cursor to the first child of its current node that extends beyond the given row-column offset.
        Parameters:
        point - the starting row-column position of the child
        Returns:
        true if the cursor successfully moved, and false if no such child was found
        Throws:
        NullPointerException - if point is null
        PointOutOfBoundsException - if point is outside the current node's positional span
        Since:
        1.7.0
      • gotoLastChild

        public boolean gotoLastChild()
        Move the cursor to the last child of its current node.

        Note that this method may be slower than gotoFirstChild() because it needs to iterate through all the children to compute the child's position.

        Returns:
        true if the cursor successfully moved, and false if there were no children
        Since:
        1.12.0
      • gotoNextSibling

        public boolean gotoNextSibling()
        Move the cursor to the next sibling of its current node.
        Returns:
        true if the cursor successfully moved, and false if there was no next sibling node
      • gotoPrevSibling

        public boolean gotoPrevSibling()
        Move the cursor to the previous sibling of its current node.

        Note that this method may be slower than gotoNextSibling() due to how node positions are stored. In the worst case, this method may need to iterate through all the previous nodes up to the destination.

        Returns:
        true if the cursor successfully moved, and false if there was no previous sibling node
        Since:
        1.12.0
      • gotoParent

        public boolean gotoParent()
        Move the cursor to the parent of its current node.
        Returns:
        true if the cursor successfully moved, and false if there was no parent node (the cursor was already on the root node)
      • gotoNode

        public boolean gotoNode​(@NotNull
                                @NotNull Node node)
        Move the cursor to an arbitrary node.
        Parameters:
        node - target node to move the cursor to.
        Returns:
        true if the cursor successfully moved, and false if it was already at the specified node
        Throws:
        NullPointerException - if node is null
        IllegalArgumentException - if node is not present in the tree
        Since:
        1.9.0
      • preorderTraversal

        public void preorderTraversal​(@NotNull
                                      @NotNull Consumer<Node> callback)
        Iteratively traverse over the parse tree, applying a callback to the nodes before they are visited.
        Parameters:
        callback - The callback consumer which will execute upon visiting a node
        Throws:
        NullPointerException - if callback is null
      • reset

        public boolean reset​(@NotNull
                             @NotNull TreeCursor other)
        Reset the cursor to the same state as another cursor.
        Parameters:
        other - the cursor to copy state from
        Returns:
        true if the cursor successfully moved, and false if it was already located at the same node
        Throws:
        NullPointerException - if other is null
        IllegalArgumentException - if the other cursor is not from the same tree
        Since:
        1.11.0
      • clone

        public TreeCursor clone()
        Clone this cursor, creating a separate, independent instance.
        Returns:
        a clone of this instance
        Since:
        1.5.1
      • isNull

        public final boolean isNull()
        Checks whether the memory address associated with this external resource is nullptr.
        Returns:
        true if the memory address is 0, otherwise false
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class Object
      • close

        public void close()
        Delete the external resource, freeing all the memory that it used.
        Specified by:
        close in interface AutoCloseable