Struct tree_sitter::Language
source · pub struct Language(/* private fields */);
Expand description
An opaque object that defines how to parse a particular language. The code
for each Language
is generated by the Tree-sitter CLI.
Implementations§
source§impl Language
impl Language
sourcepub fn version(&self) -> usize
pub fn version(&self) -> usize
Get the ABI version number that indicates which version of the
Tree-sitter CLI that was used to generate this Language
.
sourcepub fn node_kind_count(&self) -> usize
pub fn node_kind_count(&self) -> usize
Get the number of distinct node types in this language.
sourcepub fn parse_state_count(&self) -> usize
pub fn parse_state_count(&self) -> usize
Get the number of valid states in this language.
sourcepub fn node_kind_for_id(&self, id: u16) -> Option<&'static str>
pub fn node_kind_for_id(&self, id: u16) -> Option<&'static str>
Get the name of the node kind for the given numerical id.
sourcepub fn id_for_node_kind(&self, kind: &str, named: bool) -> u16
pub fn id_for_node_kind(&self, kind: &str, named: bool) -> u16
Get the numeric id for the given node kind.
sourcepub fn node_kind_is_named(&self, id: u16) -> bool
pub fn node_kind_is_named(&self, id: u16) -> bool
Check if the node type for the given numerical id is named (as opposed to an anonymous node type).
pub fn node_kind_is_visible(&self, id: u16) -> bool
sourcepub fn field_count(&self) -> usize
pub fn field_count(&self) -> usize
Get the number of distinct field names in this language.
sourcepub fn field_name_for_id(&self, field_id: u16) -> Option<&'static str>
pub fn field_name_for_id(&self, field_id: u16) -> Option<&'static str>
Get the field names for the given numerical id.
sourcepub fn field_id_for_name(
&self,
field_name: impl AsRef<[u8]>
) -> Option<NonZeroU16>
pub fn field_id_for_name( &self, field_name: impl AsRef<[u8]> ) -> Option<NonZeroU16>
Get the numerical id for the given field name.
sourcepub fn next_state(&self, state: u16, id: u16) -> u16
pub fn next_state(&self, state: u16, id: u16) -> u16
Get the next parse state. Combine this with
lookahead_iterator
to
generate completion suggestions or valid symbols in error nodes.
Example:
let state = language.next_state(node.parse_state(), node.grammar_id());
sourcepub fn lookahead_iterator(&self, state: u16) -> Option<LookaheadIterator>
pub fn lookahead_iterator(&self, state: u16) -> Option<LookaheadIterator>
Create a new lookahead iterator for this language and parse state.
This returns None
if state is invalid for this language.
Iterating LookaheadIterator
will yield valid symbols in the given
parse state. Newly created lookahead iterators will return the ERROR
symbol from LookaheadIterator::current_symbol
.
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.