shuck-parser 0.0.18

A fast, safe bash parser library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
use super::*;
use shuck_ast::{
    AnonymousFunctionCommand as AstAnonymousFunctionCommand, ArithmeticAssignOp,
    ArithmeticBinaryOp, ArithmeticPostfixOp, ArithmeticUnaryOp, BackgroundOperator, BinaryCommand,
    BourneParameterExpansion, BuiltinCommand as AstBuiltinCommand, Command as AstCommand,
    CompoundCommand as AstCompoundCommand, ForSyntax, ForeachSyntax, FunctionDef as AstFunctionDef,
    HeredocBody, HeredocBodyMode, IfSyntax, Name, ParameterExpansion, ParameterExpansionSyntax,
    ParameterOp, PrefixMatchKind, RepeatSyntax, SimpleCommand as AstSimpleCommand, SourceText,
    StmtTerminator, ZshDefaultingOp, ZshExpansionOperation, ZshExpansionTarget, ZshPatternOp,
    ZshReplacementOp, ZshTrimOp,
};

fn is_fully_quoted(word: &Word) -> bool {
    word.is_fully_quoted()
}

fn heredoc_body_is_literal(body: &HeredocBody) -> bool {
    body.mode == HeredocBodyMode::Literal
}

fn pattern_part_slices<'a>(pattern: &'a Pattern, input: &'a str) -> Vec<&'a str> {
    pattern
        .parts
        .iter()
        .map(|part| part.span.slice(input))
        .collect()
}

fn top_level_part_slices<'a>(word: &'a Word, input: &'a str) -> Vec<&'a str> {
    word.parts
        .iter()
        .map(|part| part.span.slice(input))
        .collect()
}

fn heredoc_top_level_part_slices<'a>(body: &'a HeredocBody, input: &'a str) -> Vec<&'a str> {
    body.parts
        .iter()
        .map(|part| part.span.slice(input))
        .collect()
}

fn brace_slices<'a>(word: &'a Word, input: &'a str) -> Vec<&'a str> {
    word.brace_syntax
        .iter()
        .map(|brace| brace.span.slice(input))
        .collect()
}

fn redirect_word_target(redirect: &Redirect) -> &Word {
    redirect
        .word_target()
        .expect("expected non-heredoc redirect target")
}

fn redirect_heredoc(redirect: &Redirect) -> &Heredoc {
    redirect.heredoc().expect("expected heredoc redirect")
}

fn collect_file_comments(file: &File) -> Vec<Comment> {
    let mut comments = Vec::new();
    collect_stmt_seq_comments(&file.body, &mut comments);
    comments
}

fn collect_stmt_seq_comments(sequence: &StmtSeq, comments: &mut Vec<Comment>) {
    comments.extend(sequence.leading_comments.iter().copied());
    for stmt in &sequence.stmts {
        collect_stmt_comments(stmt, comments);
    }
    comments.extend(sequence.trailing_comments.iter().copied());
}

fn collect_stmt_comments(stmt: &Stmt, comments: &mut Vec<Comment>) {
    comments.extend(stmt.leading_comments.iter().copied());
    if let Some(comment) = stmt.inline_comment {
        comments.push(comment);
    }
    collect_command_comments(&stmt.command, comments);
}

fn collect_command_comments(command: &AstCommand, comments: &mut Vec<Comment>) {
    match command {
        AstCommand::Binary(command) => {
            collect_stmt_comments(&command.left, comments);
            collect_stmt_comments(&command.right, comments);
        }
        AstCommand::Compound(command) => collect_compound_comments(command, comments),
        AstCommand::Function(function) => collect_stmt_comments(&function.body, comments),
        AstCommand::AnonymousFunction(function) => collect_stmt_comments(&function.body, comments),
        AstCommand::Simple(_) | AstCommand::Builtin(_) | AstCommand::Decl(_) => {}
    }
}

fn collect_compound_comments(command: &AstCompoundCommand, comments: &mut Vec<Comment>) {
    match command {
        AstCompoundCommand::If(command) => {
            collect_stmt_seq_comments(&command.condition, comments);
            collect_stmt_seq_comments(&command.then_branch, comments);
            for branch in &command.elif_branches {
                collect_stmt_seq_comments(&branch.0, comments);
                collect_stmt_seq_comments(&branch.1, comments);
            }
            if let Some(body) = &command.else_branch {
                collect_stmt_seq_comments(body, comments);
            }
        }
        AstCompoundCommand::For(command) => {
            collect_stmt_seq_comments(&command.body, comments);
        }
        AstCompoundCommand::Select(command) => {
            collect_stmt_seq_comments(&command.body, comments);
        }
        AstCompoundCommand::ArithmeticFor(command) => {
            collect_stmt_seq_comments(&command.body, comments);
        }
        AstCompoundCommand::While(command) => {
            collect_stmt_seq_comments(&command.condition, comments);
            collect_stmt_seq_comments(&command.body, comments);
        }
        AstCompoundCommand::Until(command) => {
            collect_stmt_seq_comments(&command.condition, comments);
            collect_stmt_seq_comments(&command.body, comments);
        }
        AstCompoundCommand::Case(command) => {
            for item in &command.cases {
                collect_stmt_seq_comments(&item.body, comments);
            }
        }
        AstCompoundCommand::Subshell(body) | AstCompoundCommand::BraceGroup(body) => {
            collect_stmt_seq_comments(body, comments);
        }
        AstCompoundCommand::Always(command) => {
            collect_stmt_seq_comments(&command.body, comments);
            collect_stmt_seq_comments(&command.always_body, comments);
        }
        AstCompoundCommand::Repeat(command) => {
            collect_stmt_seq_comments(&command.body, comments);
        }
        AstCompoundCommand::Foreach(command) => {
            collect_stmt_seq_comments(&command.body, comments);
        }
        AstCompoundCommand::Conditional(_)
        | AstCompoundCommand::Arithmetic(_)
        | AstCompoundCommand::Time(_)
        | AstCompoundCommand::Coproc(_) => {}
    }
}

fn assert_comment_ranges_valid(source: &str, output: &ParseResult) {
    let comments = collect_file_comments(&output.file);
    for (i, comment) in comments.iter().enumerate() {
        let start = usize::from(comment.range.start());
        let end = usize::from(comment.range.end());
        assert!(
            end <= source.len(),
            "comment {i}: end ({end}) exceeds source length ({})",
            source.len()
        );
        assert!(
            source.is_char_boundary(start),
            "comment {i}: start ({start}) not on char boundary"
        );
        assert!(
            source.is_char_boundary(end),
            "comment {i}: end ({end}) not on char boundary"
        );
        let text = &source[start..end];
        assert!(
            text.starts_with('#'),
            "comment {i}: expected '#' at start, got {:?}",
            text.chars().next()
        );
        assert!(
            !text.contains('\n'),
            "comment {i}: spans multiple lines: {text:?}"
        );
    }
}

fn expect_function(stmt: &Stmt) -> &AstFunctionDef {
    let AstCommand::Function(function) = &stmt.command else {
        panic!("expected function definition");
    };
    function
}

fn expect_anonymous_function(stmt: &Stmt) -> &AstAnonymousFunctionCommand {
    let AstCommand::AnonymousFunction(function) = &stmt.command else {
        panic!("expected anonymous function");
    };
    function
}

fn expect_compound(stmt: &Stmt) -> (&AstCompoundCommand, &[Redirect]) {
    let AstCommand::Compound(compound) = &stmt.command else {
        panic!("expected compound command");
    };
    (compound, stmt.redirects.as_slice())
}

fn expect_variable(expr: &ArithmeticExprNode, expected: &str) {
    let ArithmeticExpr::Variable(name) = &expr.kind else {
        panic!("expected arithmetic variable, got {:?}", expr.kind);
    };
    assert_eq!(name, expected);
}

fn expect_number(expr: &ArithmeticExprNode, input: &str, expected: &str) {
    let ArithmeticExpr::Number(number) = &expr.kind else {
        panic!("expected arithmetic number, got {:?}", expr.kind);
    };
    assert_eq!(number.slice(input), expected);
}

fn expect_shell_word(expr: &ArithmeticExprNode, input: &str, expected: &str) {
    let ArithmeticExpr::ShellWord(word) = &expr.kind else {
        panic!("expected arithmetic shell word, got {:?}", expr.kind);
    };
    assert_eq!(word.render(input), expected);
}

fn expect_subscript<'a>(reference: &'a VarRef, input: &str, expected: &str) -> &'a Subscript {
    let subscript = reference
        .subscript
        .as_ref()
        .expect("expected subscripted reference");
    assert_eq!(subscript.text.slice(input), expected);
    subscript
}

fn expect_subscript_syntax<'a>(
    reference: &'a VarRef,
    input: &str,
    expected_syntax: &str,
    expected_cooked: &str,
) -> &'a Subscript {
    let subscript = expect_subscript(reference, input, expected_cooked);
    assert_eq!(subscript.syntax_text(input), expected_syntax);
    subscript
}

fn array_access_reference(part: &WordPart) -> Option<&VarRef> {
    match part {
        WordPart::ArrayAccess(reference) => Some(reference),
        WordPart::Parameter(parameter) => match &parameter.syntax {
            ParameterExpansionSyntax::Bourne(BourneParameterExpansion::Access { reference }) => {
                Some(reference)
            }
            _ => None,
        },
        _ => None,
    }
}

fn expect_array_access(word: &Word) -> &VarRef {
    let [part] = word.parts.as_slice() else {
        panic!("expected single expansion part");
    };
    array_access_reference(&part.kind)
        .unwrap_or_else(|| panic!("expected array access part, got {:?}", part.kind))
}

fn expect_parameter(word: &Word) -> &ParameterExpansion {
    let [part] = word.parts.as_slice() else {
        panic!("expected single parameter part");
    };
    let WordPart::Parameter(parameter) = &part.kind else {
        panic!("expected parameter part, got {:?}", part.kind);
    };
    parameter
}

fn expect_zsh_qualified_glob(word: &Word) -> &ZshQualifiedGlob {
    let [part] = word.parts.as_slice() else {
        panic!("expected single qualified glob part");
    };
    let WordPart::ZshQualifiedGlob(glob) = &part.kind else {
        panic!("expected qualified glob part, got {:?}", part.kind);
    };
    glob
}

fn expect_zsh_glob_qualifiers(glob: &ZshQualifiedGlob) -> &ZshGlobQualifierGroup {
    glob.qualifiers
        .as_ref()
        .expect("expected zsh glob qualifiers")
}

fn expect_zsh_glob_pattern_segment(segment: &ZshGlobSegment) -> &Pattern {
    let ZshGlobSegment::Pattern(pattern) = segment else {
        panic!("expected pattern segment");
    };
    pattern
}

fn expect_array_length_part(part: &WordPart) -> &VarRef {
    match part {
        WordPart::ArrayLength(reference) => reference,
        WordPart::Parameter(parameter) => match &parameter.syntax {
            ParameterExpansionSyntax::Bourne(BourneParameterExpansion::Length { reference }) => {
                reference
            }
            _ => panic!("expected array length part, got {:?}", part),
        },
        _ => panic!("expected array length part, got {:?}", part),
    }
}

fn expect_array_indices_part(part: &WordPart) -> &VarRef {
    match part {
        WordPart::ArrayIndices(reference) => reference,
        WordPart::Parameter(parameter) => match &parameter.syntax {
            ParameterExpansionSyntax::Bourne(BourneParameterExpansion::Indices { reference }) => {
                reference
            }
            _ => panic!("expected array indices part, got {:?}", part),
        },
        _ => panic!("expected array indices part, got {:?}", part),
    }
}

fn expect_substring_part(
    part: &WordPart,
) -> (
    &VarRef,
    &Option<ArithmeticExprNode>,
    &Option<ArithmeticExprNode>,
) {
    match part {
        WordPart::Substring {
            reference,
            offset_ast,
            length_ast,
            ..
        } => (reference, offset_ast, length_ast),
        WordPart::Parameter(parameter) => match &parameter.syntax {
            ParameterExpansionSyntax::Bourne(BourneParameterExpansion::Slice {
                reference,
                offset_ast,
                length_ast,
                ..
            }) if !reference.has_array_selector() => (reference, offset_ast, length_ast),
            _ => panic!("expected substring part, got {:?}", part),
        },
        _ => panic!("expected substring part, got {:?}", part),
    }
}

fn expect_array_slice_part(
    part: &WordPart,
) -> (
    &VarRef,
    &Option<ArithmeticExprNode>,
    &Option<ArithmeticExprNode>,
) {
    match part {
        WordPart::ArraySlice {
            reference,
            offset_ast,
            length_ast,
            ..
        } => (reference, offset_ast, length_ast),
        WordPart::Parameter(parameter) => match &parameter.syntax {
            ParameterExpansionSyntax::Bourne(BourneParameterExpansion::Slice {
                reference,
                offset_ast,
                length_ast,
                ..
            }) if reference.has_array_selector() => (reference, offset_ast, length_ast),
            _ => panic!("expected array slice part, got {:?}", part),
        },
        _ => panic!("expected array slice part, got {:?}", part),
    }
}

fn expect_parameter_operation_part(
    part: &WordPart,
) -> (&VarRef, &ParameterOp, Option<&SourceText>) {
    match part {
        WordPart::ParameterExpansion {
            reference,
            operator,
            operand,
            ..
        } => (reference, operator, operand.as_ref()),
        WordPart::Parameter(parameter) => match &parameter.syntax {
            ParameterExpansionSyntax::Bourne(BourneParameterExpansion::Operation {
                reference,
                operator,
                operand,
                ..
            }) => (reference, operator, operand.as_ref()),
            _ => panic!("expected parameter operation part, got {:?}", part),
        },
        _ => panic!("expected parameter operation part, got {:?}", part),
    }
}

fn expect_prefix_match_part(part: &WordPart) -> (&Name, PrefixMatchKind) {
    match part {
        WordPart::PrefixMatch { prefix, kind } => (prefix, *kind),
        WordPart::Parameter(parameter) => match &parameter.syntax {
            ParameterExpansionSyntax::Bourne(BourneParameterExpansion::PrefixMatch {
                prefix,
                kind,
            }) => (prefix, *kind),
            _ => panic!("expected prefix match part, got {:?}", part),
        },
        _ => panic!("expected prefix match part, got {:?}", part),
    }
}

fn expect_indirect_expansion_part(
    part: &WordPart,
) -> (&VarRef, Option<&ParameterOp>, Option<&SourceText>, bool) {
    match part {
        WordPart::IndirectExpansion {
            reference,
            operator,
            operand,
            colon_variant,
            ..
        } => (
            reference,
            operator.as_ref(),
            operand.as_ref(),
            *colon_variant,
        ),
        WordPart::Parameter(parameter) => match &parameter.syntax {
            ParameterExpansionSyntax::Bourne(BourneParameterExpansion::Indirect {
                reference,
                operator,
                operand,
                colon_variant,
                ..
            }) => (
                reference,
                operator.as_ref(),
                operand.as_ref(),
                *colon_variant,
            ),
            _ => panic!("expected indirect expansion part, got {:?}", part),
        },
        _ => panic!("expected indirect expansion part, got {:?}", part),
    }
}

fn expect_simple(stmt: &Stmt) -> &AstSimpleCommand {
    let AstCommand::Simple(command) = &stmt.command else {
        panic!("expected simple command");
    };
    command
}

fn expect_binary(stmt: &Stmt) -> &BinaryCommand {
    let AstCommand::Binary(command) = &stmt.command else {
        panic!("expected binary command");
    };
    command
}

#[test]
fn ordinary_subscripts_keep_word_asts_while_selectors_do_not() {
    let input = "echo ${map[$key]} ${map[@]}\n";
    let output = Parser::new(input).parse().unwrap();
    let command = expect_simple(&output.file.body.stmts[0]);

    let ordinary = expect_array_access(&command.args[0]);
    let ordinary_subscript = expect_subscript(ordinary, input, "$key");
    assert_eq!(
        ordinary_subscript
            .word_ast()
            .expect("expected ordinary subscript word AST")
            .render_syntax(input),
        "$key"
    );

    let selector = expect_array_access(&command.args[1]);
    let selector_subscript = expect_subscript(selector, input, "@");
    assert!(selector_subscript.word_ast().is_none());
}

#[test]
fn parser_backed_parameter_fragments_keep_word_asts() {
    let bash_input = "echo ${name:-$fallback}\n";
    let bash_output = Parser::new(bash_input).parse().unwrap();
    let bash_command = expect_simple(&bash_output.file.body.stmts[0]);
    let bash_parameter = expect_parameter(&bash_command.args[0]);
    let ParameterExpansionSyntax::Bourne(BourneParameterExpansion::Operation {
        operand_word_ast,
        ..
    }) = &bash_parameter.syntax
    else {
        panic!("expected parameter operation");
    };
    assert_eq!(
        operand_word_ast
            .as_ref()
            .expect("expected operand word AST")
            .render_syntax(bash_input),
        "$fallback"
    );

    let zsh_input = "echo ${(j.:.)name//foo/$bar}\n";
    let zsh_output = Parser::with_dialect(zsh_input, ShellDialect::Zsh)
        .parse()
        .unwrap();
    let zsh_command = expect_simple(&zsh_output.file.body.stmts[0]);
    let zsh_parameter = expect_parameter(&zsh_command.args[0]);
    let ParameterExpansionSyntax::Zsh(syntax) = &zsh_parameter.syntax else {
        panic!("expected zsh parameter expansion");
    };
    assert_eq!(
        syntax.modifiers[0]
            .argument_word_ast()
            .expect("expected zsh modifier argument word AST")
            .render_syntax(zsh_input),
        ":"
    );
    let Some(ZshExpansionOperation::ReplacementOperation {
        pattern_word_ast,
        replacement_word_ast,
        ..
    }) = syntax.operation.as_ref()
    else {
        panic!("expected zsh replacement operation");
    };
    assert_eq!(pattern_word_ast.render_syntax(zsh_input), "foo");
    assert_eq!(
        replacement_word_ast
            .as_ref()
            .expect("expected zsh replacement word AST")
            .render_syntax(zsh_input),
        "$bar"
    );
}

mod commands;
mod heredocs;
mod redirects;
mod words;