topiary_web_tree_sitter_sys/
lib.rs

1// FIXME: Double check we construct object properties in order.
2
3use core::cell::RefCell;
4use js_sys::{Array, Error, Function, JsString, Object, Promise, Reflect, Uint8Array};
5use wasm_bindgen::{JsCast, prelude::*};
6use wasm_bindgen_futures::JsFuture;
7
8trait JsValueExt {
9    type Value;
10    fn lift_error(self) -> Result<Self::Value, JsError>;
11}
12
13impl<T> JsValueExt for Result<T, JsValue> {
14    type Value = T;
15
16    fn lift_error(self) -> Result<Self::Value, JsError> {
17        self.map_err(|err| {
18            let message = match err.dyn_into::<Error>() {
19                Ok(error) => error.message(),
20                Err(value) => JsString::from(value),
21            };
22            JsError::new(&String::from(message))
23        })
24    }
25}
26
27#[cfg(feature = "node")]
28#[wasm_bindgen]
29extern "C" {
30    #[wasm_bindgen(js_name = "global")]
31    static GLOBAL: Object;
32}
33
34#[cfg(feature = "node")]
35#[wasm_bindgen(module = "web-tree-sitter-wasm-bindgen")]
36extern "C" {
37    fn initialize_tree_sitter() -> Promise;
38}
39
40thread_local! {
41    // Ensure `web-tree-sitter` is only initialized once
42    static TREE_SITTER_INITIALIZED: RefCell<bool> = const { RefCell::new(false) };
43}
44
45pub struct TreeSitter;
46
47impl TreeSitter {
48    #[cfg(feature = "node")]
49    pub async fn init() -> Result<(), JsError> {
50        #![allow(non_snake_case)]
51
52        // Exit early if `web-tree-sitter` is already initialized
53        if TREE_SITTER_INITIALIZED.with(|cell| *cell.borrow()) {
54            return Ok(());
55        }
56
57        JsFuture::from(initialize_tree_sitter())
58            .await
59            .lift_error()?;
60
61        // Set `web-tree-sitter` to initialized
62        TREE_SITTER_INITIALIZED.with(|cell| cell.replace(true));
63
64        Ok(())
65    }
66
67    #[cfg(feature = "web-sys")]
68    pub async fn init() -> Result<(), JsError> {
69        #![allow(non_snake_case)]
70
71        // Exit early if `web-tree-sitter` is already initialized
72        if TREE_SITTER_INITIALIZED.with(|cell| *cell.borrow()) {
73            return Ok(());
74        }
75
76        let scope = &web_sys::window().unwrap_throw();
77
78        let tree_sitter = Reflect::get(scope, &"TreeSitter".into()).and_then(|property| {
79            if property.is_undefined() {
80                let message = "window.TreeSitter is not defined; load the tree-sitter javascript module first";
81                Err(JsError::new(message).into())
82            } else {
83                Ok(property)
84            }
85        })
86        .lift_error()?;
87
88        // Call `init` from `web-tree-sitter` to initialize emscripten
89        let init = Reflect::get(&tree_sitter, &"init".into())
90            .lift_error()?
91            .unchecked_into::<Function>();
92        JsFuture::from(
93            init.call0(&JsValue::UNDEFINED)
94                .lift_error()?
95                .unchecked_into::<Promise>(),
96        )
97        .await
98        .lift_error()?;
99
100        let Language = Reflect::get(&tree_sitter, &"Language".into()).lift_error()?;
101        let Parser = tree_sitter;
102
103        // Manually define `Parser` and `Language` in a fashion that works with wasm-bindgen
104        Reflect::set(scope, &"Parser".into(), &Parser).lift_error()?;
105        Reflect::set(scope, &"Language".into(), &Language).lift_error()?;
106
107        // Set `web-tree-sitter` to initialized
108        TREE_SITTER_INITIALIZED.with(|cell| cell.replace(true));
109
110        Ok(())
111    }
112
113    fn init_guard() {
114        if !TREE_SITTER_INITIALIZED.with(|cell| *cell.borrow()) {
115            wasm_bindgen::throw_str("TreeSitter::init must be called to initialize the library");
116        }
117    }
118}
119
120#[wasm_bindgen]
121extern "C" {
122    #[derive(Clone, Debug, Eq, PartialEq)]
123    #[wasm_bindgen(extends = Object)]
124    pub type Edit;
125
126    // Instance Properties
127
128    #[wasm_bindgen(method, getter, js_name = newEndIndex)]
129    pub fn new_end_index(this: &Edit) -> u32;
130
131    #[wasm_bindgen(method, getter, js_name = newEndPosition)]
132    pub fn new_end_position(this: &Edit) -> Point;
133
134    #[wasm_bindgen(method, getter, js_name = oldEndIndex)]
135    pub fn old_end_index(this: &Edit) -> u32;
136
137    #[wasm_bindgen(method, getter, js_name = oldEndPosition)]
138    pub fn old_end_position(this: &Edit) -> Point;
139
140    #[wasm_bindgen(method, getter, js_name = startIndex)]
141    pub fn start_index(this: &Edit) -> u32;
142
143    #[wasm_bindgen(method, getter, js_name = startPosition)]
144    pub fn start_position(this: &Edit) -> Point;
145}
146
147impl Edit {
148    pub fn new(
149        start_index: u32,
150        old_end_index: u32,
151        new_end_index: u32,
152        start_position: &Point,
153        old_end_position: &Point,
154        new_end_position: &Point,
155    ) -> Self {
156        let obj = Object::new();
157        Reflect::set(&obj, &"startIndex".into(), &start_index.into()).unwrap();
158        Reflect::set(&obj, &"oldEndIndex".into(), &old_end_index.into()).unwrap();
159        Reflect::set(&obj, &"newEndIndex".into(), &new_end_index.into()).unwrap();
160        Reflect::set(&obj, &"startPosition".into(), &start_position.into()).unwrap();
161        Reflect::set(&obj, &"oldEndPosition".into(), &old_end_position.into()).unwrap();
162        Reflect::set(&obj, &"newEndPosition".into(), &new_end_position.into()).unwrap();
163        JsCast::unchecked_into(obj)
164    }
165}
166
167impl Default for Edit {
168    fn default() -> Self {
169        let start_index = Default::default();
170        let old_end_index = Default::default();
171        let new_end_index = Default::default();
172        let start_position = &Default::default();
173        let old_end_position = &Default::default();
174        let new_end_position = &Default::default();
175        Self::new(
176            start_index,
177            old_end_index,
178            new_end_index,
179            start_position,
180            old_end_position,
181            new_end_position,
182        )
183    }
184}
185
186pub type Input = Function;
187
188pub type InputClosureType = dyn FnMut(u32, Option<Point>, Option<u32>) -> Option<JsString>;
189
190pub type Logger = Function;
191
192pub type LoggerClosureType = dyn FnMut(JsString, LoggerParams, JsString);
193
194#[wasm_bindgen]
195extern "C" {
196    #[derive(Clone, Debug, Eq, PartialEq)]
197    #[wasm_bindgen(extends = Object)]
198    pub type LoggerParams;
199
200    #[wasm_bindgen(method, indexing_getter)]
201    fn get(this: &LoggerParams, key: &JsString) -> JsString;
202
203    #[wasm_bindgen(method, indexing_setter)]
204    fn set(this: &LoggerParams, val: &JsString);
205
206    #[wasm_bindgen(method, indexing_deleter)]
207    fn delete(this: &LoggerParams, val: &JsString);
208}
209
210#[wasm_bindgen]
211extern "C" {
212    #[derive(Clone, Debug, PartialEq)]
213    pub type Language;
214
215    // Static Methods
216
217    #[wasm_bindgen(static_method_of = Language, js_name = load)]
218    fn __load_bytes(bytes: &Uint8Array) -> Promise;
219
220    #[wasm_bindgen(static_method_of = Language, js_name = load)]
221    fn __load_path(path: &str) -> Promise;
222
223    // Instance Properties
224
225    #[wasm_bindgen(method, getter)]
226    pub fn version(this: &Language) -> u32;
227
228    #[wasm_bindgen(method, getter, js_name = fieldCount)]
229    pub fn field_count(this: &Language) -> u16;
230
231    #[wasm_bindgen(method, getter, js_name = nodeTypeCount)]
232    pub fn node_kind_count(this: &Language) -> u16;
233
234    // Instance Methods
235
236    #[wasm_bindgen(method, js_name = fieldNameForId)]
237    pub fn field_name_for_id(this: &Language, field_id: u16) -> Option<String>;
238
239    #[wasm_bindgen(method, js_name = fieldIdForName)]
240    pub fn field_id_for_name(this: &Language, field_name: &str) -> Option<u16>;
241
242    #[wasm_bindgen(method, js_name = idForNodeType)]
243    pub fn id_for_node_kind(this: &Language, kind: &str, named: bool) -> u16;
244
245    #[wasm_bindgen(method, js_name = nodeTypeForId)]
246    pub fn node_kind_for_id(this: &Language, kind_id: u16) -> Option<String>;
247
248    #[wasm_bindgen(method, js_name = nodeTypeIsNamed)]
249    pub fn node_kind_is_named(this: &Language, kind_id: u16) -> bool;
250
251    #[wasm_bindgen(method, js_name = nodeTypeIsVisible)]
252    pub fn node_kind_is_visible(this: &Language, kind_id: u16) -> bool;
253
254    #[wasm_bindgen(catch, method)]
255    pub fn query(this: &Language, source: &JsString) -> Result<Query, QueryError>;
256}
257
258impl Language {
259    pub async fn load_bytes(bytes: &Uint8Array) -> Result<Language, LanguageError> {
260        TreeSitter::init_guard();
261        JsFuture::from(Language::__load_bytes(bytes))
262            .await
263            .map(JsCast::unchecked_into)
264            .map_err(JsCast::unchecked_into)
265    }
266
267    pub async fn load_path(path: &str) -> Result<Language, LanguageError> {
268        TreeSitter::init_guard();
269        JsFuture::from(Language::__load_path(path))
270            .await
271            .map(JsCast::unchecked_into)
272            .map_err(JsCast::unchecked_into)
273    }
274}
275
276#[wasm_bindgen]
277extern "C" {
278    #[derive(Clone, Debug, Eq, PartialEq)]
279    #[wasm_bindgen(extends = Error)]
280    pub type LanguageError;
281}
282
283#[wasm_bindgen]
284extern "C" {
285    #[derive(Clone, Debug, Eq, PartialEq)]
286    #[wasm_bindgen(extends = Object)]
287    pub type ParseOptions;
288
289    // Instance Properties
290
291    // -> Range[]
292    #[wasm_bindgen(method, getter, js_name = includedRanges)]
293    pub fn included_ranges(this: &ParseOptions) -> Option<Array>;
294}
295
296impl ParseOptions {
297    pub fn new(included_ranges: Option<&Array>) -> Self {
298        let obj = Object::new();
299        Reflect::set(&obj, &"includedRanges".into(), &included_ranges.into()).unwrap();
300        JsCast::unchecked_into(obj)
301    }
302}
303
304impl Default for ParseOptions {
305    fn default() -> Self {
306        let included_ranges = Default::default();
307        Self::new(included_ranges)
308    }
309}
310
311#[wasm_bindgen]
312extern "C" {
313    #[derive(Clone, Debug)]
314    #[wasm_bindgen(extends = Object)]
315    pub type Point;
316
317    // Instance Properties
318
319    #[wasm_bindgen(method, getter)]
320    pub fn column(this: &Point) -> u32;
321
322    #[wasm_bindgen(method, getter)]
323    pub fn row(this: &Point) -> u32;
324}
325
326impl Point {
327    pub fn new(row: u32, column: u32) -> Self {
328        let obj = Object::new();
329        Reflect::set(&obj, &"row".into(), &row.into()).unwrap();
330        Reflect::set(&obj, &"column".into(), &column.into()).unwrap();
331        JsCast::unchecked_into(obj)
332    }
333
334    #[inline(always)]
335    fn spread(&self) -> (u32, u32) {
336        (self.row(), self.column())
337    }
338}
339
340impl Default for Point {
341    fn default() -> Self {
342        let row = Default::default();
343        let column = Default::default();
344        Self::new(row, column)
345    }
346}
347
348impl Eq for Point {}
349
350impl std::hash::Hash for Point {
351    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
352        let this = self.spread();
353        this.hash(state);
354    }
355}
356
357impl Ord for Point {
358    fn cmp(&self, that: &Self) -> std::cmp::Ordering {
359        let this = self.spread();
360        let that = that.spread();
361        this.cmp(&that)
362    }
363}
364
365impl PartialEq for Point {
366    fn eq(&self, that: &Self) -> bool {
367        let this = self.spread();
368        let that = that.spread();
369        this.eq(&that)
370    }
371}
372
373impl PartialOrd for Point {
374    fn partial_cmp(&self, that: &Point) -> Option<std::cmp::Ordering> {
375        Some(self.cmp(that))
376    }
377}
378
379#[wasm_bindgen]
380extern "C" {
381    #[derive(Clone, Debug, Eq, PartialEq)]
382    #[wasm_bindgen(extends = Object)]
383    pub type PredicateOperand;
384
385    // Instance Properties
386
387    #[wasm_bindgen(method, getter)]
388    pub fn name(this: &PredicateOperand) -> JsString;
389
390    #[wasm_bindgen(method, getter)]
391    pub fn type_(this: &PredicateOperand) -> JsString;
392}
393
394impl PredicateOperand {
395    pub fn new(type_: &JsString, name: &JsString) -> Self {
396        let obj = Object::new();
397        Reflect::set(&obj, &"type".into(), &type_.into()).unwrap();
398        Reflect::set(&obj, &"name".into(), &name.into()).unwrap();
399        JsCast::unchecked_into(obj)
400    }
401}
402
403impl Default for PredicateOperand {
404    fn default() -> Self {
405        let name = &<String as Default>::default().into();
406        let type_ = &<String as Default>::default().into();
407        Self::new(name, type_)
408    }
409}
410
411#[wasm_bindgen]
412extern "C" {
413    #[derive(Clone, Debug, Eq, PartialEq)]
414    #[wasm_bindgen(extends = Object)]
415    pub type PredicateResult;
416
417    // Instance Properties
418
419    #[wasm_bindgen(method, getter)]
420    pub fn operator(this: &PredicateResult) -> JsString;
421
422    // -> PredicateOperand[]
423    #[wasm_bindgen(method, getter)]
424    pub fn operands(this: &PredicateResult) -> Box<[JsValue]>;
425}
426
427impl PredicateResult {
428    pub fn new(operator: &JsString, operands: &Array) -> Self {
429        let obj = Object::new();
430        Reflect::set(&obj, &"operator".into(), &operator.into()).unwrap();
431        Reflect::set(&obj, &"operands".into(), &operands.into()).unwrap();
432        JsCast::unchecked_into(obj)
433    }
434}
435
436impl Default for PredicateResult {
437    fn default() -> Self {
438        let operator = &<String as Default>::default().into();
439        let operands = &<Vec<JsValue> as Default>::default().into_iter().collect();
440        Self::new(operator, operands)
441    }
442}
443
444#[wasm_bindgen]
445extern "C" {
446    #[derive(Clone, Debug)]
447    pub type Query;
448
449    // Instance Properties
450
451    // -> JsString[]
452    #[wasm_bindgen(method, getter, js_name = captureNames)]
453    pub fn capture_names(this: &Query) -> Box<[JsValue]>;
454
455    // Instance Methods
456
457    #[wasm_bindgen(method)]
458    pub fn delete(this: &Query);
459
460    // -> QueryMatch[]
461    #[wasm_bindgen(method)]
462    pub fn matches(
463        this: &Query,
464        node: &SyntaxNode,
465        start_position: Option<&Point>,
466        end_position: Option<&Point>,
467    ) -> Box<[JsValue]>;
468
469    // -> QueryCapture[]
470    #[wasm_bindgen(method)]
471    pub fn captures(
472        this: &Query,
473        node: &SyntaxNode,
474        start_position: Option<&Point>,
475        end_position: Option<&Point>,
476    ) -> Box<[JsValue]>;
477
478    // -> PredicateResult[]
479    #[wasm_bindgen(method, js_name = predicatesForPattern)]
480    pub fn predicates_for_pattern(this: &Query, pattern_index: usize) -> Box<[JsValue]>;
481}
482
483#[wasm_bindgen]
484extern "C" {
485    #[derive(Clone, Debug, Eq, PartialEq)]
486    #[wasm_bindgen(extends = Object)]
487    pub type QueryCapture;
488
489    // Instance Properties
490
491    #[wasm_bindgen(method, getter)]
492    pub fn name(this: &QueryCapture) -> JsString;
493
494    #[wasm_bindgen(method, getter)]
495    pub fn node(this: &QueryCapture) -> SyntaxNode;
496}
497
498impl QueryCapture {
499    pub fn new(name: &JsString, node: &SyntaxNode) -> Self {
500        let obj = Object::new();
501        Reflect::set(&obj, &"name".into(), &name.into()).unwrap();
502        Reflect::set(&obj, &"node".into(), &node.into()).unwrap();
503        JsCast::unchecked_into(obj)
504    }
505}
506
507#[wasm_bindgen]
508extern "C" {
509    #[derive(Clone, Debug, Eq, PartialEq)]
510    #[wasm_bindgen(extends = Error)]
511    pub type QueryError;
512}
513
514#[wasm_bindgen]
515extern "C" {
516    #[derive(Clone, Debug, Eq, PartialEq)]
517    #[wasm_bindgen(extends = Object)]
518    pub type QueryMatch;
519
520    // Instance Properties
521
522    #[wasm_bindgen(method, getter)]
523    pub fn pattern(this: &QueryMatch) -> u32;
524
525    // -> QueryCapture[]
526    #[wasm_bindgen(method, getter)]
527    pub fn captures(this: &QueryMatch) -> Box<[JsValue]>;
528}
529
530impl QueryMatch {
531    pub fn new(pattern: u32, captures: &Array) -> Self {
532        let obj = Object::new();
533        Reflect::set(&obj, &"pattern".into(), &pattern.into()).unwrap();
534        Reflect::set(&obj, &"captures".into(), &captures.into()).unwrap();
535        JsCast::unchecked_into(obj)
536    }
537}
538
539#[wasm_bindgen]
540extern "C" {
541    #[derive(Clone, Debug, Eq, PartialEq)]
542    #[wasm_bindgen(extends = Object)]
543    pub type QueryPredicate;
544
545    // Instance Properties
546
547    #[wasm_bindgen(method, getter)]
548    pub fn operator(this: &QueryPredicate) -> JsString;
549
550    // -> QueryPredicateArg[]
551    #[wasm_bindgen(method, getter)]
552    pub fn operands(this: &QueryPredicate) -> Box<[JsValue]>;
553}
554
555#[wasm_bindgen]
556extern "C" {
557    #[derive(Clone, Debug, Eq, PartialEq)]
558    #[wasm_bindgen(extends = Object)]
559    pub type QueryPredicateArg;
560
561    // Instance Properties
562
563    #[wasm_bindgen(method, getter)]
564    pub fn value(this: &QueryPredicateArg) -> JsString;
565}
566
567#[wasm_bindgen]
568extern "C" {
569    #[derive(Clone, Debug)]
570    #[wasm_bindgen(extends = Object)]
571    pub type Range;
572
573    // Instance Properties
574
575    #[wasm_bindgen(method, getter, js_name = endIndex)]
576    pub fn end_index(this: &Range) -> u32;
577
578    #[wasm_bindgen(method, getter, js_name = endPosition)]
579    pub fn end_position(this: &Range) -> Point;
580
581    #[wasm_bindgen(method, getter, js_name = startIndex)]
582    pub fn start_index(this: &Range) -> u32;
583
584    #[wasm_bindgen(method, getter, js_name = startPosition)]
585    pub fn start_position(this: &Range) -> Point;
586}
587
588impl Range {
589    pub fn new(
590        start_position: &Point,
591        end_position: &Point,
592        start_index: u32,
593        end_index: u32,
594    ) -> Self {
595        let obj = Object::new();
596        Reflect::set(&obj, &"startPosition".into(), &start_position.into()).unwrap();
597        Reflect::set(&obj, &"endPosition".into(), &end_position.into()).unwrap();
598        Reflect::set(&obj, &"startIndex".into(), &start_index.into()).unwrap();
599        Reflect::set(&obj, &"endIndex".into(), &end_index.into()).unwrap();
600        JsCast::unchecked_into(obj)
601    }
602
603    #[inline(always)]
604    fn spread(&self) -> (u32, u32, Point, Point) {
605        (
606            self.start_index(),
607            self.end_index(),
608            self.start_position(),
609            self.end_position(),
610        )
611    }
612}
613
614impl Default for Range {
615    fn default() -> Range {
616        let start_position = Default::default();
617        let end_position = Default::default();
618        let start_index = Default::default();
619        let end_index = Default::default();
620        Self::new(&start_position, &end_position, start_index, end_index)
621    }
622}
623
624impl Eq for Range {}
625
626impl std::hash::Hash for Range {
627    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
628        let this = self.spread();
629        this.hash(state)
630    }
631}
632
633impl Ord for Range {
634    fn cmp(&self, that: &Self) -> std::cmp::Ordering {
635        let this = self.spread();
636        let that = that.spread();
637        this.cmp(&that)
638    }
639}
640
641impl PartialEq<Range> for Range {
642    fn eq(&self, that: &Self) -> bool {
643        let this = self.spread();
644        let that = that.spread();
645        this.eq(&that)
646    }
647}
648
649impl PartialOrd<Range> for Range {
650    fn partial_cmp(&self, that: &Self) -> Option<std::cmp::Ordering> {
651        Some(self.cmp(that))
652    }
653}
654
655#[wasm_bindgen]
656extern "C" {
657    #[derive(Clone, Debug)]
658    pub type SyntaxNode;
659
660    // Instance Properties
661
662    #[wasm_bindgen(method, getter, js_name = childCount)]
663    pub fn child_count(this: &SyntaxNode) -> u32;
664
665    #[wasm_bindgen(method, getter)]
666    pub fn children(this: &SyntaxNode) -> Box<[JsValue]>;
667
668    #[wasm_bindgen(method, getter, js_name = endIndex)]
669    pub fn end_index(this: &SyntaxNode) -> u32;
670
671    #[wasm_bindgen(method, getter, js_name = endPosition)]
672    pub fn end_position(this: &SyntaxNode) -> Point;
673
674    #[wasm_bindgen(method, getter, js_name = firstChild)]
675    pub fn first_child(this: &SyntaxNode) -> Option<SyntaxNode>;
676
677    #[wasm_bindgen(method, getter, js_name = firstNamedChild)]
678    pub fn first_named_child(this: &SyntaxNode) -> Option<SyntaxNode>;
679
680    #[wasm_bindgen(method, getter)]
681    pub fn id(this: &SyntaxNode) -> u32;
682
683    #[wasm_bindgen(method, getter, js_name = lastChild)]
684    pub fn last_child(this: &SyntaxNode) -> Option<SyntaxNode>;
685
686    #[wasm_bindgen(method, getter, js_name = lastNamedChild)]
687    pub fn last_named_child(this: &SyntaxNode) -> Option<SyntaxNode>;
688
689    #[wasm_bindgen(method, getter, js_name = namedChildCount)]
690    pub fn named_child_count(this: &SyntaxNode) -> u32;
691
692    #[wasm_bindgen(method, getter, js_name = namedChildren)]
693    pub fn named_children(this: &SyntaxNode) -> Box<[JsValue]>;
694
695    #[wasm_bindgen(method, getter, js_name = nextNamedSibling)]
696    pub fn next_named_sibling(this: &SyntaxNode) -> Option<SyntaxNode>;
697
698    #[wasm_bindgen(method, getter, js_name = nextSibling)]
699    pub fn next_sibling(this: &SyntaxNode) -> Option<SyntaxNode>;
700
701    #[wasm_bindgen(method, getter)]
702    pub fn parent(this: &SyntaxNode) -> Option<SyntaxNode>;
703
704    #[wasm_bindgen(method, getter, js_name = previousNamedSibling)]
705    pub fn previous_named_sibling(this: &SyntaxNode) -> Option<SyntaxNode>;
706
707    #[wasm_bindgen(method, getter, js_name = previousSibling)]
708    pub fn previous_sibling(this: &SyntaxNode) -> Option<SyntaxNode>;
709
710    #[wasm_bindgen(method, getter, js_name = startIndex)]
711    pub fn start_index(this: &SyntaxNode) -> u32;
712
713    #[wasm_bindgen(method, getter, js_name = startPosition)]
714    pub fn start_position(this: &SyntaxNode) -> Point;
715
716    #[wasm_bindgen(method, getter)]
717    pub fn text(this: &SyntaxNode) -> JsString;
718
719    #[wasm_bindgen(method, getter)]
720    pub fn tree(this: &SyntaxNode) -> Tree;
721
722    #[wasm_bindgen(method, getter, js_name = type)]
723    pub fn type_(this: &SyntaxNode) -> JsString;
724
725    #[wasm_bindgen(method, getter, js_name = typeId)]
726    pub fn type_id(this: &SyntaxNode) -> u16;
727
728    // Instance Methods
729
730    #[wasm_bindgen(method)]
731    pub fn child(this: &SyntaxNode, index: u32) -> Option<SyntaxNode>;
732
733    #[wasm_bindgen(method, js_name = childForFieldId)]
734    pub fn child_for_field_id(this: &SyntaxNode, field_id: u16) -> Option<SyntaxNode>;
735
736    #[wasm_bindgen(method, js_name = childForFieldName)]
737    pub fn child_for_field_name(this: &SyntaxNode, field_name: &str) -> Option<SyntaxNode>;
738
739    #[wasm_bindgen(method, js_name = descendantForIndex)]
740    pub fn descendant_for_index(this: &SyntaxNode, index: u32) -> Option<SyntaxNode>;
741
742    #[wasm_bindgen(method, js_name = descendantForIndex)]
743    pub fn descendant_for_index_range(
744        this: &SyntaxNode,
745        start_index: u32,
746        end_index: u32,
747    ) -> Option<SyntaxNode>;
748
749    #[wasm_bindgen(method, js_name = descendantForPosition)]
750    pub fn descendant_for_position(this: &SyntaxNode, position: &Point) -> Option<SyntaxNode>;
751
752    #[wasm_bindgen(method, js_name = descendantForPosition)]
753    pub fn descendant_for_position_range(
754        this: &SyntaxNode,
755        start_position: &Point,
756        end_position: &Point,
757    ) -> Option<SyntaxNode>;
758
759    #[wasm_bindgen(method, js_name = descendantsOfType)]
760    pub fn descendants_of_type_array(
761        this: &SyntaxNode,
762        type_: Box<[JsValue]>,
763        start_position: Option<&Point>,
764        end_position: Option<&Point>,
765    ) -> Box<[JsValue]>;
766
767    // -> SyntaxNode[]
768    #[wasm_bindgen(method, js_name = descendantsOfType)]
769    pub fn descendants_of_type_string(
770        this: &SyntaxNode,
771        type_: &str,
772        start_position: Option<&Point>,
773        end_position: Option<&Point>,
774    ) -> Box<[JsValue]>;
775
776    #[wasm_bindgen(method)]
777    pub fn equals(this: &SyntaxNode, other: &SyntaxNode) -> bool;
778
779    #[wasm_bindgen(method, js_name = hasChanges)]
780    pub fn has_changes(this: &SyntaxNode) -> bool;
781
782    #[wasm_bindgen(method, js_name = hasError)]
783    pub fn has_error(this: &SyntaxNode) -> bool;
784
785    #[wasm_bindgen(method, js_name = isMissing)]
786    pub fn is_missing(this: &SyntaxNode) -> bool;
787
788    #[wasm_bindgen(method, js_name = isNamed)]
789    pub fn is_named(this: &SyntaxNode) -> bool;
790
791    #[wasm_bindgen(method, js_name = namedChild)]
792    pub fn named_child(this: &SyntaxNode, index: u32) -> Option<SyntaxNode>;
793
794    #[wasm_bindgen(method, js_name = namedDescendantForIndex)]
795    pub fn named_descendant_for_index(this: &SyntaxNode, index: u32) -> Option<SyntaxNode>;
796
797    #[wasm_bindgen(method, js_name = namedDescendantForIndex)]
798    pub fn named_descendant_for_index_range(
799        this: &SyntaxNode,
800        start_index: u32,
801        end_index: u32,
802    ) -> Option<SyntaxNode>;
803
804    #[wasm_bindgen(method, js_name = namedDescendantForPosition)]
805    pub fn named_descendant_for_position(this: &SyntaxNode, position: &Point)
806    -> Option<SyntaxNode>;
807
808    #[wasm_bindgen(method, js_name = namedDescendantForPosition)]
809    pub fn named_descendant_for_position_range(
810        this: &SyntaxNode,
811        start_position: &Point,
812        end_position: &Point,
813    ) -> Option<SyntaxNode>;
814
815    #[wasm_bindgen(method, js_name = toString)]
816    pub fn to_string(this: &SyntaxNode) -> JsString;
817
818    #[wasm_bindgen(method)]
819    pub fn walk(this: &SyntaxNode) -> TreeCursor;
820}
821
822impl PartialEq<SyntaxNode> for SyntaxNode {
823    fn eq(&self, other: &SyntaxNode) -> bool {
824        self.equals(other)
825    }
826}
827
828impl Eq for SyntaxNode {}
829
830impl std::hash::Hash for SyntaxNode {
831    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
832        self.id().hash(state);
833    }
834}
835
836#[wasm_bindgen]
837extern "C" {
838    #[derive(Debug)]
839    pub type Tree;
840
841    // Instance Properties
842
843    #[wasm_bindgen(method, getter, js_name = rootNode)]
844    pub fn root_node(this: &Tree) -> SyntaxNode;
845
846    // Instance Methods
847
848    #[wasm_bindgen(method)]
849    pub fn copy(this: &Tree) -> Tree;
850
851    #[wasm_bindgen(method)]
852    pub fn delete(this: &Tree);
853
854    #[wasm_bindgen(method)]
855    pub fn edit(this: &Tree, delta: &Edit) -> Tree;
856
857    #[wasm_bindgen(method)]
858    pub fn walk(this: &Tree) -> TreeCursor;
859
860    // -> Range[]
861    #[wasm_bindgen(method, js_name = getChangedRanges)]
862    pub fn get_changed_ranges(this: &Tree, other: &Tree) -> Box<[JsValue]>;
863
864    #[wasm_bindgen(method, js_name = getLanguage)]
865    pub fn get_language(this: &Tree) -> Language;
866}
867
868impl Clone for Tree {
869    fn clone(&self) -> Tree {
870        self.copy()
871    }
872}
873
874#[wasm_bindgen]
875extern "C" {
876    #[derive(Clone, Debug)]
877    pub type TreeCursor;
878
879    // Instance Properties
880
881    #[wasm_bindgen(method, getter, js_name = endIndex)]
882    pub fn end_index(this: &TreeCursor) -> u32;
883
884    #[wasm_bindgen(method, getter, js_name = endPosition)]
885    pub fn end_position(this: &TreeCursor) -> Point;
886
887    #[wasm_bindgen(method, getter, js_name = nodeIsNamed)]
888    pub fn node_is_named(this: &TreeCursor) -> bool;
889
890    #[wasm_bindgen(method, getter, js_name = nodeText)]
891    pub fn node_text(this: &TreeCursor) -> JsString;
892
893    #[wasm_bindgen(method, getter, js_name = nodeType)]
894    pub fn node_type(this: &TreeCursor) -> JsString;
895
896    #[wasm_bindgen(method, getter, js_name = startIndex)]
897    pub fn start_index(this: &TreeCursor) -> u32;
898
899    #[wasm_bindgen(method, getter, js_name = startPosition)]
900    pub fn start_position(this: &TreeCursor) -> Point;
901
902    // Instance Methods
903
904    #[wasm_bindgen(method, js_name = currentFieldId)]
905    pub fn current_field_id(this: &TreeCursor) -> Option<u16>;
906
907    #[wasm_bindgen(method, js_name = currentFieldName)]
908    pub fn current_field_name(this: &TreeCursor) -> Option<JsString>;
909
910    #[wasm_bindgen(method, js_name = currentNode)]
911    pub fn current_node(this: &TreeCursor) -> SyntaxNode;
912
913    #[wasm_bindgen(method)]
914    pub fn delete(this: &TreeCursor);
915
916    #[wasm_bindgen(method, js_name = gotoFirstChild)]
917    pub fn goto_first_child(this: &TreeCursor) -> bool;
918
919    #[wasm_bindgen(method, js_name = gotoNextSibling)]
920    pub fn goto_next_sibling(this: &TreeCursor) -> bool;
921
922    #[wasm_bindgen(method, js_name = gotoParent)]
923    pub fn goto_parent(this: &TreeCursor) -> bool;
924
925    #[wasm_bindgen(method)]
926    pub fn reset(this: &TreeCursor, node: &SyntaxNode);
927}
928
929#[wasm_bindgen]
930extern "C" {
931    #[derive(Clone, Debug)]
932    pub type Parser;
933
934    // Static Methods
935
936    // Constructor
937
938    #[wasm_bindgen(catch, constructor)]
939    fn __new() -> Result<Parser, ParserError>;
940
941    // Instance Methods
942
943    #[wasm_bindgen(method)]
944    pub fn delete(this: &Parser);
945
946    #[wasm_bindgen(method, js_name = getLanguage)]
947    pub fn get_language(this: &Parser) -> Option<Language>;
948
949    #[wasm_bindgen(method, js_name = getLogger)]
950    pub fn get_logger(this: &Parser) -> Option<Logger>;
951
952    #[wasm_bindgen(method, js_name = getTimeoutMicros)]
953    pub fn get_timeout_micros(this: &Parser) -> f64;
954
955    #[wasm_bindgen(catch, method, js_name = parse)]
956    pub fn parse_with_function(
957        this: &Parser,
958        input: &Input,
959        previous_tree: Option<&Tree>,
960        options: Option<&ParseOptions>,
961    ) -> Result<Option<Tree>, ParserError>;
962
963    #[wasm_bindgen(catch, method, js_name = parse)]
964    pub fn parse_with_string(
965        this: &Parser,
966        input: &JsString,
967        previous_tree: Option<&Tree>,
968        options: Option<&ParseOptions>,
969    ) -> Result<Option<Tree>, ParserError>;
970
971    #[wasm_bindgen(method)]
972    pub fn reset(this: &Parser);
973
974    #[wasm_bindgen(catch, method, js_name = setLanguage)]
975    pub fn set_language(this: &Parser, language: Option<&Language>) -> Result<(), LanguageError>;
976
977    #[wasm_bindgen(method, js_name = setLogger)]
978    pub fn set_logger(this: &Parser, logger: Option<&Logger>);
979
980    #[wasm_bindgen(method, js_name = setTimeoutMicros)]
981    pub fn set_timeout_micros(this: &Parser, timeout_micros: f64);
982}
983
984impl Parser {
985    pub fn new() -> Result<Parser, ParserError> {
986        TreeSitter::init_guard();
987        let result = Parser::__new()?;
988        Ok(result)
989    }
990}
991
992#[wasm_bindgen]
993extern "C" {
994    #[derive(Clone, Debug, Eq, PartialEq)]
995    #[wasm_bindgen(extends = Error)]
996    pub type ParserError;
997}