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)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.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.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.
-
-
-
Method Detail
-
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
-
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
-
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
-
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
-
-