Skip to main content

tree_sitter/
lib.rs

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