Add iteration over names
This commit is contained in:
parent
1f52f2f1dc
commit
672d38803e
4 changed files with 51 additions and 2 deletions
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -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<Item = &'static str> + '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<Self::Item> {
|
||||
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> {
|
||||
|
|
|
|||
|
|
@ -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 */
|
||||
/**********************************/
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue