rsconstruct 0.9.126

Rust based fast build system
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
958
959
960
961
962
963
964
//! The shell side of hadolint's rules: which commands a RUN script calls,
//! with what arguments and flags, and whether it pipes.
//!
//! hadolint parses the script with `ShellCheck`'s parser and walks the tree
//! (`Hadolint.Shell`): every simple command whose name is a plain literal
//! becomes a `Command`, including the ones inside `$(...)`, `if`, loops
//! and subshells, in source order, and each one twice (the walk yields it
//! for its redirection wrapper and again for itself); each argument word
//! is flattened the way
//! `simplify` does it (quotes dropped, `$VAR` and `${VAR...}` kept as
//! `${...}`, command substitutions as `${...}` of their words run together,
//! backticks and arithmetic as `${VAR}`, brace expansions dropped). This
//! module is a shell reader written for that purpose: it understands the
//! quoting, expansion and compound-command syntax those queries depend on,
//! not the whole of bash.

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CmdPart {
    pub arg: String,
    /// Position of the word in its command (`ShellCheck`'s token id stands
    /// in for this: only the order matters).
    pub id: usize,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Command {
    pub name: String,
    pub arguments: Vec<CmdPart>,
    pub flags: Vec<CmdPart>,
}

#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ParsedShell {
    pub original: String,
    pub commands: Vec<Command>,
    pub has_pipes: bool,
}

impl Command {
    pub fn get_args(&self) -> Vec<&str> {
        self.arguments.iter().map(|a| a.arg.as_str()).collect()
    }

    /// `getArgsNoFlags`: the arguments that are not flags.
    pub fn args_no_flags(&self) -> Vec<&str> {
        let flag_ids: Vec<usize> = self.flags.iter().map(|f| f.id).collect();
        self.arguments
            .iter()
            .filter(|a| !flag_ids.contains(&a.id))
            .map(|a| a.arg.as_str())
            .collect()
    }

    pub fn has_flag(&self, flag: &str) -> bool {
        self.count_flag(flag) > 0
    }

    pub fn count_flag(&self, flag: &str) -> usize {
        self.flags.iter().filter(|f| f.arg == flag).count()
    }

    pub fn has_any_flag(&self, flags: &[&str]) -> bool {
        self.flags.iter().any(|f| flags.contains(&f.arg.as_str()))
    }

    pub fn has_arg(&self, arg: &str) -> bool {
        self.arguments.iter().any(|a| a.arg == arg)
    }

    /// `cmdHasArgs`: this command is `name` and has any of `args`.
    pub fn is_with_args(&self, name: &str, args: &[&str]) -> bool {
        self.name == name
            && self
                .arguments
                .iter()
                .any(|a| args.contains(&a.arg.as_str()))
    }

    /// `cmdsHaveArgs`.
    pub fn is_any_with_args(&self, names: &[&str], args: &[&str]) -> bool {
        names.contains(&self.name.as_str())
            && self
                .arguments
                .iter()
                .any(|a| args.contains(&a.arg.as_str()))
    }

    /// `cmdHasPrefixArg`.
    pub fn has_prefix_arg(&self, name: &str, prefix: &str) -> bool {
        self.name == name && self.arguments.iter().any(|a| a.arg.starts_with(prefix))
    }

    /// `dropFlagArg`: remove the value argument that follows each of the
    /// given flags (unless the flag carried its value inline with `=`).
    pub fn drop_flag_arg(&self, flags: &[&str]) -> Self {
        let mut drop_ids = Vec::new();
        for f in &self.flags {
            if !flags.contains(&f.arg.as_str()) {
                continue;
            }
            let inline = self
                .arguments
                .iter()
                .any(|a| a.id == f.id && a.arg.contains('='));
            if inline {
                continue;
            }
            if let Some(v) = self.value_id(f.id) {
                drop_ids.push(v);
            }
        }
        Self {
            name: self.name.clone(),
            arguments: self
                .arguments
                .iter()
                .filter(|a| !drop_ids.contains(&a.id))
                .cloned()
                .collect(),
            flags: self.flags.clone(),
        }
    }

    /// `getFlagArg`: the values following each occurrence of `flag`.
    pub fn flag_args(&self, flag: &str) -> Vec<&str> {
        let ids: Vec<usize> = self
            .flags
            .iter()
            .filter(|f| f.arg == flag)
            .filter_map(|f| self.value_id(f.id))
            .collect();
        self.arguments
            .iter()
            .filter(|a| ids.contains(&a.id))
            .map(|a| a.arg.as_str())
            .collect()
    }

    /// `getValueId`: the smallest argument id greater than `id`.
    fn value_id(&self, id: usize) -> Option<usize> {
        self.arguments
            .iter()
            .map(|a| a.id)
            .filter(|&i| i > id)
            .min()
    }

    /// `isPipInstall`.
    pub fn is_pip_install(&self) -> bool {
        let args = self.get_args();
        let std = self.name.starts_with("pip")
            && !self.name.starts_with("pipenv")
            && args.contains(&"install");
        let python =
            self.name.starts_with("python") && contains_seq(&args, &["-m", "pip", "install"]);
        std || python
    }
}

/// `isInfixOf` for argument lists.
pub fn contains_seq(args: &[&str], seq: &[&str]) -> bool {
    if seq.is_empty() {
        return true;
    }
    args.windows(seq.len()).any(|w| w == seq)
}

/// `getAllFlags`: `--name[=value]` gives `name`, `-abc` gives `a`, `b`,
/// `c`; `-` and `--` are not flags.
fn flags_of(args: &[CmdPart]) -> Vec<CmdPart> {
    let mut out = Vec::new();
    for a in args {
        let s = a.arg.as_str();
        if s == "--" || s == "-" {
            continue;
        }
        if let Some(rest) = s.strip_prefix("--") {
            let name = rest.split('=').next().unwrap_or("");
            out.push(CmdPart {
                arg: name.to_string(),
                id: a.id,
            });
        } else if let Some(rest) = s.strip_prefix('-') {
            for c in rest.chars() {
                out.push(CmdPart {
                    arg: c.to_string(),
                    id: a.id,
                });
            }
        }
    }
    out
}

pub fn parse_shell(text: &str) -> ParsedShell {
    let mut reader = Reader::new(text);
    reader.script();
    // ShellCheck gives up on a script it cannot parse (an unterminated
    // quote, a redirection without a target): hadolint then sees no
    // commands and no pipes at all.
    if reader.parse_failed {
        return ParsedShell {
            original: text.to_string(),
            commands: Vec::new(),
            has_pipes: false,
        };
    }
    ParsedShell {
        original: text.to_string(),
        commands: reader.commands,
        has_pipes: reader.pipes > 0,
    }
}

// ---- words ----------------------------------------------------------------------

/// One part of a word, as `ShellCheck`'s AST distinguishes them for `simplify`.
#[derive(Debug, Clone)]
enum Part {
    Literal(String),
    SingleQuoted(String),
    DoubleQuoted(Vec<Self>),
    /// `$NAME` or `${...}`: the text inside.
    Var(String),
    /// `$(...)`: the inner script's words, run together.
    CmdSubst(String),
    /// Backticks and `$((...))`.
    Opaque,
    Glob(String),
    /// `{a,b}`: dropped by `simplify`.
    Brace,
}

#[derive(Debug, Clone, Default)]
struct Word {
    parts: Vec<Part>,
    /// Commands found inside `$(...)` of this word, in order.
    inner: Vec<Command>,
    inner_pipes: usize,
}

impl Word {
    fn simplify(&self) -> String {
        self.parts.iter().map(simplify).collect()
    }

    /// `getLiteralString`: the word when it is literal text only.
    fn literal(&self) -> Option<String> {
        let mut out = String::new();
        for p in &self.parts {
            match p {
                Part::Literal(s) | Part::SingleQuoted(s) => out.push_str(s),
                Part::DoubleQuoted(inner) => {
                    for q in inner {
                        match q {
                            Part::Literal(s) => out.push_str(s),
                            _ => return None,
                        }
                    }
                }
                _ => return None,
            }
        }
        Some(out)
    }

    fn is_assignment(&self) -> bool {
        // NAME= or NAME+= or NAME[idx]= at the start, with a literal name
        let Some(Part::Literal(first)) = self.parts.first() else {
            return false;
        };
        let bytes = first.as_bytes();
        if bytes.is_empty() || !(bytes[0].is_ascii_alphabetic() || bytes[0] == b'_') {
            return false;
        }
        let mut i = 1;
        while i < bytes.len() && (bytes[i].is_ascii_alphanumeric() || bytes[i] == b'_') {
            i += 1;
        }
        if i < bytes.len() && bytes[i] == b'[' {
            while i < bytes.len() && bytes[i] != b']' {
                i += 1;
            }
            i += 1;
        }
        if i < bytes.len() && bytes[i] == b'+' {
            i += 1;
        }
        i < bytes.len() && bytes[i] == b'='
    }
}

fn simplify(p: &Part) -> String {
    match p {
        Part::Literal(s) | Part::SingleQuoted(s) | Part::Glob(s) => s.clone(),
        Part::DoubleQuoted(inner) => inner.iter().map(simplify).collect(),
        Part::Var(v) => format!("${{{v}}}"),
        Part::CmdSubst(s) => format!("${{{s}}}"),
        Part::Opaque => "${VAR}".to_string(),
        Part::Brace => String::new(),
    }
}

// ---- reader -----------------------------------------------------------------------

#[derive(Debug, Clone, PartialEq, Eq)]
enum Token {
    Word(Word),
    /// `;`, `&&`, `||`, `&`, `|`, `|&`, `;;`, `;&`, `;;&`, `(`, `)`, newline
    Op(String),
    End,
}

impl PartialEq for Word {
    fn eq(&self, _: &Self) -> bool {
        false
    }
}
impl Eq for Word {}

struct Reader {
    chars: Vec<char>,
    pos: usize,
    commands: Vec<Command>,
    pipes: usize,
    next_id: usize,
    /// Heredoc delimiters whose bodies start after the current line.
    pending_heredocs: Vec<String>,
    /// Something `ShellCheck`'s parser would reject outright.
    parse_failed: bool,
}

impl Reader {
    fn new(text: &str) -> Self {
        Self {
            chars: text.chars().collect(),
            pos: 0,
            commands: Vec::new(),
            pipes: 0,
            next_id: 0,
            pending_heredocs: Vec::new(),
            parse_failed: false,
        }
    }
}

const RESERVED: &[&str] = &[
    "if", "then", "else", "elif", "fi", "do", "done", "while", "until", "!", "{", "}", "time",
    "coproc",
];

impl Reader {
    fn peek(&self) -> Option<char> {
        self.chars.get(self.pos).copied()
    }

    fn peek_at(&self, n: usize) -> Option<char> {
        self.chars.get(self.pos + n).copied()
    }

    fn skip_blanks(&mut self) {
        loop {
            match self.peek() {
                Some(' ' | '\t' | '\r') => self.pos += 1,
                Some('\\') if self.peek_at(1) == Some('\n') => self.pos += 2,
                Some('#') => {
                    while self.peek().is_some_and(|c| c != '\n') {
                        self.pos += 1;
                    }
                }
                _ => break,
            }
        }
    }

    /// After a newline: skip the bodies of heredocs opened on the line.
    fn skip_heredoc_bodies(&mut self) {
        let delims = std::mem::take(&mut self.pending_heredocs);
        for d in delims {
            loop {
                if self.pos >= self.chars.len() {
                    return;
                }
                let end = self.chars[self.pos..]
                    .iter()
                    .position(|&c| c == '\n')
                    .map_or(self.chars.len(), |p| self.pos + p);
                let line: String = self.chars[self.pos..end].iter().collect();
                self.pos = (end + 1).min(self.chars.len());
                if line.trim_start_matches('\t') == d {
                    break;
                }
            }
        }
    }

    fn next_token(&mut self) -> Token {
        self.skip_blanks();
        let Some(c) = self.peek() else {
            return Token::End;
        };
        let two: String = self.chars[self.pos..(self.pos + 3).min(self.chars.len())]
            .iter()
            .collect();
        for op in [
            ";;&", "|&", "&&", "||", ";;", ";&", "<<<", ">>", "&>", "<&", ">&", "<>", ">|",
        ] {
            if two.starts_with(op) {
                self.pos += op.chars().count();
                return self.op_token(op);
            }
        }
        if two.starts_with("<<") {
            self.pos += 2;
            if self.peek() == Some('-') {
                self.pos += 1;
            }
            // the delimiter word
            self.skip_blanks();
            let w = self.word();
            let d = w.simplify();
            self.pending_heredocs
                .push(d.trim_matches(['"', '\'']).to_string());
            return self.next_token();
        }
        match c {
            '\n' => {
                self.pos += 1;
                self.skip_heredoc_bodies();
                Token::Op("\n".to_string())
            }
            ';' | '&' | '|' | '(' | ')' => {
                self.pos += 1;
                self.op_token(&c.to_string())
            }
            '<' | '>' => {
                self.pos += 1;
                self.op_token(&c.to_string())
            }
            _ => {
                // A redirection with a file descriptor: digits then < or >
                let mut i = self.pos;
                while self.chars.get(i).is_some_and(char::is_ascii_digit) {
                    i += 1;
                }
                if i > self.pos && self.chars.get(i).is_some_and(|&c| c == '<' || c == '>') {
                    self.pos = i;
                    return self.next_token();
                }
                Token::Word(self.word())
            }
        }
    }

    /// Redirection operators swallow the word that follows; `|` counts.
    fn op_token(&mut self, op: &str) -> Token {
        match op {
            "<" | ">" | ">>" | "&>" | "<&" | ">&" | "<>" | ">|" | "<<<" => {
                self.skip_blanks();
                if self
                    .peek()
                    .is_some_and(|c| !matches!(c, '\n' | ';' | '&' | '|' | '(' | ')'))
                {
                    let _ = self.word();
                } else {
                    // A redirection needs a target.
                    self.parse_failed = true;
                }
                self.next_token()
            }
            "|" | "|&" => {
                self.pipes += 1;
                Token::Op(op.to_string())
            }
            _ => Token::Op(op.to_string()),
        }
    }

    fn word(&mut self) -> Word {
        let mut word = Word::default();
        let mut lit = String::new();
        let flush = |lit: &mut String, word: &mut Word| {
            if !lit.is_empty() {
                word.parts.push(Part::Literal(std::mem::take(lit)));
            }
        };
        while let Some(c) = self.peek() {
            match c {
                ' ' | '\t' | '\n' | '\r' | ';' | '&' | '|' | '(' | ')' | '<' | '>' => break,
                '\\' => {
                    self.pos += 1;
                    match self.peek() {
                        None => {}
                        Some('\n') => self.pos += 1,
                        Some(e) => {
                            self.pos += 1;
                            lit.push(e);
                        }
                    }
                }
                '\'' => {
                    flush(&mut lit, &mut word);
                    self.pos += 1;
                    let s = self.take_until(|c| c == '\'');
                    word.parts.push(Part::SingleQuoted(s));
                }
                '"' => {
                    flush(&mut lit, &mut word);
                    self.pos += 1;
                    let parts = self.double_quoted(&mut word);
                    word.parts.push(Part::DoubleQuoted(parts));
                }
                '$' => {
                    flush(&mut lit, &mut word);
                    if let Some(p) = self.dollar(&mut word) {
                        word.parts.push(p);
                    } else {
                        lit.push('$');
                    }
                }
                '`' => {
                    flush(&mut lit, &mut word);
                    self.pos += 1;
                    let inner = self.take_until(|c| c == '`');
                    self.absorb_inner(&inner, &mut word);
                    word.parts.push(Part::Opaque);
                }
                '*' | '?' => {
                    flush(&mut lit, &mut word);
                    self.pos += 1;
                    word.parts.push(Part::Glob(c.to_string()));
                }
                '[' => {
                    // a glob class when it closes before the word ends
                    let close = self.chars[self.pos + 1..]
                        .iter()
                        .position(|&c| c == ']' || c == ' ' || c == '\n' || c == '\t');
                    match close {
                        Some(n) if self.chars.get(self.pos + 1 + n) == Some(&']') && n > 0 => {
                            flush(&mut lit, &mut word);
                            let s: String =
                                self.chars[self.pos..=self.pos + 1 + n].iter().collect();
                            self.pos += n + 2;
                            word.parts.push(Part::Glob(s));
                        }
                        _ => {
                            self.pos += 1;
                            lit.push('[');
                        }
                    }
                }
                '{' => {
                    // brace expansion: {a,b} or {1..3} with no spaces
                    let close = self.chars[self.pos + 1..]
                        .iter()
                        .position(|&c| c == '}' || c == ' ' || c == '\n' || c == '\t');
                    let text: Option<String> = close.and_then(|n| {
                        if self.chars.get(self.pos + 1 + n) == Some(&'}') {
                            Some(self.chars[self.pos + 1..self.pos + 1 + n].iter().collect())
                        } else {
                            None
                        }
                    });
                    match text {
                        Some(t) if t.contains(',') || t.contains("..") => {
                            flush(&mut lit, &mut word);
                            self.pos += t.chars().count() + 2;
                            word.parts.push(Part::Brace);
                        }
                        _ => {
                            self.pos += 1;
                            lit.push('{');
                        }
                    }
                }
                _ => {
                    self.pos += 1;
                    lit.push(c);
                }
            }
        }
        flush(&mut lit, &mut word);
        word
    }

    /// Text up to the closing character; reaching the end instead is an
    /// unterminated quote, which `ShellCheck` rejects.
    fn take_until(&mut self, stop: impl Fn(char) -> bool) -> String {
        let mut s = String::new();
        while let Some(c) = self.peek() {
            self.pos += 1;
            if stop(c) {
                return s;
            }
            s.push(c);
        }
        self.parse_failed = true;
        s
    }

    fn double_quoted(&mut self, word: &mut Word) -> Vec<Part> {
        let mut parts = Vec::new();
        let mut lit = String::new();
        loop {
            let Some(c) = self.peek() else {
                self.parse_failed = true;
                break;
            };
            match c {
                '"' => {
                    self.pos += 1;
                    break;
                }
                '\\' => {
                    self.pos += 1;
                    match self.peek() {
                        Some(e @ ('"' | '\\' | '$' | '`')) => {
                            self.pos += 1;
                            lit.push(e);
                        }
                        Some('\n') => self.pos += 1,
                        Some(_) => lit.push('\\'),
                        None => {}
                    }
                }
                '$' => {
                    if !lit.is_empty() {
                        parts.push(Part::Literal(std::mem::take(&mut lit)));
                    }
                    if let Some(p) = self.dollar(word) {
                        parts.push(p);
                    } else {
                        lit.push('$');
                    }
                }
                '`' => {
                    if !lit.is_empty() {
                        parts.push(Part::Literal(std::mem::take(&mut lit)));
                    }
                    self.pos += 1;
                    let inner = self.take_until(|c| c == '`');
                    self.absorb_inner(&inner, word);
                    parts.push(Part::Opaque);
                }
                _ => {
                    self.pos += 1;
                    lit.push(c);
                }
            }
        }
        if !lit.is_empty() {
            parts.push(Part::Literal(lit));
        }
        parts
    }

    /// At a `$`: an expansion part, or `None` for a lone dollar.
    fn dollar(&mut self, word: &mut Word) -> Option<Part> {
        match self.peek_at(1) {
            Some('(') => {
                if self.peek_at(2) == Some('(') {
                    // arithmetic
                    self.pos += 3;
                    let mut depth = 2;
                    while let Some(c) = self.peek() {
                        self.pos += 1;
                        match c {
                            '(' => depth += 1,
                            ')' => {
                                depth -= 1;
                                if depth == 0 {
                                    break;
                                }
                            }
                            _ => {}
                        }
                    }
                    return Some(Part::Opaque);
                }
                self.pos += 2;
                let inner = self.balanced(')');
                let text = self.absorb_inner(&inner, word);
                Some(Part::CmdSubst(text))
            }
            Some('{') => {
                self.pos += 2;
                let inner = self.balanced('}');
                Some(Part::Var(inner))
            }
            Some('\'') => {
                self.pos += 2;
                let s = self.take_until(|c| c == '\'');
                Some(Part::SingleQuoted(s))
            }
            Some('"') => {
                self.pos += 2;
                let parts = self.double_quoted(word);
                Some(Part::DoubleQuoted(parts))
            }
            Some(c) if c.is_ascii_alphabetic() || c == '_' => {
                self.pos += 1;
                let name = self.take_while_chars(|c| c.is_ascii_alphanumeric() || c == '_');
                Some(Part::Var(name))
            }
            Some(c)
                if c.is_ascii_digit() || matches!(c, '@' | '*' | '#' | '?' | '-' | '$' | '!') =>
            {
                self.pos += 2;
                Some(Part::Var(c.to_string()))
            }
            _ => {
                self.pos += 1;
                None
            }
        }
    }

    fn take_while_chars(&mut self, pred: impl Fn(char) -> bool) -> String {
        let start = self.pos;
        while self.peek().is_some_and(&pred) {
            self.pos += 1;
        }
        self.chars[start..self.pos].iter().collect()
    }

    /// Text up to the matching `close`, honouring nesting and quotes.
    fn balanced(&mut self, close: char) -> String {
        let open = if close == ')' { '(' } else { '{' };
        let mut depth = 1;
        let mut out = String::new();
        let mut quote: Option<char> = None;
        while let Some(c) = self.peek() {
            self.pos += 1;
            if let Some(q) = quote {
                out.push(c);
                if c == '\\' && q == '"' {
                    if let Some(n) = self.peek() {
                        self.pos += 1;
                        out.push(n);
                    }
                } else if c == q {
                    quote = None;
                }
                continue;
            }
            match c {
                '\'' | '"' => {
                    quote = Some(c);
                    out.push(c);
                }
                '\\' => {
                    out.push(c);
                    if let Some(n) = self.peek() {
                        self.pos += 1;
                        out.push(n);
                    }
                }
                _ if c == open => {
                    depth += 1;
                    out.push(c);
                }
                _ if c == close => {
                    depth -= 1;
                    if depth == 0 {
                        return out;
                    }
                    out.push(c);
                }
                _ => out.push(c),
            }
        }
        self.parse_failed = true;
        out
    }

    /// Parse the text of a command substitution: its commands join the
    /// word's inner commands, and its words make the `${...}` text.
    fn absorb_inner(&mut self, inner: &str, word: &mut Word) -> String {
        let mut sub = Self::new(inner);
        let words_text = sub.script_words();
        if sub.parse_failed {
            self.parse_failed = true;
        }
        word.inner.extend(sub.commands);
        word.inner_pipes += sub.pipes;
        words_text
    }

    // ---- commands -----------------------------------------------------------------

    fn script(&mut self) {
        self.script_words();
    }

    /// Read the whole script; returns the simplified words of all its
    /// simple commands run together (what `simplify` makes of `$(...)`).
    fn script_words(&mut self) -> String {
        let mut all_words = String::new();
        let mut words: Vec<Word> = Vec::new();
        let mut at_start = true;
        let mut in_case_pattern = false;
        let mut expect_for_list = false;
        let mut skip_cond = false;
        loop {
            let tok = self.next_token();
            match tok {
                Token::End => {
                    self.flush_command(&mut words, &mut all_words);
                    break;
                }
                Token::Op(op) => {
                    match op.as_str() {
                        ")" if in_case_pattern => {
                            in_case_pattern = false;
                            words.clear();
                            at_start = true;
                            continue;
                        }
                        ";;" | ";&" | ";;&" => {
                            self.flush_command(&mut words, &mut all_words);
                            in_case_pattern = true;
                            at_start = true;
                            continue;
                        }
                        _ => {}
                    }
                    if expect_for_list && matches!(op.as_str(), ";" | "\n") {
                        expect_for_list = false;
                        words.clear();
                        at_start = true;
                        continue;
                    }
                    self.flush_command(&mut words, &mut all_words);
                    at_start = true;
                    skip_cond = false;
                }
                Token::Word(w) => {
                    if in_case_pattern && w.literal().as_deref() == Some("esac") {
                        in_case_pattern = false;
                        at_start = true;
                        continue;
                    }
                    if in_case_pattern || expect_for_list {
                        continue;
                    }
                    if skip_cond {
                        if w.literal().as_deref() == Some("]]") {
                            skip_cond = false;
                        }
                        continue;
                    }
                    if at_start {
                        let lit = w.literal();
                        match lit.as_deref() {
                            Some("[[") => {
                                skip_cond = true;
                                continue;
                            }
                            Some("case") => {
                                // case WORD in
                                let _subject = self.next_token();
                                let _in = self.next_token();
                                in_case_pattern = true;
                                continue;
                            }
                            Some("esac") => {
                                at_start = true;
                                continue;
                            }
                            Some("for" | "select") => {
                                let _name = self.next_token();
                                // optional `in list...` up to ; or newline
                                expect_for_list = true;
                                continue;
                            }
                            Some("function") => {
                                let _name = self.next_token();
                                continue;
                            }
                            Some("in") => {
                                // `for x` followed by `in` on the next line
                                expect_for_list = true;
                                continue;
                            }
                            Some(r) if RESERVED.contains(&r) => continue,
                            _ => {}
                        }
                        if w.is_assignment() {
                            // prefix assignment: its inner commands still count
                            self.commands.extend(w.inner.iter().cloned());
                            self.pipes += w.inner_pipes;
                            all_words.push_str(&w.simplify());
                            continue;
                        }
                        // function definition: name ( )
                        let save = self.pos;
                        let save_pipes = self.pipes;
                        if let Token::Op(o) = self.next_token()
                            && o == "("
                            && let Token::Op(c) = self.next_token()
                            && c == ")"
                        {
                            continue;
                        }
                        self.pos = save;
                        self.pipes = save_pipes;
                        at_start = false;
                    }
                    words.push(w);
                }
            }
        }
        all_words
    }

    fn flush_command(&mut self, words: &mut Vec<Word>, all_words: &mut String) {
        if words.is_empty() {
            return;
        }
        let taken = std::mem::take(words);
        for w in &taken {
            all_words.push_str(&w.simplify());
        }
        let name = taken[0].literal();
        let name = match name.as_deref() {
            Some(n) if (n.ends_with("busybox") || n == "builtin") && taken.len() > 1 => {
                taken[1].literal()
            }
            other => other.map(str::to_string),
        };
        let mut inner = Vec::new();
        let mut inner_pipes = 0;
        for w in &taken {
            inner.extend(w.inner.iter().cloned());
            inner_pipes += w.inner_pipes;
        }
        if let Some(name) = name {
            let arguments: Vec<CmdPart> = taken[1..]
                .iter()
                .map(|w| {
                    self.next_id += 1;
                    CmdPart {
                        arg: w.simplify(),
                        id: self.next_id,
                    }
                })
                .collect();
            let flags = flags_of(&arguments);
            let command = Command {
                name,
                arguments,
                flags,
            };
            // ShellCheck wraps every simple command in a redirection node,
            // and hadolint's tree walk yields the command once for the
            // wrapper and once for the command itself; the rules see each
            // command twice, and DL3059's "more than 2" relies on that.
            self.commands.push(command.clone());
            self.commands.push(command);
        }
        self.commands.extend(inner);
        self.pipes += inner_pipes;
    }
}