Class Node

  • All Implemented Interfaces:
    Iterable<Node>

    public class Node
    extends Object
    implements Iterable<Node>
    A Node represents a single node in the 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ć
    • Method Detail

      • getChild

        public Node getChild​(int child)
        Get the node's child at the given index, where zero represents the first child.
        Parameters:
        child - The zero-indexed child position
        Returns:
        The Node's child at the given index
        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)
        Parameters:
        name - The child field name
        Returns:
        The node's child with the given field name
        Throws:
        NullPointerException - if the field name is null
      • getChildCount

        public int getChildCount()
        Returns:
        The node's number of children
      • getChildren

        public List<Node> getChildren()
        Returns:
        A list of the node's children
      • getContent

        public String getContent()
        Returns:
        The source code content encapsulated by this node
        Since:
        1.5.0
      • getDescendantForByteRange

        @Deprecated(since="1.6.0",
                    forRemoval=true)
        public Node getDescendantForByteRange​(int startByte,
                                              int endByte)
        Deprecated, for removal: This API element is subject to removal in a future version.
        Returns:
        The smallest node within this node that spans the given range of bytes
      • 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:
        IndexOutOfBoundsException - 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 of the arguments is outside of this node's position range
        • startPoint is a position that comes after endPoint
        Since:
        1.6.0
      • getEndByte

        public int getEndByte()
        Returns:
        The node's end byte
      • getEndPoint

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

        public String getFieldNameForChild​(int child)
        Parameters:
        child - The zero-indexed child position
        Returns:
        The field name for node's child at the given index, with zero representing the first child, null if no field is found
        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)
        Parameters:
        offset - The offset in bytes
        Returns:
        The node's first child that extends beyond the given byte offset
        Throws:
        IndexOutOfBoundsException - if the byte offset is outside the node's byte range
      • getFirstNamedChildForByte

        public Node getFirstNamedChildForByte​(int offset)
        Parameters:
        offset - The offset in bytes
        Returns:
        The node's first named child that extends beyond the given byte offset
        Throws:
        IndexOutOfBoundsException - if the byte offset is outside the node's byte range
      • 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:
        IndexOutOfBoundsException - 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 of the arguments is outside of this node's position range
        • startPoint is a position that comes after endPoint
        Since:
        1.6.0
      • getNodeString

        @Deprecated(since="1.4.0",
                    forRemoval=true)
        public String getNodeString()
        Deprecated, for removal: This API element is subject to removal in a future version.
        This operation is potentially unsafe for large trees
        Returns:
        An S-expression representing the node as a string
        See Also:
        SymbolicExpressionPrinter
      • getNextNamedSibling

        public Node getNextNamedSibling()
        Returns:
        The node's next named sibling
      • getNextSibling

        public Node getNextSibling()
        Returns:
        The node's next sibling
      • getPrevNamedSibling

        public Node getPrevNamedSibling()
        Returns:
        The node's previous named sibling
      • getPrevSibling

        public Node getPrevSibling()
        Returns:
        The node's previous sibling
      • getParent

        public Node getParent()
        Returns:
        The node's immediate parent
      • getRange

        public Range getRange()
        Returns:
        The node's range, indicating its byte and file position span
      • getStartByte

        public int getStartByte()
        Returns:
        The node's start byte
      • getStartPoint

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

        public Symbol getSymbol()
        Returns:
        The node's symbol
        Since:
        1.6.0
      • getType

        public String getType()
        Returns:
        The node's type as a string
      • hasError

        public boolean hasError()
        Returns:
        true if the node is a syntax error or contains any syntax errors, false otherwise
      • isExtra

        public boolean isExtra()
        Check if the node is 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. Missing 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()
        Returns:
        A new tree cursor starting from the given node
      • walk

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

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

        @NotNull
        public @NotNull Iterator<Node> iterator()
        Specified by:
        iterator in interface Iterable<Node>
        Returns:
        An iterator over the node subtree, starting from the current node