shuck-parser 0.0.41

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
use super::*;

#[test]
fn test_scan_command_substitution_body_len_handles_tabstripped_heredoc() {
    let source = "\n\t\t\tcat <<-EOF | tr '\\n' ' '\n\t\t\t\t{\"query\":\"field, direction\"}\n\t\t\tEOF\n\t\t)\"";

    let consumed = scan_command_substitution_body_len(source).expect("expected match");
    let body = &source[..consumed];

    assert!(body.contains("field, direction"));
    assert!(body.ends_with(')'));
}

#[test]
fn test_scan_command_substitution_body_len_handles_piped_heredoc_delimiter_without_space() {
    let source = "\ncat <<EOF|tr '\\n' ' '\n{\"query\":\"field, direction\"}\nEOF\n)\"";

    let consumed = scan_command_substitution_body_len(source).expect("expected match");
    let body = &source[..consumed];

    assert!(body.contains("field, direction"));
    assert!(body.ends_with(')'));
}

#[test]
fn test_scan_command_substitution_body_len_ignores_arithmetic_shift_for_heredoc_detection() {
    let source = "((x<<2))\nprintf %s 1,2\n)\"";

    let consumed = scan_command_substitution_body_len(source).expect("expected match");
    let body = &source[..consumed];

    assert!(body.contains("printf %s 1,2"));
    assert!(body.ends_with(')'));
}

#[test]
fn test_scan_command_substitution_body_len_handles_tabstripped_heredoc_at_eof() {
    let source = "\n\t\t\tcat <<-EOF | tr '\\n' ' '\n\t\t\t\t{\"query\":\"field, direction\"}\n\t\t\tEOF\n\t\t)";

    let consumed = scan_command_substitution_body_len(source).expect("expected match");
    let body = &source[..consumed];

    assert_eq!(body, source);
}

#[test]
fn test_scan_command_substitution_body_len_handles_piped_heredoc_at_eof() {
    let source = "cat <<EOF|tr '\\n' ' '\n{\"query\":\"field, direction\"}\nEOF\n)";

    let consumed = scan_command_substitution_body_len(source).expect("expected match");
    let body = &source[..consumed];

    assert_eq!(body, source);
}

#[test]
fn test_read_heredoc() {
    // Simulate state after reading "cat <<EOF" - positioned at newline before content
    let mut lexer = Lexer::new("\nhello\nworld\nEOF");
    let content = lexer.read_heredoc("EOF", false);
    assert_eq!(content.content, "hello\nworld\n");
}

#[test]
fn test_read_heredoc_single_line() {
    let mut lexer = Lexer::new("\ntest\nEOF");
    let content = lexer.read_heredoc("EOF", false);
    assert_eq!(content.content, "test\n");
}

#[test]
fn test_read_heredoc_full_scenario() {
    // Full scenario: "cat <<EOF\nhello\nworld\nEOF"
    let mut lexer = Lexer::new("cat <<EOF\nhello\nworld\nEOF");

    // Parser would read these tokens
    assert_next_token(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token(&mut lexer, TokenKind::HereDoc, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("EOF"));

    // Now read heredoc content
    let content = lexer.read_heredoc("EOF", false);
    assert_eq!(content.content, "hello\nworld\n");
}

#[test]
fn test_read_heredoc_with_redirect() {
    // Rest-of-line (> file.txt) is re-injected into the lexer buffer
    let mut lexer = Lexer::new("cat <<EOF > file.txt\nhello\nEOF");
    assert_next_token(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token(&mut lexer, TokenKind::HereDoc, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("EOF"));
    let content = lexer.read_heredoc("EOF", false);
    assert_eq!(content.content, "hello\n");
    // The redirect tokens are now available from the lexer
    assert_next_token(&mut lexer, TokenKind::RedirectOut, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("file.txt"));
}

#[test]
fn test_read_heredoc_reinjects_line_continued_pipeline_tail() {
    let source = "cat <<EOF | grep hello \\\n  | sort \\\n  > out.txt\nhello\nEOF\n";
    let mut lexer = Lexer::new(source);

    assert_next_token(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token(&mut lexer, TokenKind::HereDoc, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("EOF"));

    let heredoc = lexer.read_heredoc("EOF", false);
    assert_eq!(heredoc.content, "hello\n");

    assert_next_token(&mut lexer, TokenKind::Pipe, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("grep"));
    assert_next_token(&mut lexer, TokenKind::Word, Some("hello"));
    assert_next_token(&mut lexer, TokenKind::Pipe, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("sort"));
    assert_next_token(&mut lexer, TokenKind::RedirectOut, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("out.txt"));
}

#[test]
fn test_read_heredoc_does_not_continue_body_when_backslash_is_immediately_after_delimiter() {
    let source = "cat <<EOF \\\n1\n2\n3\nEOF\n| tac\n";
    let mut lexer = Lexer::new(source);

    assert_next_token(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token(&mut lexer, TokenKind::HereDoc, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("EOF"));

    let heredoc = lexer.read_heredoc("EOF", false);
    assert_eq!(heredoc.content, "1\n2\n3\n");
}

#[test]
fn test_read_heredoc_escaped_backslash_before_newline_does_not_continue_tail() {
    let source = "cat <<EOF foo\\\\\nbody\nEOF\n";
    let mut lexer = Lexer::new(source);

    assert_next_token(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token(&mut lexer, TokenKind::HereDoc, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("EOF"));

    let heredoc = lexer.read_heredoc("EOF", false);
    assert_eq!(heredoc.content, "body\n");
}

#[test]
fn test_read_heredoc_comment_backslash_does_not_continue_tail() {
    let source = "cat <<EOF # note \\\nbody\nEOF\n";
    let mut lexer = Lexer::new(source);

    assert_next_token(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token(&mut lexer, TokenKind::HereDoc, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("EOF"));

    let heredoc = lexer.read_heredoc("EOF", false);
    assert_eq!(heredoc.content, "body\n");
}

#[test]
fn test_read_heredoc_right_paren_comment_backslash_does_not_continue_tail() {
    let source = "( cat <<EOF )# note \\\nbody\nEOF\n";
    let mut lexer = Lexer::new(source);

    assert_next_token(&mut lexer, TokenKind::LeftParen, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token(&mut lexer, TokenKind::HereDoc, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("EOF"));

    let heredoc = lexer.read_heredoc("EOF", false);
    assert_eq!(heredoc.content, "body\n");

    assert_next_token(&mut lexer, TokenKind::RightParen, None);
}

#[test]
fn test_read_heredoc_blank_prefix_continues_into_operator_led_tail() {
    let source = "cat <<EOF \\\n| tac\n1\nEOF\n";
    let mut lexer = Lexer::new(source);

    assert_next_token(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token(&mut lexer, TokenKind::HereDoc, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("EOF"));

    let heredoc = lexer.read_heredoc("EOF", false);
    assert_eq!(heredoc.content, "1\n");

    assert_next_token(&mut lexer, TokenKind::Pipe, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("tac"));
}

#[test]
fn test_read_heredoc_with_redirect_preserves_following_spans() {
    let source = "cat <<EOF > file.txt\nhello\nEOF\n# done\n";
    let mut lexer = Lexer::new(source);

    assert_next_token(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token(&mut lexer, TokenKind::HereDoc, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("EOF"));

    let heredoc = lexer.read_heredoc("EOF", false);
    assert_eq!(heredoc.content, "hello\n");

    let redirect = lexer.next_lexed_token_with_comments().unwrap();
    assert_eq!(redirect.kind, TokenKind::RedirectOut);
    assert_eq!(redirect.span.slice(source), ">");

    let target = lexer.next_lexed_token_with_comments().unwrap();
    assert_eq!(target.kind, TokenKind::Word);
    assert_eq!(
        token_text(&target, lexer.input).as_deref(),
        Some("file.txt")
    );
    assert_eq!(target.span.slice(source), "file.txt");

    let newline = lexer.next_lexed_token_with_comments().unwrap();
    assert_eq!(newline.kind, TokenKind::Newline);
    assert_eq!(newline.span.slice(source), "\n");

    let comment = lexer.next_lexed_token_with_comments().unwrap();
    assert_eq!(comment.kind, TokenKind::Comment);
    assert_eq!(token_text(&comment, lexer.input).as_deref(), Some(" done"));
    assert_eq!(comment.span.slice(source), "# done");
}

#[test]
fn test_heredoc_with_comments_inside() {
    // Comments inside heredoc body should NOT appear as comment tokens
    let source = "cat <<EOF\n# not a comment\nreal line\nEOF\n# real comment\n";
    let mut lexer = Lexer::new(source);

    assert_next_token_with_comments(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token_with_comments(&mut lexer, TokenKind::HereDoc, None);
    assert_next_token_with_comments(&mut lexer, TokenKind::Word, Some("EOF"));

    let heredoc = lexer.read_heredoc("EOF", false);
    assert_eq!(heredoc.content, "# not a comment\nreal line\n");

    // After heredoc, replayed line termination should appear before
    // tokens from following source lines.
    assert_next_token_with_comments(&mut lexer, TokenKind::Newline, None);
    let comment = lexer.next_lexed_token_with_comments().unwrap();
    assert_eq!(comment.kind, TokenKind::Comment);
    assert_eq!(
        token_text(&comment, lexer.input).as_deref(),
        Some(" real comment")
    );
}

#[test]
fn test_heredoc_with_hash_in_variable() {
    // ${var#pattern} inside heredoc should not produce comment tokens
    let source = "cat <<EOF\nval=${x#prefix}\nEOF\n";
    let mut lexer = Lexer::new(source);

    assert_next_token_with_comments(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token_with_comments(&mut lexer, TokenKind::HereDoc, None);
    assert_next_token_with_comments(&mut lexer, TokenKind::Word, Some("EOF"));

    let heredoc = lexer.read_heredoc("EOF", false);
    assert_eq!(heredoc.content, "val=${x#prefix}\n");
}

#[test]
fn test_heredoc_span_does_not_leak() {
    // Heredoc content span must be within source bounds and must not
    // overlap with content before or after.
    let source = "cat <<EOF\nhello\nworld\nEOF\necho after";
    let mut lexer = Lexer::new(source);

    assert_next_token(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token(&mut lexer, TokenKind::HereDoc, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("EOF"));

    let heredoc = lexer.read_heredoc("EOF", false);
    let start = heredoc.content_span.start.offset;
    let end = heredoc.content_span.end.offset;
    assert!(
        end <= source.len(),
        "heredoc span end ({end}) exceeds source length ({})",
        source.len()
    );
    assert_eq!(&source[start..end], "hello\nworld\n");

    // Tokens after heredoc should still parse correctly
    assert_next_token(&mut lexer, TokenKind::Newline, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("echo"));
    assert_next_token(&mut lexer, TokenKind::Word, Some("after"));
}

#[test]
fn test_quoted_heredoc_preserves_following_backtick_word_spans() {
    let source = "\
cat <<\\_ACEOF
Use these variables to override the choices made by `configure' or to help
it to find libraries and programs with nonstandard names/locations.
_ACEOF
ac_dir_suffix=/`$as_echo \"$ac_dir\" | sed 's|^\\.[\\\\/]||'`
ac_top_builddir_sub=`$as_echo \"$ac_dir_suffix\" | sed 's|/[^\\\\/]*|/..|g;s|/||'`
";
    let mut lexer = Lexer::new(source);

    assert_next_token_with_comments(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token_with_comments(&mut lexer, TokenKind::HereDoc, None);
    let delimiter = lexer.next_lexed_token_with_comments().unwrap();
    assert_eq!(delimiter.kind, TokenKind::Word);
    assert_eq!(delimiter.span.slice(source), "\\_ACEOF");

    let heredoc = lexer.read_heredoc("_ACEOF", false);
    assert_eq!(
        heredoc.content,
        "Use these variables to override the choices made by `configure' or to help\nit to find libraries and programs with nonstandard names/locations.\n"
    );

    assert_next_token_with_comments(&mut lexer, TokenKind::Newline, None);

    let first = lexer.next_lexed_token_with_comments().unwrap();
    assert_eq!(first.kind, TokenKind::Word);
    assert_eq!(
        first.span.slice(source),
        "ac_dir_suffix=/`$as_echo \"$ac_dir\" | sed 's|^\\.[\\\\/]||'`"
    );
    let first_segments = first
        .word()
        .unwrap()
        .segments()
        .map(|segment| {
            (
                segment.kind(),
                segment.as_str().to_string(),
                segment.span().map(|span| span.slice(source).to_string()),
            )
        })
        .collect::<Vec<_>>();
    assert_eq!(
        first_segments,
        vec![
            (
                LexedWordSegmentKind::Plain,
                "ac_dir_suffix=/".to_string(),
                Some("ac_dir_suffix=/".to_string()),
            ),
            (
                LexedWordSegmentKind::Plain,
                "`$as_echo \"$ac_dir\" | sed 's|^\\.[\\\\/]||'`".to_string(),
                Some("`$as_echo \"$ac_dir\" | sed 's|^\\.[\\\\/]||'`".to_string()),
            ),
        ]
    );

    assert_next_token_with_comments(&mut lexer, TokenKind::Newline, None);

    let second = lexer.next_lexed_token_with_comments().unwrap();
    assert_eq!(second.kind, TokenKind::Word);
    assert_eq!(
        second.span.slice(source),
        "ac_top_builddir_sub=`$as_echo \"$ac_dir_suffix\" | sed 's|/[^\\\\/]*|/..|g;s|/||'`"
    );
    let second_segments = second
        .word()
        .unwrap()
        .segments()
        .map(|segment| {
            (
                segment.kind(),
                segment.as_str().to_string(),
                segment.span().map(|span| span.slice(source).to_string()),
            )
        })
        .collect::<Vec<_>>();
    assert_eq!(
        second_segments,
        vec![
            (
                LexedWordSegmentKind::Plain,
                "ac_top_builddir_sub=".to_string(),
                Some("ac_top_builddir_sub=".to_string()),
            ),
            (
                LexedWordSegmentKind::Plain,
                "`$as_echo \"$ac_dir_suffix\" | sed 's|/[^\\\\/]*|/..|g;s|/||'`".to_string(),
                Some("`$as_echo \"$ac_dir_suffix\" | sed 's|/[^\\\\/]*|/..|g;s|/||'`".to_string(),),
            ),
        ]
    );
}

#[test]
fn test_heredoc_with_unicode_content() {
    // Heredoc containing multi-byte characters; spans must be on char boundaries
    let source = "cat <<EOF\n# 你好\ncafé\nEOF\n";
    let mut lexer = Lexer::new(source);

    assert_next_token(&mut lexer, TokenKind::Word, Some("cat"));
    assert_next_token(&mut lexer, TokenKind::HereDoc, None);
    assert_next_token(&mut lexer, TokenKind::Word, Some("EOF"));

    let heredoc = lexer.read_heredoc("EOF", false);
    assert_eq!(heredoc.content, "# 你好\ncafé\n");
    let start = heredoc.content_span.start.offset;
    let end = heredoc.content_span.end.offset;
    assert!(
        source.is_char_boundary(start),
        "heredoc span start ({start}) not on char boundary"
    );
    assert!(
        source.is_char_boundary(end),
        "heredoc span end ({end}) not on char boundary"
    );
    assert_eq!(&source[start..end], "# 你好\ncafé\n");
}

#[test]
fn test_heredoc_in_arithmetic_fuzz_crash() {
    // Regression test: the fuzzer found that heredoc re-injection inside
    // arithmetic context can push self.offset past self.input.len(),
    // causing a panic in read_unquoted_segment's borrowed-slice path.
    let data: &[u8] = &[
        35, 33, 111, 98, 105, 110, 41, 41, 10, 40, 40, 32, 36, 111, 98, 105, 110, 41, 41, 10, 40,
        40, 32, 36, 53, 32, 43, 32, 49, 32, 6, 0, 0, 0, 0, 0, 0, 0, 41, 60, 60, 69, 41, 4, 33, 61,
        26, 40, 40, 32, 110, 119, 119, 49, 32, 119, 119, 109, 119, 119, 119, 119, 119, 119, 122,
        39, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 0, 0, 0, 0, 0, 41, 60, 60,
        69, 41, 4, 33, 61, 26, 40, 40, 32, 110, 119, 119, 49, 32, 119, 119, 109, 119, 119, 110,
        119, 119, 49, 32, 119, 119, 109, 119, 119, 119, 0, 14, 119, 122, 39, 122, 122, 122, 122,
        122, 122, 122, 47, 33, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 40, 122, 122, 122,
        122, 39, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 0, 53, 32,
        43, 32, 49, 32, 41, 41, 10, 40, 40, 32, 36, 53, 32, 43, 32, 49, 32, 6, 0, 0, 0, 0, 0, 0, 0,
        41, 60, 60, 69, 41, 4, 33, 61, 26, 40, 40, 32, 110, 119, 119, 49, 32, 119, 119, 109, 119,
        119, 119, 119, 119, 119, 122, 39, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
        122, 0, 0, 0, 0, 0, 41, 60, 60, 69, 41, 4, 33, 61, 26, 40, 40, 32, 110, 119, 119, 48, 32,
        119, 119, 109, 119, 119, 110, 119, 119, 49, 32, 119, 119, 109, 119, 119, 119, 0, 14, 119,
        122, 39, 122, 122, 122, 122, 122, 122, 122, 47, 33, 122, 122, 122, 122, 122, 122, 122, 122,
        122, 122, 40, 122, 122, 122, 122, 39, 122, 122, 122, 122, 122, 122, 122, 88, 88, 88, 88,
        122, 122, 40, 122, 122, 122, 122, 39, 122, 122, 122, 122, 122, 122, 122, 122, 122, 122,
        122, 122, 122, 122, 0, 53, 32, 43, 32, 49, 32, 53, 41, 10, 40, 40, 32, 36, 53, 32, 43, 32,
        49, 32, 6, 0, 0, 0, 0, 0, 0, 0, 41, 60, 60, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 0,
        0, 0,
    ];
    let input = std::str::from_utf8(data).unwrap();
    let script = format!("echo $(({input}))\n");
    // Must not panic.
    let _ = crate::parser::Parser::new(&script).parse();
}