Parser#
- class scubatrace.Parser(language: Language)#
Bases:
object
A parser for a specific programming language using tree-sitter.
- static traverse_tree(tree: Tree | Node) Generator[Node, None, None] #
Traverses the tree and yields all nodes in a depth-first manner.
- Parameters:
tree (Tree | Node) – The tree or node to traverse.
- Yields:
Node – The current node in the traversal.
- parse(code: str) Node #
Parses the given code and returns the root node of the tree-sitter ast.
- Parameters:
code (str) – The code to parse.
- Returns:
The root node of the tree-sitter AST.
- Return type:
Node
- query(target: str | Node, query_str: str) dict[str, list[Node]] #
Executes a tree-sitter query on the target and returns the captures.
- Parameters:
target (str | Node) – The target to query, either a string or a tree-sitter Node.
query_str (str) – The tree-sitter query to execute.
- Returns:
A dictionary where keys are capture names and values are lists of nodes captured by the query.
- Return type:
dict[str, list[Node]]
- query_all(target: str | Node, query_str: str) list[Node] #
Executes a tree-sitter query on the target and returns all captures sorted by their start point.
- Parameters:
target (str | Node) – The target to query, either a code string or a tree-sitter Node.
query_str (str) – The tree-sitter query to execute.
- Returns:
A list of all nodes captured by the query, sorted by their start point.
- Return type:
list[Node]
- query_by_capture_name(target: str | Node, query_str: str, capture_name: str) list[Node] #
Executes a tree-sitter query on the target and returns captures for a specific capture name.
- Parameters:
target (str | Node) – The target to query, either a code string or a tree-sitter Node.
query_str (str) – The tree-sitter query to execute.
capture_name (str) – The name of the capture to retrieve.
- Returns:
A list of nodes captured by the query for the specified capture name.
- Return type:
list[Node]
- query_oneshot(target: str | Node, query_str: str) Node | None #
Executes a tree-sitter query on the target and returns the first capture.
- Parameters:
target (str | Node) – The target to query, either a string or a tree-sitter Node.
query_str (str) – The tree-sitter query to execute.
- Returns:
The first node captured by the query, or None if no captures are found.
- Return type:
Node | None