Package ch.usi.si.seart.treesitter
Class TreeCursor
- java.lang.Object
-
- ch.usi.si.seart.treesitter.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 theNodetraversal 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 Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description TreeCursorclone()Clone this cursor, creating a separate, independent instance.voidclose()Delete the external resource, freeing all the memory that it used.booleanequals(Object obj)intgetCurrentDepth()StringgetCurrentFieldName()Get the field name of theNodethat the cursor is currently pointing to.NodegetCurrentNode()Get theNodethat the cursor is currently pointing to.TreeCursorNodegetCurrentTreeCursorNode()Get theTreeCursorNoderepresentation of theNodethat the cursor is currently pointing to.booleangotoFirstChild()Move the cursor to the first child of its current node.booleangotoFirstChild(int offset)Move the cursor to the first child of its current node that extends beyond the given byte offset.booleangotoFirstChild(@NotNull Point point)Move the cursor to the first child of its current node that extends beyond the given row-column offset.booleangotoLastChild()Move the cursor to the last child of its current node.booleangotoNextSibling()Move the cursor to the next sibling of its current node.booleangotoNode(@NotNull Node node)Move the cursor to an arbitrary node.booleangotoParent()Move the cursor to the parent of its current node.booleangotoPrevSibling()Move the cursor to the previous sibling of its current node.inthashCode()booleanisNull()Checks whether the memory address associated with this external resource isnullptr.voidpreorderTraversal(@NotNull Consumer<Node> callback)Iteratively traverse over the parse tree, applying a callback to the nodes before they are visited.booleanreset(@NotNull TreeCursor other)Reset the cursor to the same state as another cursor.
-
-
-
Method Detail
-
getCurrentDepth
public int getCurrentDepth()
Get the depth of the cursor's currentNoderelative to the originalNodethat 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 theNodethat the cursor is currently pointing to.- Returns:
- the tree cursor's current node
-
getCurrentFieldName
public String getCurrentFieldName()
Get the field name of theNodethat the cursor is currently pointing to.- Returns:
- the field name of the tree cursor's current node,
nullif the current node doesn't have a field
-
getCurrentTreeCursorNode
public TreeCursorNode getCurrentTreeCursorNode()
Get theTreeCursorNoderepresentation of theNodethat 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- ifoffsetis negativeByteOffsetOutOfBoundsException- ifoffsetis 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- ifpointis nullPointOutOfBoundsException- ifpointis 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- ifnodeis nullIllegalArgumentException- ifnodeis 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- ifcallbackis 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- ifotheris nullIllegalArgumentException- 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 isnullptr.- Returns:
trueif the memory address is 0, otherwisefalse
-
close
public void close()
Delete the external resource, freeing all the memory that it used.- Specified by:
closein interfaceAutoCloseable
-
-