Skip to main content
The Language struct defines how to parse a particular programming language. Language instances are typically generated by the Tree-sitter CLI and exported by language-specific crates.

Creating a Language

new

Create a new language from a language function. This is typically called automatically when you use a language from a Tree-sitter grammar crate.
LanguageFn
required
A function that returns a pointer to the language’s internal structure

Language Information

name

Get the name of this language. Returns None for older parsers that don’t include name metadata.

abi_version

Get the ABI version number that indicates which version of the Tree-sitter CLI was used to generate this language.

metadata

Get the metadata for this language. This information is generated by the CLI and relies on the language author providing the correct metadata in the language’s tree-sitter.json file. Returns: Some(LanguageMetadata) if metadata is available, or None for older parsers.

Node Types

node_kind_count

Get the number of distinct node types in this language.

node_kind_for_id

Get the name of the node kind for the given numerical id.
u16
required
The numerical id of the node kind
Returns: Some(&str) with the node kind name, or None if the id is invalid.

id_for_node_kind

Get the numeric id for the given node kind.
&str
required
The name of the node kind
bool
required
Whether to look for a named or anonymous node kind
Returns: The numerical id, or 0 if the kind doesn’t exist.

node_kind_is_named

Check if the node type for the given numerical id is named (as opposed to an anonymous node type).
u16
required
The numerical id of the node kind

node_kind_is_visible

Check if the node type for the given numerical id is visible (as opposed to a hidden node type).

node_kind_is_supertype

Check if the node type for the given numerical id is a supertype.

Fields

field_count

Get the number of distinct field names in this language.

field_name_for_id

Get the field name for the given numerical id.
u16
required
The numerical id of the field
Returns: Some(&str) with the field name, or None if the id is invalid.

field_id_for_name

Get the numerical id for the given field name.
impl AsRef<[u8]>
required
The name of the field
Returns: Some(FieldId) if the field exists, or None if it doesn’t.

Supertypes

supertypes

Get a list of all supertype symbols for the language. Returns: A slice of supertype symbol ids.

subtypes_for_supertype

Get a list of all subtype symbols for a given supertype symbol.
u16
required
The id of the supertype
Returns: A slice of subtype symbol ids.

Parse States

parse_state_count

Get the number of valid states in this language.

next_state

Get the next parse state. Combine this with lookahead_iterator to generate completion suggestions or valid symbols in error nodes.
u16
required
The current parse state
u16
required
The grammar symbol id
Returns: The next parse state id.

Lookahead

lookahead_iterator

Create a new lookahead iterator for this language and parse state. Returns None if state is invalid for this language.
u16
required
The parse state to create the iterator for
Returns: Some(LookaheadIterator) if the state is valid, or None otherwise.
Lookahead iterators can be useful to generate suggestions and improve syntax error diagnostics. To get symbols valid in an ERROR node, use the lookahead iterator on its first leaf node state. For MISSING nodes, a lookahead iterator created on the previous non-extra leaf node may be appropriate.

LookaheadIterator

An iterator over valid symbols in a parse state.

current_symbol

Get the current symbol of the lookahead iterator.

current_symbol_name

Get the current symbol name of the lookahead iterator.

language

Get the current language of the lookahead iterator.

reset

Reset the lookahead iterator to a different language and state.
&Language
required
The language to use
u16
required
The parse state
Returns: true if the reset was successful, false otherwise.

reset_state

Reset the lookahead iterator to another state in the same language.
u16
required
The parse state
Returns: true if the reset was successful, false otherwise.

iter_names

Iterate over symbol names.
The LookaheadIterator also implements the standard Iterator trait, yielding symbol ids:

LanguageMetadata

Metadata associated with a language, including semantic version information.
u8
The major version number
u8
The minor version number
u8
The patch version number

Examples

Inspecting a Language

Using Lookahead for Completions

Checking Field Existence