tree_sitter/
lib.rs

1#![doc = include_str!("./README.md")]
2#![cfg_attr(not(feature = "std"), no_std)]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4
5pub mod ffi;
6mod util;
7
8#[cfg(not(feature = "std"))]
9extern crate alloc;
10#[cfg(not(feature = "std"))]
11use alloc::{boxed::Box, format, string::String, string::ToString, vec::Vec};
12use core::{
13    ffi::{c_char, c_void, CStr},
14    fmt::{self, Write},
15    hash, iter,
16    marker::PhantomData,
17    mem::MaybeUninit,
18    num::NonZeroU16,
19    ops::{self, Deref},
20    ptr::{self, NonNull},
21    slice, str,
22    sync::atomic::AtomicUsize,
23};
24#[cfg(feature = "std")]
25use std::error;
26#[cfg(all(unix, feature = "std"))]
27use std::os::fd::AsRawFd;
28#[cfg(all(windows, feature = "std"))]
29use std::os::windows::io::AsRawHandle;
30
31pub use streaming_iterator::{StreamingIterator, StreamingIteratorMut};
32use tree_sitter_language::LanguageFn;
33
34#[cfg(feature = "wasm")]
35mod wasm_language;
36#[cfg(feature = "wasm")]
37#[cfg_attr(docsrs, doc(cfg(feature = "wasm")))]
38pub use wasm_language::*;
39
40/// The latest ABI version that is supported by the current version of the
41/// library.
42///
43/// When Languages are generated by the Tree-sitter CLI, they are
44/// assigned an ABI version number that corresponds to the current CLI version.
45/// The Tree-sitter library is generally backwards-compatible with languages
46/// generated using older CLI versions, but is not forwards-compatible.
47#[doc(alias = "TREE_SITTER_LANGUAGE_VERSION")]
48pub const LANGUAGE_VERSION: usize = ffi::TREE_SITTER_LANGUAGE_VERSION as usize;
49
50/// The earliest ABI version that is supported by the current version of the
51/// library.
52#[doc(alias = "TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION")]
53pub const MIN_COMPATIBLE_LANGUAGE_VERSION: usize =
54    ffi::TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION as usize;
55
56pub const PARSER_HEADER: &str = include_str!("../src/parser.h");
57
58/// An opaque object that defines how to parse a particular language. The code
59/// for each `Language` is generated by the Tree-sitter CLI.
60#[doc(alias = "TSLanguage")]
61#[derive(Debug, PartialEq, Eq, Hash)]
62#[repr(transparent)]
63pub struct Language(*const ffi::TSLanguage);
64
65pub struct LanguageRef<'a>(*const ffi::TSLanguage, PhantomData<&'a ()>);
66
67/// The metadata associated with a language.
68///
69/// Currently, this metadata can be used to check the [Semantic Version](https://semver.org/)
70/// of the language. This version information should be used to signal if a given parser might
71/// be incompatible with existing queries when upgrading between major versions, or minor versions
72/// if it's in zerover.
73#[doc(alias = "TSLanguageMetadata")]
74pub struct LanguageMetadata {
75    pub major_version: u8,
76    pub minor_version: u8,
77    pub patch_version: u8,
78}
79
80impl From<ffi::TSLanguageMetadata> for LanguageMetadata {
81    fn from(val: ffi::TSLanguageMetadata) -> Self {
82        Self {
83            major_version: val.major_version,
84            minor_version: val.minor_version,
85            patch_version: val.patch_version,
86        }
87    }
88}
89
90/// A tree that represents the syntactic structure of a source code file.
91#[doc(alias = "TSTree")]
92pub struct Tree(NonNull<ffi::TSTree>);
93
94/// A position in a multi-line text document, in terms of rows and columns.
95///
96/// Rows and columns are zero-based.
97#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
98pub struct Point {
99    pub row: usize,
100    pub column: usize,
101}
102
103/// A range of positions in a multi-line text document, both in terms of bytes
104/// and of rows and columns.
105#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
106pub struct Range {
107    pub start_byte: usize,
108    pub end_byte: usize,
109    pub start_point: Point,
110    pub end_point: Point,
111}
112
113/// A summary of a change to a text document.
114#[derive(Clone, Copy, Debug, PartialEq, Eq)]
115pub struct InputEdit {
116    pub start_byte: usize,
117    pub old_end_byte: usize,
118    pub new_end_byte: usize,
119    pub start_position: Point,
120    pub old_end_position: Point,
121    pub new_end_position: Point,
122}
123
124/// A single node within a syntax [`Tree`].
125#[doc(alias = "TSNode")]
126#[derive(Clone, Copy)]
127#[repr(transparent)]
128pub struct Node<'tree>(ffi::TSNode, PhantomData<&'tree ()>);
129
130/// A stateful object that this is used to produce a [`Tree`] based on some
131/// source code.
132#[doc(alias = "TSParser")]
133pub struct Parser(NonNull<ffi::TSParser>);
134
135/// A stateful object that is used to look up symbols valid in a specific parse
136/// state
137#[doc(alias = "TSLookaheadIterator")]
138pub struct LookaheadIterator(NonNull<ffi::TSLookaheadIterator>);
139struct LookaheadNamesIterator<'a>(&'a mut LookaheadIterator);
140
141/// A stateful object that is passed into a [`ParseProgressCallback`]
142/// to pass in the current state of the parser.
143pub struct ParseState(NonNull<ffi::TSParseState>);
144
145impl ParseState {
146    #[must_use]
147    pub const fn current_byte_offset(&self) -> usize {
148        unsafe { self.0.as_ref() }.current_byte_offset as usize
149    }
150
151    #[must_use]
152    pub const fn has_error(&self) -> bool {
153        unsafe { self.0.as_ref() }.has_error
154    }
155}
156
157/// A stateful object that is passed into a [`QueryProgressCallback`]
158/// to pass in the current state of the query execution.
159pub struct QueryCursorState(NonNull<ffi::TSQueryCursorState>);
160
161impl QueryCursorState {
162    #[must_use]
163    pub const fn current_byte_offset(&self) -> usize {
164        unsafe { self.0.as_ref() }.current_byte_offset as usize
165    }
166}
167
168#[derive(Default)]
169pub struct ParseOptions<'a> {
170    pub progress_callback: Option<ParseProgressCallback<'a>>,
171}
172
173impl<'a> ParseOptions<'a> {
174    #[must_use]
175    pub fn new() -> Self {
176        Self::default()
177    }
178
179    #[must_use]
180    pub fn progress_callback<F: FnMut(&ParseState) -> bool>(mut self, callback: &'a mut F) -> Self {
181        self.progress_callback = Some(callback);
182        self
183    }
184}
185
186#[derive(Default)]
187pub struct QueryCursorOptions<'a> {
188    pub progress_callback: Option<QueryProgressCallback<'a>>,
189}
190
191impl<'a> QueryCursorOptions<'a> {
192    #[must_use]
193    pub fn new() -> Self {
194        Self::default()
195    }
196
197    #[must_use]
198    pub fn progress_callback<F: FnMut(&QueryCursorState) -> bool>(
199        mut self,
200        callback: &'a mut F,
201    ) -> Self {
202        self.progress_callback = Some(callback);
203        self
204    }
205}
206
207struct QueryCursorOptionsDrop(*mut ffi::TSQueryCursorOptions);
208
209impl Drop for QueryCursorOptionsDrop {
210    fn drop(&mut self) {
211        unsafe {
212            if !(*self.0).payload.is_null() {
213                drop(Box::from_raw(
214                    (*self.0).payload.cast::<QueryProgressCallback>(),
215                ));
216            }
217            drop(Box::from_raw(self.0));
218        }
219    }
220}
221
222/// A type of log message.
223#[derive(Debug, PartialEq, Eq)]
224pub enum LogType {
225    Parse,
226    Lex,
227}
228
229type FieldId = NonZeroU16;
230
231/// A callback that receives log messages during parsing.
232type Logger<'a> = Box<dyn FnMut(LogType, &str) + 'a>;
233
234/// A callback that receives the parse state during parsing.
235type ParseProgressCallback<'a> = &'a mut dyn FnMut(&ParseState) -> bool;
236
237/// A callback that receives the query state during query execution.
238type QueryProgressCallback<'a> = &'a mut dyn FnMut(&QueryCursorState) -> bool;
239
240pub trait Decode {
241    /// A callback that decodes the next code point from the input slice. It should return the code
242    /// point, and how many bytes were decoded.
243    fn decode(bytes: &[u8]) -> (i32, u32);
244}
245
246/// A stateful object for walking a syntax [`Tree`] efficiently.
247#[doc(alias = "TSTreeCursor")]
248pub struct TreeCursor<'cursor>(ffi::TSTreeCursor, PhantomData<&'cursor ()>);
249
250/// A set of patterns that match nodes in a syntax tree.
251#[doc(alias = "TSQuery")]
252#[derive(Debug)]
253#[allow(clippy::type_complexity)]
254pub struct Query {
255    ptr: NonNull<ffi::TSQuery>,
256    capture_names: Box<[&'static str]>,
257    capture_quantifiers: Box<[Box<[CaptureQuantifier]>]>,
258    text_predicates: Box<[Box<[TextPredicateCapture]>]>,
259    property_settings: Box<[Box<[QueryProperty]>]>,
260    property_predicates: Box<[Box<[(QueryProperty, bool)]>]>,
261    general_predicates: Box<[Box<[QueryPredicate]>]>,
262}
263
264/// A quantifier for captures
265#[derive(Debug, PartialEq, Eq, Clone, Copy)]
266pub enum CaptureQuantifier {
267    Zero,
268    ZeroOrOne,
269    ZeroOrMore,
270    One,
271    OneOrMore,
272}
273
274impl From<ffi::TSQuantifier> for CaptureQuantifier {
275    fn from(value: ffi::TSQuantifier) -> Self {
276        match value {
277            ffi::TSQuantifierZero => Self::Zero,
278            ffi::TSQuantifierZeroOrOne => Self::ZeroOrOne,
279            ffi::TSQuantifierZeroOrMore => Self::ZeroOrMore,
280            ffi::TSQuantifierOne => Self::One,
281            ffi::TSQuantifierOneOrMore => Self::OneOrMore,
282            _ => panic!("Unrecognized quantifier: {value}"),
283        }
284    }
285}
286
287/// A stateful object for executing a [`Query`] on a syntax [`Tree`].
288#[doc(alias = "TSQueryCursor")]
289pub struct QueryCursor {
290    ptr: NonNull<ffi::TSQueryCursor>,
291}
292
293/// A key-value pair associated with a particular pattern in a [`Query`].
294#[derive(Debug, PartialEq, Eq)]
295pub struct QueryProperty {
296    pub key: Box<str>,
297    pub value: Option<Box<str>>,
298    pub capture_id: Option<usize>,
299}
300
301#[derive(Debug, PartialEq, Eq)]
302pub enum QueryPredicateArg {
303    Capture(u32),
304    String(Box<str>),
305}
306
307/// A key-value pair associated with a particular pattern in a [`Query`].
308#[derive(Debug, PartialEq, Eq)]
309pub struct QueryPredicate {
310    pub operator: Box<str>,
311    pub args: Box<[QueryPredicateArg]>,
312}
313
314/// A match of a [`Query`] to a particular set of [`Node`]s.
315pub struct QueryMatch<'cursor, 'tree> {
316    pub pattern_index: usize,
317    pub captures: &'cursor [QueryCapture<'tree>],
318    id: u32,
319    cursor: *mut ffi::TSQueryCursor,
320}
321
322/// A sequence of [`QueryMatch`]es associated with a given [`QueryCursor`].
323pub struct QueryMatches<'query, 'tree: 'query, T: TextProvider<I>, I: AsRef<[u8]>> {
324    ptr: *mut ffi::TSQueryCursor,
325    query: &'query Query,
326    text_provider: T,
327    buffer1: Vec<u8>,
328    buffer2: Vec<u8>,
329    current_match: Option<QueryMatch<'query, 'tree>>,
330    _options: Option<QueryCursorOptionsDrop>,
331    _phantom: PhantomData<(&'tree (), I)>,
332}
333
334/// A sequence of [`QueryCapture`]s associated with a given [`QueryCursor`].
335///
336/// During iteration, each element contains a [`QueryMatch`] and index. The index can
337/// be used to access the new capture inside of the [`QueryMatch::captures`]'s [`captures`].
338pub struct QueryCaptures<'query, 'tree: 'query, T: TextProvider<I>, I: AsRef<[u8]>> {
339    ptr: *mut ffi::TSQueryCursor,
340    query: &'query Query,
341    text_provider: T,
342    buffer1: Vec<u8>,
343    buffer2: Vec<u8>,
344    current_match: Option<(QueryMatch<'query, 'tree>, usize)>,
345    _options: Option<QueryCursorOptionsDrop>,
346    _phantom: PhantomData<(&'tree (), I)>,
347}
348
349pub trait TextProvider<I>
350where
351    I: AsRef<[u8]>,
352{
353    type I: Iterator<Item = I>;
354    fn text(&mut self, node: Node) -> Self::I;
355}
356
357/// A particular [`Node`] that has been captured with a particular name within a
358/// [`Query`].
359#[derive(Clone, Copy, Debug)]
360#[repr(C)]
361pub struct QueryCapture<'tree> {
362    pub node: Node<'tree>,
363    pub index: u32,
364}
365
366/// An error that occurred when trying to assign an incompatible [`Language`] to
367/// a [`Parser`].
368#[derive(Debug, PartialEq, Eq)]
369pub struct LanguageError {
370    version: usize,
371}
372
373/// An error that occurred in [`Parser::set_included_ranges`].
374#[derive(Debug, PartialEq, Eq)]
375pub struct IncludedRangesError(pub usize);
376
377/// An error that occurred when trying to create a [`Query`].
378#[derive(Debug, PartialEq, Eq)]
379pub struct QueryError {
380    pub row: usize,
381    pub column: usize,
382    pub offset: usize,
383    pub message: String,
384    pub kind: QueryErrorKind,
385}
386
387#[derive(Debug, PartialEq, Eq)]
388pub enum QueryErrorKind {
389    Syntax,
390    NodeType,
391    Field,
392    Capture,
393    Predicate,
394    Structure,
395    Language,
396}
397
398#[derive(Debug)]
399/// The first item is the capture index
400/// The next is capture specific, depending on what item is expected
401/// The first bool is if the capture is positive
402/// The last item is a bool signifying whether or not it's meant to match
403/// any or all captures
404enum TextPredicateCapture {
405    EqString(u32, Box<str>, bool, bool),
406    EqCapture(u32, u32, bool, bool),
407    MatchString(u32, regex::bytes::Regex, bool, bool),
408    AnyString(u32, Box<[Box<str>]>, bool),
409}
410
411// TODO: Remove this struct at some point. If `core::str::lossy::Utf8Lossy`
412// is ever stabilized.
413pub struct LossyUtf8<'a> {
414    bytes: &'a [u8],
415    in_replacement: bool,
416}
417
418impl Language {
419    #[must_use]
420    pub fn new(builder: LanguageFn) -> Self {
421        Self(unsafe { builder.into_raw()().cast() })
422    }
423
424    /// Get the name of this language. This returns `None` in older parsers.
425    #[doc(alias = "ts_language_name")]
426    #[must_use]
427    pub fn name(&self) -> Option<&'static str> {
428        let ptr = unsafe { ffi::ts_language_name(self.0) };
429        (!ptr.is_null()).then(|| unsafe { CStr::from_ptr(ptr) }.to_str().unwrap())
430    }
431
432    /// Get the ABI version number that indicates which version of the
433    /// Tree-sitter CLI that was used to generate this [`Language`].
434    #[doc(alias = "ts_language_version")]
435    #[deprecated(since = "0.25.0", note = "Use abi_version instead")]
436    #[must_use]
437    pub fn version(&self) -> usize {
438        unsafe { ffi::ts_language_version(self.0) as usize }
439    }
440
441    /// Get the ABI version number that indicates which version of the
442    /// Tree-sitter CLI that was used to generate this [`Language`].
443    #[doc(alias = "ts_language_abi_version")]
444    #[must_use]
445    pub fn abi_version(&self) -> usize {
446        unsafe { ffi::ts_language_abi_version(self.0) as usize }
447    }
448
449    /// Get the metadata for this language. This information is generated by the
450    /// CLI, and relies on the language author providing the correct metadata in
451    /// the language's `tree-sitter.json` file.
452    ///
453    /// See also [`LanguageMetadata`].
454    #[doc(alias = "ts_language_metadata")]
455    #[must_use]
456    pub fn metadata(&self) -> Option<LanguageMetadata> {
457        unsafe {
458            let ptr = ffi::ts_language_metadata(self.0);
459            (!ptr.is_null()).then(|| (*ptr).into())
460        }
461    }
462
463    /// Get the number of distinct node types in this language.
464    #[doc(alias = "ts_language_symbol_count")]
465    #[must_use]
466    pub fn node_kind_count(&self) -> usize {
467        unsafe { ffi::ts_language_symbol_count(self.0) as usize }
468    }
469
470    /// Get the number of valid states in this language.
471    #[doc(alias = "ts_language_state_count")]
472    #[must_use]
473    pub fn parse_state_count(&self) -> usize {
474        unsafe { ffi::ts_language_state_count(self.0) as usize }
475    }
476
477    /// Get a list of all supertype symbols for the language.
478    #[doc(alias = "ts_language_supertypes")]
479    #[must_use]
480    pub fn supertypes(&self) -> &[u16] {
481        let mut length = 0u32;
482        unsafe {
483            let ptr = ffi::ts_language_supertypes(self.0, core::ptr::addr_of_mut!(length));
484            if length == 0 {
485                &[]
486            } else {
487                slice::from_raw_parts(ptr.cast_mut(), length as usize)
488            }
489        }
490    }
491
492    /// Get a list of all subtype symbols for a given supertype symbol.
493    #[doc(alias = "ts_language_supertype_map")]
494    #[must_use]
495    pub fn subtypes_for_supertype(&self, supertype: u16) -> &[u16] {
496        unsafe {
497            let mut length = 0u32;
498            let ptr = ffi::ts_language_subtypes(self.0, supertype, core::ptr::addr_of_mut!(length));
499            if length == 0 {
500                &[]
501            } else {
502                slice::from_raw_parts(ptr.cast_mut(), length as usize)
503            }
504        }
505    }
506
507    /// Get the name of the node kind for the given numerical id.
508    #[doc(alias = "ts_language_symbol_name")]
509    #[must_use]
510    pub fn node_kind_for_id(&self, id: u16) -> Option<&'static str> {
511        let ptr = unsafe { ffi::ts_language_symbol_name(self.0, id) };
512        (!ptr.is_null()).then(|| unsafe { CStr::from_ptr(ptr) }.to_str().unwrap())
513    }
514
515    /// Get the numeric id for the given node kind.
516    #[doc(alias = "ts_language_symbol_for_name")]
517    #[must_use]
518    pub fn id_for_node_kind(&self, kind: &str, named: bool) -> u16 {
519        unsafe {
520            ffi::ts_language_symbol_for_name(
521                self.0,
522                kind.as_bytes().as_ptr().cast::<c_char>(),
523                kind.len() as u32,
524                named,
525            )
526        }
527    }
528
529    /// Check if the node type for the given numerical id is named (as opposed
530    /// to an anonymous node type).
531    #[must_use]
532    pub fn node_kind_is_named(&self, id: u16) -> bool {
533        unsafe { ffi::ts_language_symbol_type(self.0, id) == ffi::TSSymbolTypeRegular }
534    }
535
536    /// Check if the node type for the given numerical id is visible (as opposed
537    /// to a hidden node type).
538    #[must_use]
539    pub fn node_kind_is_visible(&self, id: u16) -> bool {
540        unsafe { ffi::ts_language_symbol_type(self.0, id) <= ffi::TSSymbolTypeAnonymous }
541    }
542
543    /// Check if the node type for the given numerical id is a supertype.
544    #[must_use]
545    pub fn node_kind_is_supertype(&self, id: u16) -> bool {
546        unsafe { ffi::ts_language_symbol_type(self.0, id) == ffi::TSSymbolTypeSupertype }
547    }
548
549    /// Get the number of distinct field names in this language.
550    #[doc(alias = "ts_language_field_count")]
551    #[must_use]
552    pub fn field_count(&self) -> usize {
553        unsafe { ffi::ts_language_field_count(self.0) as usize }
554    }
555
556    /// Get the field name for the given numerical id.
557    #[doc(alias = "ts_language_field_name_for_id")]
558    #[must_use]
559    pub fn field_name_for_id(&self, field_id: u16) -> Option<&'static str> {
560        let ptr = unsafe { ffi::ts_language_field_name_for_id(self.0, field_id) };
561        (!ptr.is_null()).then(|| unsafe { CStr::from_ptr(ptr) }.to_str().unwrap())
562    }
563
564    /// Get the numerical id for the given field name.
565    #[doc(alias = "ts_language_field_id_for_name")]
566    #[must_use]
567    pub fn field_id_for_name(&self, field_name: impl AsRef<[u8]>) -> Option<FieldId> {
568        let field_name = field_name.as_ref();
569        let id = unsafe {
570            ffi::ts_language_field_id_for_name(
571                self.0,
572                field_name.as_ptr().cast::<c_char>(),
573                field_name.len() as u32,
574            )
575        };
576        FieldId::new(id)
577    }
578
579    /// Get the next parse state. Combine this with
580    /// [`lookahead_iterator`](Language::lookahead_iterator) to
581    /// generate completion suggestions or valid symbols in error nodes.
582    ///
583    /// Example:
584    /// ```
585    /// let state = language.next_state(node.parse_state(), node.grammar_id());
586    /// ```
587    #[doc(alias = "ts_language_next_state")]
588    #[must_use]
589    pub fn next_state(&self, state: u16, id: u16) -> u16 {
590        unsafe { ffi::ts_language_next_state(self.0, state, id) }
591    }
592
593    /// Create a new lookahead iterator for this language and parse state.
594    ///
595    /// This returns `None` if state is invalid for this language.
596    ///
597    /// Iterating [`LookaheadIterator`] will yield valid symbols in the given
598    /// parse state. Newly created lookahead iterators will return the `ERROR`
599    /// symbol from [`LookaheadIterator::current_symbol`].
600    ///
601    /// Lookahead iterators can be useful to generate suggestions and improve
602    /// syntax error diagnostics. To get symbols valid in an `ERROR` node, use the
603    /// lookahead iterator on its first leaf node state. For `MISSING` nodes, a
604    /// lookahead iterator created on the previous non-extra leaf node may be
605    /// appropriate.
606    #[doc(alias = "ts_lookahead_iterator_new")]
607    #[must_use]
608    pub fn lookahead_iterator(&self, state: u16) -> Option<LookaheadIterator> {
609        let ptr = unsafe { ffi::ts_lookahead_iterator_new(self.0, state) };
610        (!ptr.is_null()).then(|| unsafe { LookaheadIterator::from_raw(ptr) })
611    }
612}
613
614impl From<LanguageFn> for Language {
615    fn from(value: LanguageFn) -> Self {
616        Self::new(value)
617    }
618}
619
620impl Clone for Language {
621    fn clone(&self) -> Self {
622        unsafe { Self(ffi::ts_language_copy(self.0)) }
623    }
624}
625
626impl Drop for Language {
627    fn drop(&mut self) {
628        unsafe { ffi::ts_language_delete(self.0) }
629    }
630}
631
632impl Deref for LanguageRef<'_> {
633    type Target = Language;
634
635    fn deref(&self) -> &Self::Target {
636        unsafe { &*(core::ptr::addr_of!(self.0).cast::<Language>()) }
637    }
638}
639
640impl Default for Parser {
641    fn default() -> Self {
642        Self::new()
643    }
644}
645
646impl Parser {
647    /// Create a new parser.
648    #[doc(alias = "ts_parser_new")]
649    #[must_use]
650    pub fn new() -> Self {
651        unsafe {
652            let parser = ffi::ts_parser_new();
653            Self(NonNull::new_unchecked(parser))
654        }
655    }
656
657    /// Set the language that the parser should use for parsing.
658    ///
659    /// Returns a Result indicating whether or not the language was successfully
660    /// assigned. True means assignment succeeded. False means there was a
661    /// version mismatch: the language was generated with an incompatible
662    /// version of the Tree-sitter CLI. Check the language's version using
663    /// [`Language::version`] and compare it to this library's
664    /// [`LANGUAGE_VERSION`] and [`MIN_COMPATIBLE_LANGUAGE_VERSION`] constants.
665    #[doc(alias = "ts_parser_set_language")]
666    pub fn set_language(&mut self, language: &Language) -> Result<(), LanguageError> {
667        let version = language.abi_version();
668        if (MIN_COMPATIBLE_LANGUAGE_VERSION..=LANGUAGE_VERSION).contains(&version) {
669            unsafe {
670                ffi::ts_parser_set_language(self.0.as_ptr(), language.0);
671            }
672            Ok(())
673        } else {
674            Err(LanguageError { version })
675        }
676    }
677
678    /// Get the parser's current language.
679    #[doc(alias = "ts_parser_language")]
680    #[must_use]
681    pub fn language(&self) -> Option<LanguageRef<'_>> {
682        let ptr = unsafe { ffi::ts_parser_language(self.0.as_ptr()) };
683        (!ptr.is_null()).then_some(LanguageRef(ptr, PhantomData))
684    }
685
686    /// Get the parser's current logger.
687    #[doc(alias = "ts_parser_logger")]
688    #[must_use]
689    pub fn logger(&self) -> Option<&Logger> {
690        let logger = unsafe { ffi::ts_parser_logger(self.0.as_ptr()) };
691        unsafe { logger.payload.cast::<Logger>().as_ref() }
692    }
693
694    /// Set the logging callback that the parser should use during parsing.
695    #[doc(alias = "ts_parser_set_logger")]
696    pub fn set_logger(&mut self, logger: Option<Logger>) {
697        let prev_logger = unsafe { ffi::ts_parser_logger(self.0.as_ptr()) };
698        if !prev_logger.payload.is_null() {
699            drop(unsafe { Box::from_raw(prev_logger.payload.cast::<Logger>()) });
700        }
701
702        let c_logger;
703        if let Some(logger) = logger {
704            let container = Box::new(logger);
705
706            unsafe extern "C" fn log(
707                payload: *mut c_void,
708                c_log_type: ffi::TSLogType,
709                c_message: *const c_char,
710            ) {
711                let callback = payload.cast::<Logger>().as_mut().unwrap();
712                if let Ok(message) = CStr::from_ptr(c_message).to_str() {
713                    let log_type = if c_log_type == ffi::TSLogTypeParse {
714                        LogType::Parse
715                    } else {
716                        LogType::Lex
717                    };
718                    callback(log_type, message);
719                }
720            }
721
722            let raw_container = Box::into_raw(container);
723
724            c_logger = ffi::TSLogger {
725                payload: raw_container.cast::<c_void>(),
726                log: Some(log),
727            };
728        } else {
729            c_logger = ffi::TSLogger {
730                payload: ptr::null_mut(),
731                log: None,
732            };
733        }
734
735        unsafe { ffi::ts_parser_set_logger(self.0.as_ptr(), c_logger) };
736    }
737
738    /// Set the destination to which the parser should write debugging graphs
739    /// during parsing. The graphs are formatted in the DOT language. You may
740    /// want to pipe these graphs directly to a `dot(1)` process in order to
741    /// generate SVG output.
742    #[doc(alias = "ts_parser_print_dot_graphs")]
743    #[cfg(not(target_os = "wasi"))]
744    #[cfg(feature = "std")]
745    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
746    pub fn print_dot_graphs(
747        &mut self,
748        #[cfg(unix)] file: &impl AsRawFd,
749        #[cfg(windows)] file: &impl AsRawHandle,
750    ) {
751        #[cfg(unix)]
752        {
753            let fd = file.as_raw_fd();
754            unsafe {
755                ffi::ts_parser_print_dot_graphs(self.0.as_ptr(), ffi::_ts_dup(fd));
756            }
757        }
758
759        #[cfg(windows)]
760        {
761            let handle = file.as_raw_handle();
762            unsafe {
763                ffi::ts_parser_print_dot_graphs(self.0.as_ptr(), ffi::_ts_dup(handle));
764            }
765        }
766    }
767
768    /// Stop the parser from printing debugging graphs while parsing.
769    #[doc(alias = "ts_parser_print_dot_graphs")]
770    #[cfg(not(target_os = "wasi"))]
771    #[cfg(feature = "std")]
772    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
773    pub fn stop_printing_dot_graphs(&mut self) {
774        unsafe { ffi::ts_parser_print_dot_graphs(self.0.as_ptr(), -1) }
775    }
776
777    /// Parse a slice of UTF8 text.
778    ///
779    /// # Arguments:
780    /// * `text` The UTF8-encoded text to parse.
781    /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the
782    ///   document has changed since `old_tree` was created, then you must edit `old_tree` to match
783    ///   the new text using [`Tree::edit`].
784    ///
785    /// Returns a [`Tree`] if parsing succeeded, or `None` if:
786    ///  * The parser has not yet had a language assigned with [`Parser::set_language`]
787    ///  * The timeout set with [`Parser::set_timeout_micros`] expired (deprecated)
788    ///  * The cancellation flag set with [`Parser::set_cancellation_flag`] was flipped (deprecated)
789    #[doc(alias = "ts_parser_parse")]
790    pub fn parse(&mut self, text: impl AsRef<[u8]>, old_tree: Option<&Tree>) -> Option<Tree> {
791        let bytes = text.as_ref();
792        let len = bytes.len();
793        self.parse_with_options(
794            &mut |i, _| (i < len).then(|| &bytes[i..]).unwrap_or_default(),
795            old_tree,
796            None,
797        )
798    }
799
800    /// Parse a slice of UTF16 text.
801    ///
802    /// # Arguments:
803    /// * `text` The UTF16-encoded text to parse.
804    /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the
805    ///   document has changed since `old_tree` was created, then you must edit `old_tree` to match
806    ///   the new text using [`Tree::edit`].
807    #[deprecated(since = "0.25.0", note = "Prefer parse_utf16_le instead")]
808    pub fn parse_utf16(
809        &mut self,
810        input: impl AsRef<[u16]>,
811        old_tree: Option<&Tree>,
812    ) -> Option<Tree> {
813        let code_points = input.as_ref();
814        let len = code_points.len();
815        self.parse_utf16_le_with_options(
816            &mut |i, _| (i < len).then(|| &code_points[i..]).unwrap_or_default(),
817            old_tree,
818            None,
819        )
820    }
821
822    /// Parse UTF8 text provided in chunks by a callback.
823    ///
824    /// # Arguments:
825    /// * `callback` A function that takes a byte offset and position and returns a slice of
826    ///   UTF8-encoded text starting at that byte offset and position. The slices can be of any
827    ///   length. If the given position is at the end of the text, the callback should return an
828    ///   empty slice.
829    /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the
830    ///   document has changed since `old_tree` was created, then you must edit `old_tree` to match
831    ///   the new text using [`Tree::edit`].
832    #[deprecated(since = "0.25.0", note = "Prefer `parse_with_options` instead")]
833    pub fn parse_with<T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>(
834        &mut self,
835        callback: &mut F,
836        old_tree: Option<&Tree>,
837    ) -> Option<Tree> {
838        self.parse_with_options(callback, old_tree, None)
839    }
840
841    /// Parse text provided in chunks by a callback.
842    ///
843    /// # Arguments:
844    /// * `callback` A function that takes a byte offset and position and returns a slice of
845    ///   UTF8-encoded text starting at that byte offset and position. The slices can be of any
846    ///   length. If the given position is at the end of the text, the callback should return an
847    ///   empty slice.
848    /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the
849    ///   document has changed since `old_tree` was created, then you must edit `old_tree` to match
850    ///   the new text using [`Tree::edit`].
851    /// * `options` Options for parsing the text. This can be used to set a progress callback.
852    pub fn parse_with_options<T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>(
853        &mut self,
854        callback: &mut F,
855        old_tree: Option<&Tree>,
856        options: Option<ParseOptions>,
857    ) -> Option<Tree> {
858        type Payload<'a, F, T> = (&'a mut F, Option<T>);
859
860        // This C function is passed to Tree-sitter as the progress callback.
861        unsafe extern "C" fn progress(state: *mut ffi::TSParseState) -> bool {
862            let callback = (*state)
863                .payload
864                .cast::<ParseProgressCallback>()
865                .as_mut()
866                .unwrap();
867            callback(&ParseState::from_raw(state))
868        }
869
870        // This C function is passed to Tree-sitter as the input callback.
871        unsafe extern "C" fn read<T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>(
872            payload: *mut c_void,
873            byte_offset: u32,
874            position: ffi::TSPoint,
875            bytes_read: *mut u32,
876        ) -> *const c_char {
877            let (callback, text) = payload.cast::<Payload<F, T>>().as_mut().unwrap();
878            *text = Some(callback(byte_offset as usize, position.into()));
879            let slice = text.as_ref().unwrap().as_ref();
880            *bytes_read = slice.len() as u32;
881            slice.as_ptr().cast::<c_char>()
882        }
883
884        let empty_options = ffi::TSParseOptions {
885            payload: ptr::null_mut(),
886            progress_callback: None,
887        };
888
889        let mut callback_ptr;
890        let parse_options = if let Some(options) = options {
891            if let Some(cb) = options.progress_callback {
892                callback_ptr = cb;
893                ffi::TSParseOptions {
894                    payload: core::ptr::addr_of_mut!(callback_ptr).cast::<c_void>(),
895                    progress_callback: Some(progress),
896                }
897            } else {
898                empty_options
899            }
900        } else {
901            empty_options
902        };
903
904        // A pointer to this payload is passed on every call to the `read` C function.
905        // The payload contains two things:
906        // 1. A reference to the rust `callback`.
907        // 2. The text that was returned from the previous call to `callback`. This allows the
908        //    callback to return owned values like vectors.
909        let mut payload: Payload<F, T> = (callback, None);
910
911        let c_input = ffi::TSInput {
912            payload: ptr::addr_of_mut!(payload).cast::<c_void>(),
913            read: Some(read::<T, F>),
914            encoding: ffi::TSInputEncodingUTF8,
915            decode: None,
916        };
917
918        let c_old_tree = old_tree.map_or(ptr::null_mut(), |t| t.0.as_ptr());
919        unsafe {
920            let c_new_tree = ffi::ts_parser_parse_with_options(
921                self.0.as_ptr(),
922                c_old_tree,
923                c_input,
924                parse_options,
925            );
926
927            NonNull::new(c_new_tree).map(Tree)
928        }
929    }
930
931    /// Parse UTF16 text provided in chunks by a callback.
932    ///
933    /// # Arguments:
934    /// * `callback` A function that takes a code point offset and position and returns a slice of
935    ///   UTF16-encoded text starting at that byte offset and position. The slices can be of any
936    ///   length. If the given position is at the end of the text, the callback should return an
937    ///   empty slice.
938    /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the
939    ///   document has changed since `old_tree` was created, then you must edit `old_tree` to match
940    ///   the new text using [`Tree::edit`].
941    #[deprecated(
942        since = "0.25.0",
943        note = "Prefer `parse_utf16_le_with_options` instead"
944    )]
945    pub fn parse_utf16_with<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>(
946        &mut self,
947        callback: &mut F,
948        old_tree: Option<&Tree>,
949    ) -> Option<Tree> {
950        self.parse_utf16_le_with_options(callback, old_tree, None)
951    }
952
953    /// Parse a slice of UTF16 little-endian text.
954    ///
955    /// # Arguments:
956    /// * `text` The UTF16-encoded text to parse.
957    /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the
958    ///   document has changed since `old_tree` was created, then you must edit `old_tree` to match
959    ///   the new text using [`Tree::edit`].
960    pub fn parse_utf16_le(
961        &mut self,
962        input: impl AsRef<[u16]>,
963        old_tree: Option<&Tree>,
964    ) -> Option<Tree> {
965        let code_points = input.as_ref();
966        let len = code_points.len();
967        self.parse_utf16_le_with_options(
968            &mut |i, _| (i < len).then(|| &code_points[i..]).unwrap_or_default(),
969            old_tree,
970            None,
971        )
972    }
973
974    /// Parse UTF16 little-endian text provided in chunks by a callback.
975    ///
976    /// # Arguments:
977    /// * `callback` A function that takes a code point offset and position and returns a slice of
978    ///   UTF16-encoded text starting at that byte offset and position. The slices can be of any
979    ///   length. If the given position is at the end of the text, the callback should return an
980    ///   empty slice.
981    /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the
982    ///   document has changed since `old_tree` was created, then you must edit `old_tree` to match
983    ///   the new text using [`Tree::edit`].
984    /// * `options` Options for parsing the text. This can be used to set a progress callback.
985    pub fn parse_utf16_le_with_options<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>(
986        &mut self,
987        callback: &mut F,
988        old_tree: Option<&Tree>,
989        options: Option<ParseOptions>,
990    ) -> Option<Tree> {
991        type Payload<'a, F, T> = (&'a mut F, Option<T>);
992
993        unsafe extern "C" fn progress(state: *mut ffi::TSParseState) -> bool {
994            let callback = (*state)
995                .payload
996                .cast::<ParseProgressCallback>()
997                .as_mut()
998                .unwrap();
999            callback(&ParseState::from_raw(state))
1000        }
1001
1002        // This C function is passed to Tree-sitter as the input callback.
1003        unsafe extern "C" fn read<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>(
1004            payload: *mut c_void,
1005            byte_offset: u32,
1006            position: ffi::TSPoint,
1007            bytes_read: *mut u32,
1008        ) -> *const c_char {
1009            let (callback, text) = payload.cast::<Payload<F, T>>().as_mut().unwrap();
1010            *text = Some(callback(
1011                (byte_offset / 2) as usize,
1012                Point {
1013                    row: position.row as usize,
1014                    column: position.column as usize / 2,
1015                },
1016            ));
1017            let slice = text.as_ref().unwrap().as_ref();
1018            *bytes_read = slice.len() as u32 * 2;
1019            slice.as_ptr().cast::<c_char>()
1020        }
1021
1022        let empty_options = ffi::TSParseOptions {
1023            payload: ptr::null_mut(),
1024            progress_callback: None,
1025        };
1026
1027        let mut callback_ptr;
1028        let parse_options = if let Some(options) = options {
1029            if let Some(cb) = options.progress_callback {
1030                callback_ptr = cb;
1031                ffi::TSParseOptions {
1032                    payload: core::ptr::addr_of_mut!(callback_ptr).cast::<c_void>(),
1033                    progress_callback: Some(progress),
1034                }
1035            } else {
1036                empty_options
1037            }
1038        } else {
1039            empty_options
1040        };
1041
1042        // A pointer to this payload is passed on every call to the `read` C function.
1043        // The payload contains two things:
1044        // 1. A reference to the rust `callback`.
1045        // 2. The text that was returned from the previous call to `callback`. This allows the
1046        //    callback to return owned values like vectors.
1047        let mut payload: Payload<F, T> = (callback, None);
1048
1049        let c_input = ffi::TSInput {
1050            payload: core::ptr::addr_of_mut!(payload).cast::<c_void>(),
1051            read: Some(read::<T, F>),
1052            encoding: ffi::TSInputEncodingUTF16LE,
1053            decode: None,
1054        };
1055
1056        let c_old_tree = old_tree.map_or(ptr::null_mut(), |t| t.0.as_ptr());
1057        unsafe {
1058            let c_new_tree = ffi::ts_parser_parse_with_options(
1059                self.0.as_ptr(),
1060                c_old_tree,
1061                c_input,
1062                parse_options,
1063            );
1064
1065            NonNull::new(c_new_tree).map(Tree)
1066        }
1067    }
1068
1069    /// Parse a slice of UTF16 big-endian text.
1070    ///
1071    /// # Arguments:
1072    /// * `text` The UTF16-encoded text to parse.
1073    /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the
1074    ///   document has changed since `old_tree` was created, then you must edit `old_tree` to match
1075    ///   the new text using [`Tree::edit`].
1076    pub fn parse_utf16_be(
1077        &mut self,
1078        input: impl AsRef<[u16]>,
1079        old_tree: Option<&Tree>,
1080    ) -> Option<Tree> {
1081        let code_points = input.as_ref();
1082        let len = code_points.len();
1083        self.parse_utf16_be_with_options(
1084            &mut |i, _| if i < len { &code_points[i..] } else { &[] },
1085            old_tree,
1086            None,
1087        )
1088    }
1089
1090    /// Parse UTF16 big-endian text provided in chunks by a callback.
1091    ///
1092    /// # Arguments:
1093    /// * `callback` A function that takes a code point offset and position and returns a slice of
1094    ///   UTF16-encoded text starting at that byte offset and position. The slices can be of any
1095    ///   length. If the given position is at the end of the text, the callback should return an
1096    ///   empty slice.
1097    /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the
1098    ///   document has changed since `old_tree` was created, then you must edit `old_tree` to match
1099    ///   the new text using [`Tree::edit`].
1100    /// * `options` Options for parsing the text. This can be used to set a progress callback.
1101    pub fn parse_utf16_be_with_options<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>(
1102        &mut self,
1103        callback: &mut F,
1104        old_tree: Option<&Tree>,
1105        options: Option<ParseOptions>,
1106    ) -> Option<Tree> {
1107        type Payload<'a, F, T> = (&'a mut F, Option<T>);
1108
1109        // This C function is passed to Tree-sitter as the progress callback.
1110        unsafe extern "C" fn progress(state: *mut ffi::TSParseState) -> bool {
1111            let callback = (*state)
1112                .payload
1113                .cast::<ParseProgressCallback>()
1114                .as_mut()
1115                .unwrap();
1116            callback(&ParseState::from_raw(state))
1117        }
1118
1119        // This C function is passed to Tree-sitter as the input callback.
1120        unsafe extern "C" fn read<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>(
1121            payload: *mut c_void,
1122            byte_offset: u32,
1123            position: ffi::TSPoint,
1124            bytes_read: *mut u32,
1125        ) -> *const c_char {
1126            let (callback, text) = payload.cast::<Payload<F, T>>().as_mut().unwrap();
1127            *text = Some(callback(
1128                (byte_offset / 2) as usize,
1129                Point {
1130                    row: position.row as usize,
1131                    column: position.column as usize / 2,
1132                },
1133            ));
1134            let slice = text.as_ref().unwrap().as_ref();
1135            *bytes_read = slice.len() as u32 * 2;
1136            slice.as_ptr().cast::<c_char>()
1137        }
1138
1139        let empty_options = ffi::TSParseOptions {
1140            payload: ptr::null_mut(),
1141            progress_callback: None,
1142        };
1143
1144        let mut callback_ptr;
1145        let parse_options = if let Some(options) = options {
1146            if let Some(cb) = options.progress_callback {
1147                callback_ptr = cb;
1148                ffi::TSParseOptions {
1149                    payload: core::ptr::addr_of_mut!(callback_ptr).cast::<c_void>(),
1150                    progress_callback: Some(progress),
1151                }
1152            } else {
1153                empty_options
1154            }
1155        } else {
1156            empty_options
1157        };
1158
1159        // A pointer to this payload is passed on every call to the `read` C function.
1160        // The payload contains two things:
1161        // 1. A reference to the rust `callback`.
1162        // 2. The text that was returned from the previous call to `callback`. This allows the
1163        //    callback to return owned values like vectors.
1164        let mut payload: Payload<F, T> = (callback, None);
1165
1166        let c_input = ffi::TSInput {
1167            payload: core::ptr::addr_of_mut!(payload).cast::<c_void>(),
1168            read: Some(read::<T, F>),
1169            encoding: ffi::TSInputEncodingUTF16BE,
1170            decode: None,
1171        };
1172
1173        let c_old_tree = old_tree.map_or(ptr::null_mut(), |t| t.0.as_ptr());
1174        unsafe {
1175            let c_new_tree = ffi::ts_parser_parse_with_options(
1176                self.0.as_ptr(),
1177                c_old_tree,
1178                c_input,
1179                parse_options,
1180            );
1181
1182            NonNull::new(c_new_tree).map(Tree)
1183        }
1184    }
1185
1186    /// Parse text provided in chunks by a callback using a custom encoding.
1187    /// This is useful for parsing text in encodings that are not UTF-8 or UTF-16.
1188    ///
1189    /// # Arguments:
1190    /// * `callback` A function that takes a byte offset and position and returns a slice of text
1191    ///   starting at that byte offset and position. The slices can be of any length. If the given
1192    ///   position is at the end of the text, the callback should return an empty slice.
1193    /// * `old_tree` A previous syntax tree parsed from the same document. If the text of the
1194    ///   document has changed since `old_tree` was created, then you must edit `old_tree` to match
1195    ///   the new text using [`Tree::edit`].
1196    /// * `options` Options for parsing the text. This can be used to set a progress callback.
1197    ///
1198    /// Additionally, you must set the generic parameter [`D`] to a type that implements the
1199    /// [`Decode`] trait. This trait has a single method, [`decode`](Decode::decode), which takes a
1200    /// slice of bytes and returns a tuple of the code point and the number of bytes consumed.
1201    /// The `decode` method should return `-1` for the code point if decoding fails.
1202    pub fn parse_custom_encoding<D: Decode, T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>(
1203        &mut self,
1204        callback: &mut F,
1205        old_tree: Option<&Tree>,
1206        options: Option<ParseOptions>,
1207    ) -> Option<Tree> {
1208        type Payload<'a, F, T> = (&'a mut F, Option<T>);
1209
1210        unsafe extern "C" fn progress(state: *mut ffi::TSParseState) -> bool {
1211            let callback = (*state)
1212                .payload
1213                .cast::<ParseProgressCallback>()
1214                .as_mut()
1215                .unwrap();
1216            callback(&ParseState::from_raw(state))
1217        }
1218
1219        // At compile time, create a C-compatible callback that calls the custom `decode` method.
1220        unsafe extern "C" fn decode_fn<D: Decode>(
1221            data: *const u8,
1222            len: u32,
1223            code_point: *mut i32,
1224        ) -> u32 {
1225            let (c, len) = D::decode(core::slice::from_raw_parts(data, len as usize));
1226            if let Some(code_point) = code_point.as_mut() {
1227                *code_point = c;
1228            }
1229            len
1230        }
1231
1232        // This C function is passed to Tree-sitter as the input callback.
1233        unsafe extern "C" fn read<T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>(
1234            payload: *mut c_void,
1235            byte_offset: u32,
1236            position: ffi::TSPoint,
1237            bytes_read: *mut u32,
1238        ) -> *const c_char {
1239            let (callback, text) = payload.cast::<Payload<F, T>>().as_mut().unwrap();
1240            *text = Some(callback(byte_offset as usize, position.into()));
1241            let slice = text.as_ref().unwrap().as_ref();
1242            *bytes_read = slice.len() as u32;
1243            slice.as_ptr().cast::<c_char>()
1244        }
1245
1246        let empty_options = ffi::TSParseOptions {
1247            payload: ptr::null_mut(),
1248            progress_callback: None,
1249        };
1250
1251        let mut callback_ptr;
1252        let parse_options = if let Some(options) = options {
1253            if let Some(cb) = options.progress_callback {
1254                callback_ptr = cb;
1255                ffi::TSParseOptions {
1256                    payload: core::ptr::addr_of_mut!(callback_ptr).cast::<c_void>(),
1257                    progress_callback: Some(progress),
1258                }
1259            } else {
1260                empty_options
1261            }
1262        } else {
1263            empty_options
1264        };
1265
1266        // A pointer to this payload is passed on every call to the `read` C function.
1267        // The payload contains two things:
1268        // 1. A reference to the rust `callback`.
1269        // 2. The text that was returned from the previous call to `callback`. This allows the
1270        //    callback to return owned values like vectors.
1271        let mut payload: Payload<F, T> = (callback, None);
1272
1273        let c_input = ffi::TSInput {
1274            payload: core::ptr::addr_of_mut!(payload).cast::<c_void>(),
1275            read: Some(read::<T, F>),
1276            encoding: ffi::TSInputEncodingCustom,
1277            // Use this custom decode callback
1278            decode: Some(decode_fn::<D>),
1279        };
1280
1281        let c_old_tree = old_tree.map_or(ptr::null_mut(), |t| t.0.as_ptr());
1282        unsafe {
1283            let c_new_tree = ffi::ts_parser_parse_with_options(
1284                self.0.as_ptr(),
1285                c_old_tree,
1286                c_input,
1287                parse_options,
1288            );
1289
1290            NonNull::new(c_new_tree).map(Tree)
1291        }
1292    }
1293
1294    /// Instruct the parser to start the next parse from the beginning.
1295    ///
1296    /// If the parser previously failed because of a timeout, cancellation,
1297    /// or callback, then by default, it will resume where it left off on the
1298    /// next call to [`parse`](Parser::parse) or other parsing functions.
1299    /// If you don't want to resume, and instead intend to use this parser to
1300    /// parse some other document, you must call `reset` first.
1301    #[doc(alias = "ts_parser_reset")]
1302    pub fn reset(&mut self) {
1303        unsafe { ffi::ts_parser_reset(self.0.as_ptr()) }
1304    }
1305
1306    /// Get the duration in microseconds that parsing is allowed to take.
1307    ///
1308    /// This is set via [`set_timeout_micros`](Parser::set_timeout_micros).
1309    #[doc(alias = "ts_parser_timeout_micros")]
1310    #[deprecated(
1311        since = "0.25.0",
1312        note = "Prefer using `parse_with_options` and using a callback"
1313    )]
1314    #[must_use]
1315    pub fn timeout_micros(&self) -> u64 {
1316        unsafe { ffi::ts_parser_timeout_micros(self.0.as_ptr()) }
1317    }
1318
1319    /// Set the maximum duration in microseconds that parsing should be allowed
1320    /// to take before halting.
1321    ///
1322    /// If parsing takes longer than this, it will halt early, returning `None`.
1323    /// See [`parse`](Parser::parse) for more information.
1324    #[doc(alias = "ts_parser_set_timeout_micros")]
1325    #[deprecated(
1326        since = "0.25.0",
1327        note = "Prefer using `parse_with_options` and using a callback"
1328    )]
1329    pub fn set_timeout_micros(&mut self, timeout_micros: u64) {
1330        unsafe { ffi::ts_parser_set_timeout_micros(self.0.as_ptr(), timeout_micros) }
1331    }
1332
1333    /// Set the ranges of text that the parser should include when parsing.
1334    ///
1335    /// By default, the parser will always include entire documents. This
1336    /// function allows you to parse only a *portion* of a document but
1337    /// still return a syntax tree whose ranges match up with the document
1338    /// as a whole. You can also pass multiple disjoint ranges.
1339    ///
1340    /// If `ranges` is empty, then the entire document will be parsed.
1341    /// Otherwise, the given ranges must be ordered from earliest to latest
1342    /// in the document, and they must not overlap. That is, the following
1343    /// must hold for all `i` < `length - 1`:
1344    /// ```text
1345    ///     ranges[i].end_byte <= ranges[i + 1].start_byte
1346    /// ```
1347    /// If this requirement is not satisfied, method will return
1348    /// [`IncludedRangesError`] error with an offset in the passed ranges
1349    /// slice pointing to a first incorrect range.
1350    #[doc(alias = "ts_parser_set_included_ranges")]
1351    pub fn set_included_ranges(&mut self, ranges: &[Range]) -> Result<(), IncludedRangesError> {
1352        let ts_ranges = ranges.iter().copied().map(Into::into).collect::<Vec<_>>();
1353        let result = unsafe {
1354            ffi::ts_parser_set_included_ranges(
1355                self.0.as_ptr(),
1356                ts_ranges.as_ptr(),
1357                ts_ranges.len() as u32,
1358            )
1359        };
1360
1361        if result {
1362            Ok(())
1363        } else {
1364            let mut prev_end_byte = 0;
1365            for (i, range) in ranges.iter().enumerate() {
1366                if range.start_byte < prev_end_byte || range.end_byte < range.start_byte {
1367                    return Err(IncludedRangesError(i));
1368                }
1369                prev_end_byte = range.end_byte;
1370            }
1371            Err(IncludedRangesError(0))
1372        }
1373    }
1374
1375    /// Get the ranges of text that the parser will include when parsing.
1376    #[doc(alias = "ts_parser_included_ranges")]
1377    #[must_use]
1378    pub fn included_ranges(&self) -> Vec<Range> {
1379        let mut count = 0u32;
1380        unsafe {
1381            let ptr =
1382                ffi::ts_parser_included_ranges(self.0.as_ptr(), core::ptr::addr_of_mut!(count));
1383            let ranges = slice::from_raw_parts(ptr, count as usize);
1384            let result = ranges.iter().copied().map(Into::into).collect();
1385            result
1386        }
1387    }
1388
1389    /// Get the parser's current cancellation flag pointer.
1390    ///
1391    /// # Safety
1392    ///
1393    /// It uses FFI
1394    #[doc(alias = "ts_parser_cancellation_flag")]
1395    #[deprecated(
1396        since = "0.25.0",
1397        note = "Prefer using `parse_with_options` and using a callback"
1398    )]
1399    #[must_use]
1400    pub unsafe fn cancellation_flag(&self) -> Option<&AtomicUsize> {
1401        ffi::ts_parser_cancellation_flag(self.0.as_ptr())
1402            .cast::<AtomicUsize>()
1403            .as_ref()
1404    }
1405
1406    /// Set the parser's current cancellation flag pointer.
1407    ///
1408    /// If a pointer is assigned, then the parser will periodically read from
1409    /// this pointer during parsing. If it reads a non-zero value, it will halt
1410    /// early, returning `None`. See [`parse`](Parser::parse) for more
1411    /// information.
1412    ///
1413    /// # Safety
1414    ///
1415    /// It uses FFI
1416    #[doc(alias = "ts_parser_set_cancellation_flag")]
1417    #[deprecated(
1418        since = "0.25.0",
1419        note = "Prefer using `parse_with_options` and using a callback"
1420    )]
1421    pub unsafe fn set_cancellation_flag(&mut self, flag: Option<&AtomicUsize>) {
1422        if let Some(flag) = flag {
1423            ffi::ts_parser_set_cancellation_flag(
1424                self.0.as_ptr(),
1425                core::ptr::from_ref::<AtomicUsize>(flag).cast::<usize>(),
1426            );
1427        } else {
1428            ffi::ts_parser_set_cancellation_flag(self.0.as_ptr(), ptr::null());
1429        }
1430    }
1431}
1432
1433impl Drop for Parser {
1434    fn drop(&mut self) {
1435        #[cfg(feature = "std")]
1436        #[cfg(not(target_os = "wasi"))]
1437        {
1438            self.stop_printing_dot_graphs();
1439        }
1440        self.set_logger(None);
1441        unsafe { ffi::ts_parser_delete(self.0.as_ptr()) }
1442    }
1443}
1444
1445impl Tree {
1446    /// Get the root node of the syntax tree.
1447    #[doc(alias = "ts_tree_root_node")]
1448    #[must_use]
1449    pub fn root_node(&self) -> Node {
1450        Node::new(unsafe { ffi::ts_tree_root_node(self.0.as_ptr()) }).unwrap()
1451    }
1452
1453    /// Get the root node of the syntax tree, but with its position shifted
1454    /// forward by the given offset.
1455    #[doc(alias = "ts_tree_root_node_with_offset")]
1456    #[must_use]
1457    pub fn root_node_with_offset(&self, offset_bytes: usize, offset_extent: Point) -> Node {
1458        Node::new(unsafe {
1459            ffi::ts_tree_root_node_with_offset(
1460                self.0.as_ptr(),
1461                offset_bytes as u32,
1462                offset_extent.into(),
1463            )
1464        })
1465        .unwrap()
1466    }
1467
1468    /// Get the language that was used to parse the syntax tree.
1469    #[doc(alias = "ts_tree_language")]
1470    #[must_use]
1471    pub fn language(&self) -> LanguageRef {
1472        LanguageRef(
1473            unsafe { ffi::ts_tree_language(self.0.as_ptr()) },
1474            PhantomData,
1475        )
1476    }
1477
1478    /// Edit the syntax tree to keep it in sync with source code that has been
1479    /// edited.
1480    ///
1481    /// You must describe the edit both in terms of byte offsets and in terms of
1482    /// row/column coordinates.
1483    #[doc(alias = "ts_tree_edit")]
1484    pub fn edit(&mut self, edit: &InputEdit) {
1485        let edit = edit.into();
1486        unsafe { ffi::ts_tree_edit(self.0.as_ptr(), &edit) };
1487    }
1488
1489    /// Create a new [`TreeCursor`] starting from the root of the tree.
1490    #[must_use]
1491    pub fn walk(&self) -> TreeCursor {
1492        self.root_node().walk()
1493    }
1494
1495    /// Compare this old edited syntax tree to a new syntax tree representing
1496    /// the same document, returning a sequence of ranges whose syntactic
1497    /// structure has changed.
1498    ///
1499    /// For this to work correctly, this syntax tree must have been edited such
1500    /// that its ranges match up to the new tree. Generally, you'll want to
1501    /// call this method right after calling one of the [`Parser::parse`]
1502    /// functions. Call it on the old tree that was passed to parse, and
1503    /// pass the new tree that was returned from `parse`.
1504    #[doc(alias = "ts_tree_get_changed_ranges")]
1505    #[must_use]
1506    pub fn changed_ranges(&self, other: &Self) -> impl ExactSizeIterator<Item = Range> {
1507        let mut count = 0u32;
1508        unsafe {
1509            let ptr = ffi::ts_tree_get_changed_ranges(
1510                self.0.as_ptr(),
1511                other.0.as_ptr(),
1512                core::ptr::addr_of_mut!(count),
1513            );
1514            util::CBufferIter::new(ptr, count as usize).map(Into::into)
1515        }
1516    }
1517
1518    /// Get the included ranges that were used to parse the syntax tree.
1519    #[doc(alias = "ts_tree_included_ranges")]
1520    #[must_use]
1521    pub fn included_ranges(&self) -> Vec<Range> {
1522        let mut count = 0u32;
1523        unsafe {
1524            let ptr = ffi::ts_tree_included_ranges(self.0.as_ptr(), core::ptr::addr_of_mut!(count));
1525            let ranges = slice::from_raw_parts(ptr, count as usize);
1526            let result = ranges.iter().copied().map(Into::into).collect();
1527            (FREE_FN)(ptr.cast::<c_void>());
1528            result
1529        }
1530    }
1531
1532    /// Print a graph of the tree to the given file descriptor.
1533    /// The graph is formatted in the DOT language. You may want to pipe this
1534    /// graph directly to a `dot(1)` process in order to generate SVG
1535    /// output.
1536    #[doc(alias = "ts_tree_print_dot_graph")]
1537    #[cfg(not(target_os = "wasi"))]
1538    #[cfg(feature = "std")]
1539    #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
1540    pub fn print_dot_graph(
1541        &self,
1542        #[cfg(unix)] file: &impl AsRawFd,
1543        #[cfg(windows)] file: &impl AsRawHandle,
1544    ) {
1545        #[cfg(unix)]
1546        {
1547            let fd = file.as_raw_fd();
1548            unsafe { ffi::ts_tree_print_dot_graph(self.0.as_ptr(), fd) }
1549        }
1550
1551        #[cfg(windows)]
1552        {
1553            let handle = file.as_raw_handle();
1554            unsafe { ffi::ts_tree_print_dot_graph(self.0.as_ptr(), handle as i32) }
1555        }
1556    }
1557}
1558
1559impl fmt::Debug for Tree {
1560    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1561        write!(f, "{{Tree {:?}}}", self.root_node())
1562    }
1563}
1564
1565impl Drop for Tree {
1566    fn drop(&mut self) {
1567        unsafe { ffi::ts_tree_delete(self.0.as_ptr()) }
1568    }
1569}
1570
1571impl Clone for Tree {
1572    fn clone(&self) -> Self {
1573        unsafe { Self(NonNull::new_unchecked(ffi::ts_tree_copy(self.0.as_ptr()))) }
1574    }
1575}
1576
1577impl<'tree> Node<'tree> {
1578    fn new(node: ffi::TSNode) -> Option<Self> {
1579        (!node.id.is_null()).then_some(Node(node, PhantomData))
1580    }
1581
1582    /// Get a numeric id for this node that is unique.
1583    ///
1584    /// Within a given syntax tree, no two nodes have the same id. However:
1585    ///
1586    /// - If a new tree is created based on an older tree, and a node from the old tree is reused in
1587    ///   the process, then that node will have the same id in both trees.
1588    ///
1589    /// - A node not marked as having changes does not guarantee it was reused.
1590    ///
1591    /// - If a node is marked as having changed in the old tree, it will not be reused.
1592    #[must_use]
1593    pub fn id(&self) -> usize {
1594        self.0.id as usize
1595    }
1596
1597    /// Get this node's type as a numerical id.
1598    #[doc(alias = "ts_node_symbol")]
1599    #[must_use]
1600    pub fn kind_id(&self) -> u16 {
1601        unsafe { ffi::ts_node_symbol(self.0) }
1602    }
1603
1604    /// Get the node's type as a numerical id as it appears in the grammar
1605    /// ignoring aliases.
1606    #[doc(alias = "ts_node_grammar_symbol")]
1607    #[must_use]
1608    pub fn grammar_id(&self) -> u16 {
1609        unsafe { ffi::ts_node_grammar_symbol(self.0) }
1610    }
1611
1612    /// Get this node's type as a string.
1613    #[doc(alias = "ts_node_type")]
1614    #[must_use]
1615    pub fn kind(&self) -> &'static str {
1616        unsafe { CStr::from_ptr(ffi::ts_node_type(self.0)) }
1617            .to_str()
1618            .unwrap()
1619    }
1620
1621    /// Get this node's symbol name as it appears in the grammar ignoring
1622    /// aliases as a string.
1623    #[doc(alias = "ts_node_grammar_type")]
1624    #[must_use]
1625    pub fn grammar_name(&self) -> &'static str {
1626        unsafe { CStr::from_ptr(ffi::ts_node_grammar_type(self.0)) }
1627            .to_str()
1628            .unwrap()
1629    }
1630
1631    /// Get the [`Language`] that was used to parse this node's syntax tree.
1632    #[doc(alias = "ts_node_language")]
1633    #[must_use]
1634    pub fn language(&self) -> LanguageRef {
1635        LanguageRef(unsafe { ffi::ts_node_language(self.0) }, PhantomData)
1636    }
1637
1638    /// Check if this node is *named*.
1639    ///
1640    /// Named nodes correspond to named rules in the grammar, whereas
1641    /// *anonymous* nodes correspond to string literals in the grammar.
1642    #[doc(alias = "ts_node_is_named")]
1643    #[must_use]
1644    pub fn is_named(&self) -> bool {
1645        unsafe { ffi::ts_node_is_named(self.0) }
1646    }
1647
1648    /// Check if this node is *extra*.
1649    ///
1650    /// Extra nodes represent things like comments, which are not required by the
1651    /// grammar, but can appear anywhere.
1652    #[doc(alias = "ts_node_is_extra")]
1653    #[must_use]
1654    pub fn is_extra(&self) -> bool {
1655        unsafe { ffi::ts_node_is_extra(self.0) }
1656    }
1657
1658    /// Check if this node has been edited.
1659    #[doc(alias = "ts_node_has_changes")]
1660    #[must_use]
1661    pub fn has_changes(&self) -> bool {
1662        unsafe { ffi::ts_node_has_changes(self.0) }
1663    }
1664
1665    /// Check if this node represents a syntax error or contains any syntax
1666    /// errors anywhere within it.
1667    #[doc(alias = "ts_node_has_error")]
1668    #[must_use]
1669    pub fn has_error(&self) -> bool {
1670        unsafe { ffi::ts_node_has_error(self.0) }
1671    }
1672
1673    /// Check if this node represents a syntax error.
1674    ///
1675    /// Syntax errors represent parts of the code that could not be incorporated
1676    /// into a valid syntax tree.
1677    #[doc(alias = "ts_node_is_error")]
1678    #[must_use]
1679    pub fn is_error(&self) -> bool {
1680        unsafe { ffi::ts_node_is_error(self.0) }
1681    }
1682
1683    /// Get this node's parse state.
1684    #[doc(alias = "ts_node_parse_state")]
1685    #[must_use]
1686    pub fn parse_state(&self) -> u16 {
1687        unsafe { ffi::ts_node_parse_state(self.0) }
1688    }
1689
1690    /// Get the parse state after this node.
1691    #[doc(alias = "ts_node_next_parse_state")]
1692    #[must_use]
1693    pub fn next_parse_state(&self) -> u16 {
1694        unsafe { ffi::ts_node_next_parse_state(self.0) }
1695    }
1696
1697    /// Check if this node is *missing*.
1698    ///
1699    /// Missing nodes are inserted by the parser in order to recover from
1700    /// certain kinds of syntax errors.
1701    #[doc(alias = "ts_node_is_missing")]
1702    #[must_use]
1703    pub fn is_missing(&self) -> bool {
1704        unsafe { ffi::ts_node_is_missing(self.0) }
1705    }
1706
1707    /// Get the byte offset where this node starts.
1708    #[doc(alias = "ts_node_start_byte")]
1709    #[must_use]
1710    pub fn start_byte(&self) -> usize {
1711        unsafe { ffi::ts_node_start_byte(self.0) as usize }
1712    }
1713
1714    /// Get the byte offset where this node ends.
1715    #[doc(alias = "ts_node_end_byte")]
1716    #[must_use]
1717    pub fn end_byte(&self) -> usize {
1718        unsafe { ffi::ts_node_end_byte(self.0) as usize }
1719    }
1720
1721    /// Get the byte range of source code that this node represents.
1722    #[must_use]
1723    pub fn byte_range(&self) -> core::ops::Range<usize> {
1724        self.start_byte()..self.end_byte()
1725    }
1726
1727    /// Get the range of source code that this node represents, both in terms of
1728    /// raw bytes and of row/column coordinates.
1729    #[must_use]
1730    pub fn range(&self) -> Range {
1731        Range {
1732            start_byte: self.start_byte(),
1733            end_byte: self.end_byte(),
1734            start_point: self.start_position(),
1735            end_point: self.end_position(),
1736        }
1737    }
1738
1739    /// Get this node's start position in terms of rows and columns.
1740    #[doc(alias = "ts_node_start_point")]
1741    #[must_use]
1742    pub fn start_position(&self) -> Point {
1743        let result = unsafe { ffi::ts_node_start_point(self.0) };
1744        result.into()
1745    }
1746
1747    /// Get this node's end position in terms of rows and columns.
1748    #[doc(alias = "ts_node_end_point")]
1749    #[must_use]
1750    pub fn end_position(&self) -> Point {
1751        let result = unsafe { ffi::ts_node_end_point(self.0) };
1752        result.into()
1753    }
1754
1755    /// Get the node's child at the given index, where zero represents the first
1756    /// child.
1757    ///
1758    /// This method is fairly fast, but its cost is technically log(i), so if
1759    /// you might be iterating over a long list of children, you should use
1760    /// [`Node::children`] instead.
1761    #[doc(alias = "ts_node_child")]
1762    #[must_use]
1763    pub fn child(&self, i: usize) -> Option<Self> {
1764        Self::new(unsafe { ffi::ts_node_child(self.0, i as u32) })
1765    }
1766
1767    /// Get this node's number of children.
1768    #[doc(alias = "ts_node_child_count")]
1769    #[must_use]
1770    pub fn child_count(&self) -> usize {
1771        unsafe { ffi::ts_node_child_count(self.0) as usize }
1772    }
1773
1774    /// Get this node's *named* child at the given index.
1775    ///
1776    /// See also [`Node::is_named`].
1777    /// This method is fairly fast, but its cost is technically log(i), so if
1778    /// you might be iterating over a long list of children, you should use
1779    /// [`Node::named_children`] instead.
1780    #[doc(alias = "ts_node_named_child")]
1781    #[must_use]
1782    pub fn named_child(&self, i: usize) -> Option<Self> {
1783        Self::new(unsafe { ffi::ts_node_named_child(self.0, i as u32) })
1784    }
1785
1786    /// Get this node's number of *named* children.
1787    ///
1788    /// See also [`Node::is_named`].
1789    #[doc(alias = "ts_node_named_child_count")]
1790    #[must_use]
1791    pub fn named_child_count(&self) -> usize {
1792        unsafe { ffi::ts_node_named_child_count(self.0) as usize }
1793    }
1794
1795    /// Get the first child with the given field name.
1796    ///
1797    /// If multiple children may have the same field name, access them using
1798    /// [`children_by_field_name`](Node::children_by_field_name)
1799    #[doc(alias = "ts_node_child_by_field_name")]
1800    #[must_use]
1801    pub fn child_by_field_name(&self, field_name: impl AsRef<[u8]>) -> Option<Self> {
1802        let field_name = field_name.as_ref();
1803        Self::new(unsafe {
1804            ffi::ts_node_child_by_field_name(
1805                self.0,
1806                field_name.as_ptr().cast::<c_char>(),
1807                field_name.len() as u32,
1808            )
1809        })
1810    }
1811
1812    /// Get this node's child with the given numerical field id.
1813    ///
1814    /// See also [`child_by_field_name`](Node::child_by_field_name). You can
1815    /// convert a field name to an id using [`Language::field_id_for_name`].
1816    #[doc(alias = "ts_node_child_by_field_id")]
1817    #[must_use]
1818    pub fn child_by_field_id(&self, field_id: u16) -> Option<Self> {
1819        Self::new(unsafe { ffi::ts_node_child_by_field_id(self.0, field_id) })
1820    }
1821
1822    /// Get the field name of this node's child at the given index.
1823    #[doc(alias = "ts_node_field_name_for_child")]
1824    #[must_use]
1825    pub fn field_name_for_child(&self, child_index: u32) -> Option<&'static str> {
1826        unsafe {
1827            let ptr = ffi::ts_node_field_name_for_child(self.0, child_index);
1828            (!ptr.is_null()).then(|| CStr::from_ptr(ptr).to_str().unwrap())
1829        }
1830    }
1831
1832    /// Get the field name of this node's named child at the given index.
1833    #[must_use]
1834    pub fn field_name_for_named_child(&self, named_child_index: u32) -> Option<&'static str> {
1835        unsafe {
1836            let ptr = ffi::ts_node_field_name_for_named_child(self.0, named_child_index);
1837            (!ptr.is_null()).then(|| CStr::from_ptr(ptr).to_str().unwrap())
1838        }
1839    }
1840
1841    /// Iterate over this node's children.
1842    ///
1843    /// A [`TreeCursor`] is used to retrieve the children efficiently. Obtain
1844    /// a [`TreeCursor`] by calling [`Tree::walk`] or [`Node::walk`]. To avoid
1845    /// unnecessary allocations, you should reuse the same cursor for
1846    /// subsequent calls to this method.
1847    ///
1848    /// If you're walking the tree recursively, you may want to use the
1849    /// [`TreeCursor`] APIs directly instead.
1850    pub fn children<'cursor>(
1851        &self,
1852        cursor: &'cursor mut TreeCursor<'tree>,
1853    ) -> impl ExactSizeIterator<Item = Node<'tree>> + 'cursor {
1854        cursor.reset(*self);
1855        cursor.goto_first_child();
1856        (0..self.child_count()).map(move |_| {
1857            let result = cursor.node();
1858            cursor.goto_next_sibling();
1859            result
1860        })
1861    }
1862
1863    /// Iterate over this node's named children.
1864    ///
1865    /// See also [`Node::children`].
1866    pub fn named_children<'cursor>(
1867        &self,
1868        cursor: &'cursor mut TreeCursor<'tree>,
1869    ) -> impl ExactSizeIterator<Item = Node<'tree>> + 'cursor {
1870        cursor.reset(*self);
1871        cursor.goto_first_child();
1872        (0..self.named_child_count()).map(move |_| {
1873            while !cursor.node().is_named() {
1874                if !cursor.goto_next_sibling() {
1875                    break;
1876                }
1877            }
1878            let result = cursor.node();
1879            cursor.goto_next_sibling();
1880            result
1881        })
1882    }
1883
1884    /// Iterate over this node's children with a given field name.
1885    ///
1886    /// See also [`Node::children`].
1887    pub fn children_by_field_name<'cursor>(
1888        &self,
1889        field_name: &str,
1890        cursor: &'cursor mut TreeCursor<'tree>,
1891    ) -> impl Iterator<Item = Node<'tree>> + 'cursor {
1892        let field_id = self.language().field_id_for_name(field_name);
1893        let mut done = field_id.is_none();
1894        if !done {
1895            cursor.reset(*self);
1896            cursor.goto_first_child();
1897        }
1898        iter::from_fn(move || {
1899            if !done {
1900                while cursor.field_id() != field_id {
1901                    if !cursor.goto_next_sibling() {
1902                        return None;
1903                    }
1904                }
1905                let result = cursor.node();
1906                if !cursor.goto_next_sibling() {
1907                    done = true;
1908                }
1909                return Some(result);
1910            }
1911            None
1912        })
1913    }
1914
1915    /// Iterate over this node's children with a given field id.
1916    ///
1917    /// See also [`Node::children_by_field_name`].
1918    pub fn children_by_field_id<'cursor>(
1919        &self,
1920        field_id: FieldId,
1921        cursor: &'cursor mut TreeCursor<'tree>,
1922    ) -> impl Iterator<Item = Node<'tree>> + 'cursor {
1923        cursor.reset(*self);
1924        cursor.goto_first_child();
1925        let mut done = false;
1926        iter::from_fn(move || {
1927            if !done {
1928                while cursor.field_id() != Some(field_id) {
1929                    if !cursor.goto_next_sibling() {
1930                        return None;
1931                    }
1932                }
1933                let result = cursor.node();
1934                if !cursor.goto_next_sibling() {
1935                    done = true;
1936                }
1937                return Some(result);
1938            }
1939            None
1940        })
1941    }
1942
1943    /// Get this node's immediate parent.
1944    /// Prefer [`child_with_descendant`](Node::child_with_descendant)
1945    /// for iterating over this node's ancestors.
1946    #[doc(alias = "ts_node_parent")]
1947    #[must_use]
1948    pub fn parent(&self) -> Option<Self> {
1949        Self::new(unsafe { ffi::ts_node_parent(self.0) })
1950    }
1951
1952    /// Get the node that contains `descendant`.
1953    ///
1954    /// Note that this can return `descendant` itself.
1955    #[doc(alias = "ts_node_child_with_descendant")]
1956    #[must_use]
1957    pub fn child_with_descendant(&self, descendant: Self) -> Option<Self> {
1958        Self::new(unsafe { ffi::ts_node_child_with_descendant(self.0, descendant.0) })
1959    }
1960
1961    /// Get this node's next sibling.
1962    #[doc(alias = "ts_node_next_sibling")]
1963    #[must_use]
1964    pub fn next_sibling(&self) -> Option<Self> {
1965        Self::new(unsafe { ffi::ts_node_next_sibling(self.0) })
1966    }
1967
1968    /// Get this node's previous sibling.
1969    #[doc(alias = "ts_node_prev_sibling")]
1970    #[must_use]
1971    pub fn prev_sibling(&self) -> Option<Self> {
1972        Self::new(unsafe { ffi::ts_node_prev_sibling(self.0) })
1973    }
1974
1975    /// Get this node's next named sibling.
1976    #[doc(alias = "ts_node_next_named_sibling")]
1977    #[must_use]
1978    pub fn next_named_sibling(&self) -> Option<Self> {
1979        Self::new(unsafe { ffi::ts_node_next_named_sibling(self.0) })
1980    }
1981
1982    /// Get this node's previous named sibling.
1983    #[doc(alias = "ts_node_prev_named_sibling")]
1984    #[must_use]
1985    pub fn prev_named_sibling(&self) -> Option<Self> {
1986        Self::new(unsafe { ffi::ts_node_prev_named_sibling(self.0) })
1987    }
1988
1989    /// Get this node's first child that contains or starts after the given byte offset.
1990    #[doc(alias = "ts_node_first_child_for_byte")]
1991    #[must_use]
1992    pub fn first_child_for_byte(&self, byte: usize) -> Option<Self> {
1993        Self::new(unsafe { ffi::ts_node_first_child_for_byte(self.0, byte as u32) })
1994    }
1995
1996    /// Get this node's first named child that contains or starts after the given byte offset.
1997    #[doc(alias = "ts_node_first_named_child_for_point")]
1998    #[must_use]
1999    pub fn first_named_child_for_byte(&self, byte: usize) -> Option<Self> {
2000        Self::new(unsafe { ffi::ts_node_first_named_child_for_byte(self.0, byte as u32) })
2001    }
2002
2003    /// Get the node's number of descendants, including one for the node itself.
2004    #[doc(alias = "ts_node_descendant_count")]
2005    #[must_use]
2006    pub fn descendant_count(&self) -> usize {
2007        unsafe { ffi::ts_node_descendant_count(self.0) as usize }
2008    }
2009
2010    /// Get the smallest node within this node that spans the given byte range.
2011    #[doc(alias = "ts_node_descendant_for_byte_range")]
2012    #[must_use]
2013    pub fn descendant_for_byte_range(&self, start: usize, end: usize) -> Option<Self> {
2014        Self::new(unsafe {
2015            ffi::ts_node_descendant_for_byte_range(self.0, start as u32, end as u32)
2016        })
2017    }
2018
2019    /// Get the smallest named node within this node that spans the given byte range.
2020    #[doc(alias = "ts_node_named_descendant_for_byte_range")]
2021    #[must_use]
2022    pub fn named_descendant_for_byte_range(&self, start: usize, end: usize) -> Option<Self> {
2023        Self::new(unsafe {
2024            ffi::ts_node_named_descendant_for_byte_range(self.0, start as u32, end as u32)
2025        })
2026    }
2027
2028    /// Get the smallest node within this node that spans the given point range.
2029    #[doc(alias = "ts_node_descendant_for_point_range")]
2030    #[must_use]
2031    pub fn descendant_for_point_range(&self, start: Point, end: Point) -> Option<Self> {
2032        Self::new(unsafe {
2033            ffi::ts_node_descendant_for_point_range(self.0, start.into(), end.into())
2034        })
2035    }
2036
2037    /// Get the smallest named node within this node that spans the given point range.
2038    #[doc(alias = "ts_node_named_descendant_for_point_range")]
2039    #[must_use]
2040    pub fn named_descendant_for_point_range(&self, start: Point, end: Point) -> Option<Self> {
2041        Self::new(unsafe {
2042            ffi::ts_node_named_descendant_for_point_range(self.0, start.into(), end.into())
2043        })
2044    }
2045
2046    /// Get an S-expression representing the node.
2047    #[doc(alias = "ts_node_string")]
2048    #[must_use]
2049    pub fn to_sexp(&self) -> String {
2050        let c_string = unsafe { ffi::ts_node_string(self.0) };
2051        let result = unsafe { CStr::from_ptr(c_string) }
2052            .to_str()
2053            .unwrap()
2054            .to_string();
2055        unsafe { (FREE_FN)(c_string.cast::<c_void>()) };
2056        result
2057    }
2058
2059    pub fn utf8_text<'a>(&self, source: &'a [u8]) -> Result<&'a str, str::Utf8Error> {
2060        str::from_utf8(&source[self.start_byte()..self.end_byte()])
2061    }
2062
2063    #[must_use]
2064    pub fn utf16_text<'a>(&self, source: &'a [u16]) -> &'a [u16] {
2065        &source[self.start_byte()..self.end_byte()]
2066    }
2067
2068    /// Create a new [`TreeCursor`] starting from this node.
2069    ///
2070    /// Note that the given node is considered the root of the cursor,
2071    /// and the cursor cannot walk outside this node.
2072    #[doc(alias = "ts_tree_cursor_new")]
2073    #[must_use]
2074    pub fn walk(&self) -> TreeCursor<'tree> {
2075        TreeCursor(unsafe { ffi::ts_tree_cursor_new(self.0) }, PhantomData)
2076    }
2077
2078    /// Edit this node to keep it in-sync with source code that has been edited.
2079    ///
2080    /// This function is only rarely needed. When you edit a syntax tree with
2081    /// the [`Tree::edit`] method, all of the nodes that you retrieve from
2082    /// the tree afterward will already reflect the edit. You only need to
2083    /// use [`Node::edit`] when you have a specific [`Node`] instance that
2084    /// you want to keep and continue to use after an edit.
2085    #[doc(alias = "ts_node_edit")]
2086    pub fn edit(&mut self, edit: &InputEdit) {
2087        let edit = edit.into();
2088        unsafe { ffi::ts_node_edit(core::ptr::addr_of_mut!(self.0), &edit) }
2089    }
2090}
2091
2092impl PartialEq for Node<'_> {
2093    fn eq(&self, other: &Self) -> bool {
2094        core::ptr::eq(self.0.id, other.0.id)
2095    }
2096}
2097
2098impl Eq for Node<'_> {}
2099
2100impl hash::Hash for Node<'_> {
2101    fn hash<H: hash::Hasher>(&self, state: &mut H) {
2102        self.0.id.hash(state);
2103        self.0.context[0].hash(state);
2104        self.0.context[1].hash(state);
2105        self.0.context[2].hash(state);
2106        self.0.context[3].hash(state);
2107    }
2108}
2109
2110impl fmt::Debug for Node<'_> {
2111    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2112        write!(
2113            f,
2114            "{{Node {} {} - {}}}",
2115            self.kind(),
2116            self.start_position(),
2117            self.end_position()
2118        )
2119    }
2120}
2121
2122impl fmt::Display for Node<'_> {
2123    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2124        let sexp = self.to_sexp();
2125        if sexp.is_empty() {
2126            write!(f, "")
2127        } else if !f.alternate() {
2128            write!(f, "{sexp}")
2129        } else {
2130            write!(f, "{}", format_sexp(&sexp, f.width().unwrap_or(0)))
2131        }
2132    }
2133}
2134
2135impl<'cursor> TreeCursor<'cursor> {
2136    /// Get the tree cursor's current [`Node`].
2137    #[doc(alias = "ts_tree_cursor_current_node")]
2138    #[must_use]
2139    pub fn node(&self) -> Node<'cursor> {
2140        Node(
2141            unsafe { ffi::ts_tree_cursor_current_node(&self.0) },
2142            PhantomData,
2143        )
2144    }
2145
2146    /// Get the numerical field id of this tree cursor's current node.
2147    ///
2148    /// See also [`field_name`](TreeCursor::field_name).
2149    #[doc(alias = "ts_tree_cursor_current_field_id")]
2150    #[must_use]
2151    pub fn field_id(&self) -> Option<FieldId> {
2152        let id = unsafe { ffi::ts_tree_cursor_current_field_id(&self.0) };
2153        FieldId::new(id)
2154    }
2155
2156    /// Get the field name of this tree cursor's current node.
2157    #[doc(alias = "ts_tree_cursor_current_field_name")]
2158    #[must_use]
2159    pub fn field_name(&self) -> Option<&'static str> {
2160        unsafe {
2161            let ptr = ffi::ts_tree_cursor_current_field_name(&self.0);
2162            (!ptr.is_null()).then(|| CStr::from_ptr(ptr).to_str().unwrap())
2163        }
2164    }
2165
2166    /// Get the depth of the cursor's current node relative to the original
2167    /// node that the cursor was constructed with.
2168    #[doc(alias = "ts_tree_cursor_current_depth")]
2169    #[must_use]
2170    pub fn depth(&self) -> u32 {
2171        unsafe { ffi::ts_tree_cursor_current_depth(&self.0) }
2172    }
2173
2174    /// Get the index of the cursor's current node out of all of the
2175    /// descendants of the original node that the cursor was constructed with
2176    #[doc(alias = "ts_tree_cursor_current_descendant_index")]
2177    #[must_use]
2178    pub fn descendant_index(&self) -> usize {
2179        unsafe { ffi::ts_tree_cursor_current_descendant_index(&self.0) as usize }
2180    }
2181
2182    /// Move this cursor to the first child of its current node.
2183    ///
2184    /// This returns `true` if the cursor successfully moved, and returns
2185    /// `false` if there were no children.
2186    #[doc(alias = "ts_tree_cursor_goto_first_child")]
2187    pub fn goto_first_child(&mut self) -> bool {
2188        unsafe { ffi::ts_tree_cursor_goto_first_child(&mut self.0) }
2189    }
2190
2191    /// Move this cursor to the last child of its current node.
2192    ///
2193    /// This returns `true` if the cursor successfully moved, and returns
2194    /// `false` if there were no children.
2195    ///
2196    /// Note that this function may be slower than
2197    /// [`goto_first_child`](TreeCursor::goto_first_child) because it needs to
2198    /// iterate through all the children to compute the child's position.
2199    #[doc(alias = "ts_tree_cursor_goto_last_child")]
2200    pub fn goto_last_child(&mut self) -> bool {
2201        unsafe { ffi::ts_tree_cursor_goto_last_child(&mut self.0) }
2202    }
2203
2204    /// Move this cursor to the parent of its current node.
2205    ///
2206    /// This returns `true` if the cursor successfully moved, and returns
2207    /// `false` if there was no parent node (the cursor was already on the
2208    /// root node).
2209    ///
2210    /// Note that the node the cursor was constructed with is considered the root
2211    /// of the cursor, and the cursor cannot walk outside this node.
2212    #[doc(alias = "ts_tree_cursor_goto_parent")]
2213    pub fn goto_parent(&mut self) -> bool {
2214        unsafe { ffi::ts_tree_cursor_goto_parent(&mut self.0) }
2215    }
2216
2217    /// Move this cursor to the next sibling of its current node.
2218    ///
2219    /// This returns `true` if the cursor successfully moved, and returns
2220    /// `false` if there was no next sibling node.
2221    ///
2222    /// Note that the node the cursor was constructed with is considered the root
2223    /// of the cursor, and the cursor cannot walk outside this node.
2224    #[doc(alias = "ts_tree_cursor_goto_next_sibling")]
2225    pub fn goto_next_sibling(&mut self) -> bool {
2226        unsafe { ffi::ts_tree_cursor_goto_next_sibling(&mut self.0) }
2227    }
2228
2229    /// Move the cursor to the node that is the nth descendant of
2230    /// the original node that the cursor was constructed with, where
2231    /// zero represents the original node itself.
2232    #[doc(alias = "ts_tree_cursor_goto_descendant")]
2233    pub fn goto_descendant(&mut self, descendant_index: usize) {
2234        unsafe { ffi::ts_tree_cursor_goto_descendant(&mut self.0, descendant_index as u32) }
2235    }
2236
2237    /// Move this cursor to the previous sibling of its current node.
2238    ///
2239    /// This returns `true` if the cursor successfully moved, and returns
2240    /// `false` if there was no previous sibling node.
2241    ///
2242    /// Note, that this function may be slower than
2243    /// [`goto_next_sibling`](TreeCursor::goto_next_sibling) due to how node
2244    /// positions are stored. In the worst case, this will need to iterate
2245    /// through all the children up to the previous sibling node to recalculate
2246    /// its position. Also note that the node the cursor was constructed with is
2247    /// considered the root of the cursor, and the cursor cannot walk outside this node.
2248    #[doc(alias = "ts_tree_cursor_goto_previous_sibling")]
2249    pub fn goto_previous_sibling(&mut self) -> bool {
2250        unsafe { ffi::ts_tree_cursor_goto_previous_sibling(&mut self.0) }
2251    }
2252
2253    /// Move this cursor to the first child of its current node that contains or
2254    /// starts after the given byte offset.
2255    ///
2256    /// This returns the index of the child node if one was found, and returns
2257    /// `None` if no such child was found.
2258    #[doc(alias = "ts_tree_cursor_goto_first_child_for_byte")]
2259    pub fn goto_first_child_for_byte(&mut self, index: usize) -> Option<usize> {
2260        let result =
2261            unsafe { ffi::ts_tree_cursor_goto_first_child_for_byte(&mut self.0, index as u32) };
2262        result.try_into().ok()
2263    }
2264
2265    /// Move this cursor to the first child of its current node that contains or
2266    /// starts after the given byte offset.
2267    ///
2268    /// This returns the index of the child node if one was found, and returns
2269    /// `None` if no such child was found.
2270    #[doc(alias = "ts_tree_cursor_goto_first_child_for_point")]
2271    pub fn goto_first_child_for_point(&mut self, point: Point) -> Option<usize> {
2272        let result =
2273            unsafe { ffi::ts_tree_cursor_goto_first_child_for_point(&mut self.0, point.into()) };
2274        result.try_into().ok()
2275    }
2276
2277    /// Re-initialize this tree cursor to start at the original node that the
2278    /// cursor was constructed with.
2279    #[doc(alias = "ts_tree_cursor_reset")]
2280    pub fn reset(&mut self, node: Node<'cursor>) {
2281        unsafe { ffi::ts_tree_cursor_reset(&mut self.0, node.0) };
2282    }
2283
2284    /// Re-initialize a tree cursor to the same position as another cursor.
2285    ///
2286    /// Unlike [`reset`](TreeCursor::reset), this will not lose parent
2287    /// information and allows reusing already created cursors.
2288    #[doc(alias = "ts_tree_cursor_reset_to")]
2289    pub fn reset_to(&mut self, cursor: &Self) {
2290        unsafe { ffi::ts_tree_cursor_reset_to(&mut self.0, &cursor.0) };
2291    }
2292}
2293
2294impl Clone for TreeCursor<'_> {
2295    fn clone(&self) -> Self {
2296        TreeCursor(unsafe { ffi::ts_tree_cursor_copy(&self.0) }, PhantomData)
2297    }
2298}
2299
2300impl Drop for TreeCursor<'_> {
2301    fn drop(&mut self) {
2302        unsafe { ffi::ts_tree_cursor_delete(&mut self.0) }
2303    }
2304}
2305
2306impl LookaheadIterator {
2307    /// Get the current language of the lookahead iterator.
2308    #[doc(alias = "ts_lookahead_iterator_language")]
2309    #[must_use]
2310    pub fn language(&self) -> LanguageRef<'_> {
2311        LanguageRef(
2312            unsafe { ffi::ts_lookahead_iterator_language(self.0.as_ptr()) },
2313            PhantomData,
2314        )
2315    }
2316
2317    /// Get the current symbol of the lookahead iterator.
2318    #[doc(alias = "ts_lookahead_iterator_current_symbol")]
2319    #[must_use]
2320    pub fn current_symbol(&self) -> u16 {
2321        unsafe { ffi::ts_lookahead_iterator_current_symbol(self.0.as_ptr()) }
2322    }
2323
2324    /// Get the current symbol name of the lookahead iterator.
2325    #[doc(alias = "ts_lookahead_iterator_current_symbol_name")]
2326    #[must_use]
2327    pub fn current_symbol_name(&self) -> &'static str {
2328        unsafe {
2329            CStr::from_ptr(ffi::ts_lookahead_iterator_current_symbol_name(
2330                self.0.as_ptr(),
2331            ))
2332            .to_str()
2333            .unwrap()
2334        }
2335    }
2336
2337    /// Reset the lookahead iterator.
2338    ///
2339    /// This returns `true` if the language was set successfully and `false`
2340    /// otherwise.
2341    #[doc(alias = "ts_lookahead_iterator_reset")]
2342    pub fn reset(&mut self, language: &Language, state: u16) -> bool {
2343        unsafe { ffi::ts_lookahead_iterator_reset(self.0.as_ptr(), language.0, state) }
2344    }
2345
2346    /// Reset the lookahead iterator to another state.
2347    ///
2348    /// This returns `true` if the iterator was reset to the given state and
2349    /// `false` otherwise.
2350    #[doc(alias = "ts_lookahead_iterator_reset_state")]
2351    pub fn reset_state(&mut self, state: u16) -> bool {
2352        unsafe { ffi::ts_lookahead_iterator_reset_state(self.0.as_ptr(), state) }
2353    }
2354
2355    /// Iterate symbol names.
2356    pub fn iter_names(&mut self) -> impl Iterator<Item = &'static str> + '_ {
2357        LookaheadNamesIterator(self)
2358    }
2359}
2360
2361impl Iterator for LookaheadNamesIterator<'_> {
2362    type Item = &'static str;
2363
2364    #[doc(alias = "ts_lookahead_iterator_next")]
2365    fn next(&mut self) -> Option<Self::Item> {
2366        unsafe { ffi::ts_lookahead_iterator_next(self.0 .0.as_ptr()) }
2367            .then(|| self.0.current_symbol_name())
2368    }
2369}
2370
2371impl Iterator for LookaheadIterator {
2372    type Item = u16;
2373
2374    #[doc(alias = "ts_lookahead_iterator_next")]
2375    fn next(&mut self) -> Option<Self::Item> {
2376        // the first symbol is always `0` so we can safely skip it
2377        unsafe { ffi::ts_lookahead_iterator_next(self.0.as_ptr()) }.then(|| self.current_symbol())
2378    }
2379}
2380
2381impl Drop for LookaheadIterator {
2382    #[doc(alias = "ts_lookahead_iterator_delete")]
2383    fn drop(&mut self) {
2384        unsafe { ffi::ts_lookahead_iterator_delete(self.0.as_ptr()) }
2385    }
2386}
2387
2388impl Query {
2389    /// Create a new query from a string containing one or more S-expression
2390    /// patterns.
2391    ///
2392    /// The query is associated with a particular language, and can only be run
2393    /// on syntax nodes parsed with that language. References to Queries can be
2394    /// shared between multiple threads.
2395    pub fn new(language: &Language, source: &str) -> Result<Self, QueryError> {
2396        let mut error_offset = 0u32;
2397        let mut error_type: ffi::TSQueryError = 0;
2398        let bytes = source.as_bytes();
2399
2400        // Compile the query.
2401        let ptr = unsafe {
2402            ffi::ts_query_new(
2403                language.0,
2404                bytes.as_ptr().cast::<c_char>(),
2405                bytes.len() as u32,
2406                core::ptr::addr_of_mut!(error_offset),
2407                core::ptr::addr_of_mut!(error_type),
2408            )
2409        };
2410
2411        // On failure, build an error based on the error code and offset.
2412        if ptr.is_null() {
2413            if error_type == ffi::TSQueryErrorLanguage {
2414                return Err(QueryError {
2415                    row: 0,
2416                    column: 0,
2417                    offset: 0,
2418                    message: LanguageError {
2419                        version: language.abi_version(),
2420                    }
2421                    .to_string(),
2422                    kind: QueryErrorKind::Language,
2423                });
2424            }
2425
2426            let offset = error_offset as usize;
2427            let mut line_start = 0;
2428            let mut row = 0;
2429            let mut line_containing_error = None;
2430            for line in source.lines() {
2431                let line_end = line_start + line.len() + 1;
2432                if line_end > offset {
2433                    line_containing_error = Some(line);
2434                    break;
2435                }
2436                line_start = line_end;
2437                row += 1;
2438            }
2439            let column = offset - line_start;
2440
2441            let kind;
2442            let message;
2443            match error_type {
2444                // Error types that report names
2445                ffi::TSQueryErrorNodeType | ffi::TSQueryErrorField | ffi::TSQueryErrorCapture => {
2446                    let suffix = source.split_at(offset).1;
2447                    let in_quotes = source.as_bytes()[offset - 1] == b'"';
2448                    let mut backslashes = 0;
2449                    let end_offset = suffix
2450                        .find(|c| {
2451                            if in_quotes {
2452                                if c == '"' && backslashes % 2 == 0 {
2453                                    true
2454                                } else if c == '\\' {
2455                                    backslashes += 1;
2456                                    false
2457                                } else {
2458                                    backslashes = 0;
2459                                    false
2460                                }
2461                            } else {
2462                                !char::is_alphanumeric(c) && c != '_' && c != '-'
2463                            }
2464                        })
2465                        .unwrap_or(suffix.len());
2466                    message = suffix.split_at(end_offset).0.to_string();
2467                    kind = match error_type {
2468                        ffi::TSQueryErrorNodeType => QueryErrorKind::NodeType,
2469                        ffi::TSQueryErrorField => QueryErrorKind::Field,
2470                        ffi::TSQueryErrorCapture => QueryErrorKind::Capture,
2471                        _ => unreachable!(),
2472                    };
2473                }
2474
2475                // Error types that report positions
2476                _ => {
2477                    message = line_containing_error.map_or_else(
2478                        || "Unexpected EOF".to_string(),
2479                        |line| line.to_string() + "\n" + &" ".repeat(offset - line_start) + "^",
2480                    );
2481                    kind = match error_type {
2482                        ffi::TSQueryErrorStructure => QueryErrorKind::Structure,
2483                        _ => QueryErrorKind::Syntax,
2484                    };
2485                }
2486            }
2487
2488            return Err(QueryError {
2489                row,
2490                column,
2491                offset,
2492                message,
2493                kind,
2494            });
2495        }
2496
2497        unsafe { Self::from_raw_parts(ptr, source) }
2498    }
2499
2500    #[doc(hidden)]
2501    unsafe fn from_raw_parts(ptr: *mut ffi::TSQuery, source: &str) -> Result<Self, QueryError> {
2502        let ptr = {
2503            struct TSQueryDrop(*mut ffi::TSQuery);
2504            impl Drop for TSQueryDrop {
2505                fn drop(&mut self) {
2506                    unsafe { ffi::ts_query_delete(self.0) }
2507                }
2508            }
2509            TSQueryDrop(ptr)
2510        };
2511
2512        let string_count = unsafe { ffi::ts_query_string_count(ptr.0) };
2513        let capture_count = unsafe { ffi::ts_query_capture_count(ptr.0) };
2514        let pattern_count = unsafe { ffi::ts_query_pattern_count(ptr.0) as usize };
2515
2516        let mut capture_names = Vec::with_capacity(capture_count as usize);
2517        let mut capture_quantifiers_vec = Vec::with_capacity(pattern_count as usize);
2518        let mut text_predicates_vec = Vec::with_capacity(pattern_count);
2519        let mut property_predicates_vec = Vec::with_capacity(pattern_count);
2520        let mut property_settings_vec = Vec::with_capacity(pattern_count);
2521        let mut general_predicates_vec = Vec::with_capacity(pattern_count);
2522
2523        // Build a vector of strings to store the capture names.
2524        for i in 0..capture_count {
2525            unsafe {
2526                let mut length = 0u32;
2527                let name =
2528                    ffi::ts_query_capture_name_for_id(ptr.0, i, core::ptr::addr_of_mut!(length))
2529                        .cast::<u8>();
2530                let name = slice::from_raw_parts(name, length as usize);
2531                let name = str::from_utf8_unchecked(name);
2532                capture_names.push(name);
2533            }
2534        }
2535
2536        // Build a vector to store capture quantifiers.
2537        for i in 0..pattern_count {
2538            let mut capture_quantifiers = Vec::with_capacity(capture_count as usize);
2539            for j in 0..capture_count {
2540                unsafe {
2541                    let quantifier = ffi::ts_query_capture_quantifier_for_id(ptr.0, i as u32, j);
2542                    capture_quantifiers.push(quantifier.into());
2543                }
2544            }
2545            capture_quantifiers_vec.push(capture_quantifiers.into());
2546        }
2547
2548        // Build a vector of strings to represent literal values used in predicates.
2549        let string_values = (0..string_count)
2550            .map(|i| unsafe {
2551                let mut length = 0u32;
2552                let value =
2553                    ffi::ts_query_string_value_for_id(ptr.0, i, core::ptr::addr_of_mut!(length))
2554                        .cast::<u8>();
2555                let value = slice::from_raw_parts(value, length as usize);
2556                let value = str::from_utf8_unchecked(value);
2557                value
2558            })
2559            .collect::<Vec<_>>();
2560
2561        // Build a vector of predicates for each pattern.
2562        for i in 0..pattern_count {
2563            let predicate_steps = unsafe {
2564                let mut length = 0u32;
2565                let raw_predicates = ffi::ts_query_predicates_for_pattern(
2566                    ptr.0,
2567                    i as u32,
2568                    core::ptr::addr_of_mut!(length),
2569                );
2570                (length > 0)
2571                    .then(|| slice::from_raw_parts(raw_predicates, length as usize))
2572                    .unwrap_or_default()
2573            };
2574
2575            let byte_offset = unsafe { ffi::ts_query_start_byte_for_pattern(ptr.0, i as u32) };
2576            let row = source
2577                .char_indices()
2578                .take_while(|(i, _)| *i < byte_offset as usize)
2579                .filter(|(_, c)| *c == '\n')
2580                .count();
2581
2582            use ffi::TSQueryPredicateStepType as T;
2583            const TYPE_DONE: T = ffi::TSQueryPredicateStepTypeDone;
2584            const TYPE_CAPTURE: T = ffi::TSQueryPredicateStepTypeCapture;
2585            const TYPE_STRING: T = ffi::TSQueryPredicateStepTypeString;
2586
2587            let mut text_predicates = Vec::new();
2588            let mut property_predicates = Vec::new();
2589            let mut property_settings = Vec::new();
2590            let mut general_predicates = Vec::new();
2591            for p in predicate_steps.split(|s| s.type_ == TYPE_DONE) {
2592                if p.is_empty() {
2593                    continue;
2594                }
2595
2596                if p[0].type_ != TYPE_STRING {
2597                    return Err(predicate_error(
2598                        row,
2599                        format!(
2600                            "Expected predicate to start with a function name. Got @{}.",
2601                            capture_names[p[0].value_id as usize],
2602                        ),
2603                    ));
2604                }
2605
2606                // Build a predicate for each of the known predicate function names.
2607                let operator_name = string_values[p[0].value_id as usize];
2608                match operator_name {
2609                    "eq?" | "not-eq?" | "any-eq?" | "any-not-eq?" => {
2610                        if p.len() != 3 {
2611                            return Err(predicate_error(
2612                                row,
2613                                format!(
2614                                "Wrong number of arguments to #eq? predicate. Expected 2, got {}.",
2615                                p.len() - 1
2616                            ),
2617                            ));
2618                        }
2619                        if p[1].type_ != TYPE_CAPTURE {
2620                            return Err(predicate_error(row, format!(
2621                                "First argument to #eq? predicate must be a capture name. Got literal \"{}\".",
2622                                string_values[p[1].value_id as usize],
2623                            )));
2624                        }
2625
2626                        let is_positive = operator_name == "eq?" || operator_name == "any-eq?";
2627                        let match_all = match operator_name {
2628                            "eq?" | "not-eq?" => true,
2629                            "any-eq?" | "any-not-eq?" => false,
2630                            _ => unreachable!(),
2631                        };
2632                        text_predicates.push(if p[2].type_ == TYPE_CAPTURE {
2633                            TextPredicateCapture::EqCapture(
2634                                p[1].value_id,
2635                                p[2].value_id,
2636                                is_positive,
2637                                match_all,
2638                            )
2639                        } else {
2640                            TextPredicateCapture::EqString(
2641                                p[1].value_id,
2642                                string_values[p[2].value_id as usize].to_string().into(),
2643                                is_positive,
2644                                match_all,
2645                            )
2646                        });
2647                    }
2648
2649                    "match?" | "not-match?" | "any-match?" | "any-not-match?" => {
2650                        if p.len() != 3 {
2651                            return Err(predicate_error(row, format!(
2652                                "Wrong number of arguments to #match? predicate. Expected 2, got {}.",
2653                                p.len() - 1
2654                            )));
2655                        }
2656                        if p[1].type_ != TYPE_CAPTURE {
2657                            return Err(predicate_error(row, format!(
2658                                "First argument to #match? predicate must be a capture name. Got literal \"{}\".",
2659                                string_values[p[1].value_id as usize],
2660                            )));
2661                        }
2662                        if p[2].type_ == TYPE_CAPTURE {
2663                            return Err(predicate_error(row, format!(
2664                                "Second argument to #match? predicate must be a literal. Got capture @{}.",
2665                                capture_names[p[2].value_id as usize],
2666                            )));
2667                        }
2668
2669                        let is_positive =
2670                            operator_name == "match?" || operator_name == "any-match?";
2671                        let match_all = match operator_name {
2672                            "match?" | "not-match?" => true,
2673                            "any-match?" | "any-not-match?" => false,
2674                            _ => unreachable!(),
2675                        };
2676                        let regex = &string_values[p[2].value_id as usize];
2677                        text_predicates.push(TextPredicateCapture::MatchString(
2678                            p[1].value_id,
2679                            regex::bytes::Regex::new(regex).map_err(|_| {
2680                                predicate_error(row, format!("Invalid regex '{regex}'"))
2681                            })?,
2682                            is_positive,
2683                            match_all,
2684                        ));
2685                    }
2686
2687                    "set!" => property_settings.push(Self::parse_property(
2688                        row,
2689                        operator_name,
2690                        &capture_names,
2691                        &string_values,
2692                        &p[1..],
2693                    )?),
2694
2695                    "is?" | "is-not?" => property_predicates.push((
2696                        Self::parse_property(
2697                            row,
2698                            operator_name,
2699                            &capture_names,
2700                            &string_values,
2701                            &p[1..],
2702                        )?,
2703                        operator_name == "is?",
2704                    )),
2705
2706                    "any-of?" | "not-any-of?" => {
2707                        if p.len() < 2 {
2708                            return Err(predicate_error(row, format!(
2709                                "Wrong number of arguments to #any-of? predicate. Expected at least 1, got {}.",
2710                                p.len() - 1
2711                            )));
2712                        }
2713                        if p[1].type_ != TYPE_CAPTURE {
2714                            return Err(predicate_error(row, format!(
2715                                "First argument to #any-of? predicate must be a capture name. Got literal \"{}\".",
2716                                string_values[p[1].value_id as usize],
2717                            )));
2718                        }
2719
2720                        let is_positive = operator_name == "any-of?";
2721                        let mut values = Vec::new();
2722                        for arg in &p[2..] {
2723                            if arg.type_ == TYPE_CAPTURE {
2724                                return Err(predicate_error(row, format!(
2725                                    "Arguments to #any-of? predicate must be literals. Got capture @{}.",
2726                                    capture_names[arg.value_id as usize],
2727                                )));
2728                            }
2729                            values.push(string_values[arg.value_id as usize]);
2730                        }
2731                        text_predicates.push(TextPredicateCapture::AnyString(
2732                            p[1].value_id,
2733                            values
2734                                .iter()
2735                                .map(|x| (*x).to_string().into())
2736                                .collect::<Vec<_>>()
2737                                .into(),
2738                            is_positive,
2739                        ));
2740                    }
2741
2742                    _ => general_predicates.push(QueryPredicate {
2743                        operator: operator_name.to_string().into(),
2744                        args: p[1..]
2745                            .iter()
2746                            .map(|a| {
2747                                if a.type_ == TYPE_CAPTURE {
2748                                    QueryPredicateArg::Capture(a.value_id)
2749                                } else {
2750                                    QueryPredicateArg::String(
2751                                        string_values[a.value_id as usize].to_string().into(),
2752                                    )
2753                                }
2754                            })
2755                            .collect(),
2756                    }),
2757                }
2758            }
2759
2760            text_predicates_vec.push(text_predicates.into());
2761            property_predicates_vec.push(property_predicates.into());
2762            property_settings_vec.push(property_settings.into());
2763            general_predicates_vec.push(general_predicates.into());
2764        }
2765
2766        let result = Self {
2767            ptr: unsafe { NonNull::new_unchecked(ptr.0) },
2768            capture_names: capture_names.into(),
2769            capture_quantifiers: capture_quantifiers_vec.into(),
2770            text_predicates: text_predicates_vec.into(),
2771            property_predicates: property_predicates_vec.into(),
2772            property_settings: property_settings_vec.into(),
2773            general_predicates: general_predicates_vec.into(),
2774        };
2775
2776        core::mem::forget(ptr);
2777
2778        Ok(result)
2779    }
2780
2781    /// Get the byte offset where the given pattern starts in the query's
2782    /// source.
2783    #[doc(alias = "ts_query_start_byte_for_pattern")]
2784    #[must_use]
2785    pub fn start_byte_for_pattern(&self, pattern_index: usize) -> usize {
2786        assert!(
2787            pattern_index < self.text_predicates.len(),
2788            "Pattern index is {pattern_index} but the pattern count is {}",
2789            self.text_predicates.len(),
2790        );
2791        unsafe {
2792            ffi::ts_query_start_byte_for_pattern(self.ptr.as_ptr(), pattern_index as u32) as usize
2793        }
2794    }
2795
2796    /// Get the byte offset where the given pattern ends in the query's
2797    /// source.
2798    #[doc(alias = "ts_query_end_byte_for_pattern")]
2799    #[must_use]
2800    pub fn end_byte_for_pattern(&self, pattern_index: usize) -> usize {
2801        assert!(
2802            pattern_index < self.text_predicates.len(),
2803            "Pattern index is {pattern_index} but the pattern count is {}",
2804            self.text_predicates.len(),
2805        );
2806        unsafe {
2807            ffi::ts_query_end_byte_for_pattern(self.ptr.as_ptr(), pattern_index as u32) as usize
2808        }
2809    }
2810
2811    /// Get the number of patterns in the query.
2812    #[doc(alias = "ts_query_pattern_count")]
2813    #[must_use]
2814    pub fn pattern_count(&self) -> usize {
2815        unsafe { ffi::ts_query_pattern_count(self.ptr.as_ptr()) as usize }
2816    }
2817
2818    /// Get the names of the captures used in the query.
2819    #[must_use]
2820    pub const fn capture_names(&self) -> &[&str] {
2821        &self.capture_names
2822    }
2823
2824    /// Get the quantifiers of the captures used in the query.
2825    #[must_use]
2826    pub const fn capture_quantifiers(&self, index: usize) -> &[CaptureQuantifier] {
2827        &self.capture_quantifiers[index]
2828    }
2829
2830    /// Get the index for a given capture name.
2831    #[must_use]
2832    pub fn capture_index_for_name(&self, name: &str) -> Option<u32> {
2833        self.capture_names
2834            .iter()
2835            .position(|n| *n == name)
2836            .map(|ix| ix as u32)
2837    }
2838
2839    /// Get the properties that are checked for the given pattern index.
2840    ///
2841    /// This includes predicates with the operators `is?` and `is-not?`.
2842    #[must_use]
2843    pub const fn property_predicates(&self, index: usize) -> &[(QueryProperty, bool)] {
2844        &self.property_predicates[index]
2845    }
2846
2847    /// Get the properties that are set for the given pattern index.
2848    ///
2849    /// This includes predicates with the operator `set!`.
2850    #[must_use]
2851    pub const fn property_settings(&self, index: usize) -> &[QueryProperty] {
2852        &self.property_settings[index]
2853    }
2854
2855    /// Get the other user-defined predicates associated with the given index.
2856    ///
2857    /// This includes predicate with operators other than:
2858    /// * `match?`
2859    /// * `eq?` and `not-eq?`
2860    /// * `is?` and `is-not?`
2861    /// * `set!`
2862    #[must_use]
2863    pub const fn general_predicates(&self, index: usize) -> &[QueryPredicate] {
2864        &self.general_predicates[index]
2865    }
2866
2867    /// Disable a certain capture within a query.
2868    ///
2869    /// This prevents the capture from being returned in matches, and also
2870    /// avoids any resource usage associated with recording the capture.
2871    #[doc(alias = "ts_query_disable_capture")]
2872    pub fn disable_capture(&mut self, name: &str) {
2873        unsafe {
2874            ffi::ts_query_disable_capture(
2875                self.ptr.as_ptr(),
2876                name.as_bytes().as_ptr().cast::<c_char>(),
2877                name.len() as u32,
2878            );
2879        }
2880    }
2881
2882    /// Disable a certain pattern within a query.
2883    ///
2884    /// This prevents the pattern from matching, and also avoids any resource
2885    /// usage associated with the pattern.
2886    #[doc(alias = "ts_query_disable_pattern")]
2887    pub fn disable_pattern(&mut self, index: usize) {
2888        unsafe { ffi::ts_query_disable_pattern(self.ptr.as_ptr(), index as u32) }
2889    }
2890
2891    /// Check if a given pattern within a query has a single root node.
2892    #[doc(alias = "ts_query_is_pattern_rooted")]
2893    #[must_use]
2894    pub fn is_pattern_rooted(&self, index: usize) -> bool {
2895        unsafe { ffi::ts_query_is_pattern_rooted(self.ptr.as_ptr(), index as u32) }
2896    }
2897
2898    /// Check if a given pattern within a query has a single root node.
2899    #[doc(alias = "ts_query_is_pattern_non_local")]
2900    #[must_use]
2901    pub fn is_pattern_non_local(&self, index: usize) -> bool {
2902        unsafe { ffi::ts_query_is_pattern_non_local(self.ptr.as_ptr(), index as u32) }
2903    }
2904
2905    /// Check if a given step in a query is 'definite'.
2906    ///
2907    /// A query step is 'definite' if its parent pattern will be guaranteed to
2908    /// match successfully once it reaches the step.
2909    #[doc(alias = "ts_query_is_pattern_guaranteed_at_step")]
2910    #[must_use]
2911    pub fn is_pattern_guaranteed_at_step(&self, byte_offset: usize) -> bool {
2912        unsafe {
2913            ffi::ts_query_is_pattern_guaranteed_at_step(self.ptr.as_ptr(), byte_offset as u32)
2914        }
2915    }
2916
2917    fn parse_property(
2918        row: usize,
2919        function_name: &str,
2920        capture_names: &[&str],
2921        string_values: &[&str],
2922        args: &[ffi::TSQueryPredicateStep],
2923    ) -> Result<QueryProperty, QueryError> {
2924        if args.is_empty() || args.len() > 3 {
2925            return Err(predicate_error(
2926                row,
2927                format!(
2928                    "Wrong number of arguments to {function_name} predicate. Expected 1 to 3, got {}.",
2929                    args.len(),
2930                ),
2931            ));
2932        }
2933
2934        let mut capture_id = None;
2935        let mut key = None;
2936        let mut value = None;
2937
2938        for arg in args {
2939            if arg.type_ == ffi::TSQueryPredicateStepTypeCapture {
2940                if capture_id.is_some() {
2941                    return Err(predicate_error(
2942                        row,
2943                        format!(
2944                            "Invalid arguments to {function_name} predicate. Unexpected second capture name @{}",
2945                            capture_names[arg.value_id as usize]
2946                        ),
2947                    ));
2948                }
2949                capture_id = Some(arg.value_id as usize);
2950            } else if key.is_none() {
2951                key = Some(&string_values[arg.value_id as usize]);
2952            } else if value.is_none() {
2953                value = Some(string_values[arg.value_id as usize]);
2954            } else {
2955                return Err(predicate_error(
2956                    row,
2957                    format!(
2958                        "Invalid arguments to {function_name} predicate. Unexpected third argument @{}",
2959                        string_values[arg.value_id as usize]
2960                    ),
2961                ));
2962            }
2963        }
2964
2965        if let Some(key) = key {
2966            Ok(QueryProperty::new(key, value, capture_id))
2967        } else {
2968            Err(predicate_error(
2969                row,
2970                format!("Invalid arguments to {function_name} predicate. Missing key argument",),
2971            ))
2972        }
2973    }
2974}
2975
2976impl Default for QueryCursor {
2977    fn default() -> Self {
2978        Self::new()
2979    }
2980}
2981
2982impl QueryCursor {
2983    /// Create a new cursor for executing a given query.
2984    ///
2985    /// The cursor stores the state that is needed to iteratively search for
2986    /// matches.
2987    #[doc(alias = "ts_query_cursor_new")]
2988    #[must_use]
2989    pub fn new() -> Self {
2990        Self {
2991            ptr: unsafe { NonNull::new_unchecked(ffi::ts_query_cursor_new()) },
2992        }
2993    }
2994
2995    /// Return the maximum number of in-progress matches for this cursor.
2996    #[doc(alias = "ts_query_cursor_match_limit")]
2997    #[must_use]
2998    pub fn match_limit(&self) -> u32 {
2999        unsafe { ffi::ts_query_cursor_match_limit(self.ptr.as_ptr()) }
3000    }
3001
3002    /// Set the maximum number of in-progress matches for this cursor.  The
3003    /// limit must be > 0 and <= 65536.
3004    #[doc(alias = "ts_query_cursor_set_match_limit")]
3005    pub fn set_match_limit(&mut self, limit: u32) {
3006        unsafe {
3007            ffi::ts_query_cursor_set_match_limit(self.ptr.as_ptr(), limit);
3008        }
3009    }
3010
3011    /// Set the maximum duration in microseconds that query execution should be allowed to
3012    /// take before halting.
3013    ///
3014    /// If query execution takes longer than this, it will halt early, returning None.
3015    #[doc(alias = "ts_query_cursor_set_timeout_micros")]
3016    #[deprecated(
3017        since = "0.25.0",
3018        note = "Prefer using `matches_with_options` or `captures_with_options` and using a callback"
3019    )]
3020    pub fn set_timeout_micros(&mut self, timeout: u64) {
3021        unsafe {
3022            ffi::ts_query_cursor_set_timeout_micros(self.ptr.as_ptr(), timeout);
3023        }
3024    }
3025
3026    /// Get the duration in microseconds that query execution is allowed to take.
3027    ///
3028    /// This is set via [`set_timeout_micros`](QueryCursor::set_timeout_micros).
3029    #[doc(alias = "ts_query_cursor_timeout_micros")]
3030    #[deprecated(
3031        since = "0.25.0",
3032        note = "Prefer using `matches_with_options` or `captures_with_options` and using a callback"
3033    )]
3034    #[must_use]
3035    pub fn timeout_micros(&self) -> u64 {
3036        unsafe { ffi::ts_query_cursor_timeout_micros(self.ptr.as_ptr()) }
3037    }
3038
3039    /// Check if, on its last execution, this cursor exceeded its maximum number
3040    /// of in-progress matches.
3041    #[doc(alias = "ts_query_cursor_did_exceed_match_limit")]
3042    #[must_use]
3043    pub fn did_exceed_match_limit(&self) -> bool {
3044        unsafe { ffi::ts_query_cursor_did_exceed_match_limit(self.ptr.as_ptr()) }
3045    }
3046
3047    /// Iterate over all of the matches in the order that they were found.
3048    ///
3049    /// Each match contains the index of the pattern that matched, and a list of
3050    /// captures. Because multiple patterns can match the same set of nodes,
3051    /// one match may contain captures that appear *before* some of the
3052    /// captures from a previous match.
3053    ///
3054    /// Iterating over a `QueryMatches` object requires the `StreamingIterator`
3055    /// or `StreamingIteratorMut` trait to be in scope. This can be done via
3056    /// `use tree_sitter::StreamingIterator` or `use tree_sitter::StreamingIteratorMut`
3057    #[doc(alias = "ts_query_cursor_exec")]
3058    pub fn matches<'query, 'cursor: 'query, 'tree, T: TextProvider<I>, I: AsRef<[u8]>>(
3059        &'cursor mut self,
3060        query: &'query Query,
3061        node: Node<'tree>,
3062        text_provider: T,
3063    ) -> QueryMatches<'query, 'tree, T, I> {
3064        let ptr = self.ptr.as_ptr();
3065        unsafe { ffi::ts_query_cursor_exec(ptr, query.ptr.as_ptr(), node.0) };
3066        QueryMatches {
3067            ptr,
3068            query,
3069            text_provider,
3070            buffer1: Vec::default(),
3071            buffer2: Vec::default(),
3072            current_match: None,
3073            _options: None,
3074            _phantom: PhantomData,
3075        }
3076    }
3077
3078    /// Iterate over all of the matches in the order that they were found, with options.
3079    ///
3080    /// Each match contains the index of the pattern that matched, and a list of
3081    /// captures. Because multiple patterns can match the same set of nodes,
3082    /// one match may contain captures that appear *before* some of the
3083    /// captures from a previous match.
3084    #[doc(alias = "ts_query_cursor_exec_with_options")]
3085    pub fn matches_with_options<
3086        'query,
3087        'cursor: 'query,
3088        'tree,
3089        T: TextProvider<I>,
3090        I: AsRef<[u8]>,
3091    >(
3092        &'cursor mut self,
3093        query: &'query Query,
3094        node: Node<'tree>,
3095        text_provider: T,
3096        options: QueryCursorOptions,
3097    ) -> QueryMatches<'query, 'tree, T, I> {
3098        unsafe extern "C" fn progress(state: *mut ffi::TSQueryCursorState) -> bool {
3099            let callback = (*state)
3100                .payload
3101                .cast::<QueryProgressCallback>()
3102                .as_mut()
3103                .unwrap();
3104            (callback)(&QueryCursorState::from_raw(state))
3105        }
3106
3107        let query_options = options.progress_callback.map(|cb| {
3108            QueryCursorOptionsDrop(Box::into_raw(Box::new(ffi::TSQueryCursorOptions {
3109                payload: Box::into_raw(Box::new(cb)).cast::<c_void>(),
3110                progress_callback: Some(progress),
3111            })))
3112        });
3113
3114        let ptr = self.ptr.as_ptr();
3115        unsafe {
3116            ffi::ts_query_cursor_exec_with_options(
3117                ptr,
3118                query.ptr.as_ptr(),
3119                node.0,
3120                query_options.as_ref().map_or(ptr::null_mut(), |q| q.0),
3121            );
3122        }
3123        QueryMatches {
3124            ptr,
3125            query,
3126            text_provider,
3127            buffer1: Vec::default(),
3128            buffer2: Vec::default(),
3129            current_match: None,
3130            _options: query_options,
3131            _phantom: PhantomData,
3132        }
3133    }
3134
3135    /// Iterate over all of the individual captures in the order that they
3136    /// appear.
3137    ///
3138    /// This is useful if you don't care about which pattern matched, and just
3139    /// want a single, ordered sequence of captures.
3140    ///
3141    /// Iterating over a `QueryCaptures` object requires the `StreamingIterator`
3142    /// or `StreamingIteratorMut` trait to be in scope. This can be done via
3143    /// `use tree_sitter::StreamingIterator` or `use tree_sitter::StreamingIteratorMut`
3144    #[doc(alias = "ts_query_cursor_exec")]
3145    pub fn captures<'query, 'cursor: 'query, 'tree, T: TextProvider<I>, I: AsRef<[u8]>>(
3146        &'cursor mut self,
3147        query: &'query Query,
3148        node: Node<'tree>,
3149        text_provider: T,
3150    ) -> QueryCaptures<'query, 'tree, T, I> {
3151        let ptr = self.ptr.as_ptr();
3152        unsafe { ffi::ts_query_cursor_exec(ptr, query.ptr.as_ptr(), node.0) };
3153        QueryCaptures {
3154            ptr,
3155            query,
3156            text_provider,
3157            buffer1: Vec::default(),
3158            buffer2: Vec::default(),
3159            current_match: None,
3160            _options: None,
3161            _phantom: PhantomData,
3162        }
3163    }
3164
3165    /// Iterate over all of the individual captures in the order that they
3166    /// appear, with options.
3167    ///
3168    /// This is useful if you don't care about which pattern matched, and just
3169    /// want a single, ordered sequence of captures.
3170    #[doc(alias = "ts_query_cursor_exec")]
3171    pub fn captures_with_options<
3172        'query,
3173        'cursor: 'query,
3174        'tree,
3175        T: TextProvider<I>,
3176        I: AsRef<[u8]>,
3177    >(
3178        &'cursor mut self,
3179        query: &'query Query,
3180        node: Node<'tree>,
3181        text_provider: T,
3182        options: QueryCursorOptions,
3183    ) -> QueryCaptures<'query, 'tree, T, I> {
3184        unsafe extern "C" fn progress(state: *mut ffi::TSQueryCursorState) -> bool {
3185            let callback = (*state)
3186                .payload
3187                .cast::<QueryProgressCallback>()
3188                .as_mut()
3189                .unwrap();
3190            (callback)(&QueryCursorState::from_raw(state))
3191        }
3192
3193        let query_options = options.progress_callback.map(|cb| {
3194            QueryCursorOptionsDrop(Box::into_raw(Box::new(ffi::TSQueryCursorOptions {
3195                payload: Box::into_raw(Box::new(cb)).cast::<c_void>(),
3196                progress_callback: Some(progress),
3197            })))
3198        });
3199
3200        let ptr = self.ptr.as_ptr();
3201        unsafe {
3202            ffi::ts_query_cursor_exec_with_options(
3203                ptr,
3204                query.ptr.as_ptr(),
3205                node.0,
3206                query_options.as_ref().map_or(ptr::null_mut(), |q| q.0),
3207            );
3208        }
3209        QueryCaptures {
3210            ptr,
3211            query,
3212            text_provider,
3213            buffer1: Vec::default(),
3214            buffer2: Vec::default(),
3215            current_match: None,
3216            _options: query_options,
3217            _phantom: PhantomData,
3218        }
3219    }
3220
3221    /// Set the range in which the query will be executed, in terms of byte
3222    /// offsets.
3223    #[doc(alias = "ts_query_cursor_set_byte_range")]
3224    pub fn set_byte_range(&mut self, range: ops::Range<usize>) -> &mut Self {
3225        unsafe {
3226            ffi::ts_query_cursor_set_byte_range(
3227                self.ptr.as_ptr(),
3228                range.start as u32,
3229                range.end as u32,
3230            );
3231        }
3232        self
3233    }
3234
3235    /// Set the range in which the query will be executed, in terms of rows and
3236    /// columns.
3237    #[doc(alias = "ts_query_cursor_set_point_range")]
3238    pub fn set_point_range(&mut self, range: ops::Range<Point>) -> &mut Self {
3239        unsafe {
3240            ffi::ts_query_cursor_set_point_range(
3241                self.ptr.as_ptr(),
3242                range.start.into(),
3243                range.end.into(),
3244            );
3245        }
3246        self
3247    }
3248
3249    /// Set the maximum start depth for a query cursor.
3250    ///
3251    /// This prevents cursors from exploring children nodes at a certain depth.
3252    /// Note if a pattern includes many children, then they will still be
3253    /// checked.
3254    ///
3255    /// The zero max start depth value can be used as a special behavior and
3256    /// it helps to destructure a subtree by staying on a node and using
3257    /// captures for interested parts. Note that the zero max start depth
3258    /// only limit a search depth for a pattern's root node but other nodes
3259    /// that are parts of the pattern may be searched at any depth what
3260    /// defined by the pattern structure.
3261    ///
3262    /// Set to `None` to remove the maximum start depth.
3263    #[doc(alias = "ts_query_cursor_set_max_start_depth")]
3264    pub fn set_max_start_depth(&mut self, max_start_depth: Option<u32>) -> &mut Self {
3265        unsafe {
3266            ffi::ts_query_cursor_set_max_start_depth(
3267                self.ptr.as_ptr(),
3268                max_start_depth.unwrap_or(u32::MAX),
3269            );
3270        }
3271        self
3272    }
3273}
3274
3275impl<'tree> QueryMatch<'_, 'tree> {
3276    #[must_use]
3277    pub const fn id(&self) -> u32 {
3278        self.id
3279    }
3280
3281    #[doc(alias = "ts_query_cursor_remove_match")]
3282    pub fn remove(&self) {
3283        unsafe { ffi::ts_query_cursor_remove_match(self.cursor, self.id) }
3284    }
3285
3286    pub fn nodes_for_capture_index(
3287        &self,
3288        capture_ix: u32,
3289    ) -> impl Iterator<Item = Node<'tree>> + '_ {
3290        self.captures
3291            .iter()
3292            .filter_map(move |capture| (capture.index == capture_ix).then_some(capture.node))
3293    }
3294
3295    fn new(m: &ffi::TSQueryMatch, cursor: *mut ffi::TSQueryCursor) -> Self {
3296        QueryMatch {
3297            cursor,
3298            id: m.id,
3299            pattern_index: m.pattern_index as usize,
3300            captures: (m.capture_count > 0)
3301                .then(|| unsafe {
3302                    slice::from_raw_parts(
3303                        m.captures.cast::<QueryCapture<'tree>>(),
3304                        m.capture_count as usize,
3305                    )
3306                })
3307                .unwrap_or_default(),
3308        }
3309    }
3310
3311    pub fn satisfies_text_predicates<I: AsRef<[u8]>>(
3312        &self,
3313        query: &Query,
3314        buffer1: &mut Vec<u8>,
3315        buffer2: &mut Vec<u8>,
3316        text_provider: &mut impl TextProvider<I>,
3317    ) -> bool {
3318        struct NodeText<'a, T> {
3319            buffer: &'a mut Vec<u8>,
3320            first_chunk: Option<T>,
3321        }
3322        impl<'a, T: AsRef<[u8]>> NodeText<'a, T> {
3323            fn new(buffer: &'a mut Vec<u8>) -> Self {
3324                Self {
3325                    buffer,
3326                    first_chunk: None,
3327                }
3328            }
3329
3330            fn get_text(&mut self, chunks: &mut impl Iterator<Item = T>) -> &[u8] {
3331                self.first_chunk = chunks.next();
3332                if let Some(next_chunk) = chunks.next() {
3333                    self.buffer.clear();
3334                    self.buffer
3335                        .extend_from_slice(self.first_chunk.as_ref().unwrap().as_ref());
3336                    self.buffer.extend_from_slice(next_chunk.as_ref());
3337                    for chunk in chunks {
3338                        self.buffer.extend_from_slice(chunk.as_ref());
3339                    }
3340                    self.buffer.as_slice()
3341                } else if let Some(ref first_chunk) = self.first_chunk {
3342                    first_chunk.as_ref()
3343                } else {
3344                    &[]
3345                }
3346            }
3347        }
3348
3349        let mut node_text1 = NodeText::new(buffer1);
3350        let mut node_text2 = NodeText::new(buffer2);
3351
3352        query.text_predicates[self.pattern_index]
3353            .iter()
3354            .all(|predicate| match predicate {
3355                TextPredicateCapture::EqCapture(i, j, is_positive, match_all_nodes) => {
3356                    let mut nodes_1 = self.nodes_for_capture_index(*i);
3357                    let mut nodes_2 = self.nodes_for_capture_index(*j);
3358                    while let (Some(node1), Some(node2)) = (nodes_1.next(), nodes_2.next()) {
3359                        let mut text1 = text_provider.text(node1);
3360                        let mut text2 = text_provider.text(node2);
3361                        let text1 = node_text1.get_text(&mut text1);
3362                        let text2 = node_text2.get_text(&mut text2);
3363                        let is_positive_match = text1 == text2;
3364                        if is_positive_match != *is_positive && *match_all_nodes {
3365                            return false;
3366                        }
3367                        if is_positive_match == *is_positive && !*match_all_nodes {
3368                            return true;
3369                        }
3370                    }
3371                    nodes_1.next().is_none() && nodes_2.next().is_none()
3372                }
3373                TextPredicateCapture::EqString(i, s, is_positive, match_all_nodes) => {
3374                    let nodes = self.nodes_for_capture_index(*i);
3375                    for node in nodes {
3376                        let mut text = text_provider.text(node);
3377                        let text = node_text1.get_text(&mut text);
3378                        let is_positive_match = text == s.as_bytes();
3379                        if is_positive_match != *is_positive && *match_all_nodes {
3380                            return false;
3381                        }
3382                        if is_positive_match == *is_positive && !*match_all_nodes {
3383                            return true;
3384                        }
3385                    }
3386                    true
3387                }
3388                TextPredicateCapture::MatchString(i, r, is_positive, match_all_nodes) => {
3389                    let nodes = self.nodes_for_capture_index(*i);
3390                    for node in nodes {
3391                        let mut text = text_provider.text(node);
3392                        let text = node_text1.get_text(&mut text);
3393                        let is_positive_match = r.is_match(text);
3394                        if is_positive_match != *is_positive && *match_all_nodes {
3395                            return false;
3396                        }
3397                        if is_positive_match == *is_positive && !*match_all_nodes {
3398                            return true;
3399                        }
3400                    }
3401                    true
3402                }
3403                TextPredicateCapture::AnyString(i, v, is_positive) => {
3404                    let nodes = self.nodes_for_capture_index(*i);
3405                    for node in nodes {
3406                        let mut text = text_provider.text(node);
3407                        let text = node_text1.get_text(&mut text);
3408                        if (v.iter().any(|s| text == s.as_bytes())) != *is_positive {
3409                            return false;
3410                        }
3411                    }
3412                    true
3413                }
3414            })
3415    }
3416}
3417
3418impl QueryProperty {
3419    #[must_use]
3420    pub fn new(key: &str, value: Option<&str>, capture_id: Option<usize>) -> Self {
3421        Self {
3422            capture_id,
3423            key: key.to_string().into(),
3424            value: value.map(|s| s.to_string().into()),
3425        }
3426    }
3427}
3428
3429/// Provide a `StreamingIterator` instead of the traditional `Iterator`, as the
3430/// underlying object in the C library gets updated on each iteration. Copies would
3431/// have their internal state overwritten, leading to Undefined Behavior
3432impl<'query, 'tree: 'query, T: TextProvider<I>, I: AsRef<[u8]>> StreamingIterator
3433    for QueryMatches<'query, 'tree, T, I>
3434{
3435    type Item = QueryMatch<'query, 'tree>;
3436
3437    fn advance(&mut self) {
3438        self.current_match = unsafe {
3439            loop {
3440                let mut m = MaybeUninit::<ffi::TSQueryMatch>::uninit();
3441                if ffi::ts_query_cursor_next_match(self.ptr, m.as_mut_ptr()) {
3442                    let result = QueryMatch::new(&m.assume_init(), self.ptr);
3443                    if result.satisfies_text_predicates(
3444                        self.query,
3445                        &mut self.buffer1,
3446                        &mut self.buffer2,
3447                        &mut self.text_provider,
3448                    ) {
3449                        break Some(result);
3450                    }
3451                } else {
3452                    break None;
3453                }
3454            }
3455        };
3456    }
3457
3458    fn get(&self) -> Option<&Self::Item> {
3459        self.current_match.as_ref()
3460    }
3461}
3462
3463impl<'query, 'tree: 'query, T: TextProvider<I>, I: AsRef<[u8]>> StreamingIteratorMut
3464    for QueryMatches<'query, 'tree, T, I>
3465{
3466    fn get_mut(&mut self) -> Option<&mut Self::Item> {
3467        self.current_match.as_mut()
3468    }
3469}
3470
3471impl<'query, 'tree: 'query, T: TextProvider<I>, I: AsRef<[u8]>> StreamingIterator
3472    for QueryCaptures<'query, 'tree, T, I>
3473{
3474    type Item = (QueryMatch<'query, 'tree>, usize);
3475
3476    fn advance(&mut self) {
3477        self.current_match = unsafe {
3478            loop {
3479                let mut capture_index = 0u32;
3480                let mut m = MaybeUninit::<ffi::TSQueryMatch>::uninit();
3481                if ffi::ts_query_cursor_next_capture(
3482                    self.ptr,
3483                    m.as_mut_ptr(),
3484                    core::ptr::addr_of_mut!(capture_index),
3485                ) {
3486                    let result = QueryMatch::new(&m.assume_init(), self.ptr);
3487                    if result.satisfies_text_predicates(
3488                        self.query,
3489                        &mut self.buffer1,
3490                        &mut self.buffer2,
3491                        &mut self.text_provider,
3492                    ) {
3493                        break Some((result, capture_index as usize));
3494                    }
3495                    result.remove();
3496                } else {
3497                    break None;
3498                }
3499            }
3500        }
3501    }
3502
3503    fn get(&self) -> Option<&Self::Item> {
3504        self.current_match.as_ref()
3505    }
3506}
3507
3508impl<'query, 'tree: 'query, T: TextProvider<I>, I: AsRef<[u8]>> StreamingIteratorMut
3509    for QueryCaptures<'query, 'tree, T, I>
3510{
3511    fn get_mut(&mut self) -> Option<&mut Self::Item> {
3512        self.current_match.as_mut()
3513    }
3514}
3515
3516impl<T: TextProvider<I>, I: AsRef<[u8]>> QueryMatches<'_, '_, T, I> {
3517    #[doc(alias = "ts_query_cursor_set_byte_range")]
3518    pub fn set_byte_range(&mut self, range: ops::Range<usize>) {
3519        unsafe {
3520            ffi::ts_query_cursor_set_byte_range(self.ptr, range.start as u32, range.end as u32);
3521        }
3522    }
3523
3524    #[doc(alias = "ts_query_cursor_set_point_range")]
3525    pub fn set_point_range(&mut self, range: ops::Range<Point>) {
3526        unsafe {
3527            ffi::ts_query_cursor_set_point_range(self.ptr, range.start.into(), range.end.into());
3528        }
3529    }
3530}
3531
3532impl<T: TextProvider<I>, I: AsRef<[u8]>> QueryCaptures<'_, '_, T, I> {
3533    #[doc(alias = "ts_query_cursor_set_byte_range")]
3534    pub fn set_byte_range(&mut self, range: ops::Range<usize>) {
3535        unsafe {
3536            ffi::ts_query_cursor_set_byte_range(self.ptr, range.start as u32, range.end as u32);
3537        }
3538    }
3539
3540    #[doc(alias = "ts_query_cursor_set_point_range")]
3541    pub fn set_point_range(&mut self, range: ops::Range<Point>) {
3542        unsafe {
3543            ffi::ts_query_cursor_set_point_range(self.ptr, range.start.into(), range.end.into());
3544        }
3545    }
3546}
3547
3548impl fmt::Debug for QueryMatch<'_, '_> {
3549    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3550        write!(
3551            f,
3552            "QueryMatch {{ id: {}, pattern_index: {}, captures: {:?} }}",
3553            self.id, self.pattern_index, self.captures
3554        )
3555    }
3556}
3557
3558impl<F, R, I> TextProvider<I> for F
3559where
3560    F: FnMut(Node) -> R,
3561    R: Iterator<Item = I>,
3562    I: AsRef<[u8]>,
3563{
3564    type I = R;
3565
3566    fn text(&mut self, node: Node) -> Self::I {
3567        (self)(node)
3568    }
3569}
3570
3571impl<'a> TextProvider<&'a [u8]> for &'a [u8] {
3572    type I = iter::Once<&'a [u8]>;
3573
3574    fn text(&mut self, node: Node) -> Self::I {
3575        iter::once(&self[node.byte_range()])
3576    }
3577}
3578
3579impl PartialEq for Query {
3580    fn eq(&self, other: &Self) -> bool {
3581        self.ptr == other.ptr
3582    }
3583}
3584
3585impl Drop for Query {
3586    fn drop(&mut self) {
3587        unsafe { ffi::ts_query_delete(self.ptr.as_ptr()) }
3588    }
3589}
3590
3591impl Drop for QueryCursor {
3592    fn drop(&mut self) {
3593        unsafe { ffi::ts_query_cursor_delete(self.ptr.as_ptr()) }
3594    }
3595}
3596
3597impl Point {
3598    #[must_use]
3599    pub const fn new(row: usize, column: usize) -> Self {
3600        Self { row, column }
3601    }
3602}
3603
3604impl fmt::Display for Point {
3605    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3606        write!(f, "({}, {})", self.row, self.column)
3607    }
3608}
3609
3610impl From<Point> for ffi::TSPoint {
3611    fn from(val: Point) -> Self {
3612        Self {
3613            row: val.row as u32,
3614            column: val.column as u32,
3615        }
3616    }
3617}
3618
3619impl From<ffi::TSPoint> for Point {
3620    fn from(point: ffi::TSPoint) -> Self {
3621        Self {
3622            row: point.row as usize,
3623            column: point.column as usize,
3624        }
3625    }
3626}
3627
3628impl From<Range> for ffi::TSRange {
3629    fn from(val: Range) -> Self {
3630        Self {
3631            start_byte: val.start_byte as u32,
3632            end_byte: val.end_byte as u32,
3633            start_point: val.start_point.into(),
3634            end_point: val.end_point.into(),
3635        }
3636    }
3637}
3638
3639impl From<ffi::TSRange> for Range {
3640    fn from(range: ffi::TSRange) -> Self {
3641        Self {
3642            start_byte: range.start_byte as usize,
3643            end_byte: range.end_byte as usize,
3644            start_point: range.start_point.into(),
3645            end_point: range.end_point.into(),
3646        }
3647    }
3648}
3649
3650impl From<&'_ InputEdit> for ffi::TSInputEdit {
3651    fn from(val: &'_ InputEdit) -> Self {
3652        Self {
3653            start_byte: val.start_byte as u32,
3654            old_end_byte: val.old_end_byte as u32,
3655            new_end_byte: val.new_end_byte as u32,
3656            start_point: val.start_position.into(),
3657            old_end_point: val.old_end_position.into(),
3658            new_end_point: val.new_end_position.into(),
3659        }
3660    }
3661}
3662
3663impl<'a> LossyUtf8<'a> {
3664    #[must_use]
3665    pub const fn new(bytes: &'a [u8]) -> Self {
3666        LossyUtf8 {
3667            bytes,
3668            in_replacement: false,
3669        }
3670    }
3671}
3672
3673impl<'a> Iterator for LossyUtf8<'a> {
3674    type Item = &'a str;
3675
3676    fn next(&mut self) -> Option<&'a str> {
3677        if self.bytes.is_empty() {
3678            return None;
3679        }
3680        if self.in_replacement {
3681            self.in_replacement = false;
3682            return Some("\u{fffd}");
3683        }
3684        match core::str::from_utf8(self.bytes) {
3685            Ok(valid) => {
3686                self.bytes = &[];
3687                Some(valid)
3688            }
3689            Err(error) => {
3690                if let Some(error_len) = error.error_len() {
3691                    let error_start = error.valid_up_to();
3692                    if error_start > 0 {
3693                        let result =
3694                            unsafe { core::str::from_utf8_unchecked(&self.bytes[..error_start]) };
3695                        self.bytes = &self.bytes[(error_start + error_len)..];
3696                        self.in_replacement = true;
3697                        Some(result)
3698                    } else {
3699                        self.bytes = &self.bytes[error_len..];
3700                        Some("\u{fffd}")
3701                    }
3702                } else {
3703                    None
3704                }
3705            }
3706        }
3707    }
3708}
3709
3710#[must_use]
3711const fn predicate_error(row: usize, message: String) -> QueryError {
3712    QueryError {
3713        kind: QueryErrorKind::Predicate,
3714        row,
3715        column: 0,
3716        offset: 0,
3717        message,
3718    }
3719}
3720
3721impl fmt::Display for IncludedRangesError {
3722    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3723        write!(f, "Incorrect range by index: {}", self.0)
3724    }
3725}
3726
3727impl fmt::Display for LanguageError {
3728    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3729        write!(
3730            f,
3731            "Incompatible language version {}. Expected minimum {}, maximum {}",
3732            self.version, MIN_COMPATIBLE_LANGUAGE_VERSION, LANGUAGE_VERSION,
3733        )
3734    }
3735}
3736
3737impl fmt::Display for QueryError {
3738    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3739        let msg = match self.kind {
3740            QueryErrorKind::Field => "Invalid field name ",
3741            QueryErrorKind::NodeType => "Invalid node type ",
3742            QueryErrorKind::Capture => "Invalid capture name ",
3743            QueryErrorKind::Predicate => "Invalid predicate: ",
3744            QueryErrorKind::Structure => "Impossible pattern:\n",
3745            QueryErrorKind::Syntax => "Invalid syntax:\n",
3746            QueryErrorKind::Language => "",
3747        };
3748        if msg.is_empty() {
3749            write!(f, "{}", self.message)
3750        } else {
3751            write!(
3752                f,
3753                "Query error at {}:{}. {}{}",
3754                self.row + 1,
3755                self.column + 1,
3756                msg,
3757                self.message
3758            )
3759        }
3760    }
3761}
3762
3763#[doc(hidden)]
3764#[must_use]
3765pub fn format_sexp(sexp: &str, initial_indent_level: usize) -> String {
3766    let mut indent_level = initial_indent_level;
3767    let mut formatted = String::new();
3768    let mut has_field = false;
3769
3770    let mut c_iter = sexp.chars().peekable();
3771    let mut s = String::with_capacity(sexp.len());
3772    let mut quote = '\0';
3773    let mut saw_paren = false;
3774    let mut did_last = false;
3775
3776    let mut fetch_next_str = |next: &mut String| {
3777        next.clear();
3778        while let Some(c) = c_iter.next() {
3779            if c == '\'' || c == '"' {
3780                quote = c;
3781            } else if c == ' ' || (c == ')' && quote != '\0') {
3782                if let Some(next_c) = c_iter.peek() {
3783                    if *next_c == quote {
3784                        next.push(c);
3785                        next.push(*next_c);
3786                        c_iter.next();
3787                        quote = '\0';
3788                        continue;
3789                    }
3790                }
3791                break;
3792            }
3793            if c == ')' {
3794                saw_paren = true;
3795                break;
3796            }
3797            next.push(c);
3798        }
3799
3800        // at the end
3801        if c_iter.peek().is_none() && next.is_empty() {
3802            if saw_paren {
3803                // but did we see a ) before ending?
3804                saw_paren = false;
3805                return Some(());
3806            }
3807            if !did_last {
3808                // but did we account for the end empty string as if we're splitting?
3809                did_last = true;
3810                return Some(());
3811            }
3812            return None;
3813        }
3814        Some(())
3815    };
3816
3817    while fetch_next_str(&mut s).is_some() {
3818        if s.is_empty() && indent_level > 0 {
3819            // ")"
3820            indent_level -= 1;
3821            write!(formatted, ")").unwrap();
3822        } else if s.starts_with('(') {
3823            if has_field {
3824                has_field = false;
3825            } else {
3826                if indent_level > 0 {
3827                    writeln!(formatted).unwrap();
3828                    for _ in 0..indent_level {
3829                        write!(formatted, "  ").unwrap();
3830                    }
3831                }
3832                indent_level += 1;
3833            }
3834
3835            // "(node_name"
3836            write!(formatted, "{s}").unwrap();
3837
3838            // "(MISSING node_name" or "(UNEXPECTED 'x'"
3839            if s.starts_with("(MISSING") || s.starts_with("(UNEXPECTED") {
3840                fetch_next_str(&mut s).unwrap();
3841                if s.is_empty() {
3842                    while indent_level > 0 {
3843                        indent_level -= 1;
3844                        write!(formatted, ")").unwrap();
3845                    }
3846                } else {
3847                    write!(formatted, " {s}").unwrap();
3848                }
3849            }
3850        } else if s.ends_with(':') {
3851            // "field:"
3852            writeln!(formatted).unwrap();
3853            for _ in 0..indent_level {
3854                write!(formatted, "  ").unwrap();
3855            }
3856            write!(formatted, "{s} ").unwrap();
3857            has_field = true;
3858            indent_level += 1;
3859        }
3860    }
3861
3862    formatted
3863}
3864
3865pub fn wasm_stdlib_symbols() -> impl Iterator<Item = &'static str> {
3866    const WASM_STDLIB_SYMBOLS: &str = include_str!(concat!(env!("OUT_DIR"), "/stdlib-symbols.txt"));
3867
3868    WASM_STDLIB_SYMBOLS
3869        .lines()
3870        .map(|s| s.trim_matches(|c| c == '"' || c == ','))
3871}
3872
3873extern "C" {
3874    fn free(ptr: *mut c_void);
3875}
3876
3877static mut FREE_FN: unsafe extern "C" fn(ptr: *mut c_void) = free;
3878
3879/// Sets the memory allocation functions that the core library should use.
3880///
3881/// # Safety
3882///
3883/// This function uses FFI and mutates a static global.
3884#[doc(alias = "ts_set_allocator")]
3885pub unsafe fn set_allocator(
3886    new_malloc: Option<unsafe extern "C" fn(size: usize) -> *mut c_void>,
3887    new_calloc: Option<unsafe extern "C" fn(nmemb: usize, size: usize) -> *mut c_void>,
3888    new_realloc: Option<unsafe extern "C" fn(ptr: *mut c_void, size: usize) -> *mut c_void>,
3889    new_free: Option<unsafe extern "C" fn(ptr: *mut c_void)>,
3890) {
3891    FREE_FN = new_free.unwrap_or(free);
3892    ffi::ts_set_allocator(new_malloc, new_calloc, new_realloc, new_free);
3893}
3894
3895#[cfg(feature = "std")]
3896#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
3897impl error::Error for IncludedRangesError {}
3898#[cfg(feature = "std")]
3899#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
3900impl error::Error for LanguageError {}
3901#[cfg(feature = "std")]
3902#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
3903impl error::Error for QueryError {}
3904
3905unsafe impl Send for Language {}
3906unsafe impl Sync for Language {}
3907
3908unsafe impl Send for Node<'_> {}
3909unsafe impl Sync for Node<'_> {}
3910
3911unsafe impl Send for LookaheadIterator {}
3912unsafe impl Sync for LookaheadIterator {}
3913
3914unsafe impl Send for LookaheadNamesIterator<'_> {}
3915unsafe impl Sync for LookaheadNamesIterator<'_> {}
3916
3917unsafe impl Send for Parser {}
3918unsafe impl Sync for Parser {}
3919
3920unsafe impl Send for Query {}
3921unsafe impl Sync for Query {}
3922
3923unsafe impl Send for QueryCursor {}
3924unsafe impl Sync for QueryCursor {}
3925
3926unsafe impl Send for Tree {}
3927unsafe impl Sync for Tree {}
3928
3929unsafe impl Send for TreeCursor<'_> {}
3930unsafe impl Sync for TreeCursor<'_> {}