Next: , Previous: , Up: Parsing Program Source   [Contents][Index]


37.4 Accessing Node Information

Before going further, make sure you have read the basic conventions about tree-sitter nodes in the previous node.

Basic information

Every node is associated with a parser, and that parser is associated with a buffer. The following functions let you retrieve them.

Function: treesit-node-parser node

This function returns node’s associated parser.

Function: treesit-node-buffer node

This function returns node’s parser’s associated buffer.

Function: treesit-node-language node

This function returns node’s parser’s associated language.

Each node represents a piece of text in the buffer. Functions below finds relevant information about that text.

Function: treesit-node-start node

Return the start position of node.

Function: treesit-node-end node

Return the end position of node.

Function: treesit-node-text node &optional object

Returns the buffer text that node represents. (If node is retrieved from parsing a string, it will be text from that string.)

Here are some basic checks on tree-sitter nodes.

Function: treesit-node-p object

Checks if object is a tree-sitter syntax node.

Function: treesit-node-eq node1 node2

Checks if node1 and node2 are the same node in a syntax tree.

Property information

In general, nodes in a concrete syntax tree fall into two categories: named nodes and anonymous nodes. Whether a node is named or anonymous is determined by the language definition (see named node).

Apart from being named/anonymous, a node can have other properties. A node can be “missing”: missing nodes are inserted by the parser in order to recover from certain kinds of syntax errors, i.e., something should probably be there according to the grammar, but not there.

A node can be “extra”: extra nodes represent things like comments, which can appear anywhere in the text.

A node “has changes” if the buffer changed since when the node is retrieved, i.e., outdated.

A node “has error” if the text it spans contains a syntax error. It can be the node itself has an error, or one of its children/grandchildren... has an error.

Function: treesit-node-check node property

This function checks if node has property. property can be 'named, 'missing, 'extra, 'has-changes, or 'has-error.

Function: treesit-node-type node

Named nodes have “types” (see node type). For example, a named node can be a string_literal node, where string_literal is its type.

This function returns node’s type as a string.

Information as a child or parent

Function: treesit-node-index node &optional named

This function returns the index of node as a child node of its parent. If named is non-nil, it only count named nodes (see named node).

Function: treesit-node-field-name node

A child of a parent node could have a field name (see field name). This function returns the field name of node as a child of its parent.

Function: treesit-node-field-name-for-child node n

This function returns the field name of the n’th child of node.

Function: treesit-child-count node &optional named

This function finds the number of children of node. If named is non-nil, it only counts named child (see named node).


Next: Pattern Matching Tree-sitter Nodes, Previous: Retrieving Node, Up: Parsing Program Source   [Contents][Index]