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: tree-sitter-node-parser node

This function returns node’s associated parser.

Function: tree-sitter-node-buffer node

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

Function: tree-sitter-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: tree-sitter-node-start node

Return the start position of node.

Function: tree-sitter-node-end node

Return the end position of node.

Function: tree-sitter-node-text node &optional object

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

Here are some basic checks on tree-sitter nodes.

Function: tree-sitter-node-p object

Checks if object is a tree-sitter syntax node.

Function: tree-sitter-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. In this case, the node’s start and end position would be off and we better throw it away and retrieve a new one.

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 (grand)children has an error.

Function: tree-sitter-node-check node property

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

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

Function: tree-sitter-node-type node

Return node’s type as a string.

Information as a child or parent

Function: tree-sitter-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: tree-sitter-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: tree-sitter-node-field-name-for-child node n

This is a more primitive function that returns the field name of the n’th child of node.

Function: tree-sitter-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]