Class Node

  • All Implemented Interfaces:
    Iterable<Node>

    public class Node
    extends Object
    implements Iterable<Node>
    A single node within a syntax Tree. It tracks its start and end positions in the source code, as well as its relation to other nodes like its parent, siblings and children.
    Since:
    1.0.0
    Author:
    Tommy MacWilliam, Ozren Dabić
    • Constructor Detail

      • Node

        public Node()
    • Method Detail

      • getChild

        public Node getChild​(int child)
        Get the child Node at the given index.
        Parameters:
        child - the zero-indexed child position
        Returns:
        the child
        Throws:
        IndexOutOfBoundsException - if the index is a negative number or if it is greater or equal to the total number of children
      • getChildByFieldName

        public Node getChildByFieldName​(@NotNull
                                        @NotNull String name)
        Get the child Node residing in a given named field.
        Parameters:
        name - the child field name
        Returns:
        the node's child with the given field name
        Throws:
        NullPointerException - if name is null
      • getChildCount

        public int getChildCount()
        Get the number of children associated with this node.
        Returns:
        the count of this node's children
      • getChildren

        public List<Node> getChildren()
        Get an ordered list of this node's children.
        Returns:
        this node's children
      • getContent

        public String getContent()
        Get the source code content encapsulated by this node.
        Returns:
        the node's source code
        Since:
        1.5.0
      • getDescendant

        public Node getDescendant​(int startByte,
                                  int endByte)
        Get the smallest node within this node that spans the given range of bytes.
        Parameters:
        startByte - the start byte of the range
        endByte - the end byte of the range
        Returns:
        a descendant node
        Throws:
        ByteOffsetOutOfBoundsException - if either argument is outside of this node's byte range
        IllegalArgumentException - if:
        • startByte < 0
        • endByte < 0
        • startByte > endByte
        Since:
        1.6.0
      • getDescendant

        public Node getDescendant​(@NotNull
                                  @NotNull Point startPoint,
                                  @NotNull
                                  @NotNull Point endPoint)
        Get the smallest node within this node that spans the given range of points.
        Parameters:
        startPoint - the start point of the range
        endPoint - the end point of the range
        Returns:
        a descendant node
        Throws:
        NullPointerException - if either argument is null
        IllegalArgumentException - if any point coordinates are negative, or if startPoint is a position that comes after endPoint
        PointOutOfBoundsException - if any of the arguments is outside of this node's position range
        Since:
        1.6.0
      • getDescendantCount

        public int getDescendantCount()
        Get the node's number of descendants, including one for the node itself.
        Returns:
        the count of this node's descendants
        Since:
        1.11.0
      • getEndByte

        public int getEndByte()
        Get the byte offset where this node ends.
        Returns:
        the node's end byte
      • getEndPoint

        public Point getEndPoint()
        Get the node's end position in terms of rows and columns.
        Returns:
        the node's end position
      • getFieldNameForChild

        public String getFieldNameForChild​(int child)
        Get the field name of a child Node residing at a given index.
        Parameters:
        child - the zero-indexed child position
        Returns:
        the child field name, null if child does not reside in a field
        Throws:
        IndexOutOfBoundsException - if the index is a negative number or if it is greater or equal to the total number of children
      • getFirstChildForByte

        public Node getFirstChildForByte​(int offset)
        Get the first child Node that extends beyond the given byte offset.
        Parameters:
        offset - the offset in bytes
        Returns:
        the child
        Throws:
        ByteOffsetOutOfBoundsException - if the byte offset is outside the node's byte range
      • getFirstNamedChildForByte

        public Node getFirstNamedChildForByte​(int offset)
        Get the first named child Node that extends beyond the given byte offset.
        Parameters:
        offset - the offset in bytes
        Returns:
        the child
        Throws:
        ByteOffsetOutOfBoundsException - if the byte offset is outside the node's byte range
      • getLanguage

        public Language getLanguage()
        Get the Language that was used to parse this node’s syntax tree.
        Returns:
        the node's language
        Since:
        1.9.0
      • getNamedChild

        public Node getNamedChild​(int child)
        Get the named child Node at the given index.
        Parameters:
        child - the zero-indexed child position
        Returns:
        the named child
        Throws:
        IndexOutOfBoundsException - if the index is a negative number or if it is greater or equal to the total number of named children
        Since:
        1.9.0
      • getNamedChildCount

        public int getNamedChildCount()
        Get the number of named children associated with this node.
        Returns:
        the count of this node's named children
        Since:
        1.9.0
      • getNamedChildren

        public List<Node> getNamedChildren()
        Get an ordered list of this node's named children.
        Returns:
        this node's named children
        Since:
        1.9.0
      • getNamedDescendant

        public Node getNamedDescendant​(int startByte,
                                       int endByte)
        Get the smallest named node within this node that spans the given range of bytes.
        Parameters:
        startByte - the start byte of the range
        endByte - the end byte of the range
        Returns:
        a named descendant node
        Throws:
        ByteOffsetOutOfBoundsException - if either argument is outside of this node's byte range
        IllegalArgumentException - if:
        • startByte < 0
        • endByte < 0
        • startByte > endByte
        Since:
        1.6.0
      • getNamedDescendant

        public Node getNamedDescendant​(@NotNull
                                       @NotNull Point startPoint,
                                       @NotNull
                                       @NotNull Point endPoint)
        Get the smallest named node within this node that spans the given range of points.
        Parameters:
        startPoint - the start point of the range
        endPoint - the end point of the range
        Returns:
        a named descendant node
        Throws:
        NullPointerException - if either argument is null
        IllegalArgumentException - if any point coordinates are negative, or if startPoint is a position that comes after endPoint
        PointOutOfBoundsException - if any of the arguments is outside of this node's position range
        Since:
        1.6.0
      • getNextNamedSibling

        public Node getNextNamedSibling()
        Get the next named sibling Node.
        Returns:
        the next named sibling, null if there is none
      • getNextParseState

        public int getNextParseState()
        Get the parse state after this Node.
        Returns:
        the next parse state
        Since:
        1.11.0
      • getNextSibling

        public Node getNextSibling()
        Get the next sibling Node.
        Returns:
        the next sibling, null if there is none
      • getPrevNamedSibling

        public Node getPrevNamedSibling()
        Get the previous named sibling Node.
        Returns:
        the previous named sibling, null if there is none
      • getPrevSibling

        public Node getPrevSibling()
        Get the previous sibling Node.
        Returns:
        the previous sibling, null if there is none
      • getParent

        public Node getParent()
        Get the parent Node.
        Returns:
        the parent, null if there is none
      • getParseState

        public int getParseState()
        Get the parse state of this Node.
        Returns:
        the parse state
        Since:
        1.11.0
      • getRange

        public Range getRange()
        Get the node's Range, indicating its byte and row-column position span.
        Returns:
        the node's range
      • getStartByte

        public int getStartByte()
        Get the byte offset where this node starts.
        Returns:
        the node's start byte
      • getStartPoint

        public Point getStartPoint()
        Get the node's start position in terms of rows and columns.
        Returns:
        the node's start position
      • getSymbol

        public Symbol getSymbol()
        Get the syntax tree Symbol associated with this node.
        Returns:
        the node's symbol
        Since:
        1.6.0
      • getType

        public String getType()
        Get the node's type as a string.
        Returns:
        the node's type
      • hasChanges

        public boolean hasChanges()
        Check if this node, or any of its children, has been edited.
        Returns:
        true if the node has been edited, or contains one such child in its subtree, false otherwise
        Since:
        1.11.0
      • hasError

        public boolean hasError()
        Check if this node represents a syntax error or contains any syntax errors anywhere within it. Syntax errors represent parts of the code that could not be incorporated into a valid syntax tree.
        Returns:
        true if the node is an ERROR, or contains one such child in its subtree, false otherwise
      • isError

        public boolean isError()
        Check if this node represents a syntax error. Syntax errors represent parts of the code that could not be incorporated into a valid syntax tree.
        Returns:
        true if the node is an ERROR, false otherwise
        Since:
        1.11.0
      • isExtra

        public boolean isExtra()
        Check if the node is an extra. Extra nodes represent things like comments, which are not required by the grammar, but can appear anywhere.
        Returns:
        true if the node is an extra, false otherwise
      • isMissing

        public boolean isMissing()
        Check if the node is missing. These nodes are inserted by the parser in order to recover from certain kinds of syntax errors.
        Returns:
        true if the node is MISSING, false otherwise
      • isNamed

        public boolean isNamed()
        Check if the node is named. Named nodes correspond to named rules in the grammar, whereas anonymous nodes correspond to string literals in the grammar.
        Returns:
        true if the node is named, false otherwise
      • isNull

        public boolean isNull()
        Check if the node is null node.
        Returns:
        true if id == 0, false otherwise
      • walk

        public TreeCursor walk()
        Create a new TreeCursor starting from this node.
        Returns:
        a tree cursor
      • walk

        public QueryCursor walk​(@NotNull
                                @NotNull Query query)
        Create a new QueryCursor starting from this node.
        Parameters:
        query - the query to run against this node's subtree
        Returns:
        a query cursor
        Since:
        1.5.0
      • hashCode

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

        @NotNull
        public @NotNull Iterator<Node> iterator()
        Create an iterator over the current node's subtree. The subtree is traversed in a depth-first manner. First iterator element is always the current node.
        Specified by:
        iterator in interface Iterable<Node>
        Returns:
        the subtree iterator