agent-shell-parser 0.7.0

Shared parsing substrate for agent hook binaries — JSON input, shell tokenization
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
use super::redirect::detect_redirections;
use super::tokenize::shlex_or_whitespace_words;
use super::types::{Operator, Redirection, Word};
use tree_sitter::Node;

/// Strip the outermost matching quote pair from a word.
///
/// Handles single quotes, double quotes, and `$'...'` ANSI-C quotes.
/// Unmatched or absent quotes leave the word unchanged. Escape sequences
/// inside `$'...'` are left as-is (they are source text, not interpreted).
fn strip_quotes(word: &str) -> Word {
    // $'...' ANSI-C quotes
    if let Some(inner) = word.strip_prefix("$'") {
        if let Some(inner) = inner.strip_suffix('\'') {
            return Word::from(inner);
        }
        return Word::from(word);
    }
    // Single quotes
    if let Some(inner) = word.strip_prefix('\'') {
        if let Some(inner) = inner.strip_suffix('\'') {
            return Word::from(inner);
        }
        return Word::from(word);
    }
    // Double quotes
    if let Some(inner) = word.strip_prefix('"') {
        if let Some(inner) = inner.strip_suffix('"') {
            return Word::from(inner);
        }
        return Word::from(word);
    }
    Word::from(word)
}

pub(super) struct WalkResult {
    pub(super) segments: Vec<SegmentInfo>,
    pub(super) operators: Vec<Operator>,
}

pub(super) struct SegmentInfo {
    pub(super) start: usize,
    pub(super) end: usize,
    pub(super) redirection: Option<Redirection>,
    /// Pre-tokenized words for this segment.
    ///
    /// Always populated — either via tree-sitter word extraction (for known
    /// node types like `command`, `declaration_command`, `test_command`) or
    /// via explicit shlex tokenization at the call site (for unknown node
    /// types and heredoc loose words). There is no implicit fallback.
    pub(super) words: Vec<Word>,
}

impl WalkResult {
    pub(super) fn empty() -> Self {
        Self {
            segments: vec![],
            operators: vec![],
        }
    }

    pub(super) fn single_with_words(
        start: usize,
        end: usize,
        redir: Option<Redirection>,
        words: Vec<Word>,
    ) -> Self {
        Self {
            segments: vec![SegmentInfo {
                start,
                end,
                redirection: redir,
                words,
            }],
            operators: vec![],
        }
    }

    pub(super) fn append(&mut self, other: WalkResult, join_op: Option<Operator>) {
        if other.segments.is_empty() {
            return;
        }
        if !self.segments.is_empty() {
            if let Some(op) = join_op {
                self.operators.push(op);
            }
        }
        self.segments.extend(other.segments);
        self.operators.extend(other.operators);
    }
}

/// For `list`/`pipeline`, only the last segment gets the redirect.
/// For control-flow bodies, every segment gets it.
fn propagate_redirect(result: &mut WalkResult, node_kind: &str, redir: &Redirection) {
    if node_kind == "list" || node_kind == "pipeline" {
        if let Some(last) = result.segments.last_mut() {
            if last.redirection.is_none() {
                last.redirection = Some(redir.clone());
            }
        }
    } else {
        for seg in &mut result.segments {
            if seg.redirection.is_none() {
                seg.redirection = Some(redir.clone());
            }
        }
    }
}

/// Extract word-level tokens from a `command` node's named children.
///
/// Each named child of a tree-sitter `command` node represents one shell
/// word: `command_name`, `word`, `raw_string`, `string`,
/// `command_substitution`, `process_substitution`, `variable_assignment`
/// (for leading `KEY=VALUE`), `concatenation`, etc.
///
/// The full source text of each child is used, preserving quotes and
/// substitution delimiters. This matches shell semantics: `$(echo test)`
/// is one word, `'hello world'` is one word.
fn extract_command_words(node: Node, source: &[u8]) -> Vec<Word> {
    let mut words = Vec::new();
    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        // Skip redirect-related nodes — they are not command words.
        if matches!(
            child.kind(),
            "file_redirect" | "herestring_redirect" | "heredoc_redirect" | "heredoc_body"
        ) {
            continue;
        }
        if let Ok(text) = child.utf8_text(source) {
            words.push(strip_quotes(text));
        }
    }
    words
}

/// Extract word-level tokens from a `declaration_command` node.
///
/// Declaration commands (`export`, `declare`, `local`, `readonly`, `typeset`)
/// have the keyword as an anonymous child and `variable_assignment` or
/// `word` nodes as named children. We include the keyword as the first word.
fn extract_declaration_words(node: Node, source: &[u8]) -> Vec<Word> {
    let mut words = Vec::new();
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        match child.kind() {
            // Anonymous keyword nodes: export, declare, local, readonly, typeset
            "export" | "declare" | "local" | "readonly" | "typeset" => {
                if let Ok(text) = child.utf8_text(source) {
                    words.push(strip_quotes(text));
                }
            }
            _ if child.is_named() => {
                // Skip redirect-related nodes.
                if matches!(
                    child.kind(),
                    "file_redirect" | "herestring_redirect" | "heredoc_redirect" | "heredoc_body"
                ) {
                    continue;
                }
                if let Ok(text) = child.utf8_text(source) {
                    words.push(strip_quotes(text));
                }
            }
            _ => {}
        }
    }
    words
}

/// Extract word-level tokens from a `variable_assignments` (plural) node.
///
/// This node wraps multiple `variable_assignment` children. Each child
/// becomes one word (e.g. `FOO=bar BAZ=qux` -> `["FOO=bar", "BAZ=qux"]`).
fn extract_variable_assignments_words(node: Node, source: &[u8]) -> Vec<Word> {
    let mut words = Vec::new();
    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        if let Ok(text) = child.utf8_text(source) {
            let trimmed = text.trim();
            if !trimmed.is_empty() {
                words.push(strip_quotes(trimmed));
            }
        }
    }
    words
}

/// Extract word-level tokens from an `unset_command` node.
///
/// Structure: `unset` (anonymous) followed by `variable_name` children.
fn extract_unset_words(node: Node, source: &[u8]) -> Vec<Word> {
    let mut words = Vec::new();
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        match child.kind() {
            "unset" | "unsetenv" => {
                if let Ok(text) = child.utf8_text(source) {
                    words.push(strip_quotes(text));
                }
            }
            _ if child.is_named() => {
                if let Ok(text) = child.utf8_text(source) {
                    words.push(strip_quotes(text));
                }
            }
            _ => {}
        }
    }
    words
}

/// Extract word-level tokens from a `test_command` node.
///
/// tree-sitter-bash parses `[[ -f "foo bar" ]]` into structured children:
/// `[[` (anonymous), `test_operator`, string/word, `]]` (anonymous).
/// This function walks those children (including nested `binary_expression`
/// and `unary_expression`) and collects words, stripping quotes. The
/// bracket delimiters (`[[`, `]]`, `[`, `]`) are included as words.
fn extract_test_words(node: Node, source: &[u8]) -> Vec<Word> {
    let mut words = Vec::new();
    extract_test_words_recursive(node, source, &mut words);
    words
}

fn extract_test_words_recursive(node: Node, source: &[u8], words: &mut Vec<Word>) {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        match child.kind() {
            // Bracket delimiters — include as words
            "[[" | "]]" | "[" | "]" => {
                if let Ok(text) = child.utf8_text(source) {
                    words.push(Word::from(text));
                }
            }
            // Compound test expressions — recurse into them
            "binary_expression" | "unary_expression" => {
                extract_test_words_recursive(child, source, words);
            }
            // Operators and leaf tokens — extract text
            "test_operator" => {
                if let Ok(text) = child.utf8_text(source) {
                    words.push(Word::from(text));
                }
            }
            // Named nodes (string, word, variable, etc.) — extract and strip quotes
            _ if child.is_named() => {
                if let Ok(text) = child.utf8_text(source) {
                    words.push(strip_quotes(text));
                }
            }
            // Anonymous operators like ==, !=, =~, -eq, &&, ||, etc.
            _ => {
                let text = child.utf8_text(source).unwrap_or("");
                if !text.is_empty() && text != "(" && text != ")" {
                    // Skip parentheses used for grouping, keep operators
                    if text.starts_with('-')
                        || text.contains('=')
                        || text == "!"
                        || text == ">"
                        || text == "<"
                        || text == "&&"
                        || text == "||"
                    {
                        words.push(Word::from(text));
                    }
                }
            }
        }
    }
}

pub(super) fn walk_ast(node: Node, source: &[u8]) -> WalkResult {
    match node.kind() {
        "program" => walk_program(node, source),
        "list" => walk_list(node, source),
        "pipeline" => walk_pipeline(node, source),
        "command" => {
            let redir = detect_redirections(node, source);
            let words = extract_command_words(node, source);
            WalkResult::single_with_words(node.start_byte(), node.end_byte(), redir, words)
        }
        "declaration_command" => {
            let redir = detect_redirections(node, source);
            let words = extract_declaration_words(node, source);
            WalkResult::single_with_words(node.start_byte(), node.end_byte(), redir, words)
        }
        "unset_command" => {
            let redir = detect_redirections(node, source);
            let words = extract_unset_words(node, source);
            WalkResult::single_with_words(node.start_byte(), node.end_byte(), redir, words)
        }
        "test_command" => {
            let redir = detect_redirections(node, source);
            let words = extract_test_words(node, source);
            WalkResult::single_with_words(node.start_byte(), node.end_byte(), redir, words)
        }
        "variable_assignment" => {
            // Bare variable assignment (no command). The whole text is
            // effectively one "word". Use full text as a single-element list.
            let text = node.utf8_text(source).unwrap_or("").trim();
            let words: Vec<Word> = if text.is_empty() {
                vec![]
            } else {
                vec![strip_quotes(text)]
            };
            WalkResult::single_with_words(node.start_byte(), node.end_byte(), None, words)
        }
        "variable_assignments" => {
            // Multiple bare variable assignments (e.g. `FOO=bar BAZ=qux`).
            // Iterate named children to produce one word per assignment.
            let words = extract_variable_assignments_words(node, source);
            WalkResult::single_with_words(node.start_byte(), node.end_byte(), None, words)
        }
        "redirected_statement" => walk_redirected(node, source),
        "for_statement" | "while_statement" | "until_statement" | "c_style_for_statement" => {
            walk_loop(node, source)
        }
        "if_statement" => walk_if(node, source),
        "case_statement" => walk_case(node, source),
        "subshell" | "compound_statement" | "do_group" | "else_clause" | "elif_clause" => {
            walk_block(node, source)
        }
        "case_item" => walk_case_item(node, source),
        "negated_command" => walk_negated(node, source),
        "function_definition" => walk_function(node, source),
        "comment" | "heredoc_body" => WalkResult::empty(),
        "ERROR" => WalkResult::empty(),
        _ if node.is_named() => {
            // Unknown node type — shlex fallback, explicit and auditable.
            let text = node.utf8_text(source).unwrap_or("");
            let words = shlex_or_whitespace_words(text);
            WalkResult::single_with_words(node.start_byte(), node.end_byte(), None, words)
        }
        _ => WalkResult::empty(),
    }
}

/// Top-level `program` node. Detects `&` (background) between children.
fn walk_program(node: Node, source: &[u8]) -> WalkResult {
    let mut result = WalkResult::empty();
    let mut cursor = node.walk();
    let all: Vec<Node> = node.children(&mut cursor).collect();

    for (i, child) in all.iter().enumerate() {
        if !child.is_named() {
            continue;
        }
        let join_op = if result.segments.is_empty() {
            None
        } else {
            let bg = (0..i)
                .rev()
                .take_while(|&j| !all[j].is_named())
                .any(|j| all[j].kind() == "&");
            Some(if bg {
                Operator::Background
            } else {
                Operator::Semi
            })
        };
        result.append(walk_ast(*child, source), join_op);
    }
    result
}

/// `list` — left-recursive binary: `a && b || c` → `list(list(a,&&,b),||,c)`.
///
/// Iterative left-descent to avoid stack overflow on deeply nested chains
/// (e.g. 20,000+ `&&`-chained commands).
fn walk_list(node: Node, source: &[u8]) -> WalkResult {
    // Collect (right_child, operator) pairs by iteratively descending into
    // the left-recursive spine of `list` nodes.
    let mut parts: Vec<(Node, Operator)> = Vec::new();
    let mut current = node;

    loop {
        let mut cursor = current.walk();
        let named: Vec<Node> = current.named_children(&mut cursor).collect();

        if named.len() < 2 {
            // Degenerate list node (0 or 1 children) — treat current as the
            // leftmost base and stop descending.
            break;
        }

        let op = list_operator(current);
        // Save the right child and the operator joining left to right.
        parts.push((named[1], op));

        if named[0].kind() == "list" {
            // Left child is another list — descend iteratively.
            current = named[0];
        } else {
            // Left child is not a list — it is the leftmost base node.
            current = named[0];
            break;
        }
    }

    // `current` is now the leftmost non-list node (or a degenerate list).
    // Walk it to produce the initial result.
    let mut result = walk_ast(current, source);

    // Replay the collected right-hand sides from left to right (they were
    // pushed in right-to-left order during descent).
    for (right_node, op) in parts.into_iter().rev() {
        result.append(walk_ast(right_node, source), Some(op));
    }

    result
}

/// tree-sitter-bash `list` nodes only contain `&&` or `||`.
/// The background `&` operator appears at the `program` level instead.
fn list_operator(node: Node) -> Operator {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if !child.is_named() {
            match child.kind() {
                "&&" => return Operator::And,
                "||" => return Operator::Or,
                _ => {}
            }
        }
    }
    Operator::Semi
}

fn walk_pipeline(node: Node, source: &[u8]) -> WalkResult {
    let mut result = WalkResult::empty();
    let mut pending_op: Option<Operator> = None;
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.is_named() {
            result.append(walk_ast(child, source), pending_op.take());
        } else {
            match child.kind() {
                "|" => pending_op = Some(Operator::Pipe),
                "|&" => pending_op = Some(Operator::PipeErr),
                _ => {}
            }
        }
    }
    result
}

fn walk_redirected(node: Node, source: &[u8]) -> WalkResult {
    let redir = detect_redirections(node, source);

    // First pass: heredoc_redirect with same-line commands.
    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        if child.kind() == "heredoc_redirect" {
            let inner = walk_heredoc_redirect(child, source);
            if !inner.segments.is_empty() {
                let mut full = WalkResult::empty();
                let mut c2 = node.walk();
                for sib in node.named_children(&mut c2) {
                    if sib.kind() == "heredoc_redirect" {
                        break;
                    }
                    if matches!(sib.kind(), "file_redirect" | "herestring_redirect") {
                        continue;
                    }
                    if is_leaf_command(sib) {
                        let end = effective_end(node).min(child.start_byte());
                        let words = extract_leaf_words(sib, source);
                        let wr = WalkResult::single_with_words(
                            sib.start_byte(),
                            end,
                            redir.clone(),
                            words,
                        );
                        full.append(wr, None);
                    } else {
                        let mut body = walk_ast(sib, source);
                        if let Some(ref r) = redir {
                            propagate_redirect(&mut body, sib.kind(), r);
                        }
                        full.append(body, None);
                    }
                    break;
                }
                let join_op = heredoc_join_operator(child);
                full.append(inner, Some(join_op));
                return full;
            }
        }
    }

    // Second pass: normal body.
    let mut cursor2 = node.walk();
    for child in node.named_children(&mut cursor2) {
        if matches!(
            child.kind(),
            "file_redirect" | "herestring_redirect" | "heredoc_redirect"
        ) {
            continue;
        }
        if is_leaf_command(child) {
            let end = effective_end(node);
            let words = extract_leaf_words(child, source);
            return WalkResult::single_with_words(node.start_byte(), end, redir, words);
        }
        let mut result = walk_ast(child, source);
        if let Some(ref r) = redir {
            propagate_redirect(&mut result, child.kind(), r);
        }
        return result;
    }

    let end = effective_end(node);
    // Redirected statement with no recognized body — shlex the visible text.
    let text = source
        .get(node.start_byte()..end)
        .and_then(|b| std::str::from_utf8(b).ok())
        .unwrap_or("");
    let words = shlex_or_whitespace_words(text);
    WalkResult::single_with_words(node.start_byte(), end, redir, words)
}

fn walk_heredoc_redirect(node: Node, source: &[u8]) -> WalkResult {
    let mut result = WalkResult::empty();
    let mut cursor = node.walk();
    let mut loose_words_start: Option<usize> = None;
    let mut loose_words_end: usize = 0;

    for child in node.named_children(&mut cursor) {
        match child.kind() {
            "pipeline" | "list" | "command" | "redirected_statement" => {
                if let Some(start) = loose_words_start.take() {
                    // Heredoc loose words — no tree-sitter structure, use shlex.
                    let text = source
                        .get(start..loose_words_end)
                        .and_then(|b| std::str::from_utf8(b).ok())
                        .unwrap_or("");
                    let words = shlex_or_whitespace_words(text);
                    result.append(
                        WalkResult::single_with_words(start, loose_words_end, None, words),
                        Some(Operator::Semi),
                    );
                }
                let op = heredoc_operator_before(node, child);
                result.append(walk_ast(child, source), Some(op));
            }
            "word" => {
                if loose_words_start.is_none() {
                    loose_words_start = Some(child.start_byte());
                }
                loose_words_end = child.end_byte();
            }
            _ => {}
        }
    }

    if let Some(start) = loose_words_start {
        // Heredoc trailing loose words — no tree-sitter structure, use shlex.
        let text = source
            .get(start..loose_words_end)
            .and_then(|b| std::str::from_utf8(b).ok())
            .unwrap_or("");
        let words = shlex_or_whitespace_words(text);
        result.append(
            WalkResult::single_with_words(start, loose_words_end, None, words),
            Some(Operator::Semi),
        );
    }

    result
}

fn heredoc_operator_before(heredoc_node: Node, child: Node) -> Operator {
    let mut cursor = heredoc_node.walk();
    let mut last_op = None;
    for sib in heredoc_node.children(&mut cursor) {
        if sib.start_byte() >= child.start_byte() {
            break;
        }
        if !sib.is_named() {
            match sib.kind() {
                "&&" => last_op = Some(Operator::And),
                "||" => last_op = Some(Operator::Or),
                "|&" => last_op = Some(Operator::PipeErr),
                "|" => last_op = Some(Operator::Pipe),
                _ => {}
            }
        }
    }
    last_op.unwrap_or(Operator::Pipe)
}

fn heredoc_join_operator(heredoc_node: Node) -> Operator {
    let mut cursor = heredoc_node.walk();
    for child in heredoc_node.children(&mut cursor) {
        if !child.is_named() {
            match child.kind() {
                "&&" => return Operator::And,
                "||" => return Operator::Or,
                "|&" => return Operator::PipeErr,
                _ => {}
            }
        } else {
            match child.kind() {
                "pipeline" => return Operator::Pipe,
                "command" | "list" | "redirected_statement" => break,
                _ => {}
            }
        }
    }
    Operator::Pipe
}

/// Loop statements: `for`, `while`, `until`, `c_style_for`.
///
/// For `while`/`until`, the condition is a command — walked alongside the body.
/// For `for`/`c_style_for`, only the `do_group` body is walked; iteration
/// values are not commands (substitutions there become structural).
fn walk_loop(node: Node, source: &[u8]) -> WalkResult {
    let mut result = WalkResult::empty();
    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        match child.kind() {
            "do_group" => result.append(walk_block(child, source), Some(Operator::Semi)),
            _ if node.kind() == "while_statement" || node.kind() == "until_statement" => {
                result.append(walk_ast(child, source), Some(Operator::Semi));
            }
            _ => {}
        }
    }
    result
}

fn walk_if(node: Node, source: &[u8]) -> WalkResult {
    let mut result = WalkResult::empty();
    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        match child.kind() {
            "command"
            | "declaration_command"
            | "test_command"
            | "pipeline"
            | "list"
            | "redirected_statement"
            | "compound_statement"
            | "subshell"
            | "negated_command" => {
                result.append(walk_ast(child, source), Some(Operator::Semi));
            }
            "else_clause" | "elif_clause" => {
                result.append(walk_ast(child, source), Some(Operator::Semi));
            }
            _ => {}
        }
    }
    result
}

fn walk_case(node: Node, source: &[u8]) -> WalkResult {
    let mut result = WalkResult::empty();
    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        if child.kind() == "case_item" {
            result.append(walk_case_item(child, source), Some(Operator::Semi));
        }
    }
    result
}

fn walk_case_item(node: Node, source: &[u8]) -> WalkResult {
    let mut result = WalkResult::empty();
    let mut past_paren = false;
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if !child.is_named() && child.kind() == ")" {
            past_paren = true;
            continue;
        }
        if past_paren && child.is_named() {
            result.append(walk_ast(child, source), Some(Operator::Semi));
        }
    }
    result
}

fn walk_block(node: Node, source: &[u8]) -> WalkResult {
    let mut result = WalkResult::empty();
    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        result.append(walk_ast(child, source), Some(Operator::Semi));
    }
    result
}

fn walk_negated(node: Node, source: &[u8]) -> WalkResult {
    let mut cursor = node.walk();
    if let Some(child) = node.named_children(&mut cursor).next() {
        return walk_ast(child, source);
    }
    WalkResult::empty()
}

fn walk_function(node: Node, source: &[u8]) -> WalkResult {
    let mut cursor = node.walk();
    for child in node.named_children(&mut cursor) {
        if child.kind() == "word" {
            continue;
        }
        return walk_ast(child, source);
    }
    WalkResult::empty()
}

fn is_leaf_command(node: Node) -> bool {
    matches!(
        node.kind(),
        "command"
            | "declaration_command"
            | "test_command"
            | "unset_command"
            | "variable_assignment"
            | "variable_assignments"
    )
}

/// Extract tree-sitter words from a leaf command node.
///
/// All leaf command node types have word-level extraction.
fn extract_leaf_words(node: Node, source: &[u8]) -> Vec<Word> {
    match node.kind() {
        "command" => extract_command_words(node, source),
        "declaration_command" => extract_declaration_words(node, source),
        "unset_command" => extract_unset_words(node, source),
        "test_command" => extract_test_words(node, source),
        "variable_assignment" => {
            let text = node.utf8_text(source).unwrap_or("").trim();
            if text.is_empty() {
                vec![]
            } else {
                vec![strip_quotes(text)]
            }
        }
        "variable_assignments" => extract_variable_assignments_words(node, source),
        _ => {
            // Unknown leaf type — shlex fallback, explicit and auditable.
            let text = node.utf8_text(source).unwrap_or("");
            shlex_or_whitespace_words(text)
        }
    }
}

fn effective_end(node: Node) -> usize {
    let mut end = node.end_byte();
    trim_at_heredoc_body(node, &mut end);
    end
}

fn trim_at_heredoc_body(node: Node, end: &mut usize) {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "heredoc_body" {
            *end = (*end).min(child.start_byte());
            return;
        }
        trim_at_heredoc_body(child, end);
    }
}

#[cfg(test)]
mod walk_tests {
    use super::strip_quotes;

    #[test]
    fn strip_quotes_empty_string() {
        assert_eq!(strip_quotes(""), "");
    }

    #[test]
    fn strip_quotes_empty_single_quotes() {
        let w = strip_quotes("''");
        assert_eq!(w, "");
    }

    #[test]
    fn strip_quotes_empty_double_quotes() {
        let w = strip_quotes("\"\"");
        assert_eq!(w, "");
    }

    #[test]
    fn strip_quotes_ansi_c_quotes() {
        let w = strip_quotes("$'hello'");
        assert_eq!(w, "hello");
    }

    #[test]
    fn strip_quotes_unclosed_double_quote() {
        let w = strip_quotes("\"unclosed");
        assert_eq!(w, "\"unclosed");
    }

    #[test]
    fn strip_quotes_unmatched_single_quote() {
        let w = strip_quotes("'unmatched");
        assert_eq!(w, "'unmatched");
    }
}