Class Tree

  • All Implemented Interfaces:
    AutoCloseable, Cloneable, Iterable<Node>

    public class Tree
    extends Object
    implements Iterable<Node>, Cloneable
    A Tree represents the syntax tree of an entire source code file. It contains Node instances that indicate the structure of the source code.
    Since:
    1.0.0
    Author:
    Tommy MacWilliam, Ozren Dabić
    • Method Detail

      • edit

        public void edit​(@NotNull
                         @NotNull InputEdit edit)
        Edit the syntax tree to keep it in sync with source code that has been edited.
        Parameters:
        edit - changes made to the source code in terms of both byte offsets and row/column coordinates
      • getChangedRanges

        public List<Range> getChangedRanges​(@NotNull
                                            @NotNull Tree other)
        Compare this old edited syntax tree to a new syntax tree representing the same document, returning a sequence of Range instances, their coordinates corresponding to changes made to the syntactic structure.

        For this to work correctly, this syntax tree must have been edited such that its ranges match up to the new tree. Generally, you'll want to call this method right after calling one of the Parser methods.

        Parameters:
        other - the tree to compare with
        Returns:
        a list of ranges that have been changed
        Throws:
        NullPointerException - if other is null
        Since:
        1.12.0
        See Also:
        Parser.parse(String, Tree), Parser.parse(Path, Tree)
      • getRootNode

        public Node getRootNode()
        Get the topmost Node of the syntax tree.
        Returns:
        the root node of the syntax tree
      • iterator

        @NotNull
        public @NotNull Iterator<Node> iterator()
        Returns an iterator over the entire syntax tree, starting from the root.
        Specified by:
        iterator in interface Iterable<Node>
        Returns:
        an iterator over syntax tree nodes
        See Also:
        Node.iterator()
      • clone

        public Tree clone()
        Clone this tree, creating a separate, independent instance.
        Returns:
        a clone of this instance
        Since:
        1.6.0
      • 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