tree-sitter-sg 0.27.0

Rust bindings to the Tree-sitter parsing library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
//! GLR parser state, lifecycle, and top-level parse loop.
//!
//! [`TSParser`] owns the mutable objects used during a parse: the lexer, the
//! graph-structured [`Stack`], subtree allocation pools, external-scanner
//! state, and the best accepted tree. [`ts_parser_parse`] is the outer driver.
//! It advances every active stack version, condenses compatible or inferior
//! versions, invokes recovery when all useful versions are paused, and finally
//! balances and returns the accepted tree.
//!
//! The action-level work is divided by purpose:
//!
//! - `lexing` obtains or reuses a lookahead token and manages external scanners;
//! - `advance` interprets parse-table entries and manages versions;
//! - `actions` executes shift, reduce, and accept stack/tree mutations;
//! - `recovery` searches for a useful continuation after invalid input;
//! - `balancing` prepares the accepted subtree for long-lived navigation; and
//! - `logging` renders parser, stack, and tree diagnostics.
//!
//! Generated languages and the public API enter through C-compatible types,
//! but parser-owned state is internal and uses Rust layout.

use core::ffi::{c_char, c_void};
use core::fmt::Write;
use core::ptr;

use crate::ffi::{
    TSInput, TSInputEncoding, TSInputEncodingUTF8, TSLanguage, TSLogger, TSParseOptions,
    TSParseState, TSPoint, TSRange, TSStateId, TSSymbol, TREE_SITTER_LANGUAGE_VERSION,
    TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION,
};

use super::alloc::{free, malloc};
use super::error_costs::ERROR_COST_PER_SKIPPED_TREE;
use super::language::{
    language_full, language_table_entry, language_table_entry_from_index, TableEntry,
};
use super::length::length_zero;
use super::lexer::{
    lexer_delete, lexer_included_ranges, lexer_included_ranges_slice, lexer_new, lexer_reset,
    lexer_set_included_ranges, lexer_set_input, Lexer,
};
use super::reduce_action::ReduceActionSet;
use super::stack::{stack_clear, stack_delete, stack_new, Stack, StackVersion};
use super::subtree::{
    subtree_array_prepare_scratch, subtree_pool_delete, subtree_pool_new,
    subtree_pool_prepare_for_parse, subtree_pool_retain_arena, subtree_publish, Subtree,
    SubtreeArray, SubtreePool, NULL_SUBTREE,
};
use super::tree::TSTree;
use super::utils::Array;
use super::utils::{ptr_mut, ptr_ref};

// ---------------------------------------------------------------------------
// Extern C functions
// ---------------------------------------------------------------------------

extern "C" {
    // libc
    fn fprintf(f: *mut c_void, fmt: *const i8, ...) -> i32;
    fn fputs(s: *const i8, f: *mut c_void) -> i32;
    fn fputc(c: i32, f: *mut c_void) -> i32;
    // `fdopen` is spelled `_fdopen` on Windows (declared at the call site);
    // `fclose` keeps its name on all platforms.
    #[cfg(not(target_os = "windows"))]
    fn fdopen(fd: i32, mode: *const i8) -> *mut c_void;
    fn fclose(f: *mut c_void) -> i32;
}

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

const MAX_VERSION_COUNT: u32 = 6;
const MAX_VERSION_COUNT_OVERFLOW: u32 = 4;
const MAX_SUMMARY_DEPTH: u32 = 16;
const MAX_COST_DIFFERENCE: u32 = 18 * ERROR_COST_PER_SKIPPED_TREE;
const OP_COUNT_PER_PARSER_CALLBACK_CHECK: u32 = 100;

// ---------------------------------------------------------------------------
// Types
// ---------------------------------------------------------------------------

/// One-token cache shared by stack versions at the same byte offset.
///
/// GLR versions often ask the lexer for the same position and external scanner
/// state. The cache stores the concrete token plus the last external token that
/// determined scanner state, so another version can reuse it only when scanner
/// state is equivalent.
struct TokenCache {
    /// Retained lookahead token.
    token: Subtree,
    /// Retained token carrying the external scanner state used for `token`.
    last_external_token: Subtree,
    /// Byte offset where `token` was lexed.
    byte_index: u32,
}

#[derive(Clone, Copy)]
struct GotoRow {
    start: u32,
    length: u32,
}

#[derive(Clone, Copy)]
struct GotoEntry {
    symbol: TSSymbol,
    state: TSStateId,
}

#[derive(Clone, Copy)]
struct ActionEntry {
    symbol: TSSymbol,
    action_index: u16,
}

/// Parser-owned projection of terminal actions in compressed small states.
///
/// The generated representation groups symbols that share an action index.
/// That is compact, but every lookup scans group headers and symbols. This
/// projection expands each real terminal mapping once when a language is
/// installed, then searches only the selected state's sorted terminal row.
struct ActionTable {
    rows: Array<GotoRow>,
    entries: Array<ActionEntry>,
    large_state_count: u32,
    token_count: u32,
}

impl ActionTable {
    const fn new() -> Self {
        Self {
            rows: Array::new(),
            entries: Array::new(),
            large_state_count: 0,
            token_count: 0,
        }
    }

    unsafe fn clear(&mut self) {
        self.rows.clear();
        self.entries.clear();
        self.large_state_count = 0;
        self.token_count = 0;
    }

    unsafe fn delete(&mut self) {
        if !self.rows.contents.is_null() {
            self.rows.delete();
        }
        if !self.entries.contents.is_null() {
            self.entries.delete();
        }
    }

    unsafe fn rebuild(&mut self, language: *const TSLanguage) {
        self.clear();
        if language.is_null() {
            return;
        }

        let language = language_full(language);
        self.large_state_count = language.large_state_count;
        self.token_count = language.token_count;
        self.rows
            .reserve(language.state_count - language.large_state_count);

        for state in language.large_state_count..language.state_count {
            let table_index = *language
                .small_parse_table_map
                .add((state - language.large_state_count) as usize);
            self.append_small_row(language.small_parse_table.add(table_index as usize));
        }
    }

    unsafe fn append_small_row(&mut self, mut data: *const u16) {
        let row_start = self.entries.size;
        let group_count = u32::from(*data);
        data = data.add(1);

        for _ in 0..group_count {
            let action_index = *data;
            let group_symbol_count = u32::from(*data.add(1));
            data = data.add(2);
            for _ in 0..group_symbol_count {
                let symbol = *data;
                if u32::from(symbol) < self.token_count {
                    self.entries.push(ActionEntry {
                        symbol,
                        action_index,
                    });
                }
                data = data.add(1);
            }
        }

        let row_length = self.entries.size - row_start;
        let row =
            &mut self.entries.as_mut_slice()[row_start as usize..(row_start + row_length) as usize];
        for i in 1..row.len() {
            let entry = row[i];
            let mut j = i;
            while j > 0 && row[j - 1].symbol > entry.symbol {
                row[j] = row[j - 1];
                j -= 1;
            }
            row[j] = entry;
        }
        self.rows.push(GotoRow {
            start: row_start,
            length: row_length,
        });
    }

    #[inline]
    unsafe fn lookup(&self, state: TSStateId, symbol: TSSymbol) -> Option<u16> {
        if u32::from(state) < self.large_state_count || u32::from(symbol) >= self.token_count {
            return None;
        }
        let row_index = u32::from(state) - self.large_state_count;
        let row = self.rows.get_unchecked(row_index);
        let entries =
            &self.entries.as_slice()[row.start as usize..(row.start + row.length) as usize];

        if entries.len() <= 8 {
            return Some(
                entries
                    .iter()
                    .find(|entry| entry.symbol == symbol)
                    .map_or(0, |entry| entry.action_index),
            );
        }

        Some(
            entries
                .binary_search_by_key(&symbol, |entry| entry.symbol)
                .map_or(0, |index| entries[index].action_index),
        )
    }
}

/// Parser-owned projection of compressed small-state nonterminal transitions.
///
/// Generated small parse tables interleave terminal actions and nonterminal
/// gotos. Reductions need only the latter, so this index is built once when a
/// language is installed and avoids rescanning unrelated groups during every
/// reduction. Entries remain sparse: one four-byte pair per actual goto.
struct GotoTable {
    rows: Array<GotoRow>,
    entries: Array<GotoEntry>,
    large_state_count: u32,
}

impl GotoTable {
    const fn new() -> Self {
        Self {
            rows: Array::new(),
            entries: Array::new(),
            large_state_count: 0,
        }
    }

    unsafe fn clear(&mut self) {
        self.rows.clear();
        self.entries.clear();
        self.large_state_count = 0;
    }

    unsafe fn delete(&mut self) {
        if !self.rows.contents.is_null() {
            self.rows.delete();
        }
        if !self.entries.contents.is_null() {
            self.entries.delete();
        }
    }

    unsafe fn rebuild(&mut self, language: *const TSLanguage) {
        self.clear();
        if language.is_null() {
            return;
        }

        let language = language_full(language);
        self.large_state_count = language.large_state_count;
        self.rows
            .reserve(language.state_count - language.large_state_count);

        for state in language.large_state_count..language.state_count {
            let table_index = *language
                .small_parse_table_map
                .add((state - language.large_state_count) as usize);
            self.append_small_row(
                language.small_parse_table.add(table_index as usize),
                language.token_count,
                language.symbol_count,
            );
        }
    }

    unsafe fn append_small_row(
        &mut self,
        mut data: *const u16,
        token_count: u32,
        symbol_count: u32,
    ) {
        let row_start = self.entries.size;
        let group_count = u32::from(*data);
        data = data.add(1);

        for _ in 0..group_count {
            let section_value = *data;
            let group_symbol_count = u32::from(*data.add(1));
            data = data.add(2);
            for _ in 0..group_symbol_count {
                let symbol = *data;
                if u32::from(symbol) >= token_count && u32::from(symbol) < symbol_count {
                    self.entries.push(GotoEntry {
                        symbol,
                        state: section_value,
                    });
                }
                data = data.add(1);
            }
        }

        let row_length = self.entries.size - row_start;
        let row =
            &mut self.entries.as_mut_slice()[row_start as usize..(row_start + row_length) as usize];
        for i in 1..row.len() {
            let entry = row[i];
            let mut j = i;
            while j > 0 && row[j - 1].symbol > entry.symbol {
                row[j] = row[j - 1];
                j -= 1;
            }
            row[j] = entry;
        }
        self.rows.push(GotoRow {
            start: row_start,
            length: row_length,
        });
    }

    #[inline]
    unsafe fn lookup(&self, state: TSStateId, symbol: TSSymbol) -> Option<TSStateId> {
        if u32::from(state) < self.large_state_count {
            return None;
        }
        let row_index = u32::from(state) - self.large_state_count;
        let row = self.rows.get_unchecked(row_index);
        let entries =
            &self.entries.as_slice()[row.start as usize..(row.start + row.length) as usize];

        if entries.len() <= 8 {
            return Some(
                entries
                    .iter()
                    .find(|entry| entry.symbol == symbol)
                    .map_or(0, |entry| entry.state),
            );
        }

        Some(
            entries
                .binary_search_by_key(&symbol, |entry| entry.symbol)
                .map_or(0, |index| entries[index].state),
        )
    }
}

#[cfg(test)]
mod goto_table_tests {
    use super::{ActionTable, GotoTable};

    #[test]
    fn projects_and_sorts_terminal_action_entries() {
        unsafe {
            let compressed_row = [4, 7, 2, 1, 2, 42, 3, 12, 9, 10, 99, 2, 15, 11, 77, 1, 3];
            let mut table = ActionTable::new();
            table.large_state_count = 4;
            table.token_count = 10;
            table.append_small_row(compressed_row.as_ptr());

            assert_eq!(table.lookup(3, 1), None);
            assert_eq!(table.lookup(4, 1), Some(7));
            assert_eq!(table.lookup(4, 2), Some(7));
            assert_eq!(table.lookup(4, 3), Some(77));
            assert_eq!(table.lookup(4, 9), Some(42));
            assert_eq!(table.lookup(4, 4), Some(0));
            assert_eq!(table.lookup(4, 10), None);
            table.delete();
        }
    }

    #[test]
    fn projects_and_sorts_nonterminal_entries() {
        unsafe {
            let compressed_row = [4, 7, 2, 1, 2, 42, 3, 12, 9, 10, 99, 2, 15, 11, 77, 1, 13];
            let mut table = GotoTable::new();
            table.large_state_count = 4;
            table.append_small_row(compressed_row.as_ptr(), 10, 14);

            assert_eq!(table.lookup(3, 10), None);
            assert_eq!(table.lookup(4, 10), Some(42));
            assert_eq!(table.lookup(4, 11), Some(99));
            assert_eq!(table.lookup(4, 12), Some(42));
            assert_eq!(table.lookup(4, 13), Some(77));
            assert_eq!(table.lookup(4, 14), Some(0));
            table.delete();
        }
    }

    #[test]
    fn searches_large_sparse_rows() {
        unsafe {
            let compressed_row = [1, 5, 9, 18, 10, 17, 11, 16, 12, 15, 13, 14];
            let mut table = GotoTable::new();
            table.large_state_count = 8;
            table.append_small_row(compressed_row.as_ptr(), 10, 19);

            for symbol in 10..=18 {
                assert_eq!(table.lookup(8, symbol), Some(5));
            }
            assert_eq!(table.lookup(8, 9), Some(0));
            table.delete();
        }
    }
}

/// Summary used to compare and prune stack versions.
#[derive(Clone, Copy)]
struct ErrorStatus {
    /// Accumulated recovery/error cost.
    cost: u32,
    /// Number of visible nodes since the last error.
    node_count: u32,
    /// Dynamic precedence for tie-breaking.
    dynamic_precedence: i32,
    /// Whether the version is currently in error recovery.
    is_in_error: bool,
}

/// `ErrorComparison`
#[derive(PartialEq, Eq)]
enum ErrorComparison {
    TakeLeft,
    PreferLeft,
    None,
    PreferRight,
    TakeRight,
}

/// `TSStringInput` — for string-based parsing
struct TSStringInput {
    string: *const c_char,
    length: u32,
}

/// Main parser runtime state.
///
/// One `TSParser` owns all mutable state for a parse: lexer callbacks, GLR
/// stack versions, parser scratch arrays, external scanner state, and the final
/// accepted tree. The public C API only observes pointers to this opaque type,
/// so its fields deliberately use Rust layout.
pub struct TSParser {
    /// Input adapter and `TSLexer` callback surface.
    lexer: Lexer,
    /// Persistent GLR parse stack.
    stack: *mut Stack,
    /// Free lists used while releasing or mutating subtrees.
    tree_pool: SubtreePool,
    /// Active language tables and callbacks.
    language: *const TSLanguage,
    /// Scratch set of reductions considered during recovery.
    reduce_actions: ReduceActionSet,
    /// Best accepted root found so far.
    finished_tree: Subtree,
    /// Scratch arrays for stripping and comparing trailing extras.
    trailing_extras: SubtreeArray,
    trailing_extras2: SubtreeArray,
    /// Scratch child array used for subtree comparisons.
    scratch_trees: SubtreeArray,
    /// Cached lexer result for repeated same-position lookups.
    token_cache: TokenCache,
    /// Sparse nonterminal transitions for compressed small parse states.
    goto_table: GotoTable,
    /// Sparse terminal actions for compressed small parse states.
    action_table: ActionTable,
    /// Language-owned external scanner payload.
    external_scanner_payload: *mut c_void,
    /// Optional parse debug graph output.
    dot_graph_file: *mut c_void,
    /// Number of accepted trees seen in this parse.
    accept_count: u32,
    /// Progress-callback operation counter.
    operation_count: u32,
    /// Public parse cancellation/progress options.
    parse_options: TSParseOptions,
    /// Mutable status passed to the progress callback.
    parse_state: TSParseState,
    /// Set when balancing was canceled by the progress callback.
    canceled_balancing: bool,
    /// Set once any accepted tree contains an error.
    has_error: bool,
}

#[inline]
fn parse_options_none() -> TSParseOptions {
    TSParseOptions {
        payload: ptr::null_mut(),
        progress_callback: None,
    }
}

#[inline]
const fn parse_state_empty() -> TSParseState {
    TSParseState {
        payload: ptr::null_mut(),
        current_byte_offset: 0,
        has_error: false,
    }
}

// ---------------------------------------------------------------------------
// Internal helpers — StringInput
// ---------------------------------------------------------------------------

unsafe extern "C" fn ts_string_input_read(
    payload: *mut c_void,
    byte: u32,
    _point: TSPoint,
    length: *mut u32,
) -> *const c_char {
    let input = ptr_ref(payload.cast::<TSStringInput>());
    if byte >= input.length {
        *length = 0;
        c"".as_ptr()
    } else {
        *length = input.length - byte;
        input.string.add(byte as usize)
    }
}

mod logging;
use logging::{parser_log, parser_log_stack, parser_log_tree};

/// Resolve one terminal parse-table entry through the parser-private sparse
/// projection when the state uses a compressed row. Dense large states and
/// nonterminal/error symbols retain the language's canonical lookup path.
#[inline]
unsafe fn parser_table_entry(
    self_: &TSParser,
    state: TSStateId,
    symbol: TSSymbol,
    result: &mut TableEntry,
) {
    if let Some(action_index) = self_.action_table.lookup(state, symbol) {
        language_table_entry_from_index(self_.language, action_index, result);
    } else {
        language_table_entry(self_.language, state, symbol, result);
    }
}

mod advance;
use advance::{parser_advance, parser_condense_stack};

mod lexing;
use lexing::{
    parser_external_scanner_create, parser_external_scanner_destroy, parser_set_cached_token,
};

mod actions;
mod recovery;

mod balancing;
use balancing::parser_balance_subtree;

unsafe fn parser_has_outstanding_parse(self_: &TSParser) -> bool {
    self_.canceled_balancing
        || !self_.external_scanner_payload.is_null()
        || ptr_ref(self_.stack).head(0).state() != 1
        || ptr_mut(self_.stack).node_count_since_error(0) != 0
}

unsafe fn parser_take_finished_tree(self_: &mut TSParser) -> *mut TSTree {
    subtree_publish(self_.tree_pool.arena());
    let arena = subtree_pool_retain_arena(&mut self_.tree_pool);
    let result = TSTree::new(
        self_.finished_tree,
        arena,
        self_.language,
        lexer_included_ranges_slice(&self_.lexer),
    );
    self_.finished_tree = NULL_SUBTREE;
    result
}

// ---------------------------------------------------------------------------
// Exported functions — lifecycle
// ---------------------------------------------------------------------------

#[no_mangle]
pub unsafe extern "C" fn ts_parser_new() -> *mut TSParser {
    let self_ = malloc(core::mem::size_of::<TSParser>()).cast::<TSParser>();
    ptr::write(
        self_,
        TSParser {
            lexer: lexer_new(),
            stack: ptr::null_mut(),
            tree_pool: subtree_pool_new(32),
            language: ptr::null(),
            reduce_actions: Array::new(),
            finished_tree: NULL_SUBTREE,
            trailing_extras: SubtreeArray::new(),
            trailing_extras2: SubtreeArray::new(),
            scratch_trees: SubtreeArray::new(),
            token_cache: TokenCache {
                token: NULL_SUBTREE,
                last_external_token: NULL_SUBTREE,
                byte_index: 0,
            },
            goto_table: GotoTable::new(),
            action_table: ActionTable::new(),
            external_scanner_payload: ptr::null_mut(),
            dot_graph_file: ptr::null_mut(),
            accept_count: 0,
            operation_count: 0,
            parse_options: parse_options_none(),
            parse_state: parse_state_empty(),
            canceled_balancing: false,
            has_error: false,
        },
    );
    let parser = ptr_mut(self_);
    parser.reduce_actions.reserve(4);
    parser.stack = stack_new(&mut parser.tree_pool);
    parser_set_cached_token(parser, 0, NULL_SUBTREE, NULL_SUBTREE);
    self_
}

#[no_mangle]
pub unsafe extern "C" fn ts_parser_delete(self_: *mut TSParser) {
    if self_.is_null() {
        return;
    }

    ts_parser_reset(self_);
    let parser = ptr_mut(self_);
    stack_delete(ptr_mut(parser.stack));
    if !parser.reduce_actions.contents.is_null() {
        parser.reduce_actions.delete();
    }
    lexer_delete(&mut parser.lexer);
    parser_set_cached_token(parser, 0, NULL_SUBTREE, NULL_SUBTREE);
    subtree_pool_delete(&mut parser.tree_pool);
    parser.trailing_extras.delete();
    parser.trailing_extras2.delete();
    parser.scratch_trees.delete();
    parser.goto_table.delete();
    parser.action_table.delete();
    free(self_.cast::<c_void>());
}

// ---------------------------------------------------------------------------
// Exported functions — configuration
// ---------------------------------------------------------------------------

#[no_mangle]
pub unsafe extern "C" fn ts_parser_language(self_: *const TSParser) -> *const TSLanguage {
    let parser = ptr_ref(self_);
    parser.language
}

#[no_mangle]
pub unsafe extern "C" fn ts_parser_set_language(
    self_: *mut TSParser,
    language: *const TSLanguage,
) -> bool {
    ts_parser_reset(self_);
    let parser = ptr_mut(self_);
    parser.language = ptr::null();
    parser.goto_table.clear();
    parser.action_table.clear();
    if !language.is_null() {
        let language_data = language_full(language);
        if language_data.abi_version > TREE_SITTER_LANGUAGE_VERSION
            || language_data.abi_version < TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION
        {
            return false;
        }
    }

    parser.language = language;
    parser.goto_table.rebuild(language);
    parser.action_table.rebuild(language);
    true
}

#[no_mangle]
pub unsafe extern "C" fn ts_parser_logger(self_: *const TSParser) -> TSLogger {
    let parser = ptr_ref(self_);
    ptr::read(&parser.lexer.logger)
}

#[no_mangle]
pub unsafe extern "C" fn ts_parser_set_logger(self_: *mut TSParser, logger: TSLogger) {
    let parser = ptr_mut(self_);
    parser.lexer.logger = logger;
}

#[no_mangle]
pub unsafe extern "C" fn ts_parser_print_dot_graphs(self_: *mut TSParser, fd: i32) {
    let parser = ptr_mut(self_);
    if !parser.dot_graph_file.is_null() {
        fclose(parser.dot_graph_file);
    }

    if fd >= 0 {
        #[cfg(target_os = "windows")]
        {
            extern "C" {
                fn _fdopen(fd: i32, mode: *const i8) -> *mut c_void;
            }
            parser.dot_graph_file = _fdopen(fd, c"a".as_ptr().cast::<i8>());
        }
        #[cfg(not(target_os = "windows"))]
        {
            parser.dot_graph_file = fdopen(fd, c"a".as_ptr().cast::<i8>());
        }
    } else {
        parser.dot_graph_file = ptr::null_mut();
    }
}

#[no_mangle]
pub unsafe extern "C" fn ts_parser_set_included_ranges(
    self_: *mut TSParser,
    ranges: *const TSRange,
    count: u32,
) -> bool {
    let parser = ptr_mut(self_);
    lexer_set_included_ranges(&mut parser.lexer, ranges, count)
}

#[no_mangle]
pub unsafe extern "C" fn ts_parser_included_ranges(
    self_: *const TSParser,
    count: *mut u32,
) -> *const TSRange {
    let parser = ptr_ref(self_);
    lexer_included_ranges(&parser.lexer, count)
}

#[no_mangle]
pub unsafe extern "C" fn ts_parser_reset(self_: *mut TSParser) {
    let parser = ptr_mut(self_);
    parser_external_scanner_destroy(parser);

    lexer_reset(&mut parser.lexer, length_zero());
    stack_clear(ptr_mut(parser.stack));
    parser.trailing_extras.clear();
    parser.trailing_extras2.clear();
    parser.scratch_trees.clear();
    parser_set_cached_token(parser, 0, NULL_SUBTREE, NULL_SUBTREE);
    if !parser.finished_tree.is_null() {
        parser.finished_tree.release(&mut parser.tree_pool);
        parser.finished_tree = NULL_SUBTREE;
    }
    parser.accept_count = 0;
    parser.has_error = false;
    parser.canceled_balancing = false;
    parser.parse_options = parse_options_none();
    parser.parse_state = parse_state_empty();
}

// ---------------------------------------------------------------------------
// Exported functions — parsing
// ---------------------------------------------------------------------------

#[no_mangle]
/// Parse one input document and return a new tree.
///
/// The driver owns the outer GLR loop:
/// - initialize the lexer and external scanner;
/// - process every active stack version until none can advance normally;
/// - condense/merge/prune stack versions;
/// - recover when all versions are paused at errors;
/// - balance the accepted tree and transfer its root into `TSTree`.
///
/// Returning null means parsing was canceled. Parser-owned scratch state is
/// reset before returning unless the parse is intentionally resumable.
pub unsafe extern "C-unwind" fn ts_parser_parse(
    self_: *mut TSParser,
    old_tree: *const TSTree,
    input: TSInput,
) -> *mut TSTree {
    let _ = old_tree;
    let parser = ptr_mut(self_);
    if parser.language.is_null() || input.read.is_none() {
        return ptr::null_mut();
    }

    lexer_set_input(&mut parser.lexer, input);
    parser.operation_count = 0;

    if parser_has_outstanding_parse(parser) {
        parser_log(parser, |_, log| log.write_str("resume_parsing"));
        if parser.canceled_balancing {
            debug_assert!(!parser.finished_tree.is_null());
            if !parser_balance_subtree(parser) {
                parser.canceled_balancing = true;
                return ptr::null_mut();
            }
            parser.canceled_balancing = false;
            parser_log(parser, |_, log| log.write_str("done"));
            parser_log_tree(parser, parser.finished_tree);

            let result = parser_take_finished_tree(parser);

            ts_parser_reset(self_);
            return result;
        }
    } else {
        subtree_pool_prepare_for_parse(&mut parser.tree_pool);
        subtree_array_prepare_scratch(&mut parser.tree_pool, &mut parser.scratch_trees);
        parser_external_scanner_create(parser);
        parser_log(parser, |_, log| log.write_str("new_parse"));
    }

    let mut last_position: u32 = 0;
    let mut version_count: StackVersion;
    loop {
        let mut version: StackVersion = 0;
        loop {
            version_count = ptr_ref(parser.stack).version_count();
            if version >= version_count {
                break;
            }

            while ptr_ref(parser.stack).head(version).is_active() {
                parser_log(parser, |context, log| {
                    let stack = ptr_ref(context.stack);
                    let head = stack.head(version);
                    write!(
                        log,
                        "process version:{version}, version_count:{}, state:{}, row:{}, col:{}",
                        stack.version_count(),
                        i32::from(head.state()),
                        head.position().extent.row,
                        head.position().extent.column
                    )
                });

                if !parser_advance(parser, version) {
                    return ptr::null_mut();
                }

                parser_log_stack(parser);

                let position = ptr_ref(parser.stack).head(version).position().bytes;
                if position > last_position || (version > 0 && position == last_position) {
                    last_position = position;
                    break;
                }
            }
            version += 1;
        }

        // After advancing each version of the stack, re-sort the versions by their cost,
        // removing any versions that are no longer worth pursuing.
        let min_error_cost = parser_condense_stack(parser);

        // If there's already a finished parse tree that's better than any in-progress version,
        // then terminate parsing. Clear the parse stack to remove any extra references to subtrees
        // within the finished tree, ensuring that these subtrees can be safely mutated in-place
        // for rebalancing.
        if !parser.finished_tree.is_null()
            && parser.finished_tree.error_cost(parser.tree_pool.arena()) < min_error_cost
        {
            stack_clear(ptr_mut(parser.stack));
            break;
        }

        if version_count == 0 {
            break;
        }
    }

    // balance:
    debug_assert!(!parser.finished_tree.is_null());
    if !parser_balance_subtree(parser) {
        parser.canceled_balancing = true;
        return ptr::null_mut();
    }
    parser.canceled_balancing = false;
    parser_log(parser, |_, log| log.write_str("done"));
    parser_log_tree(parser, parser.finished_tree);

    let result = parser_take_finished_tree(parser);

    // exit:
    ts_parser_reset(self_);
    result
}

#[no_mangle]
pub unsafe extern "C-unwind" fn ts_parser_parse_with_options(
    self_: *mut TSParser,
    old_tree: *const TSTree,
    input: TSInput,
    parse_options: TSParseOptions,
) -> *mut TSTree {
    {
        let parser = ptr_mut(self_);
        parser.parse_options = parse_options;
        parser.parse_state.payload = parse_options.payload;
    }
    let result = ts_parser_parse(self_, old_tree, input);
    // Reset parser options before further parse calls.
    let parser = ptr_mut(self_);
    parser.parse_options = parse_options_none();
    result
}

#[no_mangle]
pub unsafe extern "C-unwind" fn ts_parser_parse_string(
    self_: *mut TSParser,
    old_tree: *const TSTree,
    string: *const i8,
    length: u32,
) -> *mut TSTree {
    ts_parser_parse_string_encoding(self_, old_tree, string, length, TSInputEncodingUTF8)
}

#[no_mangle]
pub unsafe extern "C-unwind" fn ts_parser_parse_string_encoding(
    self_: *mut TSParser,
    old_tree: *const TSTree,
    string: *const i8,
    length: u32,
    encoding: TSInputEncoding,
) -> *mut TSTree {
    let input = TSStringInput {
        string: string.cast::<c_char>(),
        length,
    };
    ts_parser_parse(
        self_,
        old_tree,
        TSInput {
            payload: core::ptr::addr_of!(input) as *mut c_void,
            read: Some(ts_string_input_read),
            encoding,
            decode: None,
        },
    )
}