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

        public Node getDescendantForByteRange​(int startByte,
                                              int endByte)
        Parameters:
        startByte - The starting byte of the range
        endByte - The ending byte of the range
        Returns:
        The smallest node within this node that spans the given range of bytes
        Throws:
        IllegalArgumentException - if startByte > endByte
      • 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
      • 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
      • 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