1#![doc = include_str!("./README.md")]
2#![cfg_attr(not(feature = "std"), no_std)]
3#![cfg_attr(docsrs, feature(doc_cfg))]
4
5pub mod ffi;
6mod util;
7
8#[cfg(not(feature = "std"))]
9extern crate alloc;
10#[cfg(not(feature = "std"))]
11use alloc::{boxed::Box, format, string::String, string::ToString, vec::Vec};
12use core::{
13 ffi::{c_char, c_void, CStr},
14 fmt::{self, Write},
15 hash, iter,
16 marker::PhantomData,
17 mem::MaybeUninit,
18 num::NonZeroU16,
19 ops::{self, Deref},
20 ptr::{self, NonNull},
21 slice, str,
22 sync::atomic::AtomicUsize,
23};
24#[cfg(feature = "std")]
25use std::error;
26#[cfg(all(unix, feature = "std"))]
27use std::os::fd::AsRawFd;
28#[cfg(all(windows, feature = "std"))]
29use std::os::windows::io::AsRawHandle;
30
31pub use streaming_iterator::{StreamingIterator, StreamingIteratorMut};
32use tree_sitter_language::LanguageFn;
33
34#[cfg(feature = "wasm")]
35mod wasm_language;
36#[cfg(feature = "wasm")]
37#[cfg_attr(docsrs, doc(cfg(feature = "wasm")))]
38pub use wasm_language::*;
39
40#[doc(alias = "TREE_SITTER_LANGUAGE_VERSION")]
48pub const LANGUAGE_VERSION: usize = ffi::TREE_SITTER_LANGUAGE_VERSION as usize;
49
50#[doc(alias = "TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION")]
53pub const MIN_COMPATIBLE_LANGUAGE_VERSION: usize =
54 ffi::TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION as usize;
55
56pub const PARSER_HEADER: &str = include_str!("../src/parser.h");
57
58#[doc(alias = "TSLanguage")]
61#[derive(Debug, PartialEq, Eq, Hash)]
62#[repr(transparent)]
63pub struct Language(*const ffi::TSLanguage);
64
65pub struct LanguageRef<'a>(*const ffi::TSLanguage, PhantomData<&'a ()>);
66
67#[doc(alias = "TSLanguageMetadata")]
74pub struct LanguageMetadata {
75 pub major_version: u8,
76 pub minor_version: u8,
77 pub patch_version: u8,
78}
79
80impl From<ffi::TSLanguageMetadata> for LanguageMetadata {
81 fn from(val: ffi::TSLanguageMetadata) -> Self {
82 Self {
83 major_version: val.major_version,
84 minor_version: val.minor_version,
85 patch_version: val.patch_version,
86 }
87 }
88}
89
90#[doc(alias = "TSTree")]
92pub struct Tree(NonNull<ffi::TSTree>);
93
94#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
98pub struct Point {
99 pub row: usize,
100 pub column: usize,
101}
102
103#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
106pub struct Range {
107 pub start_byte: usize,
108 pub end_byte: usize,
109 pub start_point: Point,
110 pub end_point: Point,
111}
112
113#[derive(Clone, Copy, Debug, PartialEq, Eq)]
115pub struct InputEdit {
116 pub start_byte: usize,
117 pub old_end_byte: usize,
118 pub new_end_byte: usize,
119 pub start_position: Point,
120 pub old_end_position: Point,
121 pub new_end_position: Point,
122}
123
124#[doc(alias = "TSNode")]
126#[derive(Clone, Copy)]
127#[repr(transparent)]
128pub struct Node<'tree>(ffi::TSNode, PhantomData<&'tree ()>);
129
130#[doc(alias = "TSParser")]
133pub struct Parser(NonNull<ffi::TSParser>);
134
135#[doc(alias = "TSLookaheadIterator")]
138pub struct LookaheadIterator(NonNull<ffi::TSLookaheadIterator>);
139struct LookaheadNamesIterator<'a>(&'a mut LookaheadIterator);
140
141pub struct ParseState(NonNull<ffi::TSParseState>);
144
145impl ParseState {
146 #[must_use]
147 pub const fn current_byte_offset(&self) -> usize {
148 unsafe { self.0.as_ref() }.current_byte_offset as usize
149 }
150
151 #[must_use]
152 pub const fn has_error(&self) -> bool {
153 unsafe { self.0.as_ref() }.has_error
154 }
155}
156
157pub struct QueryCursorState(NonNull<ffi::TSQueryCursorState>);
160
161impl QueryCursorState {
162 #[must_use]
163 pub const fn current_byte_offset(&self) -> usize {
164 unsafe { self.0.as_ref() }.current_byte_offset as usize
165 }
166}
167
168#[derive(Default)]
169pub struct ParseOptions<'a> {
170 pub progress_callback: Option<ParseProgressCallback<'a>>,
171}
172
173impl<'a> ParseOptions<'a> {
174 #[must_use]
175 pub fn new() -> Self {
176 Self::default()
177 }
178
179 #[must_use]
180 pub fn progress_callback<F: FnMut(&ParseState) -> bool>(mut self, callback: &'a mut F) -> Self {
181 self.progress_callback = Some(callback);
182 self
183 }
184}
185
186#[derive(Default)]
187pub struct QueryCursorOptions<'a> {
188 pub progress_callback: Option<QueryProgressCallback<'a>>,
189}
190
191impl<'a> QueryCursorOptions<'a> {
192 #[must_use]
193 pub fn new() -> Self {
194 Self::default()
195 }
196
197 #[must_use]
198 pub fn progress_callback<F: FnMut(&QueryCursorState) -> bool>(
199 mut self,
200 callback: &'a mut F,
201 ) -> Self {
202 self.progress_callback = Some(callback);
203 self
204 }
205}
206
207struct QueryCursorOptionsDrop(*mut ffi::TSQueryCursorOptions);
208
209impl Drop for QueryCursorOptionsDrop {
210 fn drop(&mut self) {
211 unsafe {
212 if !(*self.0).payload.is_null() {
213 drop(Box::from_raw(
214 (*self.0).payload.cast::<QueryProgressCallback>(),
215 ));
216 }
217 drop(Box::from_raw(self.0));
218 }
219 }
220}
221
222#[derive(Debug, PartialEq, Eq)]
224pub enum LogType {
225 Parse,
226 Lex,
227}
228
229type FieldId = NonZeroU16;
230
231type Logger<'a> = Box<dyn FnMut(LogType, &str) + 'a>;
233
234type ParseProgressCallback<'a> = &'a mut dyn FnMut(&ParseState) -> bool;
236
237type QueryProgressCallback<'a> = &'a mut dyn FnMut(&QueryCursorState) -> bool;
239
240pub trait Decode {
241 fn decode(bytes: &[u8]) -> (i32, u32);
244}
245
246#[doc(alias = "TSTreeCursor")]
248pub struct TreeCursor<'cursor>(ffi::TSTreeCursor, PhantomData<&'cursor ()>);
249
250#[doc(alias = "TSQuery")]
252#[derive(Debug)]
253#[allow(clippy::type_complexity)]
254pub struct Query {
255 ptr: NonNull<ffi::TSQuery>,
256 capture_names: Box<[&'static str]>,
257 capture_quantifiers: Box<[Box<[CaptureQuantifier]>]>,
258 text_predicates: Box<[Box<[TextPredicateCapture]>]>,
259 property_settings: Box<[Box<[QueryProperty]>]>,
260 property_predicates: Box<[Box<[(QueryProperty, bool)]>]>,
261 general_predicates: Box<[Box<[QueryPredicate]>]>,
262}
263
264#[derive(Debug, PartialEq, Eq, Clone, Copy)]
266pub enum CaptureQuantifier {
267 Zero,
268 ZeroOrOne,
269 ZeroOrMore,
270 One,
271 OneOrMore,
272}
273
274impl From<ffi::TSQuantifier> for CaptureQuantifier {
275 fn from(value: ffi::TSQuantifier) -> Self {
276 match value {
277 ffi::TSQuantifierZero => Self::Zero,
278 ffi::TSQuantifierZeroOrOne => Self::ZeroOrOne,
279 ffi::TSQuantifierZeroOrMore => Self::ZeroOrMore,
280 ffi::TSQuantifierOne => Self::One,
281 ffi::TSQuantifierOneOrMore => Self::OneOrMore,
282 _ => panic!("Unrecognized quantifier: {value}"),
283 }
284 }
285}
286
287#[doc(alias = "TSQueryCursor")]
289pub struct QueryCursor {
290 ptr: NonNull<ffi::TSQueryCursor>,
291}
292
293#[derive(Debug, PartialEq, Eq)]
295pub struct QueryProperty {
296 pub key: Box<str>,
297 pub value: Option<Box<str>>,
298 pub capture_id: Option<usize>,
299}
300
301#[derive(Debug, PartialEq, Eq)]
302pub enum QueryPredicateArg {
303 Capture(u32),
304 String(Box<str>),
305}
306
307#[derive(Debug, PartialEq, Eq)]
309pub struct QueryPredicate {
310 pub operator: Box<str>,
311 pub args: Box<[QueryPredicateArg]>,
312}
313
314pub struct QueryMatch<'cursor, 'tree> {
316 pub pattern_index: usize,
317 pub captures: &'cursor [QueryCapture<'tree>],
318 id: u32,
319 cursor: *mut ffi::TSQueryCursor,
320}
321
322pub struct QueryMatches<'query, 'tree: 'query, T: TextProvider<I>, I: AsRef<[u8]>> {
324 ptr: *mut ffi::TSQueryCursor,
325 query: &'query Query,
326 text_provider: T,
327 buffer1: Vec<u8>,
328 buffer2: Vec<u8>,
329 current_match: Option<QueryMatch<'query, 'tree>>,
330 _options: Option<QueryCursorOptionsDrop>,
331 _phantom: PhantomData<(&'tree (), I)>,
332}
333
334pub struct QueryCaptures<'query, 'tree: 'query, T: TextProvider<I>, I: AsRef<[u8]>> {
339 ptr: *mut ffi::TSQueryCursor,
340 query: &'query Query,
341 text_provider: T,
342 buffer1: Vec<u8>,
343 buffer2: Vec<u8>,
344 current_match: Option<(QueryMatch<'query, 'tree>, usize)>,
345 _options: Option<QueryCursorOptionsDrop>,
346 _phantom: PhantomData<(&'tree (), I)>,
347}
348
349pub trait TextProvider<I>
350where
351 I: AsRef<[u8]>,
352{
353 type I: Iterator<Item = I>;
354 fn text(&mut self, node: Node) -> Self::I;
355}
356
357#[derive(Clone, Copy, Debug)]
360#[repr(C)]
361pub struct QueryCapture<'tree> {
362 pub node: Node<'tree>,
363 pub index: u32,
364}
365
366#[derive(Debug, PartialEq, Eq)]
369pub struct LanguageError {
370 version: usize,
371}
372
373#[derive(Debug, PartialEq, Eq)]
375pub struct IncludedRangesError(pub usize);
376
377#[derive(Debug, PartialEq, Eq)]
379pub struct QueryError {
380 pub row: usize,
381 pub column: usize,
382 pub offset: usize,
383 pub message: String,
384 pub kind: QueryErrorKind,
385}
386
387#[derive(Debug, PartialEq, Eq)]
388pub enum QueryErrorKind {
389 Syntax,
390 NodeType,
391 Field,
392 Capture,
393 Predicate,
394 Structure,
395 Language,
396}
397
398#[derive(Debug)]
399enum TextPredicateCapture {
405 EqString(u32, Box<str>, bool, bool),
406 EqCapture(u32, u32, bool, bool),
407 MatchString(u32, regex::bytes::Regex, bool, bool),
408 AnyString(u32, Box<[Box<str>]>, bool),
409}
410
411pub struct LossyUtf8<'a> {
414 bytes: &'a [u8],
415 in_replacement: bool,
416}
417
418impl Language {
419 #[must_use]
420 pub fn new(builder: LanguageFn) -> Self {
421 Self(unsafe { builder.into_raw()().cast() })
422 }
423
424 #[doc(alias = "ts_language_name")]
426 #[must_use]
427 pub fn name(&self) -> Option<&'static str> {
428 let ptr = unsafe { ffi::ts_language_name(self.0) };
429 (!ptr.is_null()).then(|| unsafe { CStr::from_ptr(ptr) }.to_str().unwrap())
430 }
431
432 #[doc(alias = "ts_language_version")]
435 #[deprecated(since = "0.25.0", note = "Use abi_version instead")]
436 #[must_use]
437 pub fn version(&self) -> usize {
438 unsafe { ffi::ts_language_version(self.0) as usize }
439 }
440
441 #[doc(alias = "ts_language_abi_version")]
444 #[must_use]
445 pub fn abi_version(&self) -> usize {
446 unsafe { ffi::ts_language_abi_version(self.0) as usize }
447 }
448
449 #[doc(alias = "ts_language_metadata")]
455 #[must_use]
456 pub fn metadata(&self) -> Option<LanguageMetadata> {
457 unsafe {
458 let ptr = ffi::ts_language_metadata(self.0);
459 (!ptr.is_null()).then(|| (*ptr).into())
460 }
461 }
462
463 #[doc(alias = "ts_language_symbol_count")]
465 #[must_use]
466 pub fn node_kind_count(&self) -> usize {
467 unsafe { ffi::ts_language_symbol_count(self.0) as usize }
468 }
469
470 #[doc(alias = "ts_language_state_count")]
472 #[must_use]
473 pub fn parse_state_count(&self) -> usize {
474 unsafe { ffi::ts_language_state_count(self.0) as usize }
475 }
476
477 #[doc(alias = "ts_language_supertypes")]
479 #[must_use]
480 pub fn supertypes(&self) -> &[u16] {
481 let mut length = 0u32;
482 unsafe {
483 let ptr = ffi::ts_language_supertypes(self.0, core::ptr::addr_of_mut!(length));
484 if length == 0 {
485 &[]
486 } else {
487 slice::from_raw_parts(ptr.cast_mut(), length as usize)
488 }
489 }
490 }
491
492 #[doc(alias = "ts_language_supertype_map")]
494 #[must_use]
495 pub fn subtypes_for_supertype(&self, supertype: u16) -> &[u16] {
496 unsafe {
497 let mut length = 0u32;
498 let ptr = ffi::ts_language_subtypes(self.0, supertype, core::ptr::addr_of_mut!(length));
499 if length == 0 {
500 &[]
501 } else {
502 slice::from_raw_parts(ptr.cast_mut(), length as usize)
503 }
504 }
505 }
506
507 #[doc(alias = "ts_language_symbol_name")]
509 #[must_use]
510 pub fn node_kind_for_id(&self, id: u16) -> Option<&'static str> {
511 let ptr = unsafe { ffi::ts_language_symbol_name(self.0, id) };
512 (!ptr.is_null()).then(|| unsafe { CStr::from_ptr(ptr) }.to_str().unwrap())
513 }
514
515 #[doc(alias = "ts_language_symbol_for_name")]
517 #[must_use]
518 pub fn id_for_node_kind(&self, kind: &str, named: bool) -> u16 {
519 unsafe {
520 ffi::ts_language_symbol_for_name(
521 self.0,
522 kind.as_bytes().as_ptr().cast::<c_char>(),
523 kind.len() as u32,
524 named,
525 )
526 }
527 }
528
529 #[must_use]
532 pub fn node_kind_is_named(&self, id: u16) -> bool {
533 unsafe { ffi::ts_language_symbol_type(self.0, id) == ffi::TSSymbolTypeRegular }
534 }
535
536 #[must_use]
539 pub fn node_kind_is_visible(&self, id: u16) -> bool {
540 unsafe { ffi::ts_language_symbol_type(self.0, id) <= ffi::TSSymbolTypeAnonymous }
541 }
542
543 #[must_use]
545 pub fn node_kind_is_supertype(&self, id: u16) -> bool {
546 unsafe { ffi::ts_language_symbol_type(self.0, id) == ffi::TSSymbolTypeSupertype }
547 }
548
549 #[doc(alias = "ts_language_field_count")]
551 #[must_use]
552 pub fn field_count(&self) -> usize {
553 unsafe { ffi::ts_language_field_count(self.0) as usize }
554 }
555
556 #[doc(alias = "ts_language_field_name_for_id")]
558 #[must_use]
559 pub fn field_name_for_id(&self, field_id: u16) -> Option<&'static str> {
560 let ptr = unsafe { ffi::ts_language_field_name_for_id(self.0, field_id) };
561 (!ptr.is_null()).then(|| unsafe { CStr::from_ptr(ptr) }.to_str().unwrap())
562 }
563
564 #[doc(alias = "ts_language_field_id_for_name")]
566 #[must_use]
567 pub fn field_id_for_name(&self, field_name: impl AsRef<[u8]>) -> Option<FieldId> {
568 let field_name = field_name.as_ref();
569 let id = unsafe {
570 ffi::ts_language_field_id_for_name(
571 self.0,
572 field_name.as_ptr().cast::<c_char>(),
573 field_name.len() as u32,
574 )
575 };
576 FieldId::new(id)
577 }
578
579 #[doc(alias = "ts_language_next_state")]
588 #[must_use]
589 pub fn next_state(&self, state: u16, id: u16) -> u16 {
590 unsafe { ffi::ts_language_next_state(self.0, state, id) }
591 }
592
593 #[doc(alias = "ts_lookahead_iterator_new")]
607 #[must_use]
608 pub fn lookahead_iterator(&self, state: u16) -> Option<LookaheadIterator> {
609 let ptr = unsafe { ffi::ts_lookahead_iterator_new(self.0, state) };
610 (!ptr.is_null()).then(|| unsafe { LookaheadIterator::from_raw(ptr) })
611 }
612}
613
614impl From<LanguageFn> for Language {
615 fn from(value: LanguageFn) -> Self {
616 Self::new(value)
617 }
618}
619
620impl Clone for Language {
621 fn clone(&self) -> Self {
622 unsafe { Self(ffi::ts_language_copy(self.0)) }
623 }
624}
625
626impl Drop for Language {
627 fn drop(&mut self) {
628 unsafe { ffi::ts_language_delete(self.0) }
629 }
630}
631
632impl Deref for LanguageRef<'_> {
633 type Target = Language;
634
635 fn deref(&self) -> &Self::Target {
636 unsafe { &*(core::ptr::addr_of!(self.0).cast::<Language>()) }
637 }
638}
639
640impl Default for Parser {
641 fn default() -> Self {
642 Self::new()
643 }
644}
645
646impl Parser {
647 #[doc(alias = "ts_parser_new")]
649 #[must_use]
650 pub fn new() -> Self {
651 unsafe {
652 let parser = ffi::ts_parser_new();
653 Self(NonNull::new_unchecked(parser))
654 }
655 }
656
657 #[doc(alias = "ts_parser_set_language")]
666 pub fn set_language(&mut self, language: &Language) -> Result<(), LanguageError> {
667 let version = language.abi_version();
668 if (MIN_COMPATIBLE_LANGUAGE_VERSION..=LANGUAGE_VERSION).contains(&version) {
669 unsafe {
670 ffi::ts_parser_set_language(self.0.as_ptr(), language.0);
671 }
672 Ok(())
673 } else {
674 Err(LanguageError { version })
675 }
676 }
677
678 #[doc(alias = "ts_parser_language")]
680 #[must_use]
681 pub fn language(&self) -> Option<LanguageRef<'_>> {
682 let ptr = unsafe { ffi::ts_parser_language(self.0.as_ptr()) };
683 (!ptr.is_null()).then_some(LanguageRef(ptr, PhantomData))
684 }
685
686 #[doc(alias = "ts_parser_logger")]
688 #[must_use]
689 pub fn logger(&self) -> Option<&Logger> {
690 let logger = unsafe { ffi::ts_parser_logger(self.0.as_ptr()) };
691 unsafe { logger.payload.cast::<Logger>().as_ref() }
692 }
693
694 #[doc(alias = "ts_parser_set_logger")]
696 pub fn set_logger(&mut self, logger: Option<Logger>) {
697 let prev_logger = unsafe { ffi::ts_parser_logger(self.0.as_ptr()) };
698 if !prev_logger.payload.is_null() {
699 drop(unsafe { Box::from_raw(prev_logger.payload.cast::<Logger>()) });
700 }
701
702 let c_logger;
703 if let Some(logger) = logger {
704 let container = Box::new(logger);
705
706 unsafe extern "C" fn log(
707 payload: *mut c_void,
708 c_log_type: ffi::TSLogType,
709 c_message: *const c_char,
710 ) {
711 let callback = payload.cast::<Logger>().as_mut().unwrap();
712 if let Ok(message) = CStr::from_ptr(c_message).to_str() {
713 let log_type = if c_log_type == ffi::TSLogTypeParse {
714 LogType::Parse
715 } else {
716 LogType::Lex
717 };
718 callback(log_type, message);
719 }
720 }
721
722 let raw_container = Box::into_raw(container);
723
724 c_logger = ffi::TSLogger {
725 payload: raw_container.cast::<c_void>(),
726 log: Some(log),
727 };
728 } else {
729 c_logger = ffi::TSLogger {
730 payload: ptr::null_mut(),
731 log: None,
732 };
733 }
734
735 unsafe { ffi::ts_parser_set_logger(self.0.as_ptr(), c_logger) };
736 }
737
738 #[doc(alias = "ts_parser_print_dot_graphs")]
743 #[cfg(not(target_os = "wasi"))]
744 #[cfg(feature = "std")]
745 #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
746 pub fn print_dot_graphs(
747 &mut self,
748 #[cfg(unix)] file: &impl AsRawFd,
749 #[cfg(windows)] file: &impl AsRawHandle,
750 ) {
751 #[cfg(unix)]
752 {
753 let fd = file.as_raw_fd();
754 unsafe {
755 ffi::ts_parser_print_dot_graphs(self.0.as_ptr(), ffi::_ts_dup(fd));
756 }
757 }
758
759 #[cfg(windows)]
760 {
761 let handle = file.as_raw_handle();
762 unsafe {
763 ffi::ts_parser_print_dot_graphs(self.0.as_ptr(), ffi::_ts_dup(handle));
764 }
765 }
766 }
767
768 #[doc(alias = "ts_parser_print_dot_graphs")]
770 #[cfg(not(target_os = "wasi"))]
771 #[cfg(feature = "std")]
772 #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
773 pub fn stop_printing_dot_graphs(&mut self) {
774 unsafe { ffi::ts_parser_print_dot_graphs(self.0.as_ptr(), -1) }
775 }
776
777 #[doc(alias = "ts_parser_parse")]
790 pub fn parse(&mut self, text: impl AsRef<[u8]>, old_tree: Option<&Tree>) -> Option<Tree> {
791 let bytes = text.as_ref();
792 let len = bytes.len();
793 self.parse_with_options(
794 &mut |i, _| (i < len).then(|| &bytes[i..]).unwrap_or_default(),
795 old_tree,
796 None,
797 )
798 }
799
800 #[deprecated(since = "0.25.0", note = "Prefer parse_utf16_le instead")]
808 pub fn parse_utf16(
809 &mut self,
810 input: impl AsRef<[u16]>,
811 old_tree: Option<&Tree>,
812 ) -> Option<Tree> {
813 let code_points = input.as_ref();
814 let len = code_points.len();
815 self.parse_utf16_le_with_options(
816 &mut |i, _| (i < len).then(|| &code_points[i..]).unwrap_or_default(),
817 old_tree,
818 None,
819 )
820 }
821
822 #[deprecated(since = "0.25.0", note = "Prefer `parse_with_options` instead")]
833 pub fn parse_with<T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>(
834 &mut self,
835 callback: &mut F,
836 old_tree: Option<&Tree>,
837 ) -> Option<Tree> {
838 self.parse_with_options(callback, old_tree, None)
839 }
840
841 pub fn parse_with_options<T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>(
853 &mut self,
854 callback: &mut F,
855 old_tree: Option<&Tree>,
856 options: Option<ParseOptions>,
857 ) -> Option<Tree> {
858 type Payload<'a, F, T> = (&'a mut F, Option<T>);
859
860 unsafe extern "C" fn progress(state: *mut ffi::TSParseState) -> bool {
862 let callback = (*state)
863 .payload
864 .cast::<ParseProgressCallback>()
865 .as_mut()
866 .unwrap();
867 callback(&ParseState::from_raw(state))
868 }
869
870 unsafe extern "C" fn read<T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>(
872 payload: *mut c_void,
873 byte_offset: u32,
874 position: ffi::TSPoint,
875 bytes_read: *mut u32,
876 ) -> *const c_char {
877 let (callback, text) = payload.cast::<Payload<F, T>>().as_mut().unwrap();
878 *text = Some(callback(byte_offset as usize, position.into()));
879 let slice = text.as_ref().unwrap().as_ref();
880 *bytes_read = slice.len() as u32;
881 slice.as_ptr().cast::<c_char>()
882 }
883
884 let empty_options = ffi::TSParseOptions {
885 payload: ptr::null_mut(),
886 progress_callback: None,
887 };
888
889 let mut callback_ptr;
890 let parse_options = if let Some(options) = options {
891 if let Some(cb) = options.progress_callback {
892 callback_ptr = cb;
893 ffi::TSParseOptions {
894 payload: core::ptr::addr_of_mut!(callback_ptr).cast::<c_void>(),
895 progress_callback: Some(progress),
896 }
897 } else {
898 empty_options
899 }
900 } else {
901 empty_options
902 };
903
904 let mut payload: Payload<F, T> = (callback, None);
910
911 let c_input = ffi::TSInput {
912 payload: ptr::addr_of_mut!(payload).cast::<c_void>(),
913 read: Some(read::<T, F>),
914 encoding: ffi::TSInputEncodingUTF8,
915 decode: None,
916 };
917
918 let c_old_tree = old_tree.map_or(ptr::null_mut(), |t| t.0.as_ptr());
919 unsafe {
920 let c_new_tree = ffi::ts_parser_parse_with_options(
921 self.0.as_ptr(),
922 c_old_tree,
923 c_input,
924 parse_options,
925 );
926
927 NonNull::new(c_new_tree).map(Tree)
928 }
929 }
930
931 #[deprecated(
942 since = "0.25.0",
943 note = "Prefer `parse_utf16_le_with_options` instead"
944 )]
945 pub fn parse_utf16_with<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>(
946 &mut self,
947 callback: &mut F,
948 old_tree: Option<&Tree>,
949 ) -> Option<Tree> {
950 self.parse_utf16_le_with_options(callback, old_tree, None)
951 }
952
953 pub fn parse_utf16_le(
961 &mut self,
962 input: impl AsRef<[u16]>,
963 old_tree: Option<&Tree>,
964 ) -> Option<Tree> {
965 let code_points = input.as_ref();
966 let len = code_points.len();
967 self.parse_utf16_le_with_options(
968 &mut |i, _| (i < len).then(|| &code_points[i..]).unwrap_or_default(),
969 old_tree,
970 None,
971 )
972 }
973
974 pub fn parse_utf16_le_with_options<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>(
986 &mut self,
987 callback: &mut F,
988 old_tree: Option<&Tree>,
989 options: Option<ParseOptions>,
990 ) -> Option<Tree> {
991 type Payload<'a, F, T> = (&'a mut F, Option<T>);
992
993 unsafe extern "C" fn progress(state: *mut ffi::TSParseState) -> bool {
994 let callback = (*state)
995 .payload
996 .cast::<ParseProgressCallback>()
997 .as_mut()
998 .unwrap();
999 callback(&ParseState::from_raw(state))
1000 }
1001
1002 unsafe extern "C" fn read<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>(
1004 payload: *mut c_void,
1005 byte_offset: u32,
1006 position: ffi::TSPoint,
1007 bytes_read: *mut u32,
1008 ) -> *const c_char {
1009 let (callback, text) = payload.cast::<Payload<F, T>>().as_mut().unwrap();
1010 *text = Some(callback(
1011 (byte_offset / 2) as usize,
1012 Point {
1013 row: position.row as usize,
1014 column: position.column as usize / 2,
1015 },
1016 ));
1017 let slice = text.as_ref().unwrap().as_ref();
1018 *bytes_read = slice.len() as u32 * 2;
1019 slice.as_ptr().cast::<c_char>()
1020 }
1021
1022 let empty_options = ffi::TSParseOptions {
1023 payload: ptr::null_mut(),
1024 progress_callback: None,
1025 };
1026
1027 let mut callback_ptr;
1028 let parse_options = if let Some(options) = options {
1029 if let Some(cb) = options.progress_callback {
1030 callback_ptr = cb;
1031 ffi::TSParseOptions {
1032 payload: core::ptr::addr_of_mut!(callback_ptr).cast::<c_void>(),
1033 progress_callback: Some(progress),
1034 }
1035 } else {
1036 empty_options
1037 }
1038 } else {
1039 empty_options
1040 };
1041
1042 let mut payload: Payload<F, T> = (callback, None);
1048
1049 let c_input = ffi::TSInput {
1050 payload: core::ptr::addr_of_mut!(payload).cast::<c_void>(),
1051 read: Some(read::<T, F>),
1052 encoding: ffi::TSInputEncodingUTF16LE,
1053 decode: None,
1054 };
1055
1056 let c_old_tree = old_tree.map_or(ptr::null_mut(), |t| t.0.as_ptr());
1057 unsafe {
1058 let c_new_tree = ffi::ts_parser_parse_with_options(
1059 self.0.as_ptr(),
1060 c_old_tree,
1061 c_input,
1062 parse_options,
1063 );
1064
1065 NonNull::new(c_new_tree).map(Tree)
1066 }
1067 }
1068
1069 pub fn parse_utf16_be(
1077 &mut self,
1078 input: impl AsRef<[u16]>,
1079 old_tree: Option<&Tree>,
1080 ) -> Option<Tree> {
1081 let code_points = input.as_ref();
1082 let len = code_points.len();
1083 self.parse_utf16_be_with_options(
1084 &mut |i, _| if i < len { &code_points[i..] } else { &[] },
1085 old_tree,
1086 None,
1087 )
1088 }
1089
1090 pub fn parse_utf16_be_with_options<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>(
1102 &mut self,
1103 callback: &mut F,
1104 old_tree: Option<&Tree>,
1105 options: Option<ParseOptions>,
1106 ) -> Option<Tree> {
1107 type Payload<'a, F, T> = (&'a mut F, Option<T>);
1108
1109 unsafe extern "C" fn progress(state: *mut ffi::TSParseState) -> bool {
1111 let callback = (*state)
1112 .payload
1113 .cast::<ParseProgressCallback>()
1114 .as_mut()
1115 .unwrap();
1116 callback(&ParseState::from_raw(state))
1117 }
1118
1119 unsafe extern "C" fn read<T: AsRef<[u16]>, F: FnMut(usize, Point) -> T>(
1121 payload: *mut c_void,
1122 byte_offset: u32,
1123 position: ffi::TSPoint,
1124 bytes_read: *mut u32,
1125 ) -> *const c_char {
1126 let (callback, text) = payload.cast::<Payload<F, T>>().as_mut().unwrap();
1127 *text = Some(callback(
1128 (byte_offset / 2) as usize,
1129 Point {
1130 row: position.row as usize,
1131 column: position.column as usize / 2,
1132 },
1133 ));
1134 let slice = text.as_ref().unwrap().as_ref();
1135 *bytes_read = slice.len() as u32 * 2;
1136 slice.as_ptr().cast::<c_char>()
1137 }
1138
1139 let empty_options = ffi::TSParseOptions {
1140 payload: ptr::null_mut(),
1141 progress_callback: None,
1142 };
1143
1144 let mut callback_ptr;
1145 let parse_options = if let Some(options) = options {
1146 if let Some(cb) = options.progress_callback {
1147 callback_ptr = cb;
1148 ffi::TSParseOptions {
1149 payload: core::ptr::addr_of_mut!(callback_ptr).cast::<c_void>(),
1150 progress_callback: Some(progress),
1151 }
1152 } else {
1153 empty_options
1154 }
1155 } else {
1156 empty_options
1157 };
1158
1159 let mut payload: Payload<F, T> = (callback, None);
1165
1166 let c_input = ffi::TSInput {
1167 payload: core::ptr::addr_of_mut!(payload).cast::<c_void>(),
1168 read: Some(read::<T, F>),
1169 encoding: ffi::TSInputEncodingUTF16BE,
1170 decode: None,
1171 };
1172
1173 let c_old_tree = old_tree.map_or(ptr::null_mut(), |t| t.0.as_ptr());
1174 unsafe {
1175 let c_new_tree = ffi::ts_parser_parse_with_options(
1176 self.0.as_ptr(),
1177 c_old_tree,
1178 c_input,
1179 parse_options,
1180 );
1181
1182 NonNull::new(c_new_tree).map(Tree)
1183 }
1184 }
1185
1186 pub fn parse_custom_encoding<D: Decode, T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>(
1203 &mut self,
1204 callback: &mut F,
1205 old_tree: Option<&Tree>,
1206 options: Option<ParseOptions>,
1207 ) -> Option<Tree> {
1208 type Payload<'a, F, T> = (&'a mut F, Option<T>);
1209
1210 unsafe extern "C" fn progress(state: *mut ffi::TSParseState) -> bool {
1211 let callback = (*state)
1212 .payload
1213 .cast::<ParseProgressCallback>()
1214 .as_mut()
1215 .unwrap();
1216 callback(&ParseState::from_raw(state))
1217 }
1218
1219 unsafe extern "C" fn decode_fn<D: Decode>(
1221 data: *const u8,
1222 len: u32,
1223 code_point: *mut i32,
1224 ) -> u32 {
1225 let (c, len) = D::decode(std::slice::from_raw_parts(data, len as usize));
1226 if let Some(code_point) = code_point.as_mut() {
1227 *code_point = c;
1228 }
1229 len
1230 }
1231
1232 unsafe extern "C" fn read<T: AsRef<[u8]>, F: FnMut(usize, Point) -> T>(
1234 payload: *mut c_void,
1235 byte_offset: u32,
1236 position: ffi::TSPoint,
1237 bytes_read: *mut u32,
1238 ) -> *const c_char {
1239 let (callback, text) = payload.cast::<Payload<F, T>>().as_mut().unwrap();
1240 *text = Some(callback(byte_offset as usize, position.into()));
1241 let slice = text.as_ref().unwrap().as_ref();
1242 *bytes_read = slice.len() as u32;
1243 slice.as_ptr().cast::<c_char>()
1244 }
1245
1246 let empty_options = ffi::TSParseOptions {
1247 payload: ptr::null_mut(),
1248 progress_callback: None,
1249 };
1250
1251 let mut callback_ptr;
1252 let parse_options = if let Some(options) = options {
1253 if let Some(cb) = options.progress_callback {
1254 callback_ptr = cb;
1255 ffi::TSParseOptions {
1256 payload: core::ptr::addr_of_mut!(callback_ptr).cast::<c_void>(),
1257 progress_callback: Some(progress),
1258 }
1259 } else {
1260 empty_options
1261 }
1262 } else {
1263 empty_options
1264 };
1265
1266 let mut payload: Payload<F, T> = (callback, None);
1272
1273 let c_input = ffi::TSInput {
1274 payload: core::ptr::addr_of_mut!(payload).cast::<c_void>(),
1275 read: Some(read::<T, F>),
1276 encoding: ffi::TSInputEncodingCustom,
1277 decode: Some(decode_fn::<D>),
1279 };
1280
1281 let c_old_tree = old_tree.map_or(ptr::null_mut(), |t| t.0.as_ptr());
1282 unsafe {
1283 let c_new_tree = ffi::ts_parser_parse_with_options(
1284 self.0.as_ptr(),
1285 c_old_tree,
1286 c_input,
1287 parse_options,
1288 );
1289
1290 NonNull::new(c_new_tree).map(Tree)
1291 }
1292 }
1293
1294 #[doc(alias = "ts_parser_reset")]
1302 pub fn reset(&mut self) {
1303 unsafe { ffi::ts_parser_reset(self.0.as_ptr()) }
1304 }
1305
1306 #[doc(alias = "ts_parser_timeout_micros")]
1310 #[deprecated(
1311 since = "0.25.0",
1312 note = "Prefer using `parse_with_options` and using a callback"
1313 )]
1314 #[must_use]
1315 pub fn timeout_micros(&self) -> u64 {
1316 unsafe { ffi::ts_parser_timeout_micros(self.0.as_ptr()) }
1317 }
1318
1319 #[doc(alias = "ts_parser_set_timeout_micros")]
1325 #[deprecated(
1326 since = "0.25.0",
1327 note = "Prefer using `parse_with_options` and using a callback"
1328 )]
1329 pub fn set_timeout_micros(&mut self, timeout_micros: u64) {
1330 unsafe { ffi::ts_parser_set_timeout_micros(self.0.as_ptr(), timeout_micros) }
1331 }
1332
1333 #[doc(alias = "ts_parser_set_included_ranges")]
1351 pub fn set_included_ranges(&mut self, ranges: &[Range]) -> Result<(), IncludedRangesError> {
1352 let ts_ranges = ranges.iter().copied().map(Into::into).collect::<Vec<_>>();
1353 let result = unsafe {
1354 ffi::ts_parser_set_included_ranges(
1355 self.0.as_ptr(),
1356 ts_ranges.as_ptr(),
1357 ts_ranges.len() as u32,
1358 )
1359 };
1360
1361 if result {
1362 Ok(())
1363 } else {
1364 let mut prev_end_byte = 0;
1365 for (i, range) in ranges.iter().enumerate() {
1366 if range.start_byte < prev_end_byte || range.end_byte < range.start_byte {
1367 return Err(IncludedRangesError(i));
1368 }
1369 prev_end_byte = range.end_byte;
1370 }
1371 Err(IncludedRangesError(0))
1372 }
1373 }
1374
1375 #[doc(alias = "ts_parser_included_ranges")]
1377 #[must_use]
1378 pub fn included_ranges(&self) -> Vec<Range> {
1379 let mut count = 0u32;
1380 unsafe {
1381 let ptr =
1382 ffi::ts_parser_included_ranges(self.0.as_ptr(), core::ptr::addr_of_mut!(count));
1383 let ranges = slice::from_raw_parts(ptr, count as usize);
1384 let result = ranges.iter().copied().map(Into::into).collect();
1385 result
1386 }
1387 }
1388
1389 #[doc(alias = "ts_parser_cancellation_flag")]
1395 #[deprecated(
1396 since = "0.25.0",
1397 note = "Prefer using `parse_with_options` and using a callback"
1398 )]
1399 #[must_use]
1400 pub unsafe fn cancellation_flag(&self) -> Option<&AtomicUsize> {
1401 ffi::ts_parser_cancellation_flag(self.0.as_ptr())
1402 .cast::<AtomicUsize>()
1403 .as_ref()
1404 }
1405
1406 #[doc(alias = "ts_parser_set_cancellation_flag")]
1417 #[deprecated(
1418 since = "0.25.0",
1419 note = "Prefer using `parse_with_options` and using a callback"
1420 )]
1421 pub unsafe fn set_cancellation_flag(&mut self, flag: Option<&AtomicUsize>) {
1422 if let Some(flag) = flag {
1423 ffi::ts_parser_set_cancellation_flag(
1424 self.0.as_ptr(),
1425 std::ptr::from_ref::<AtomicUsize>(flag).cast::<usize>(),
1426 );
1427 } else {
1428 ffi::ts_parser_set_cancellation_flag(self.0.as_ptr(), ptr::null());
1429 }
1430 }
1431}
1432
1433impl Drop for Parser {
1434 fn drop(&mut self) {
1435 self.stop_printing_dot_graphs();
1436 self.set_logger(None);
1437 unsafe { ffi::ts_parser_delete(self.0.as_ptr()) }
1438 }
1439}
1440
1441impl Tree {
1442 #[doc(alias = "ts_tree_root_node")]
1444 #[must_use]
1445 pub fn root_node(&self) -> Node {
1446 Node::new(unsafe { ffi::ts_tree_root_node(self.0.as_ptr()) }).unwrap()
1447 }
1448
1449 #[doc(alias = "ts_tree_root_node_with_offset")]
1452 #[must_use]
1453 pub fn root_node_with_offset(&self, offset_bytes: usize, offset_extent: Point) -> Node {
1454 Node::new(unsafe {
1455 ffi::ts_tree_root_node_with_offset(
1456 self.0.as_ptr(),
1457 offset_bytes as u32,
1458 offset_extent.into(),
1459 )
1460 })
1461 .unwrap()
1462 }
1463
1464 #[doc(alias = "ts_tree_language")]
1466 #[must_use]
1467 pub fn language(&self) -> LanguageRef {
1468 LanguageRef(
1469 unsafe { ffi::ts_tree_language(self.0.as_ptr()) },
1470 PhantomData,
1471 )
1472 }
1473
1474 #[doc(alias = "ts_tree_edit")]
1480 pub fn edit(&mut self, edit: &InputEdit) {
1481 let edit = edit.into();
1482 unsafe { ffi::ts_tree_edit(self.0.as_ptr(), &edit) };
1483 }
1484
1485 #[must_use]
1487 pub fn walk(&self) -> TreeCursor {
1488 self.root_node().walk()
1489 }
1490
1491 #[doc(alias = "ts_tree_get_changed_ranges")]
1501 #[must_use]
1502 pub fn changed_ranges(&self, other: &Self) -> impl ExactSizeIterator<Item = Range> {
1503 let mut count = 0u32;
1504 unsafe {
1505 let ptr = ffi::ts_tree_get_changed_ranges(
1506 self.0.as_ptr(),
1507 other.0.as_ptr(),
1508 core::ptr::addr_of_mut!(count),
1509 );
1510 util::CBufferIter::new(ptr, count as usize).map(Into::into)
1511 }
1512 }
1513
1514 #[doc(alias = "ts_tree_included_ranges")]
1516 #[must_use]
1517 pub fn included_ranges(&self) -> Vec<Range> {
1518 let mut count = 0u32;
1519 unsafe {
1520 let ptr = ffi::ts_tree_included_ranges(self.0.as_ptr(), core::ptr::addr_of_mut!(count));
1521 let ranges = slice::from_raw_parts(ptr, count as usize);
1522 let result = ranges.iter().copied().map(Into::into).collect();
1523 (FREE_FN)(ptr.cast::<c_void>());
1524 result
1525 }
1526 }
1527
1528 #[doc(alias = "ts_tree_print_dot_graph")]
1533 #[cfg(not(target_os = "wasi"))]
1534 #[cfg(feature = "std")]
1535 #[cfg_attr(docsrs, doc(cfg(feature = "std")))]
1536 pub fn print_dot_graph(
1537 &self,
1538 #[cfg(unix)] file: &impl AsRawFd,
1539 #[cfg(windows)] file: &impl AsRawHandle,
1540 ) {
1541 #[cfg(unix)]
1542 {
1543 let fd = file.as_raw_fd();
1544 unsafe { ffi::ts_tree_print_dot_graph(self.0.as_ptr(), fd) }
1545 }
1546
1547 #[cfg(windows)]
1548 {
1549 let handle = file.as_raw_handle();
1550 unsafe { ffi::ts_tree_print_dot_graph(self.0.as_ptr(), handle as i32) }
1551 }
1552 }
1553}
1554
1555impl fmt::Debug for Tree {
1556 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1557 write!(f, "{{Tree {:?}}}", self.root_node())
1558 }
1559}
1560
1561impl Drop for Tree {
1562 fn drop(&mut self) {
1563 unsafe { ffi::ts_tree_delete(self.0.as_ptr()) }
1564 }
1565}
1566
1567impl Clone for Tree {
1568 fn clone(&self) -> Self {
1569 unsafe { Self(NonNull::new_unchecked(ffi::ts_tree_copy(self.0.as_ptr()))) }
1570 }
1571}
1572
1573impl<'tree> Node<'tree> {
1574 fn new(node: ffi::TSNode) -> Option<Self> {
1575 (!node.id.is_null()).then_some(Node(node, PhantomData))
1576 }
1577
1578 #[must_use]
1589 pub fn id(&self) -> usize {
1590 self.0.id as usize
1591 }
1592
1593 #[doc(alias = "ts_node_symbol")]
1595 #[must_use]
1596 pub fn kind_id(&self) -> u16 {
1597 unsafe { ffi::ts_node_symbol(self.0) }
1598 }
1599
1600 #[doc(alias = "ts_node_grammar_symbol")]
1603 #[must_use]
1604 pub fn grammar_id(&self) -> u16 {
1605 unsafe { ffi::ts_node_grammar_symbol(self.0) }
1606 }
1607
1608 #[doc(alias = "ts_node_type")]
1610 #[must_use]
1611 pub fn kind(&self) -> &'static str {
1612 unsafe { CStr::from_ptr(ffi::ts_node_type(self.0)) }
1613 .to_str()
1614 .unwrap()
1615 }
1616
1617 #[doc(alias = "ts_node_grammar_type")]
1620 #[must_use]
1621 pub fn grammar_name(&self) -> &'static str {
1622 unsafe { CStr::from_ptr(ffi::ts_node_grammar_type(self.0)) }
1623 .to_str()
1624 .unwrap()
1625 }
1626
1627 #[doc(alias = "ts_node_language")]
1629 #[must_use]
1630 pub fn language(&self) -> LanguageRef {
1631 LanguageRef(unsafe { ffi::ts_node_language(self.0) }, PhantomData)
1632 }
1633
1634 #[doc(alias = "ts_node_is_named")]
1639 #[must_use]
1640 pub fn is_named(&self) -> bool {
1641 unsafe { ffi::ts_node_is_named(self.0) }
1642 }
1643
1644 #[doc(alias = "ts_node_is_extra")]
1649 #[must_use]
1650 pub fn is_extra(&self) -> bool {
1651 unsafe { ffi::ts_node_is_extra(self.0) }
1652 }
1653
1654 #[doc(alias = "ts_node_has_changes")]
1656 #[must_use]
1657 pub fn has_changes(&self) -> bool {
1658 unsafe { ffi::ts_node_has_changes(self.0) }
1659 }
1660
1661 #[doc(alias = "ts_node_has_error")]
1664 #[must_use]
1665 pub fn has_error(&self) -> bool {
1666 unsafe { ffi::ts_node_has_error(self.0) }
1667 }
1668
1669 #[doc(alias = "ts_node_is_error")]
1674 #[must_use]
1675 pub fn is_error(&self) -> bool {
1676 unsafe { ffi::ts_node_is_error(self.0) }
1677 }
1678
1679 #[doc(alias = "ts_node_parse_state")]
1681 #[must_use]
1682 pub fn parse_state(&self) -> u16 {
1683 unsafe { ffi::ts_node_parse_state(self.0) }
1684 }
1685
1686 #[doc(alias = "ts_node_next_parse_state")]
1688 #[must_use]
1689 pub fn next_parse_state(&self) -> u16 {
1690 unsafe { ffi::ts_node_next_parse_state(self.0) }
1691 }
1692
1693 #[doc(alias = "ts_node_is_missing")]
1698 #[must_use]
1699 pub fn is_missing(&self) -> bool {
1700 unsafe { ffi::ts_node_is_missing(self.0) }
1701 }
1702
1703 #[doc(alias = "ts_node_start_byte")]
1705 #[must_use]
1706 pub fn start_byte(&self) -> usize {
1707 unsafe { ffi::ts_node_start_byte(self.0) as usize }
1708 }
1709
1710 #[doc(alias = "ts_node_end_byte")]
1712 #[must_use]
1713 pub fn end_byte(&self) -> usize {
1714 unsafe { ffi::ts_node_end_byte(self.0) as usize }
1715 }
1716
1717 #[must_use]
1719 pub fn byte_range(&self) -> core::ops::Range<usize> {
1720 self.start_byte()..self.end_byte()
1721 }
1722
1723 #[must_use]
1726 pub fn range(&self) -> Range {
1727 Range {
1728 start_byte: self.start_byte(),
1729 end_byte: self.end_byte(),
1730 start_point: self.start_position(),
1731 end_point: self.end_position(),
1732 }
1733 }
1734
1735 #[doc(alias = "ts_node_start_point")]
1737 #[must_use]
1738 pub fn start_position(&self) -> Point {
1739 let result = unsafe { ffi::ts_node_start_point(self.0) };
1740 result.into()
1741 }
1742
1743 #[doc(alias = "ts_node_end_point")]
1745 #[must_use]
1746 pub fn end_position(&self) -> Point {
1747 let result = unsafe { ffi::ts_node_end_point(self.0) };
1748 result.into()
1749 }
1750
1751 #[doc(alias = "ts_node_child")]
1758 #[must_use]
1759 pub fn child(&self, i: usize) -> Option<Self> {
1760 Self::new(unsafe { ffi::ts_node_child(self.0, i as u32) })
1761 }
1762
1763 #[doc(alias = "ts_node_child_count")]
1765 #[must_use]
1766 pub fn child_count(&self) -> usize {
1767 unsafe { ffi::ts_node_child_count(self.0) as usize }
1768 }
1769
1770 #[doc(alias = "ts_node_named_child")]
1777 #[must_use]
1778 pub fn named_child(&self, i: usize) -> Option<Self> {
1779 Self::new(unsafe { ffi::ts_node_named_child(self.0, i as u32) })
1780 }
1781
1782 #[doc(alias = "ts_node_named_child_count")]
1786 #[must_use]
1787 pub fn named_child_count(&self) -> usize {
1788 unsafe { ffi::ts_node_named_child_count(self.0) as usize }
1789 }
1790
1791 #[doc(alias = "ts_node_child_by_field_name")]
1796 #[must_use]
1797 pub fn child_by_field_name(&self, field_name: impl AsRef<[u8]>) -> Option<Self> {
1798 let field_name = field_name.as_ref();
1799 Self::new(unsafe {
1800 ffi::ts_node_child_by_field_name(
1801 self.0,
1802 field_name.as_ptr().cast::<c_char>(),
1803 field_name.len() as u32,
1804 )
1805 })
1806 }
1807
1808 #[doc(alias = "ts_node_child_by_field_id")]
1813 #[must_use]
1814 pub fn child_by_field_id(&self, field_id: u16) -> Option<Self> {
1815 Self::new(unsafe { ffi::ts_node_child_by_field_id(self.0, field_id) })
1816 }
1817
1818 #[doc(alias = "ts_node_field_name_for_child")]
1820 #[must_use]
1821 pub fn field_name_for_child(&self, child_index: u32) -> Option<&'static str> {
1822 unsafe {
1823 let ptr = ffi::ts_node_field_name_for_child(self.0, child_index);
1824 (!ptr.is_null()).then(|| CStr::from_ptr(ptr).to_str().unwrap())
1825 }
1826 }
1827
1828 #[must_use]
1830 pub fn field_name_for_named_child(&self, named_child_index: u32) -> Option<&'static str> {
1831 unsafe {
1832 let ptr = ffi::ts_node_field_name_for_named_child(self.0, named_child_index);
1833 (!ptr.is_null()).then(|| CStr::from_ptr(ptr).to_str().unwrap())
1834 }
1835 }
1836
1837 pub fn children<'cursor>(
1847 &self,
1848 cursor: &'cursor mut TreeCursor<'tree>,
1849 ) -> impl ExactSizeIterator<Item = Node<'tree>> + 'cursor {
1850 cursor.reset(*self);
1851 cursor.goto_first_child();
1852 (0..self.child_count()).map(move |_| {
1853 let result = cursor.node();
1854 cursor.goto_next_sibling();
1855 result
1856 })
1857 }
1858
1859 pub fn named_children<'cursor>(
1863 &self,
1864 cursor: &'cursor mut TreeCursor<'tree>,
1865 ) -> impl ExactSizeIterator<Item = Node<'tree>> + 'cursor {
1866 cursor.reset(*self);
1867 cursor.goto_first_child();
1868 (0..self.named_child_count()).map(move |_| {
1869 while !cursor.node().is_named() {
1870 if !cursor.goto_next_sibling() {
1871 break;
1872 }
1873 }
1874 let result = cursor.node();
1875 cursor.goto_next_sibling();
1876 result
1877 })
1878 }
1879
1880 pub fn children_by_field_name<'cursor>(
1884 &self,
1885 field_name: &str,
1886 cursor: &'cursor mut TreeCursor<'tree>,
1887 ) -> impl Iterator<Item = Node<'tree>> + 'cursor {
1888 let field_id = self.language().field_id_for_name(field_name);
1889 let mut done = field_id.is_none();
1890 if !done {
1891 cursor.reset(*self);
1892 cursor.goto_first_child();
1893 }
1894 iter::from_fn(move || {
1895 if !done {
1896 while cursor.field_id() != field_id {
1897 if !cursor.goto_next_sibling() {
1898 return None;
1899 }
1900 }
1901 let result = cursor.node();
1902 if !cursor.goto_next_sibling() {
1903 done = true;
1904 }
1905 return Some(result);
1906 }
1907 None
1908 })
1909 }
1910
1911 pub fn children_by_field_id<'cursor>(
1915 &self,
1916 field_id: FieldId,
1917 cursor: &'cursor mut TreeCursor<'tree>,
1918 ) -> impl Iterator<Item = Node<'tree>> + 'cursor {
1919 cursor.reset(*self);
1920 cursor.goto_first_child();
1921 let mut done = false;
1922 iter::from_fn(move || {
1923 if !done {
1924 while cursor.field_id() != Some(field_id) {
1925 if !cursor.goto_next_sibling() {
1926 return None;
1927 }
1928 }
1929 let result = cursor.node();
1930 if !cursor.goto_next_sibling() {
1931 done = true;
1932 }
1933 return Some(result);
1934 }
1935 None
1936 })
1937 }
1938
1939 #[doc(alias = "ts_node_parent")]
1943 #[must_use]
1944 pub fn parent(&self) -> Option<Self> {
1945 Self::new(unsafe { ffi::ts_node_parent(self.0) })
1946 }
1947
1948 #[doc(alias = "ts_node_child_with_descendant")]
1952 #[must_use]
1953 pub fn child_with_descendant(&self, descendant: Self) -> Option<Self> {
1954 Self::new(unsafe { ffi::ts_node_child_with_descendant(self.0, descendant.0) })
1955 }
1956
1957 #[doc(alias = "ts_node_next_sibling")]
1959 #[must_use]
1960 pub fn next_sibling(&self) -> Option<Self> {
1961 Self::new(unsafe { ffi::ts_node_next_sibling(self.0) })
1962 }
1963
1964 #[doc(alias = "ts_node_prev_sibling")]
1966 #[must_use]
1967 pub fn prev_sibling(&self) -> Option<Self> {
1968 Self::new(unsafe { ffi::ts_node_prev_sibling(self.0) })
1969 }
1970
1971 #[doc(alias = "ts_node_next_named_sibling")]
1973 #[must_use]
1974 pub fn next_named_sibling(&self) -> Option<Self> {
1975 Self::new(unsafe { ffi::ts_node_next_named_sibling(self.0) })
1976 }
1977
1978 #[doc(alias = "ts_node_prev_named_sibling")]
1980 #[must_use]
1981 pub fn prev_named_sibling(&self) -> Option<Self> {
1982 Self::new(unsafe { ffi::ts_node_prev_named_sibling(self.0) })
1983 }
1984
1985 #[doc(alias = "ts_node_first_child_for_byte")]
1987 #[must_use]
1988 pub fn first_child_for_byte(&self, byte: usize) -> Option<Self> {
1989 Self::new(unsafe { ffi::ts_node_first_child_for_byte(self.0, byte as u32) })
1990 }
1991
1992 #[doc(alias = "ts_node_first_named_child_for_point")]
1994 #[must_use]
1995 pub fn first_named_child_for_byte(&self, byte: usize) -> Option<Self> {
1996 Self::new(unsafe { ffi::ts_node_first_named_child_for_byte(self.0, byte as u32) })
1997 }
1998
1999 #[doc(alias = "ts_node_descendant_count")]
2001 #[must_use]
2002 pub fn descendant_count(&self) -> usize {
2003 unsafe { ffi::ts_node_descendant_count(self.0) as usize }
2004 }
2005
2006 #[doc(alias = "ts_node_descendant_for_byte_range")]
2008 #[must_use]
2009 pub fn descendant_for_byte_range(&self, start: usize, end: usize) -> Option<Self> {
2010 Self::new(unsafe {
2011 ffi::ts_node_descendant_for_byte_range(self.0, start as u32, end as u32)
2012 })
2013 }
2014
2015 #[doc(alias = "ts_node_named_descendant_for_byte_range")]
2017 #[must_use]
2018 pub fn named_descendant_for_byte_range(&self, start: usize, end: usize) -> Option<Self> {
2019 Self::new(unsafe {
2020 ffi::ts_node_named_descendant_for_byte_range(self.0, start as u32, end as u32)
2021 })
2022 }
2023
2024 #[doc(alias = "ts_node_descendant_for_point_range")]
2026 #[must_use]
2027 pub fn descendant_for_point_range(&self, start: Point, end: Point) -> Option<Self> {
2028 Self::new(unsafe {
2029 ffi::ts_node_descendant_for_point_range(self.0, start.into(), end.into())
2030 })
2031 }
2032
2033 #[doc(alias = "ts_node_named_descendant_for_point_range")]
2035 #[must_use]
2036 pub fn named_descendant_for_point_range(&self, start: Point, end: Point) -> Option<Self> {
2037 Self::new(unsafe {
2038 ffi::ts_node_named_descendant_for_point_range(self.0, start.into(), end.into())
2039 })
2040 }
2041
2042 #[doc(alias = "ts_node_string")]
2044 #[must_use]
2045 pub fn to_sexp(&self) -> String {
2046 let c_string = unsafe { ffi::ts_node_string(self.0) };
2047 let result = unsafe { CStr::from_ptr(c_string) }
2048 .to_str()
2049 .unwrap()
2050 .to_string();
2051 unsafe { (FREE_FN)(c_string.cast::<c_void>()) };
2052 result
2053 }
2054
2055 pub fn utf8_text<'a>(&self, source: &'a [u8]) -> Result<&'a str, str::Utf8Error> {
2056 str::from_utf8(&source[self.start_byte()..self.end_byte()])
2057 }
2058
2059 #[must_use]
2060 pub fn utf16_text<'a>(&self, source: &'a [u16]) -> &'a [u16] {
2061 &source[self.start_byte()..self.end_byte()]
2062 }
2063
2064 #[doc(alias = "ts_tree_cursor_new")]
2069 #[must_use]
2070 pub fn walk(&self) -> TreeCursor<'tree> {
2071 TreeCursor(unsafe { ffi::ts_tree_cursor_new(self.0) }, PhantomData)
2072 }
2073
2074 #[doc(alias = "ts_node_edit")]
2082 pub fn edit(&mut self, edit: &InputEdit) {
2083 let edit = edit.into();
2084 unsafe { ffi::ts_node_edit(core::ptr::addr_of_mut!(self.0), &edit) }
2085 }
2086}
2087
2088impl PartialEq for Node<'_> {
2089 fn eq(&self, other: &Self) -> bool {
2090 self.0.id == other.0.id
2091 }
2092}
2093
2094impl Eq for Node<'_> {}
2095
2096impl hash::Hash for Node<'_> {
2097 fn hash<H: hash::Hasher>(&self, state: &mut H) {
2098 self.0.id.hash(state);
2099 self.0.context[0].hash(state);
2100 self.0.context[1].hash(state);
2101 self.0.context[2].hash(state);
2102 self.0.context[3].hash(state);
2103 }
2104}
2105
2106impl fmt::Debug for Node<'_> {
2107 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2108 write!(
2109 f,
2110 "{{Node {} {} - {}}}",
2111 self.kind(),
2112 self.start_position(),
2113 self.end_position()
2114 )
2115 }
2116}
2117
2118impl fmt::Display for Node<'_> {
2119 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
2120 let sexp = self.to_sexp();
2121 if sexp.is_empty() {
2122 write!(f, "")
2123 } else if !f.alternate() {
2124 write!(f, "{sexp}")
2125 } else {
2126 write!(f, "{}", format_sexp(&sexp, f.width().unwrap_or(0)))
2127 }
2128 }
2129}
2130
2131impl<'cursor> TreeCursor<'cursor> {
2132 #[doc(alias = "ts_tree_cursor_current_node")]
2134 #[must_use]
2135 pub fn node(&self) -> Node<'cursor> {
2136 Node(
2137 unsafe { ffi::ts_tree_cursor_current_node(&self.0) },
2138 PhantomData,
2139 )
2140 }
2141
2142 #[doc(alias = "ts_tree_cursor_current_field_id")]
2146 #[must_use]
2147 pub fn field_id(&self) -> Option<FieldId> {
2148 let id = unsafe { ffi::ts_tree_cursor_current_field_id(&self.0) };
2149 FieldId::new(id)
2150 }
2151
2152 #[doc(alias = "ts_tree_cursor_current_field_name")]
2154 #[must_use]
2155 pub fn field_name(&self) -> Option<&'static str> {
2156 unsafe {
2157 let ptr = ffi::ts_tree_cursor_current_field_name(&self.0);
2158 (!ptr.is_null()).then(|| CStr::from_ptr(ptr).to_str().unwrap())
2159 }
2160 }
2161
2162 #[doc(alias = "ts_tree_cursor_current_depth")]
2165 #[must_use]
2166 pub fn depth(&self) -> u32 {
2167 unsafe { ffi::ts_tree_cursor_current_depth(&self.0) }
2168 }
2169
2170 #[doc(alias = "ts_tree_cursor_current_descendant_index")]
2173 #[must_use]
2174 pub fn descendant_index(&self) -> usize {
2175 unsafe { ffi::ts_tree_cursor_current_descendant_index(&self.0) as usize }
2176 }
2177
2178 #[doc(alias = "ts_tree_cursor_goto_first_child")]
2183 pub fn goto_first_child(&mut self) -> bool {
2184 unsafe { ffi::ts_tree_cursor_goto_first_child(&mut self.0) }
2185 }
2186
2187 #[doc(alias = "ts_tree_cursor_goto_last_child")]
2196 pub fn goto_last_child(&mut self) -> bool {
2197 unsafe { ffi::ts_tree_cursor_goto_last_child(&mut self.0) }
2198 }
2199
2200 #[doc(alias = "ts_tree_cursor_goto_parent")]
2209 pub fn goto_parent(&mut self) -> bool {
2210 unsafe { ffi::ts_tree_cursor_goto_parent(&mut self.0) }
2211 }
2212
2213 #[doc(alias = "ts_tree_cursor_goto_next_sibling")]
2221 pub fn goto_next_sibling(&mut self) -> bool {
2222 unsafe { ffi::ts_tree_cursor_goto_next_sibling(&mut self.0) }
2223 }
2224
2225 #[doc(alias = "ts_tree_cursor_goto_descendant")]
2229 pub fn goto_descendant(&mut self, descendant_index: usize) {
2230 unsafe { ffi::ts_tree_cursor_goto_descendant(&mut self.0, descendant_index as u32) }
2231 }
2232
2233 #[doc(alias = "ts_tree_cursor_goto_previous_sibling")]
2245 pub fn goto_previous_sibling(&mut self) -> bool {
2246 unsafe { ffi::ts_tree_cursor_goto_previous_sibling(&mut self.0) }
2247 }
2248
2249 #[doc(alias = "ts_tree_cursor_goto_first_child_for_byte")]
2255 pub fn goto_first_child_for_byte(&mut self, index: usize) -> Option<usize> {
2256 let result =
2257 unsafe { ffi::ts_tree_cursor_goto_first_child_for_byte(&mut self.0, index as u32) };
2258 result.try_into().ok()
2259 }
2260
2261 #[doc(alias = "ts_tree_cursor_goto_first_child_for_point")]
2267 pub fn goto_first_child_for_point(&mut self, point: Point) -> Option<usize> {
2268 let result =
2269 unsafe { ffi::ts_tree_cursor_goto_first_child_for_point(&mut self.0, point.into()) };
2270 result.try_into().ok()
2271 }
2272
2273 #[doc(alias = "ts_tree_cursor_reset")]
2276 pub fn reset(&mut self, node: Node<'cursor>) {
2277 unsafe { ffi::ts_tree_cursor_reset(&mut self.0, node.0) };
2278 }
2279
2280 #[doc(alias = "ts_tree_cursor_reset_to")]
2285 pub fn reset_to(&mut self, cursor: &Self) {
2286 unsafe { ffi::ts_tree_cursor_reset_to(&mut self.0, &cursor.0) };
2287 }
2288}
2289
2290impl Clone for TreeCursor<'_> {
2291 fn clone(&self) -> Self {
2292 TreeCursor(unsafe { ffi::ts_tree_cursor_copy(&self.0) }, PhantomData)
2293 }
2294}
2295
2296impl Drop for TreeCursor<'_> {
2297 fn drop(&mut self) {
2298 unsafe { ffi::ts_tree_cursor_delete(&mut self.0) }
2299 }
2300}
2301
2302impl LookaheadIterator {
2303 #[doc(alias = "ts_lookahead_iterator_language")]
2305 #[must_use]
2306 pub fn language(&self) -> LanguageRef<'_> {
2307 LanguageRef(
2308 unsafe { ffi::ts_lookahead_iterator_language(self.0.as_ptr()) },
2309 PhantomData,
2310 )
2311 }
2312
2313 #[doc(alias = "ts_lookahead_iterator_current_symbol")]
2315 #[must_use]
2316 pub fn current_symbol(&self) -> u16 {
2317 unsafe { ffi::ts_lookahead_iterator_current_symbol(self.0.as_ptr()) }
2318 }
2319
2320 #[doc(alias = "ts_lookahead_iterator_current_symbol_name")]
2322 #[must_use]
2323 pub fn current_symbol_name(&self) -> &'static str {
2324 unsafe {
2325 CStr::from_ptr(ffi::ts_lookahead_iterator_current_symbol_name(
2326 self.0.as_ptr(),
2327 ))
2328 .to_str()
2329 .unwrap()
2330 }
2331 }
2332
2333 #[doc(alias = "ts_lookahead_iterator_reset")]
2338 pub fn reset(&mut self, language: &Language, state: u16) -> bool {
2339 unsafe { ffi::ts_lookahead_iterator_reset(self.0.as_ptr(), language.0, state) }
2340 }
2341
2342 #[doc(alias = "ts_lookahead_iterator_reset_state")]
2347 pub fn reset_state(&mut self, state: u16) -> bool {
2348 unsafe { ffi::ts_lookahead_iterator_reset_state(self.0.as_ptr(), state) }
2349 }
2350
2351 pub fn iter_names(&mut self) -> impl Iterator<Item = &'static str> + '_ {
2353 LookaheadNamesIterator(self)
2354 }
2355}
2356
2357impl Iterator for LookaheadNamesIterator<'_> {
2358 type Item = &'static str;
2359
2360 #[doc(alias = "ts_lookahead_iterator_next")]
2361 fn next(&mut self) -> Option<Self::Item> {
2362 unsafe { ffi::ts_lookahead_iterator_next(self.0 .0.as_ptr()) }
2363 .then(|| self.0.current_symbol_name())
2364 }
2365}
2366
2367impl Iterator for LookaheadIterator {
2368 type Item = u16;
2369
2370 #[doc(alias = "ts_lookahead_iterator_next")]
2371 fn next(&mut self) -> Option<Self::Item> {
2372 unsafe { ffi::ts_lookahead_iterator_next(self.0.as_ptr()) }.then(|| self.current_symbol())
2374 }
2375}
2376
2377impl Drop for LookaheadIterator {
2378 #[doc(alias = "ts_lookahead_iterator_delete")]
2379 fn drop(&mut self) {
2380 unsafe { ffi::ts_lookahead_iterator_delete(self.0.as_ptr()) }
2381 }
2382}
2383
2384impl Query {
2385 pub fn new(language: &Language, source: &str) -> Result<Self, QueryError> {
2392 let mut error_offset = 0u32;
2393 let mut error_type: ffi::TSQueryError = 0;
2394 let bytes = source.as_bytes();
2395
2396 let ptr = unsafe {
2398 ffi::ts_query_new(
2399 language.0,
2400 bytes.as_ptr().cast::<c_char>(),
2401 bytes.len() as u32,
2402 core::ptr::addr_of_mut!(error_offset),
2403 core::ptr::addr_of_mut!(error_type),
2404 )
2405 };
2406
2407 if ptr.is_null() {
2409 if error_type == ffi::TSQueryErrorLanguage {
2410 return Err(QueryError {
2411 row: 0,
2412 column: 0,
2413 offset: 0,
2414 message: LanguageError {
2415 version: language.abi_version(),
2416 }
2417 .to_string(),
2418 kind: QueryErrorKind::Language,
2419 });
2420 }
2421
2422 let offset = error_offset as usize;
2423 let mut line_start = 0;
2424 let mut row = 0;
2425 let mut line_containing_error = None;
2426 for line in source.lines() {
2427 let line_end = line_start + line.len() + 1;
2428 if line_end > offset {
2429 line_containing_error = Some(line);
2430 break;
2431 }
2432 line_start = line_end;
2433 row += 1;
2434 }
2435 let column = offset - line_start;
2436
2437 let kind;
2438 let message;
2439 match error_type {
2440 ffi::TSQueryErrorNodeType | ffi::TSQueryErrorField | ffi::TSQueryErrorCapture => {
2442 let suffix = source.split_at(offset).1;
2443 let in_quotes = source.as_bytes()[offset - 1] == b'"';
2444 let mut backslashes = 0;
2445 let end_offset = suffix
2446 .find(|c| {
2447 if in_quotes {
2448 if c == '"' && backslashes % 2 == 0 {
2449 true
2450 } else if c == '\\' {
2451 backslashes += 1;
2452 false
2453 } else {
2454 backslashes = 0;
2455 false
2456 }
2457 } else {
2458 !char::is_alphanumeric(c) && c != '_' && c != '-'
2459 }
2460 })
2461 .unwrap_or(suffix.len());
2462 message = suffix.split_at(end_offset).0.to_string();
2463 kind = match error_type {
2464 ffi::TSQueryErrorNodeType => QueryErrorKind::NodeType,
2465 ffi::TSQueryErrorField => QueryErrorKind::Field,
2466 ffi::TSQueryErrorCapture => QueryErrorKind::Capture,
2467 _ => unreachable!(),
2468 };
2469 }
2470
2471 _ => {
2473 message = line_containing_error.map_or_else(
2474 || "Unexpected EOF".to_string(),
2475 |line| line.to_string() + "\n" + &" ".repeat(offset - line_start) + "^",
2476 );
2477 kind = match error_type {
2478 ffi::TSQueryErrorStructure => QueryErrorKind::Structure,
2479 _ => QueryErrorKind::Syntax,
2480 };
2481 }
2482 }
2483
2484 return Err(QueryError {
2485 row,
2486 column,
2487 offset,
2488 message,
2489 kind,
2490 });
2491 }
2492
2493 unsafe { Self::from_raw_parts(ptr, source) }
2494 }
2495
2496 #[doc(hidden)]
2497 unsafe fn from_raw_parts(ptr: *mut ffi::TSQuery, source: &str) -> Result<Self, QueryError> {
2498 let ptr = {
2499 struct TSQueryDrop(*mut ffi::TSQuery);
2500 impl Drop for TSQueryDrop {
2501 fn drop(&mut self) {
2502 unsafe { ffi::ts_query_delete(self.0) }
2503 }
2504 }
2505 TSQueryDrop(ptr)
2506 };
2507
2508 let string_count = unsafe { ffi::ts_query_string_count(ptr.0) };
2509 let capture_count = unsafe { ffi::ts_query_capture_count(ptr.0) };
2510 let pattern_count = unsafe { ffi::ts_query_pattern_count(ptr.0) as usize };
2511
2512 let mut capture_names = Vec::with_capacity(capture_count as usize);
2513 let mut capture_quantifiers_vec = Vec::with_capacity(pattern_count as usize);
2514 let mut text_predicates_vec = Vec::with_capacity(pattern_count);
2515 let mut property_predicates_vec = Vec::with_capacity(pattern_count);
2516 let mut property_settings_vec = Vec::with_capacity(pattern_count);
2517 let mut general_predicates_vec = Vec::with_capacity(pattern_count);
2518
2519 for i in 0..capture_count {
2521 unsafe {
2522 let mut length = 0u32;
2523 let name =
2524 ffi::ts_query_capture_name_for_id(ptr.0, i, core::ptr::addr_of_mut!(length))
2525 .cast::<u8>();
2526 let name = slice::from_raw_parts(name, length as usize);
2527 let name = str::from_utf8_unchecked(name);
2528 capture_names.push(name);
2529 }
2530 }
2531
2532 for i in 0..pattern_count {
2534 let mut capture_quantifiers = Vec::with_capacity(capture_count as usize);
2535 for j in 0..capture_count {
2536 unsafe {
2537 let quantifier = ffi::ts_query_capture_quantifier_for_id(ptr.0, i as u32, j);
2538 capture_quantifiers.push(quantifier.into());
2539 }
2540 }
2541 capture_quantifiers_vec.push(capture_quantifiers.into());
2542 }
2543
2544 let string_values = (0..string_count)
2546 .map(|i| unsafe {
2547 let mut length = 0u32;
2548 let value =
2549 ffi::ts_query_string_value_for_id(ptr.0, i, core::ptr::addr_of_mut!(length))
2550 .cast::<u8>();
2551 let value = slice::from_raw_parts(value, length as usize);
2552 let value = str::from_utf8_unchecked(value);
2553 value
2554 })
2555 .collect::<Vec<_>>();
2556
2557 for i in 0..pattern_count {
2559 let predicate_steps = unsafe {
2560 let mut length = 0u32;
2561 let raw_predicates = ffi::ts_query_predicates_for_pattern(
2562 ptr.0,
2563 i as u32,
2564 core::ptr::addr_of_mut!(length),
2565 );
2566 (length > 0)
2567 .then(|| slice::from_raw_parts(raw_predicates, length as usize))
2568 .unwrap_or_default()
2569 };
2570
2571 let byte_offset = unsafe { ffi::ts_query_start_byte_for_pattern(ptr.0, i as u32) };
2572 let row = source
2573 .char_indices()
2574 .take_while(|(i, _)| *i < byte_offset as usize)
2575 .filter(|(_, c)| *c == '\n')
2576 .count();
2577
2578 use ffi::TSQueryPredicateStepType as T;
2579 const TYPE_DONE: T = ffi::TSQueryPredicateStepTypeDone;
2580 const TYPE_CAPTURE: T = ffi::TSQueryPredicateStepTypeCapture;
2581 const TYPE_STRING: T = ffi::TSQueryPredicateStepTypeString;
2582
2583 let mut text_predicates = Vec::new();
2584 let mut property_predicates = Vec::new();
2585 let mut property_settings = Vec::new();
2586 let mut general_predicates = Vec::new();
2587 for p in predicate_steps.split(|s| s.type_ == TYPE_DONE) {
2588 if p.is_empty() {
2589 continue;
2590 }
2591
2592 if p[0].type_ != TYPE_STRING {
2593 return Err(predicate_error(
2594 row,
2595 format!(
2596 "Expected predicate to start with a function name. Got @{}.",
2597 capture_names[p[0].value_id as usize],
2598 ),
2599 ));
2600 }
2601
2602 let operator_name = string_values[p[0].value_id as usize];
2604 match operator_name {
2605 "eq?" | "not-eq?" | "any-eq?" | "any-not-eq?" => {
2606 if p.len() != 3 {
2607 return Err(predicate_error(
2608 row,
2609 format!(
2610 "Wrong number of arguments to #eq? predicate. Expected 2, got {}.",
2611 p.len() - 1
2612 ),
2613 ));
2614 }
2615 if p[1].type_ != TYPE_CAPTURE {
2616 return Err(predicate_error(row, format!(
2617 "First argument to #eq? predicate must be a capture name. Got literal \"{}\".",
2618 string_values[p[1].value_id as usize],
2619 )));
2620 }
2621
2622 let is_positive = operator_name == "eq?" || operator_name == "any-eq?";
2623 let match_all = match operator_name {
2624 "eq?" | "not-eq?" => true,
2625 "any-eq?" | "any-not-eq?" => false,
2626 _ => unreachable!(),
2627 };
2628 text_predicates.push(if p[2].type_ == TYPE_CAPTURE {
2629 TextPredicateCapture::EqCapture(
2630 p[1].value_id,
2631 p[2].value_id,
2632 is_positive,
2633 match_all,
2634 )
2635 } else {
2636 TextPredicateCapture::EqString(
2637 p[1].value_id,
2638 string_values[p[2].value_id as usize].to_string().into(),
2639 is_positive,
2640 match_all,
2641 )
2642 });
2643 }
2644
2645 "match?" | "not-match?" | "any-match?" | "any-not-match?" => {
2646 if p.len() != 3 {
2647 return Err(predicate_error(row, format!(
2648 "Wrong number of arguments to #match? predicate. Expected 2, got {}.",
2649 p.len() - 1
2650 )));
2651 }
2652 if p[1].type_ != TYPE_CAPTURE {
2653 return Err(predicate_error(row, format!(
2654 "First argument to #match? predicate must be a capture name. Got literal \"{}\".",
2655 string_values[p[1].value_id as usize],
2656 )));
2657 }
2658 if p[2].type_ == TYPE_CAPTURE {
2659 return Err(predicate_error(row, format!(
2660 "Second argument to #match? predicate must be a literal. Got capture @{}.",
2661 capture_names[p[2].value_id as usize],
2662 )));
2663 }
2664
2665 let is_positive =
2666 operator_name == "match?" || operator_name == "any-match?";
2667 let match_all = match operator_name {
2668 "match?" | "not-match?" => true,
2669 "any-match?" | "any-not-match?" => false,
2670 _ => unreachable!(),
2671 };
2672 let regex = &string_values[p[2].value_id as usize];
2673 text_predicates.push(TextPredicateCapture::MatchString(
2674 p[1].value_id,
2675 regex::bytes::Regex::new(regex).map_err(|_| {
2676 predicate_error(row, format!("Invalid regex '{regex}'"))
2677 })?,
2678 is_positive,
2679 match_all,
2680 ));
2681 }
2682
2683 "set!" => property_settings.push(Self::parse_property(
2684 row,
2685 operator_name,
2686 &capture_names,
2687 &string_values,
2688 &p[1..],
2689 )?),
2690
2691 "is?" | "is-not?" => property_predicates.push((
2692 Self::parse_property(
2693 row,
2694 operator_name,
2695 &capture_names,
2696 &string_values,
2697 &p[1..],
2698 )?,
2699 operator_name == "is?",
2700 )),
2701
2702 "any-of?" | "not-any-of?" => {
2703 if p.len() < 2 {
2704 return Err(predicate_error(row, format!(
2705 "Wrong number of arguments to #any-of? predicate. Expected at least 1, got {}.",
2706 p.len() - 1
2707 )));
2708 }
2709 if p[1].type_ != TYPE_CAPTURE {
2710 return Err(predicate_error(row, format!(
2711 "First argument to #any-of? predicate must be a capture name. Got literal \"{}\".",
2712 string_values[p[1].value_id as usize],
2713 )));
2714 }
2715
2716 let is_positive = operator_name == "any-of?";
2717 let mut values = Vec::new();
2718 for arg in &p[2..] {
2719 if arg.type_ == TYPE_CAPTURE {
2720 return Err(predicate_error(row, format!(
2721 "Arguments to #any-of? predicate must be literals. Got capture @{}.",
2722 capture_names[arg.value_id as usize],
2723 )));
2724 }
2725 values.push(string_values[arg.value_id as usize]);
2726 }
2727 text_predicates.push(TextPredicateCapture::AnyString(
2728 p[1].value_id,
2729 values
2730 .iter()
2731 .map(|x| (*x).to_string().into())
2732 .collect::<Vec<_>>()
2733 .into(),
2734 is_positive,
2735 ));
2736 }
2737
2738 _ => general_predicates.push(QueryPredicate {
2739 operator: operator_name.to_string().into(),
2740 args: p[1..]
2741 .iter()
2742 .map(|a| {
2743 if a.type_ == TYPE_CAPTURE {
2744 QueryPredicateArg::Capture(a.value_id)
2745 } else {
2746 QueryPredicateArg::String(
2747 string_values[a.value_id as usize].to_string().into(),
2748 )
2749 }
2750 })
2751 .collect(),
2752 }),
2753 }
2754 }
2755
2756 text_predicates_vec.push(text_predicates.into());
2757 property_predicates_vec.push(property_predicates.into());
2758 property_settings_vec.push(property_settings.into());
2759 general_predicates_vec.push(general_predicates.into());
2760 }
2761
2762 let result = Self {
2763 ptr: unsafe { NonNull::new_unchecked(ptr.0) },
2764 capture_names: capture_names.into(),
2765 capture_quantifiers: capture_quantifiers_vec.into(),
2766 text_predicates: text_predicates_vec.into(),
2767 property_predicates: property_predicates_vec.into(),
2768 property_settings: property_settings_vec.into(),
2769 general_predicates: general_predicates_vec.into(),
2770 };
2771
2772 core::mem::forget(ptr);
2773
2774 Ok(result)
2775 }
2776
2777 #[doc(alias = "ts_query_start_byte_for_pattern")]
2780 #[must_use]
2781 pub fn start_byte_for_pattern(&self, pattern_index: usize) -> usize {
2782 assert!(
2783 pattern_index < self.text_predicates.len(),
2784 "Pattern index is {pattern_index} but the pattern count is {}",
2785 self.text_predicates.len(),
2786 );
2787 unsafe {
2788 ffi::ts_query_start_byte_for_pattern(self.ptr.as_ptr(), pattern_index as u32) as usize
2789 }
2790 }
2791
2792 #[doc(alias = "ts_query_end_byte_for_pattern")]
2795 #[must_use]
2796 pub fn end_byte_for_pattern(&self, pattern_index: usize) -> usize {
2797 assert!(
2798 pattern_index < self.text_predicates.len(),
2799 "Pattern index is {pattern_index} but the pattern count is {}",
2800 self.text_predicates.len(),
2801 );
2802 unsafe {
2803 ffi::ts_query_end_byte_for_pattern(self.ptr.as_ptr(), pattern_index as u32) as usize
2804 }
2805 }
2806
2807 #[doc(alias = "ts_query_pattern_count")]
2809 #[must_use]
2810 pub fn pattern_count(&self) -> usize {
2811 unsafe { ffi::ts_query_pattern_count(self.ptr.as_ptr()) as usize }
2812 }
2813
2814 #[must_use]
2816 pub const fn capture_names(&self) -> &[&str] {
2817 &self.capture_names
2818 }
2819
2820 #[must_use]
2822 pub const fn capture_quantifiers(&self, index: usize) -> &[CaptureQuantifier] {
2823 &self.capture_quantifiers[index]
2824 }
2825
2826 #[must_use]
2828 pub fn capture_index_for_name(&self, name: &str) -> Option<u32> {
2829 self.capture_names
2830 .iter()
2831 .position(|n| *n == name)
2832 .map(|ix| ix as u32)
2833 }
2834
2835 #[must_use]
2839 pub const fn property_predicates(&self, index: usize) -> &[(QueryProperty, bool)] {
2840 &self.property_predicates[index]
2841 }
2842
2843 #[must_use]
2847 pub const fn property_settings(&self, index: usize) -> &[QueryProperty] {
2848 &self.property_settings[index]
2849 }
2850
2851 #[must_use]
2859 pub const fn general_predicates(&self, index: usize) -> &[QueryPredicate] {
2860 &self.general_predicates[index]
2861 }
2862
2863 #[doc(alias = "ts_query_disable_capture")]
2868 pub fn disable_capture(&mut self, name: &str) {
2869 unsafe {
2870 ffi::ts_query_disable_capture(
2871 self.ptr.as_ptr(),
2872 name.as_bytes().as_ptr().cast::<c_char>(),
2873 name.len() as u32,
2874 );
2875 }
2876 }
2877
2878 #[doc(alias = "ts_query_disable_pattern")]
2883 pub fn disable_pattern(&mut self, index: usize) {
2884 unsafe { ffi::ts_query_disable_pattern(self.ptr.as_ptr(), index as u32) }
2885 }
2886
2887 #[doc(alias = "ts_query_is_pattern_rooted")]
2889 #[must_use]
2890 pub fn is_pattern_rooted(&self, index: usize) -> bool {
2891 unsafe { ffi::ts_query_is_pattern_rooted(self.ptr.as_ptr(), index as u32) }
2892 }
2893
2894 #[doc(alias = "ts_query_is_pattern_non_local")]
2896 #[must_use]
2897 pub fn is_pattern_non_local(&self, index: usize) -> bool {
2898 unsafe { ffi::ts_query_is_pattern_non_local(self.ptr.as_ptr(), index as u32) }
2899 }
2900
2901 #[doc(alias = "ts_query_is_pattern_guaranteed_at_step")]
2906 #[must_use]
2907 pub fn is_pattern_guaranteed_at_step(&self, byte_offset: usize) -> bool {
2908 unsafe {
2909 ffi::ts_query_is_pattern_guaranteed_at_step(self.ptr.as_ptr(), byte_offset as u32)
2910 }
2911 }
2912
2913 fn parse_property(
2914 row: usize,
2915 function_name: &str,
2916 capture_names: &[&str],
2917 string_values: &[&str],
2918 args: &[ffi::TSQueryPredicateStep],
2919 ) -> Result<QueryProperty, QueryError> {
2920 if args.is_empty() || args.len() > 3 {
2921 return Err(predicate_error(
2922 row,
2923 format!(
2924 "Wrong number of arguments to {function_name} predicate. Expected 1 to 3, got {}.",
2925 args.len(),
2926 ),
2927 ));
2928 }
2929
2930 let mut capture_id = None;
2931 let mut key = None;
2932 let mut value = None;
2933
2934 for arg in args {
2935 if arg.type_ == ffi::TSQueryPredicateStepTypeCapture {
2936 if capture_id.is_some() {
2937 return Err(predicate_error(
2938 row,
2939 format!(
2940 "Invalid arguments to {function_name} predicate. Unexpected second capture name @{}",
2941 capture_names[arg.value_id as usize]
2942 ),
2943 ));
2944 }
2945 capture_id = Some(arg.value_id as usize);
2946 } else if key.is_none() {
2947 key = Some(&string_values[arg.value_id as usize]);
2948 } else if value.is_none() {
2949 value = Some(string_values[arg.value_id as usize]);
2950 } else {
2951 return Err(predicate_error(
2952 row,
2953 format!(
2954 "Invalid arguments to {function_name} predicate. Unexpected third argument @{}",
2955 string_values[arg.value_id as usize]
2956 ),
2957 ));
2958 }
2959 }
2960
2961 if let Some(key) = key {
2962 Ok(QueryProperty::new(key, value, capture_id))
2963 } else {
2964 Err(predicate_error(
2965 row,
2966 format!("Invalid arguments to {function_name} predicate. Missing key argument",),
2967 ))
2968 }
2969 }
2970}
2971
2972impl Default for QueryCursor {
2973 fn default() -> Self {
2974 Self::new()
2975 }
2976}
2977
2978impl QueryCursor {
2979 #[doc(alias = "ts_query_cursor_new")]
2984 #[must_use]
2985 pub fn new() -> Self {
2986 Self {
2987 ptr: unsafe { NonNull::new_unchecked(ffi::ts_query_cursor_new()) },
2988 }
2989 }
2990
2991 #[doc(alias = "ts_query_cursor_match_limit")]
2993 #[must_use]
2994 pub fn match_limit(&self) -> u32 {
2995 unsafe { ffi::ts_query_cursor_match_limit(self.ptr.as_ptr()) }
2996 }
2997
2998 #[doc(alias = "ts_query_cursor_set_match_limit")]
3001 pub fn set_match_limit(&mut self, limit: u32) {
3002 unsafe {
3003 ffi::ts_query_cursor_set_match_limit(self.ptr.as_ptr(), limit);
3004 }
3005 }
3006
3007 #[doc(alias = "ts_query_cursor_set_timeout_micros")]
3012 #[deprecated(
3013 since = "0.25.0",
3014 note = "Prefer using `matches_with_options` or `captures_with_options` and using a callback"
3015 )]
3016 pub fn set_timeout_micros(&mut self, timeout: u64) {
3017 unsafe {
3018 ffi::ts_query_cursor_set_timeout_micros(self.ptr.as_ptr(), timeout);
3019 }
3020 }
3021
3022 #[doc(alias = "ts_query_cursor_timeout_micros")]
3026 #[deprecated(
3027 since = "0.25.0",
3028 note = "Prefer using `matches_with_options` or `captures_with_options` and using a callback"
3029 )]
3030 #[must_use]
3031 pub fn timeout_micros(&self) -> u64 {
3032 unsafe { ffi::ts_query_cursor_timeout_micros(self.ptr.as_ptr()) }
3033 }
3034
3035 #[doc(alias = "ts_query_cursor_did_exceed_match_limit")]
3038 #[must_use]
3039 pub fn did_exceed_match_limit(&self) -> bool {
3040 unsafe { ffi::ts_query_cursor_did_exceed_match_limit(self.ptr.as_ptr()) }
3041 }
3042
3043 #[doc(alias = "ts_query_cursor_exec")]
3054 pub fn matches<'query, 'cursor: 'query, 'tree, T: TextProvider<I>, I: AsRef<[u8]>>(
3055 &'cursor mut self,
3056 query: &'query Query,
3057 node: Node<'tree>,
3058 text_provider: T,
3059 ) -> QueryMatches<'query, 'tree, T, I> {
3060 let ptr = self.ptr.as_ptr();
3061 unsafe { ffi::ts_query_cursor_exec(ptr, query.ptr.as_ptr(), node.0) };
3062 QueryMatches {
3063 ptr,
3064 query,
3065 text_provider,
3066 buffer1: Vec::default(),
3067 buffer2: Vec::default(),
3068 current_match: None,
3069 _options: None,
3070 _phantom: PhantomData,
3071 }
3072 }
3073
3074 #[doc(alias = "ts_query_cursor_exec_with_options")]
3081 pub fn matches_with_options<
3082 'query,
3083 'cursor: 'query,
3084 'tree,
3085 T: TextProvider<I>,
3086 I: AsRef<[u8]>,
3087 >(
3088 &'cursor mut self,
3089 query: &'query Query,
3090 node: Node<'tree>,
3091 text_provider: T,
3092 options: QueryCursorOptions,
3093 ) -> QueryMatches<'query, 'tree, T, I> {
3094 unsafe extern "C" fn progress(state: *mut ffi::TSQueryCursorState) -> bool {
3095 let callback = (*state)
3096 .payload
3097 .cast::<QueryProgressCallback>()
3098 .as_mut()
3099 .unwrap();
3100 (callback)(&QueryCursorState::from_raw(state))
3101 }
3102
3103 let query_options = options.progress_callback.map(|cb| {
3104 QueryCursorOptionsDrop(Box::into_raw(Box::new(ffi::TSQueryCursorOptions {
3105 payload: Box::into_raw(Box::new(cb)).cast::<c_void>(),
3106 progress_callback: Some(progress),
3107 })))
3108 });
3109
3110 let ptr = self.ptr.as_ptr();
3111 unsafe {
3112 ffi::ts_query_cursor_exec_with_options(
3113 ptr,
3114 query.ptr.as_ptr(),
3115 node.0,
3116 query_options.as_ref().map_or(ptr::null_mut(), |q| q.0),
3117 );
3118 }
3119 QueryMatches {
3120 ptr,
3121 query,
3122 text_provider,
3123 buffer1: Vec::default(),
3124 buffer2: Vec::default(),
3125 current_match: None,
3126 _options: query_options,
3127 _phantom: PhantomData,
3128 }
3129 }
3130
3131 #[doc(alias = "ts_query_cursor_exec")]
3141 pub fn captures<'query, 'cursor: 'query, 'tree, T: TextProvider<I>, I: AsRef<[u8]>>(
3142 &'cursor mut self,
3143 query: &'query Query,
3144 node: Node<'tree>,
3145 text_provider: T,
3146 ) -> QueryCaptures<'query, 'tree, T, I> {
3147 let ptr = self.ptr.as_ptr();
3148 unsafe { ffi::ts_query_cursor_exec(ptr, query.ptr.as_ptr(), node.0) };
3149 QueryCaptures {
3150 ptr,
3151 query,
3152 text_provider,
3153 buffer1: Vec::default(),
3154 buffer2: Vec::default(),
3155 current_match: None,
3156 _options: None,
3157 _phantom: PhantomData,
3158 }
3159 }
3160
3161 #[doc(alias = "ts_query_cursor_exec")]
3167 pub fn captures_with_options<
3168 'query,
3169 'cursor: 'query,
3170 'tree,
3171 T: TextProvider<I>,
3172 I: AsRef<[u8]>,
3173 >(
3174 &'cursor mut self,
3175 query: &'query Query,
3176 node: Node<'tree>,
3177 text_provider: T,
3178 options: QueryCursorOptions,
3179 ) -> QueryCaptures<'query, 'tree, T, I> {
3180 unsafe extern "C" fn progress(state: *mut ffi::TSQueryCursorState) -> bool {
3181 let callback = (*state)
3182 .payload
3183 .cast::<QueryProgressCallback>()
3184 .as_mut()
3185 .unwrap();
3186 (callback)(&QueryCursorState::from_raw(state))
3187 }
3188
3189 let query_options = options.progress_callback.map(|cb| {
3190 QueryCursorOptionsDrop(Box::into_raw(Box::new(ffi::TSQueryCursorOptions {
3191 payload: Box::into_raw(Box::new(cb)).cast::<c_void>(),
3192 progress_callback: Some(progress),
3193 })))
3194 });
3195
3196 let ptr = self.ptr.as_ptr();
3197 unsafe {
3198 ffi::ts_query_cursor_exec_with_options(
3199 ptr,
3200 query.ptr.as_ptr(),
3201 node.0,
3202 query_options.as_ref().map_or(ptr::null_mut(), |q| q.0),
3203 );
3204 }
3205 QueryCaptures {
3206 ptr,
3207 query,
3208 text_provider,
3209 buffer1: Vec::default(),
3210 buffer2: Vec::default(),
3211 current_match: None,
3212 _options: query_options,
3213 _phantom: PhantomData,
3214 }
3215 }
3216
3217 #[doc(alias = "ts_query_cursor_set_byte_range")]
3220 pub fn set_byte_range(&mut self, range: ops::Range<usize>) -> &mut Self {
3221 unsafe {
3222 ffi::ts_query_cursor_set_byte_range(
3223 self.ptr.as_ptr(),
3224 range.start as u32,
3225 range.end as u32,
3226 );
3227 }
3228 self
3229 }
3230
3231 #[doc(alias = "ts_query_cursor_set_point_range")]
3234 pub fn set_point_range(&mut self, range: ops::Range<Point>) -> &mut Self {
3235 unsafe {
3236 ffi::ts_query_cursor_set_point_range(
3237 self.ptr.as_ptr(),
3238 range.start.into(),
3239 range.end.into(),
3240 );
3241 }
3242 self
3243 }
3244
3245 #[doc(alias = "ts_query_cursor_set_max_start_depth")]
3260 pub fn set_max_start_depth(&mut self, max_start_depth: Option<u32>) -> &mut Self {
3261 unsafe {
3262 ffi::ts_query_cursor_set_max_start_depth(
3263 self.ptr.as_ptr(),
3264 max_start_depth.unwrap_or(u32::MAX),
3265 );
3266 }
3267 self
3268 }
3269}
3270
3271impl<'tree> QueryMatch<'_, 'tree> {
3272 #[must_use]
3273 pub const fn id(&self) -> u32 {
3274 self.id
3275 }
3276
3277 #[doc(alias = "ts_query_cursor_remove_match")]
3278 pub fn remove(&self) {
3279 unsafe { ffi::ts_query_cursor_remove_match(self.cursor, self.id) }
3280 }
3281
3282 pub fn nodes_for_capture_index(
3283 &self,
3284 capture_ix: u32,
3285 ) -> impl Iterator<Item = Node<'tree>> + '_ {
3286 self.captures
3287 .iter()
3288 .filter_map(move |capture| (capture.index == capture_ix).then_some(capture.node))
3289 }
3290
3291 fn new(m: &ffi::TSQueryMatch, cursor: *mut ffi::TSQueryCursor) -> Self {
3292 QueryMatch {
3293 cursor,
3294 id: m.id,
3295 pattern_index: m.pattern_index as usize,
3296 captures: (m.capture_count > 0)
3297 .then(|| unsafe {
3298 slice::from_raw_parts(
3299 m.captures.cast::<QueryCapture<'tree>>(),
3300 m.capture_count as usize,
3301 )
3302 })
3303 .unwrap_or_default(),
3304 }
3305 }
3306
3307 pub fn satisfies_text_predicates<I: AsRef<[u8]>>(
3308 &self,
3309 query: &Query,
3310 buffer1: &mut Vec<u8>,
3311 buffer2: &mut Vec<u8>,
3312 text_provider: &mut impl TextProvider<I>,
3313 ) -> bool {
3314 struct NodeText<'a, T> {
3315 buffer: &'a mut Vec<u8>,
3316 first_chunk: Option<T>,
3317 }
3318 impl<'a, T: AsRef<[u8]>> NodeText<'a, T> {
3319 fn new(buffer: &'a mut Vec<u8>) -> Self {
3320 Self {
3321 buffer,
3322 first_chunk: None,
3323 }
3324 }
3325
3326 fn get_text(&mut self, chunks: &mut impl Iterator<Item = T>) -> &[u8] {
3327 self.first_chunk = chunks.next();
3328 if let Some(next_chunk) = chunks.next() {
3329 self.buffer.clear();
3330 self.buffer
3331 .extend_from_slice(self.first_chunk.as_ref().unwrap().as_ref());
3332 self.buffer.extend_from_slice(next_chunk.as_ref());
3333 for chunk in chunks {
3334 self.buffer.extend_from_slice(chunk.as_ref());
3335 }
3336 self.buffer.as_slice()
3337 } else if let Some(ref first_chunk) = self.first_chunk {
3338 first_chunk.as_ref()
3339 } else {
3340 &[]
3341 }
3342 }
3343 }
3344
3345 let mut node_text1 = NodeText::new(buffer1);
3346 let mut node_text2 = NodeText::new(buffer2);
3347
3348 query.text_predicates[self.pattern_index]
3349 .iter()
3350 .all(|predicate| match predicate {
3351 TextPredicateCapture::EqCapture(i, j, is_positive, match_all_nodes) => {
3352 let mut nodes_1 = self.nodes_for_capture_index(*i);
3353 let mut nodes_2 = self.nodes_for_capture_index(*j);
3354 while let (Some(node1), Some(node2)) = (nodes_1.next(), nodes_2.next()) {
3355 let mut text1 = text_provider.text(node1);
3356 let mut text2 = text_provider.text(node2);
3357 let text1 = node_text1.get_text(&mut text1);
3358 let text2 = node_text2.get_text(&mut text2);
3359 let is_positive_match = text1 == text2;
3360 if is_positive_match != *is_positive && *match_all_nodes {
3361 return false;
3362 }
3363 if is_positive_match == *is_positive && !*match_all_nodes {
3364 return true;
3365 }
3366 }
3367 nodes_1.next().is_none() && nodes_2.next().is_none()
3368 }
3369 TextPredicateCapture::EqString(i, s, is_positive, match_all_nodes) => {
3370 let nodes = self.nodes_for_capture_index(*i);
3371 for node in nodes {
3372 let mut text = text_provider.text(node);
3373 let text = node_text1.get_text(&mut text);
3374 let is_positive_match = text == s.as_bytes();
3375 if is_positive_match != *is_positive && *match_all_nodes {
3376 return false;
3377 }
3378 if is_positive_match == *is_positive && !*match_all_nodes {
3379 return true;
3380 }
3381 }
3382 true
3383 }
3384 TextPredicateCapture::MatchString(i, r, is_positive, match_all_nodes) => {
3385 let nodes = self.nodes_for_capture_index(*i);
3386 for node in nodes {
3387 let mut text = text_provider.text(node);
3388 let text = node_text1.get_text(&mut text);
3389 let is_positive_match = r.is_match(text);
3390 if is_positive_match != *is_positive && *match_all_nodes {
3391 return false;
3392 }
3393 if is_positive_match == *is_positive && !*match_all_nodes {
3394 return true;
3395 }
3396 }
3397 true
3398 }
3399 TextPredicateCapture::AnyString(i, v, is_positive) => {
3400 let nodes = self.nodes_for_capture_index(*i);
3401 for node in nodes {
3402 let mut text = text_provider.text(node);
3403 let text = node_text1.get_text(&mut text);
3404 if (v.iter().any(|s| text == s.as_bytes())) != *is_positive {
3405 return false;
3406 }
3407 }
3408 true
3409 }
3410 })
3411 }
3412}
3413
3414impl QueryProperty {
3415 #[must_use]
3416 pub fn new(key: &str, value: Option<&str>, capture_id: Option<usize>) -> Self {
3417 Self {
3418 capture_id,
3419 key: key.to_string().into(),
3420 value: value.map(|s| s.to_string().into()),
3421 }
3422 }
3423}
3424
3425impl<'query, 'tree: 'query, T: TextProvider<I>, I: AsRef<[u8]>> StreamingIterator
3429 for QueryMatches<'query, 'tree, T, I>
3430{
3431 type Item = QueryMatch<'query, 'tree>;
3432
3433 fn advance(&mut self) {
3434 self.current_match = unsafe {
3435 loop {
3436 let mut m = MaybeUninit::<ffi::TSQueryMatch>::uninit();
3437 if ffi::ts_query_cursor_next_match(self.ptr, m.as_mut_ptr()) {
3438 let result = QueryMatch::new(&m.assume_init(), self.ptr);
3439 if result.satisfies_text_predicates(
3440 self.query,
3441 &mut self.buffer1,
3442 &mut self.buffer2,
3443 &mut self.text_provider,
3444 ) {
3445 break Some(result);
3446 }
3447 } else {
3448 break None;
3449 }
3450 }
3451 };
3452 }
3453
3454 fn get(&self) -> Option<&Self::Item> {
3455 self.current_match.as_ref()
3456 }
3457}
3458
3459impl<'query, 'tree: 'query, T: TextProvider<I>, I: AsRef<[u8]>> StreamingIteratorMut
3460 for QueryMatches<'query, 'tree, T, I>
3461{
3462 fn get_mut(&mut self) -> Option<&mut Self::Item> {
3463 self.current_match.as_mut()
3464 }
3465}
3466
3467impl<'query, 'tree: 'query, T: TextProvider<I>, I: AsRef<[u8]>> StreamingIterator
3468 for QueryCaptures<'query, 'tree, T, I>
3469{
3470 type Item = (QueryMatch<'query, 'tree>, usize);
3471
3472 fn advance(&mut self) {
3473 self.current_match = unsafe {
3474 loop {
3475 let mut capture_index = 0u32;
3476 let mut m = MaybeUninit::<ffi::TSQueryMatch>::uninit();
3477 if ffi::ts_query_cursor_next_capture(
3478 self.ptr,
3479 m.as_mut_ptr(),
3480 core::ptr::addr_of_mut!(capture_index),
3481 ) {
3482 let result = QueryMatch::new(&m.assume_init(), self.ptr);
3483 if result.satisfies_text_predicates(
3484 self.query,
3485 &mut self.buffer1,
3486 &mut self.buffer2,
3487 &mut self.text_provider,
3488 ) {
3489 break Some((result, capture_index as usize));
3490 }
3491 result.remove();
3492 } else {
3493 break None;
3494 }
3495 }
3496 }
3497 }
3498
3499 fn get(&self) -> Option<&Self::Item> {
3500 self.current_match.as_ref()
3501 }
3502}
3503
3504impl<'query, 'tree: 'query, T: TextProvider<I>, I: AsRef<[u8]>> StreamingIteratorMut
3505 for QueryCaptures<'query, 'tree, T, I>
3506{
3507 fn get_mut(&mut self) -> Option<&mut Self::Item> {
3508 self.current_match.as_mut()
3509 }
3510}
3511
3512impl<T: TextProvider<I>, I: AsRef<[u8]>> QueryMatches<'_, '_, T, I> {
3513 #[doc(alias = "ts_query_cursor_set_byte_range")]
3514 pub fn set_byte_range(&mut self, range: ops::Range<usize>) {
3515 unsafe {
3516 ffi::ts_query_cursor_set_byte_range(self.ptr, range.start as u32, range.end as u32);
3517 }
3518 }
3519
3520 #[doc(alias = "ts_query_cursor_set_point_range")]
3521 pub fn set_point_range(&mut self, range: ops::Range<Point>) {
3522 unsafe {
3523 ffi::ts_query_cursor_set_point_range(self.ptr, range.start.into(), range.end.into());
3524 }
3525 }
3526}
3527
3528impl<T: TextProvider<I>, I: AsRef<[u8]>> QueryCaptures<'_, '_, T, I> {
3529 #[doc(alias = "ts_query_cursor_set_byte_range")]
3530 pub fn set_byte_range(&mut self, range: ops::Range<usize>) {
3531 unsafe {
3532 ffi::ts_query_cursor_set_byte_range(self.ptr, range.start as u32, range.end as u32);
3533 }
3534 }
3535
3536 #[doc(alias = "ts_query_cursor_set_point_range")]
3537 pub fn set_point_range(&mut self, range: ops::Range<Point>) {
3538 unsafe {
3539 ffi::ts_query_cursor_set_point_range(self.ptr, range.start.into(), range.end.into());
3540 }
3541 }
3542}
3543
3544impl fmt::Debug for QueryMatch<'_, '_> {
3545 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3546 write!(
3547 f,
3548 "QueryMatch {{ id: {}, pattern_index: {}, captures: {:?} }}",
3549 self.id, self.pattern_index, self.captures
3550 )
3551 }
3552}
3553
3554impl<F, R, I> TextProvider<I> for F
3555where
3556 F: FnMut(Node) -> R,
3557 R: Iterator<Item = I>,
3558 I: AsRef<[u8]>,
3559{
3560 type I = R;
3561
3562 fn text(&mut self, node: Node) -> Self::I {
3563 (self)(node)
3564 }
3565}
3566
3567impl<'a> TextProvider<&'a [u8]> for &'a [u8] {
3568 type I = iter::Once<&'a [u8]>;
3569
3570 fn text(&mut self, node: Node) -> Self::I {
3571 iter::once(&self[node.byte_range()])
3572 }
3573}
3574
3575impl PartialEq for Query {
3576 fn eq(&self, other: &Self) -> bool {
3577 self.ptr == other.ptr
3578 }
3579}
3580
3581impl Drop for Query {
3582 fn drop(&mut self) {
3583 unsafe { ffi::ts_query_delete(self.ptr.as_ptr()) }
3584 }
3585}
3586
3587impl Drop for QueryCursor {
3588 fn drop(&mut self) {
3589 unsafe { ffi::ts_query_cursor_delete(self.ptr.as_ptr()) }
3590 }
3591}
3592
3593impl Point {
3594 #[must_use]
3595 pub const fn new(row: usize, column: usize) -> Self {
3596 Self { row, column }
3597 }
3598}
3599
3600impl fmt::Display for Point {
3601 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3602 write!(f, "({}, {})", self.row, self.column)
3603 }
3604}
3605
3606impl From<Point> for ffi::TSPoint {
3607 fn from(val: Point) -> Self {
3608 Self {
3609 row: val.row as u32,
3610 column: val.column as u32,
3611 }
3612 }
3613}
3614
3615impl From<ffi::TSPoint> for Point {
3616 fn from(point: ffi::TSPoint) -> Self {
3617 Self {
3618 row: point.row as usize,
3619 column: point.column as usize,
3620 }
3621 }
3622}
3623
3624impl From<Range> for ffi::TSRange {
3625 fn from(val: Range) -> Self {
3626 Self {
3627 start_byte: val.start_byte as u32,
3628 end_byte: val.end_byte as u32,
3629 start_point: val.start_point.into(),
3630 end_point: val.end_point.into(),
3631 }
3632 }
3633}
3634
3635impl From<ffi::TSRange> for Range {
3636 fn from(range: ffi::TSRange) -> Self {
3637 Self {
3638 start_byte: range.start_byte as usize,
3639 end_byte: range.end_byte as usize,
3640 start_point: range.start_point.into(),
3641 end_point: range.end_point.into(),
3642 }
3643 }
3644}
3645
3646impl From<&'_ InputEdit> for ffi::TSInputEdit {
3647 fn from(val: &'_ InputEdit) -> Self {
3648 Self {
3649 start_byte: val.start_byte as u32,
3650 old_end_byte: val.old_end_byte as u32,
3651 new_end_byte: val.new_end_byte as u32,
3652 start_point: val.start_position.into(),
3653 old_end_point: val.old_end_position.into(),
3654 new_end_point: val.new_end_position.into(),
3655 }
3656 }
3657}
3658
3659impl<'a> LossyUtf8<'a> {
3660 #[must_use]
3661 pub const fn new(bytes: &'a [u8]) -> Self {
3662 LossyUtf8 {
3663 bytes,
3664 in_replacement: false,
3665 }
3666 }
3667}
3668
3669impl<'a> Iterator for LossyUtf8<'a> {
3670 type Item = &'a str;
3671
3672 fn next(&mut self) -> Option<&'a str> {
3673 if self.bytes.is_empty() {
3674 return None;
3675 }
3676 if self.in_replacement {
3677 self.in_replacement = false;
3678 return Some("\u{fffd}");
3679 }
3680 match core::str::from_utf8(self.bytes) {
3681 Ok(valid) => {
3682 self.bytes = &[];
3683 Some(valid)
3684 }
3685 Err(error) => {
3686 if let Some(error_len) = error.error_len() {
3687 let error_start = error.valid_up_to();
3688 if error_start > 0 {
3689 let result =
3690 unsafe { core::str::from_utf8_unchecked(&self.bytes[..error_start]) };
3691 self.bytes = &self.bytes[(error_start + error_len)..];
3692 self.in_replacement = true;
3693 Some(result)
3694 } else {
3695 self.bytes = &self.bytes[error_len..];
3696 Some("\u{fffd}")
3697 }
3698 } else {
3699 None
3700 }
3701 }
3702 }
3703 }
3704}
3705
3706#[must_use]
3707const fn predicate_error(row: usize, message: String) -> QueryError {
3708 QueryError {
3709 kind: QueryErrorKind::Predicate,
3710 row,
3711 column: 0,
3712 offset: 0,
3713 message,
3714 }
3715}
3716
3717impl fmt::Display for IncludedRangesError {
3718 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3719 write!(f, "Incorrect range by index: {}", self.0)
3720 }
3721}
3722
3723impl fmt::Display for LanguageError {
3724 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3725 write!(
3726 f,
3727 "Incompatible language version {}. Expected minimum {}, maximum {}",
3728 self.version, MIN_COMPATIBLE_LANGUAGE_VERSION, LANGUAGE_VERSION,
3729 )
3730 }
3731}
3732
3733impl fmt::Display for QueryError {
3734 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3735 let msg = match self.kind {
3736 QueryErrorKind::Field => "Invalid field name ",
3737 QueryErrorKind::NodeType => "Invalid node type ",
3738 QueryErrorKind::Capture => "Invalid capture name ",
3739 QueryErrorKind::Predicate => "Invalid predicate: ",
3740 QueryErrorKind::Structure => "Impossible pattern:\n",
3741 QueryErrorKind::Syntax => "Invalid syntax:\n",
3742 QueryErrorKind::Language => "",
3743 };
3744 if msg.is_empty() {
3745 write!(f, "{}", self.message)
3746 } else {
3747 write!(
3748 f,
3749 "Query error at {}:{}. {}{}",
3750 self.row + 1,
3751 self.column + 1,
3752 msg,
3753 self.message
3754 )
3755 }
3756 }
3757}
3758
3759#[doc(hidden)]
3760#[must_use]
3761pub fn format_sexp(sexp: &str, initial_indent_level: usize) -> String {
3762 let mut indent_level = initial_indent_level;
3763 let mut formatted = String::new();
3764 let mut has_field = false;
3765
3766 let mut c_iter = sexp.chars().peekable();
3767 let mut s = String::with_capacity(sexp.len());
3768 let mut quote = '\0';
3769 let mut saw_paren = false;
3770 let mut did_last = false;
3771
3772 let mut fetch_next_str = |next: &mut String| {
3773 next.clear();
3774 while let Some(c) = c_iter.next() {
3775 if c == '\'' || c == '"' {
3776 quote = c;
3777 } else if c == ' ' || (c == ')' && quote != '\0') {
3778 if let Some(next_c) = c_iter.peek() {
3779 if *next_c == quote {
3780 next.push(c);
3781 next.push(*next_c);
3782 c_iter.next();
3783 quote = '\0';
3784 continue;
3785 }
3786 }
3787 break;
3788 }
3789 if c == ')' {
3790 saw_paren = true;
3791 break;
3792 }
3793 next.push(c);
3794 }
3795
3796 if c_iter.peek().is_none() && next.is_empty() {
3798 if saw_paren {
3799 saw_paren = false;
3801 return Some(());
3802 }
3803 if !did_last {
3804 did_last = true;
3806 return Some(());
3807 }
3808 return None;
3809 }
3810 Some(())
3811 };
3812
3813 while fetch_next_str(&mut s).is_some() {
3814 if s.is_empty() && indent_level > 0 {
3815 indent_level -= 1;
3817 write!(formatted, ")").unwrap();
3818 } else if s.starts_with('(') {
3819 if has_field {
3820 has_field = false;
3821 } else {
3822 if indent_level > 0 {
3823 writeln!(formatted).unwrap();
3824 for _ in 0..indent_level {
3825 write!(formatted, " ").unwrap();
3826 }
3827 }
3828 indent_level += 1;
3829 }
3830
3831 write!(formatted, "{s}").unwrap();
3833
3834 if s.starts_with("(MISSING") || s.starts_with("(UNEXPECTED") {
3836 fetch_next_str(&mut s).unwrap();
3837 if s.is_empty() {
3838 while indent_level > 0 {
3839 indent_level -= 1;
3840 write!(formatted, ")").unwrap();
3841 }
3842 } else {
3843 write!(formatted, " {s}").unwrap();
3844 }
3845 }
3846 } else if s.ends_with(':') {
3847 writeln!(formatted).unwrap();
3849 for _ in 0..indent_level {
3850 write!(formatted, " ").unwrap();
3851 }
3852 write!(formatted, "{s} ").unwrap();
3853 has_field = true;
3854 indent_level += 1;
3855 }
3856 }
3857
3858 formatted
3859}
3860
3861pub fn wasm_stdlib_symbols() -> impl Iterator<Item = &'static str> {
3862 const WASM_STDLIB_SYMBOLS: &str = include_str!(concat!(env!("OUT_DIR"), "/stdlib-symbols.txt"));
3863
3864 WASM_STDLIB_SYMBOLS
3865 .lines()
3866 .map(|s| s.trim_matches(|c| c == '"' || c == ','))
3867}
3868
3869extern "C" {
3870 fn free(ptr: *mut c_void);
3871}
3872
3873static mut FREE_FN: unsafe extern "C" fn(ptr: *mut c_void) = free;
3874
3875#[doc(alias = "ts_set_allocator")]
3881pub unsafe fn set_allocator(
3882 new_malloc: Option<unsafe extern "C" fn(size: usize) -> *mut c_void>,
3883 new_calloc: Option<unsafe extern "C" fn(nmemb: usize, size: usize) -> *mut c_void>,
3884 new_realloc: Option<unsafe extern "C" fn(ptr: *mut c_void, size: usize) -> *mut c_void>,
3885 new_free: Option<unsafe extern "C" fn(ptr: *mut c_void)>,
3886) {
3887 FREE_FN = new_free.unwrap_or(free);
3888 ffi::ts_set_allocator(new_malloc, new_calloc, new_realloc, new_free);
3889}
3890
3891#[cfg(feature = "std")]
3892#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
3893impl error::Error for IncludedRangesError {}
3894#[cfg(feature = "std")]
3895#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
3896impl error::Error for LanguageError {}
3897#[cfg(feature = "std")]
3898#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
3899impl error::Error for QueryError {}
3900
3901unsafe impl Send for Language {}
3902unsafe impl Sync for Language {}
3903
3904unsafe impl Send for Node<'_> {}
3905unsafe impl Sync for Node<'_> {}
3906
3907unsafe impl Send for LookaheadIterator {}
3908unsafe impl Sync for LookaheadIterator {}
3909
3910unsafe impl Send for LookaheadNamesIterator<'_> {}
3911unsafe impl Sync for LookaheadNamesIterator<'_> {}
3912
3913unsafe impl Send for Parser {}
3914unsafe impl Sync for Parser {}
3915
3916unsafe impl Send for Query {}
3917unsafe impl Sync for Query {}
3918
3919unsafe impl Send for QueryCursor {}
3920unsafe impl Sync for QueryCursor {}
3921
3922unsafe impl Send for Tree {}
3923unsafe impl Sync for Tree {}
3924
3925unsafe impl Send for TreeCursor<'_> {}
3926unsafe impl Sync for TreeCursor<'_> {}