reef-shell 0.3.0

Bash compatibility layer for fish shell
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
//! Abstract syntax tree for bash commands.
//!
//! All AST nodes borrow from the input string (`&'a str`) — zero-copy.
//! The parser produces a `Vec<Cmd<'a>>` representing the top-level command list.

use std::borrow::Cow;

// ---------------------------------------------------------------------------
// Commands
// ---------------------------------------------------------------------------

/// A complete command — foreground or background.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Cmd<'a> {
    /// A foreground command list.
    List(AndOrList<'a>),
    /// A background job (`cmd &`).
    Job(AndOrList<'a>),
}

/// A chain of commands connected by `&&` and `||`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AndOrList<'a> {
    /// The first pipeline in the chain.
    pub first: Pipeline<'a>,
    /// Subsequent `&&` / `||` pipelines.
    pub rest: Vec<AndOr<'a>>,
}

/// A single `&&` or `||` link in an and-or chain.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum AndOr<'a> {
    /// `&&` — run if the previous succeeded.
    And(Pipeline<'a>),
    /// `||` — run if the previous failed.
    Or(Pipeline<'a>),
}

/// A pipeline: one or more commands connected by `|`.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Pipeline<'a> {
    /// A single command (no pipe).
    Single(Executable<'a>),
    /// `[!] cmd1 | cmd2 | ...` — bool is true if negated.
    Pipe(bool, Vec<Executable<'a>>),
}

/// An executable unit: simple command, compound command, or function definition.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Executable<'a> {
    /// A simple command (possibly with assignments and redirections).
    Simple(SimpleCmd<'a>),
    /// A compound command (`if`, `for`, `while`, etc.).
    Compound(CompoundCmd<'a>),
    /// A function definition: `name() { body; }`.
    FuncDef(&'a str, CompoundCmd<'a>),
}

// ---------------------------------------------------------------------------
// Simple command
// ---------------------------------------------------------------------------

/// A simple command with optional prefix assignments and suffix words/redirects.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SimpleCmd<'a> {
    /// Assignments and redirections before the command name.
    pub prefix: Vec<CmdPrefix<'a>>,
    /// Arguments and redirections after the command name.
    pub suffix: Vec<CmdSuffix<'a>>,
}

/// A prefix element: variable assignment or redirection.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CmdPrefix<'a> {
    /// `NAME=value` — scalar assignment.
    Assign(&'a str, Option<Word<'a>>),
    /// `arr=(word ...)` — array assignment.
    ArrayAssign(&'a str, Vec<Word<'a>>),
    /// `arr+=(word ...)` — array append.
    ArrayAppend(&'a str, Vec<Word<'a>>),
    /// An I/O redirection.
    Redirect(Redir<'a>),
}

/// A suffix element: argument word or redirection.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CmdSuffix<'a> {
    /// A regular argument word.
    Word(Word<'a>),
    /// An I/O redirection.
    Redirect(Redir<'a>),
}

// ---------------------------------------------------------------------------
// Compound commands
// ---------------------------------------------------------------------------

/// A compound command with optional trailing redirections.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompoundCmd<'a> {
    /// The compound command body.
    pub kind: CompoundKind<'a>,
    /// Redirections applied to the entire compound command.
    pub redirects: Vec<Redir<'a>>,
}

/// The body of a compound command.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum CompoundKind<'a> {
    /// `for var [in words]; do body; done`
    For {
        /// Loop variable name.
        var: &'a str,
        /// Word list (None = `"$@"`).
        words: Option<Vec<Word<'a>>>,
        /// Loop body commands.
        body: Vec<Cmd<'a>>,
    },
    /// `while guard; do body; done`
    While(GuardBody<'a>),
    /// `until guard; do body; done`
    Until(GuardBody<'a>),
    /// `if cond; then body; [elif ...;] [else ...;] fi`
    If {
        /// Condition–body pairs (first is `if`, rest are `elif`).
        conditionals: Vec<GuardBody<'a>>,
        /// Optional `else` branch.
        else_branch: Option<Vec<Cmd<'a>>>,
    },
    /// `case word in pattern) body;; ... esac`
    Case {
        /// The word being matched.
        word: Word<'a>,
        /// Pattern–body arms.
        arms: Vec<CaseArm<'a>>,
    },
    /// C-style for loop: `for (( init; cond; step )); do body; done`
    CFor {
        /// Initialization expression.
        init: Option<Arith<'a>>,
        /// Condition expression.
        cond: Option<Arith<'a>>,
        /// Step expression.
        step: Option<Arith<'a>>,
        /// Loop body commands.
        body: Vec<Cmd<'a>>,
    },
    /// `{ body; }` — brace group.
    Brace(Vec<Cmd<'a>>),
    /// `( body )` — subshell.
    Subshell(Vec<Cmd<'a>>),
    /// `[[ expression ]]` — extended test command.
    DoubleBracket(Vec<Cmd<'a>>),
    /// `(( expression ))` — arithmetic command.
    Arithmetic(Arith<'a>),
}

/// A guard (condition) and body pair, used by `while`, `until`, and `if`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GuardBody<'a> {
    /// The condition commands.
    pub guard: Vec<Cmd<'a>>,
    /// The body commands.
    pub body: Vec<Cmd<'a>>,
}

/// A single arm in a `case` statement.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CaseArm<'a> {
    /// Patterns to match against (separated by `|`).
    pub patterns: Vec<Word<'a>>,
    /// Commands to execute if a pattern matches.
    pub body: Vec<Cmd<'a>>,
}

// ---------------------------------------------------------------------------
// Words
// ---------------------------------------------------------------------------

/// A shell word: either a single part or a concatenation of parts.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Word<'a> {
    /// A word consisting of a single part.
    Simple(WordPart<'a>),
    /// A word formed by concatenating multiple parts (e.g., `"hello"$var`).
    Concat(Vec<WordPart<'a>>),
}

/// A fragment of a word: bare text, quoted text, or a substitution.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum WordPart<'a> {
    /// Unquoted content.
    Bare(Atom<'a>),
    /// Double-quoted content (may contain expansions).
    DQuoted(Vec<Atom<'a>>),
    /// Single-quoted content (literal text, no expansions).
    SQuoted(&'a str),
}

/// An atomic element within a word: literal text, expansion, or glob.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Atom<'a> {
    /// Literal text.
    Lit(&'a str),
    /// Backslash-escaped character.
    Escaped(Cow<'a, str>),
    /// Parameter reference (`$var`, `$1`, `$@`, etc.).
    Param(Param<'a>),
    /// Substitution (`$(cmd)`, `${var...}`, `$((expr))`).
    Subst(Box<Subst<'a>>),
    /// `*` glob wildcard.
    Star,
    /// `?` glob wildcard.
    Question,
    /// `[` glob bracket open.
    SquareOpen,
    /// `]` glob bracket close.
    SquareClose,
    /// `~` tilde expansion.
    Tilde,
    /// `<(cmd)` — process substitution (input).
    ProcSubIn(Vec<Cmd<'a>>),
    /// ANSI-C `$'...'` — raw content between the quotes (escape sequences unresolved).
    AnsiCQuoted(&'a str),
    /// Brace range expansion: `{start..end[..step]}`.
    BraceRange {
        /// Range start value.
        start: &'a str,
        /// Range end value.
        end: &'a str,
        /// Optional step value.
        step: Option<&'a str>,
    },
}

// ---------------------------------------------------------------------------
// Parameters
// ---------------------------------------------------------------------------

/// A shell parameter reference.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Param<'a> {
    /// Named variable (`$var`).
    Var(&'a str),
    /// Positional parameter (`$1`, `$2`, ...).
    Positional(u32),
    /// `$@` — all positional parameters (separate words).
    At,
    /// `$*` — all positional parameters (single word).
    Star,
    /// `$#` — number of positional parameters.
    Pound,
    /// `$?` — exit status of last command.
    Status,
    /// `$$` — process ID.
    Pid,
    /// `$!` — PID of last background process.
    Bang,
    /// `$-` — current shell option flags.
    Dash,
}

// ---------------------------------------------------------------------------
// Substitutions
// ---------------------------------------------------------------------------

/// A substitution or parameter expansion.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Subst<'a> {
    /// Command substitution: `$(cmd)` or `` `cmd` ``.
    Cmd(Vec<Cmd<'a>>),
    /// Arithmetic expansion: `$((expr))`.
    Arith(Option<Arith<'a>>),
    /// String length: `${#var}`.
    Len(Param<'a>),
    /// `${!var}` — indirect variable expansion.
    Indirect(&'a str),
    /// `${!prefix*}` / `${!prefix@}` — list variables matching prefix.
    PrefixList(&'a str),
    /// `${var@Q}` — parameter transformation (quoting).
    Transform(&'a str, u8),
    /// `${var:-word}` / `${var-word}` — default value.
    Default(Param<'a>, Option<Word<'a>>),
    /// `${var:=word}` / `${var=word}` — assign default.
    Assign(Param<'a>, Option<Word<'a>>),
    /// `${var:?word}` / `${var?word}` — error if unset.
    Error(Param<'a>, Option<Word<'a>>),
    /// `${var:+word}` / `${var+word}` — alternate value.
    Alt(Param<'a>, Option<Word<'a>>),
    /// `${var%pattern}` — remove shortest suffix match.
    TrimSuffixSmall(Param<'a>, Option<Word<'a>>),
    /// `${var%%pattern}` — remove longest suffix match.
    TrimSuffixLarge(Param<'a>, Option<Word<'a>>),
    /// `${var#pattern}` — remove shortest prefix match.
    TrimPrefixSmall(Param<'a>, Option<Word<'a>>),
    /// `${var##pattern}` — remove longest prefix match.
    TrimPrefixLarge(Param<'a>, Option<Word<'a>>),
    /// `${var/pattern/replacement}` — replace first match.
    Replace(Param<'a>, Option<Word<'a>>, Option<Word<'a>>),
    /// `${var//pattern/replacement}` — replace all matches.
    ReplaceAll(Param<'a>, Option<Word<'a>>, Option<Word<'a>>),
    /// `${var/#pattern/replacement}` — replace prefix match.
    ReplacePrefix(Param<'a>, Option<Word<'a>>, Option<Word<'a>>),
    /// `${var/%pattern/replacement}` — replace suffix match.
    ReplaceSuffix(Param<'a>, Option<Word<'a>>, Option<Word<'a>>),
    /// `${var:offset:length}` — substring extraction.
    Substring(Param<'a>, &'a str, Option<&'a str>),
    /// `${var^}` / `${var^^}` — uppercase (bool: all if true).
    Upper(bool, Param<'a>),
    /// `${var,}` / `${var,,}` — lowercase (bool: all if true).
    Lower(bool, Param<'a>),
    /// `${arr[index]}` — array element access (index is a Word for $((expr)) support).
    ArrayElement(&'a str, Word<'a>),
    /// `${arr[@]}` or `${arr[*]}` — all array elements.
    ArrayAll(&'a str),
    /// `${#arr[@]}` — array length.
    ArrayLen(&'a str),
    /// `${arr[@]:offset:length}` — array slice.
    ArraySlice(&'a str, &'a str, Option<&'a str>),
}

// ---------------------------------------------------------------------------
// Arithmetic
// ---------------------------------------------------------------------------

/// An arithmetic expression node (used in `$(( ))`, `(( ))`, and C-style for).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Arith<'a> {
    /// Variable reference.
    Var(&'a str),
    /// Integer literal.
    Lit(i64),

    /// Addition.
    Add(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Subtraction.
    Sub(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Multiplication.
    Mul(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Division.
    Div(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Modulo.
    Rem(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Exponentiation.
    Pow(Box<Arith<'a>>, Box<Arith<'a>>),

    /// Less than.
    Lt(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Less than or equal.
    Le(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Greater than.
    Gt(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Greater than or equal.
    Ge(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Equal.
    Eq(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Not equal.
    Ne(Box<Arith<'a>>, Box<Arith<'a>>),

    /// Bitwise AND.
    BitAnd(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Bitwise OR.
    BitOr(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Bitwise XOR.
    BitXor(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Logical AND.
    LogAnd(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Logical OR.
    LogOr(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Left shift.
    Shl(Box<Arith<'a>>, Box<Arith<'a>>),
    /// Right shift.
    Shr(Box<Arith<'a>>, Box<Arith<'a>>),

    /// Unary plus.
    Pos(Box<Arith<'a>>),
    /// Unary minus.
    Neg(Box<Arith<'a>>),
    /// Logical NOT.
    LogNot(Box<Arith<'a>>),
    /// Bitwise NOT.
    BitNot(Box<Arith<'a>>),

    /// Pre-increment (`++var`).
    PreInc(&'a str),
    /// Post-increment (`var++`).
    PostInc(&'a str),
    /// Pre-decrement (`--var`).
    PreDec(&'a str),
    /// Post-decrement (`var--`).
    PostDec(&'a str),

    /// Ternary operator (`cond ? then : else`).
    Ternary(Box<Arith<'a>>, Box<Arith<'a>>, Box<Arith<'a>>),
    /// Assignment (`var = expr`).
    Assign(&'a str, Box<Arith<'a>>),
}

// ---------------------------------------------------------------------------
// Heredoc body
// ---------------------------------------------------------------------------

/// The body of a heredoc (here-document).
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum HeredocBody<'a> {
    /// Quoted delimiter — no expansion (literal text).
    Literal(&'a str),
    /// Unquoted delimiter — variable and command expansion.
    Interpolated(Vec<Atom<'a>>),
}

// ---------------------------------------------------------------------------
// Redirects
// ---------------------------------------------------------------------------

/// An I/O redirection.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Redir<'a> {
    /// `[n]< word` — read from file.
    Read(Option<u16>, Word<'a>),
    /// `[n]> word` — write to file.
    Write(Option<u16>, Word<'a>),
    /// `[n]>> word` — append to file.
    Append(Option<u16>, Word<'a>),
    /// `[n]<> word` — open for reading and writing.
    ReadWrite(Option<u16>, Word<'a>),
    /// `[n]>| word` — write, overriding noclobber.
    Clobber(Option<u16>, Word<'a>),
    /// `[n]<& word` — duplicate input fd.
    DupRead(Option<u16>, Word<'a>),
    /// `[n]>& word` — duplicate output fd.
    DupWrite(Option<u16>, Word<'a>),
    /// `<<< word` — here-string.
    HereString(Word<'a>),
    /// `<< [-]DELIM ... DELIM` — here-document.
    Heredoc(HeredocBody<'a>),
    /// `&> word` — redirect both stdout and stderr.
    WriteAll(Word<'a>),
    /// `&>> word` — append both stdout and stderr.
    AppendAll(Word<'a>),
}