diff --git a/lib/binding_rust/bindings.rs b/lib/binding_rust/bindings.rs index d2239893..5ad38721 100644 --- a/lib/binding_rust/bindings.rs +++ b/lib/binding_rust/bindings.rs @@ -738,6 +738,12 @@ extern "C" { #[doc = " Get the current symbol of the lookahead iterator;"] pub fn ts_lookahead_iterator_current_symbol(arg1: *const TSLookaheadIterator) -> TSSymbol; } +extern "C" { + #[doc = " Get the current symbol type of the lookahead iterator as a null terminated\n string."] + pub fn ts_lookahead_iterator_current_symbol_name( + arg1: *const TSLookaheadIterator, + ) -> *const ::std::os::raw::c_char; +} extern "C" { #[doc = " Set the allocation functions used by the library.\n\n By default, Tree-sitter uses the standard libc allocation functions,\n but aborts the process when an allocation fails. This function lets\n you supply alternative allocation functions at runtime.\n\n If you pass `NULL` for any parameter, Tree-sitter will switch back to\n its default implementation of that function.\n\n If you call this function after the library has already been used, then\n you must ensure that either:\n 1. All the existing objects have been freed.\n 2. The new allocator shares its state with the old one, so it is capable\n of freeing memory that was allocated by the old allocator."] pub fn ts_set_allocator( diff --git a/lib/binding_rust/lib.rs b/lib/binding_rust/lib.rs index cdf1ffcf..49263b28 100644 --- a/lib/binding_rust/lib.rs +++ b/lib/binding_rust/lib.rs @@ -1497,6 +1497,18 @@ impl<'a> LookaheadIterator<'a> { unsafe { ffi::ts_lookahead_iterator_current_symbol(self.0.as_ptr()) } } + /// Get the current symbol name of the lookahead iterator. + #[doc(alias = "ts_lookahead_iterator_current_symbol_name")] + pub fn current_symbol_name(&self) -> &'static str { + unsafe { + CStr::from_ptr(ffi::ts_lookahead_iterator_current_symbol_name( + self.0.as_ptr(), + )) + .to_str() + .unwrap() + } + } + /// Reset the lookahead iterator. /// /// This returns `true` if the language was set successfully and `false` @@ -1514,6 +1526,26 @@ impl<'a> LookaheadIterator<'a> { pub fn reset_state(&self, state: u16) -> bool { unsafe { ffi::ts_lookahead_iterator_reset_state(self.0.as_ptr(), state) } } + + /// Iterate symbol names. + pub fn iter_names(&'a self) -> impl Iterator + 'a { + NameLookaheadIterator(&self) + } +} + +struct NameLookaheadIterator<'a>(&'a LookaheadIterator<'a>); + +impl<'a> Iterator for NameLookaheadIterator<'a> { + type Item = &'static str; + + #[doc(alias = "ts_lookahead_iterator_advance")] + fn next(&mut self) -> Option { + if !(unsafe { ffi::ts_lookahead_iterator_advance(self.0 .0.as_ptr()) }) { + None + } else { + Some(self.0.current_symbol_name()) + } + } } impl<'a> Iterator for LookaheadIterator<'a> { diff --git a/lib/include/tree_sitter/api.h b/lib/include/tree_sitter/api.h index 4bd2f43d..0fc9faf5 100644 --- a/lib/include/tree_sitter/api.h +++ b/lib/include/tree_sitter/api.h @@ -1107,6 +1107,12 @@ bool ts_lookahead_iterator_advance(TSLookaheadIterator *); */ TSSymbol ts_lookahead_iterator_current_symbol(const TSLookaheadIterator *); +/** + * Get the current symbol type of the lookahead iterator as a null terminated + * string. +*/ +const char *ts_lookahead_iterator_current_symbol_name(const TSLookaheadIterator *); + /**********************************/ /* Section - Global Configuration */ /**********************************/ diff --git a/lib/src/language.c b/lib/src/language.c index d8d07c2c..df5c6de1 100644 --- a/lib/src/language.c +++ b/lib/src/language.c @@ -181,7 +181,7 @@ bool ts_lookahead_iterator_reset_state(TSLookaheadIterator * self, TSStateId sta } const TSLanguage * ts_lookahead_iterator_language(const TSLookaheadIterator *self) { - LookaheadIterator *iterator = (LookaheadIterator *)self; + const LookaheadIterator *iterator = (const LookaheadIterator *)self; return iterator->language; } @@ -198,6 +198,11 @@ bool ts_lookahead_iterator_advance(TSLookaheadIterator *self) { } TSSymbol ts_lookahead_iterator_current_symbol(const TSLookaheadIterator *self) { - LookaheadIterator *iterator = (LookaheadIterator *)self; + const LookaheadIterator *iterator = (const LookaheadIterator *)self; return iterator->symbol; } + +const char *ts_lookahead_iterator_current_symbol_name(const TSLookaheadIterator *self) { + const LookaheadIterator *iterator = (const LookaheadIterator *)self; + return ts_language_symbol_name(iterator->language, iterator->symbol); +}