Next: Pattern Matching Tree-sitter Nodes, Previous: Retrieving Node, Up: Parsing Program Source [Contents][Index]
Before going further, make sure you have read the basic conventions about tree-sitter nodes in the previous node.
Every node is associated with a parser, and that parser is associated with a buffer. The following functions let you retrieve them.
This function returns node’s associated parser.
This function returns node’s parser’s associated buffer.
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.
Return the start position of node.
Return the end position of node.
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.
Checks if object is a tree-sitter syntax node.
Checks if node1 and node2 are the same node in a syntax tree.
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.
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.
Return node’s type as a string.
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).
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.
This is a more primitive function that returns the field name of the n’th child of node.
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]