aboutsummaryrefslogtreecommitdiffstats
path: root/src/parse/mod.rs
blob: 8fa2e231373d466da93526a356acf1114c662397 (plain) (blame)
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
use crate::BString;

#[cfg(test)]
mod test;

pub trait Stage: PartialEq {
    type Str: std::fmt::Debug + Clone + PartialEq;
}

pub trait CmdDisplay {
    fn cdisplay(&self, w: &mut dyn std::io::Write) -> std::io::Result<()>;
}

#[derive(Debug, Clone, PartialEq)]
pub struct PreExpansion;
#[derive(Debug, Clone, PartialEq)]
pub struct PostExpansion;

impl Stage for PreExpansion {
    type Str = ExpString;
}

impl Stage for PostExpansion {
    type Str = BString;
}

type Res<T, E> = std::result::Result<T, E>;

pub trait Expander {
    type Error;
    fn expand_var(&mut self, v: BString) -> Res<BString, Self::Error>;
    fn expand_cmd(&mut self, c: Ast<PostExpansion>) -> Res<BString, Self::Error>;
}

#[derive(Debug, Clone, PartialEq)]
pub enum Ast<T: Stage> {
    FunDecl(FunDecl<T>),
    VarAssign(VarAssign<T>),
    Pipes(Pipes<T>),
}

pub fn decl(name: ExpString, body: Ast<PreExpansion>) -> Ast<PreExpansion> {
    Ast::FunDecl(FunDecl {
        name: name,
        body: FunBody {
            body: Box::new(body),
        },
    })
}

pub fn assign(var: ExpString, val: ExpString) -> Ast<PreExpansion> {
    Ast::VarAssign(VarAssign { var, val })
}

pub fn pipes<const N: usize>(cmds: [Command<PreExpansion>; N]) -> Ast<PreExpansion> {
    Ast::Pipes(Pipes {
        cmds: cmds.to_vec(),
    })
}

pub fn estr(x: &[u8]) -> ExpString {
    ExpString {
        parts: vec![StringPart::Boring(x.to_vec())],
    }
}

pub fn str<const N: usize>(parts: [StringPart; N]) -> ExpString {
    ExpString {
        parts: parts.to_vec(),
    }
}

pub fn plain(x: &[u8]) -> StringPart {
    StringPart::Boring(x.to_vec())
}

pub fn var(x: &[u8]) -> StringPart {
    StringPart::Var(VarName { name: x.to_vec() })
}

pub fn cmdp(x: Ast<PreExpansion>) -> StringPart {
    StringPart::Cmd(x)
}

pub fn cmd<const N: usize>(x: [ExpString; N]) -> Command<PreExpansion> {
    Command {
        cmd: x[0].clone(),
        args: x[1..].to_vec(),
    }
}

impl CmdDisplay for Ast<PreExpansion> {
    fn cdisplay(&self, w: &mut dyn std::io::Write) -> std::io::Result<()> {
        match self {
            Ast::FunDecl(fun_decl) => {
                write!(w, "decl(")?;
                fun_decl.name.cdisplay(w)?;
                write!(w, ", ")?;
                fun_decl.body.body.cdisplay(w)?;
                write!(w, ")")?;
            }
            Ast::VarAssign(var_assign) => {
                write!(w, "assign(")?;
                var_assign.var.cdisplay(w)?;
                write!(w, ", ")?;
                var_assign.val.cdisplay(w)?;
                write!(w, ")")?;
            }
            Ast::Pipes(pipes) => {
                write!(w, "pipes([")?;
                for cmd in pipes.cmds.iter() {
                    cmd.cdisplay(w)?;
                    write!(w, ",")?;
                }
                write!(w, "])")?;
            }
        }
        Ok(())
    }
}

impl CmdDisplay for ExpString {
    fn cdisplay(&self, w: &mut dyn std::io::Write) -> std::io::Result<()> {
        if self.parts.len() == 1 && self.parts[0].is_boring() {
            write!(
                w,
                "estr(b\"{}\")",
                self.parts[0].clone().unwrap_boring().escape_ascii()
            )
        } else {
            write!(w, "str([")?;
            let mut first = true;
            for part in self.parts.iter() {
                if !first {
                    write!(w, ",")?;
                }
                first = false;
                part.cdisplay(w)?;
            }
            write!(w, "])")
        }
    }
}

impl CmdDisplay for StringPart {
    fn cdisplay(&self, w: &mut dyn std::io::Write) -> std::io::Result<()> {
        match self {
            StringPart::Boring(items) => {
                write!(w, "plain(")?;
                items.as_slice().cdisplay(w)?;
                write!(w, ")")
            }
            StringPart::Var(var_name) => {
                write!(w, "var(")?;
                var_name.name.as_slice().cdisplay(w)?;
                write!(w, ")")
            }
            StringPart::Cmd(ast) => {
                write!(w, "cmdp(")?;
                ast.cdisplay(w)?;
                write!(w, ")")
            }
        }
    }
}

impl CmdDisplay for Command<PreExpansion> {
    fn cdisplay(&self, w: &mut dyn std::io::Write) -> std::io::Result<()> {
        write!(w, "cmd([")?;
        self.cmd.cdisplay(w)?;
        for arg in self.args.iter() {
            write!(w, ", ")?;
            arg.cdisplay(w)?;
        }
        write!(w, "])")
    }
}

impl CmdDisplay for &[u8] {
    fn cdisplay(&self, w: &mut dyn std::io::Write) -> std::io::Result<()> {
        write!(w, "b\"")?;
        write!(w, "{}", self.escape_ascii())?;
        write!(w, "\"")
    }
}

impl Ast<PreExpansion> {
    pub fn expand<E: Expander>(self, e: &mut E) -> Res<Ast<PostExpansion>, E::Error> {
        match self {
            Ast::VarAssign(va) => Ok(Ast::VarAssign(va.expand(e)?)),
            Ast::Pipes(pipes) => Ok(Ast::Pipes(pipes.expand(e)?)),
            Ast::FunDecl(fd) => Ok(Ast::FunDecl(fd.expand(e)?)),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct FunBody {
    pub body: Box<Ast<PreExpansion>>,
}

impl Parse for FunBody {
    fn parse(b: &mut Cursor<'_>) -> Result<Self> {
        b.spaces();

        if b.is_empty() {
            return Err(ParseError::Eof);
        }

        if b.peek() != b'{' {
            return Err(ParseError::Expected('{'));
        }

        b.adv();
        let body = Box::new(Ast::parse(b)?);
        if b.is_empty() {
            if b.is_completion() {
                Ok(Self { body })
            } else {
                Err(ParseError::Eof)
            }
        } else if b.peek() == b'}' {
            Ok(Self { body })
        } else {
            Err(ParseError::Expected('}'))
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct FunDecl<S: Stage> {
    pub name: S::Str,
    pub body: FunBody,
}

impl Parse for FunDecl<PreExpansion> {
    fn parse(b: &mut Cursor<'_>) -> Result<Self> {
        if !b.buf.starts_with(b"fun ") && !b.buf.starts_with(b"fun\t") {
            return Err(ParseError::NotAFunDecl);
        }
        b.advance(4);
        b.spaces();
        let name = ExpString::parse(b)?;
        let body = FunBody::parse(b)?;
        Ok(Self { name, body })
    }
}

impl FunDecl<PreExpansion> {
    fn expand<E: Expander>(self, e: &mut E) -> Res<FunDecl<PostExpansion>, E::Error> {
        Ok(FunDecl {
            name: self.name.expand(e)?,
            body: self.body,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct VarAssign<S: Stage> {
    pub var: S::Str,
    pub val: S::Str,
}

impl Parse for VarAssign<PreExpansion> {
    fn parse(b: &mut Cursor<'_>) -> Result<Self> {
        if !b.buf.starts_with(b"set ") && !b.buf.starts_with(b"set\t") {
            return Err(ParseError::NotAVarAssign);
        }
        b.advance(4);
        b.spaces();
        let var = ExpString::parse(b)?;
        b.spaces();

        if b.is_empty() {
            return Err(ParseError::Eof);
        }
        let eq = b.adv();
        if eq != b'=' {
            return Err(ParseError::Expected('='));
        }
        let val = ExpString::parse(b)?;

        Ok(Self { var, val })
    }
}

impl VarAssign<PreExpansion> {
    fn expand<E: Expander>(self, e: &mut E) -> Res<VarAssign<PostExpansion>, E::Error> {
        Ok(VarAssign {
            var: self.var.expand(e)?,
            val: self.val.expand(e)?,
        })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Pipes<T: Stage> {
    pub cmds: Vec<Command<T>>,
}

impl Pipes<PreExpansion> {
    fn expand<E: Expander>(self, e: &mut E) -> Res<Pipes<PostExpansion>, E::Error> {
        let mut cmds = Vec::with_capacity(self.cmds.len());
        for cmd in self.cmds.into_iter() {
            cmds.push(cmd.expand(e)?);
        }
        Ok(Pipes { cmds })
    }
}

#[derive(Debug, Clone, PartialEq)]
pub enum StringPart {
    Boring(BString),
    Var(VarName),
    Cmd(Ast<PreExpansion>),
}

impl StringPart {
    pub fn is_boring(&self) -> bool {
        matches!(self, StringPart::Boring(..))
    }
    pub fn unwrap_boring(self) -> BString {
        match self {
            StringPart::Boring(items) => items,
            _ => panic!("unwrap on non-boring value"),
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
/// `"hi ${var} $(cmd) "` gets mapped to `[Boring("hi "), Var("var"), String(" "), Cmd(...), Boring(" ")]`
pub struct ExpString {
    parts: Vec<StringPart>,
}

impl ExpString {
    fn expand<E: Expander>(self, e: &mut E) -> Res<BString, E::Error> {
        let mut out = BString::new();
        for part in self.parts.into_iter() {
            let mut x = match part {
                StringPart::Boring(items) => items,
                StringPart::Var(v) => e.expand_var(v.name)?,
                StringPart::Cmd(ast) => {
                    let exp = ast.expand(e)?;
                    e.expand_cmd(exp)?
                }
            };
            out.append(&mut x);
        }
        Ok(out)
    }
}

fn is_symbol(x: u8) -> bool {
    match x {
        b';' | b'|' | b'{' | b'}' | b'$' | b'(' | b')' | b'\'' | b'"' => true,
        _ => false,
    }
}

fn is_var_begin(x: u8) -> bool {
    x.is_ascii_alphanumeric()
}
fn is_var_name(x: u8) -> bool {
    x.is_ascii_alphanumeric() || x == b'_'
}

#[derive(Debug, Clone, PartialEq)]
pub struct VarName {
    name: BString,
}

impl Parse for VarName {
    fn parse(b: &mut Cursor<'_>) -> Result<Self> {
        if b.is_empty() {
            return Err(ParseError::Eof);
        }

        let mut name = BString::new();

        if b.peek().is_ascii_digit() {
            while b.has() && b.peek().is_ascii_digit() {
                name.push(b.adv());
            }
            return Ok(Self { name });
        }

        if !is_var_begin(b.peek()) {
            return Err(ParseError::ExpectedAlphabetic);
        }

        while b.has() {
            let x = b.peek();
            if is_var_name(x) {
                b.adv();
                name.push(x)
            } else {
                break;
            }
        }

        Ok(Self { name })
    }
}

impl Parse for ExpString {
    fn parse(b: &mut Cursor<'_>) -> Result<Self> {
        b.spaces();
        if b.is_empty() {
            return Err(ParseError::NotAString);
        }

        let mut parts = Vec::new();
        let p = &mut parts;
        let mut escaping = false;
        let add_char = |p: &mut Vec<StringPart>, x: u8| match p.last_mut() {
            Some(StringPart::Boring(v)) => v.push(x),
            _ => p.push(StringPart::Boring(vec![x])),
        };
        let mut already_parsed = false;

        'cont: while b.has() {
            let mut delim = b.peek();
            if delim == b'\'' || delim == b'"' {
                b.adv();
            } else if is_symbol(delim) && delim != b'$' {
                return if already_parsed {
                    Ok(Self { parts })
                } else {
                    Err(ParseError::NotAString)
                };
            } else {
                delim = b' ';
            }

            already_parsed = false;

            while b.has() {
                let x = b.peek();

                if escaping {
                    add_char(p, x);
                    escaping = false;
                    b.adv();
                    continue;
                }

                if delim == b' ' && (x.is_ascii_whitespace() || (is_symbol(x) && x != b'$')) {
                    if x == b'\'' || x == b'"' {
                        break;
                    } else {
                        return Ok(Self { parts });
                    }
                }

                if x == delim {
                    b.adv();
                    already_parsed = true;
                    continue 'cont;
                }

                b.adv();

                if delim == b'\'' {
                    // no fancy stuff here
                    add_char(p, x);
                    continue;
                }

                if x == b'\\' {
                    escaping = true;
                    continue;
                }

                if x == b'$' {
                    if !b.has() {
                        add_char(p, x);
                        continue;
                    }

                    let x = b.peek();

                    if x == b'?' || x == b'!' {
                        b.adv();
                        p.push(StringPart::Var(VarName { name: vec![x] }))
                    } else if is_var_begin(x) {
                        let v = VarName::parse(b)?;
                        p.push(StringPart::Var(v));
                    } else if x == b'{' {
                        b.adv();
                        let v = VarName::parse(b)?;

                        if !b.has() {
                            return Err(ParseError::Eof);
                        } else if b.peek() == b':' {
                            todo!(": in var expansion")
                        }

                        if !b.has() {
                            return Err(ParseError::Eof);
                        } else if b.peek() != b'}' {
                            return Err(ParseError::Incomplete);
                        }

                        b.adv();
                        p.push(StringPart::Var(v));
                    } else if x == b'(' {
                        b.adv();
                        let cmd = Ast::parse(b)?;
                        b.spaces();
                        if b.is_empty() {
                            return Err(ParseError::Eof);
                        } else if b.peek() == b')' {
                            b.adv();
                            p.push(StringPart::Cmd(cmd));
                        } else {
                            return Err(ParseError::Expected(')'));
                        }
                    } else {
                        // doesn't seem to be a variable or expansion, just add $ back into the string
                        add_char(p, b'$');
                        continue;
                    }

                    if delim == b' ' {
                        already_parsed = true;
                    }
                    continue;
                }

                add_char(p, x);

                if delim == b' ' {
                    already_parsed = true;
                }
            }

            if b.has() && b"\"'".contains(&b.peek()) {
                continue;
            }

            break;
        }

        if b.is_completion() || already_parsed {
            Ok(Self { parts })
        } else {
            Err(ParseError::Eof)
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct Command<T: Stage> {
    pub cmd: T::Str,
    pub args: Vec<T::Str>,
}

impl Command<PreExpansion> {
    fn expand<E: Expander>(self, e: &mut E) -> Res<Command<PostExpansion>, E::Error> {
        let cmd = self.cmd.expand(e)?;
        let mut args = Vec::with_capacity(self.args.len());
        for arg in self.args.into_iter() {
            args.push(arg.expand(e)?);
        }
        Ok(Command { cmd, args })
    }
}

#[allow(unused)]
#[derive(Debug)]
pub enum ParseError {
    /// "clean" EOF, i.e. not in the middle of something
    Eof,

    /// "unclean" EOF, i.e. EOF after beginning a quoted string
    Incomplete,

    ExpectedAlphabetic,

    Unknown(u8),

    Expected(char),

    NotAString,

    NotAFunDecl,

    NotAVarAssign,
}

type Result<T> = std::result::Result<T, ParseError>;

pub fn do_parse(x: &[u8]) -> Res<Ast<PreExpansion>, (ParseError, &[u8])> {
    let mut c = Cursor::new(x, ParseMode::Command);
    match Ast::parse(&mut c) {
        Ok(ast) => Ok(ast),
        Err(e) => Err((e, c.buf)),
    }
}

pub enum CompletionKind {
    Command,
    Argument,
    None,
}

pub struct CompletionContext {
    pub kind: CompletionKind,
    pub partial: BString,
}

impl CompletionContext {
    pub fn none() -> Self {
        Self {
            kind: CompletionKind::None,
            partial: BString::new(),
        }
    }
}

fn expstr_cc(s: &ExpString, kind: CompletionKind) -> CompletionContext {
    if s.parts.len() > 1 || !s.parts[0].is_boring() {
        CompletionContext::none()
    } else {
        CompletionContext {
            kind,
            partial: s.parts[0].clone().unwrap_boring().clone(),
        }
    }
}

pub fn completion_context<'a>(x: &'a [u8]) -> CompletionContext {
    let mut cursor = Cursor::new(x, ParseMode::Completion);
    let ast = Ast::parse(&mut cursor);
    match ast {
        Ok(Ast::Pipes(pipes)) if cursor.spaced == false => {
            if let Some(cmd) = pipes.cmds.last() {
                if cmd.args.is_empty() {
                    expstr_cc(&cmd.cmd, CompletionKind::Command)
                } else {
                    expstr_cc(&cmd.args[cmd.args.len() - 1], CompletionKind::Argument)
                }
            } else {
                CompletionContext::none()
            }
        }
        _ => CompletionContext::none(),
    }
}

trait Parse: Sized {
    fn parse(b: &mut Cursor<'_>) -> Result<Self>;
}

enum ParseMode {
    Command,
    Completion,
}

struct Cursor<'a> {
    buf: &'a [u8],
    mode: ParseMode,

    /// if the last byte that was consumed was whitespace or part of a word
    spaced: bool,

    backtrace: bool,
}

impl<'a> Cursor<'a> {
    fn new(buf: &'a [u8], mode: ParseMode) -> Self {
        Self {
            buf,
            mode,
            spaced: false,
            backtrace: false,
        }
    }

    // non empty
    fn has(&self) -> bool {
        !self.buf.is_empty()
    }

    fn is_empty(&self) -> bool {
        self.buf.is_empty()
    }

    fn bt(&self, word: &str) {
        if self.backtrace {
            let bt = std::backtrace::Backtrace::capture();
            let bt = format!("{bt}");
            println!("{word} {}\r", self.buf[0] as char);
            for l in bt.lines().skip(4).take(2) {
                println!("{l}\r");
            }
            println!("\r");
        }
    }

    fn peek(&self) -> u8 {
        self.bt("peek");
        self.buf[0]
    }

    fn adv(&mut self) -> u8 {
        self.bt("adv");
        let out = self.buf[0];
        self.buf = &self.buf[1..];
        self.spaced = false;
        out
    }

    fn advance(&mut self, amt: usize) -> &[u8] {
        self.bt(&format!("adv({amt})"));
        let out = &self.buf[..amt];
        self.buf = &self.buf[amt..];
        self.spaced = false;
        out
    }

    fn peek_space(&self) -> bool {
        if self.buf.is_empty() {
            return false;
        }
        matches!(self.buf[0], b' ' | b'\t' | b'\n' | b'\r')
    }

    fn spaces(&mut self) {
        while self.peek_space() {
            self.adv();
            self.spaced = true;
        }
    }

    fn is_completion(&self) -> bool {
        match self.mode {
            ParseMode::Completion => true,
            _ => false,
        }
    }

    fn parse<T: Parse>(&mut self) -> Result<T> {
        T::parse(self)
    }
}

impl Parse for Ast<PreExpansion> {
    fn parse(b: &mut Cursor<'_>) -> Result<Self> {
        b.spaces();

        let orig_len = b.buf.len();
        let x = VarAssign::parse(b);
        if let Ok(va) = x {
            return Ok(Self::VarAssign(va));
        } else if b.buf.len() != orig_len {
            x?;
        }

        let orig_len = b.buf.len();
        let x = FunDecl::parse(b);
        if let Ok(fd) = x {
            return Ok(Self::FunDecl(fd));
        } else if b.buf.len() != orig_len {
            x?;
        }

        Ok(Self::Pipes(b.parse()?))
    }
}

impl Parse for Command<PreExpansion> {
    fn parse(b: &mut Cursor<'_>) -> Result<Self> {
        let path: ExpString = b.parse()?;
        let mut args = Vec::new();
        loop {
            match ExpString::parse(b) {
                Ok(arg) => args.push(arg),
                Err(ParseError::NotAString) => break,
                Err(e) => Err(e)?,
            }
        }
        let x = Ok(Self { cmd: path, args });
        x
    }
}

impl Parse for Pipes<PreExpansion> {
    fn parse(b: &mut Cursor<'_>) -> Result<Self> {
        let mut cmds: Vec<Command<PreExpansion>> = vec![b.parse()?];

        loop {
            b.spaces();
            if b.is_empty() {
                return Ok(Pipes { cmds });
            }

            let c = b.peek();
            if c == b'|' {
                b.adv();
                cmds.push(b.parse()?);
            } else if is_symbol(c) {
                return Ok(Pipes { cmds });
            } else {
                Err(ParseError::Unknown(c))?;
            }
        }
    }
}