nu-parser 0.115.0

Nushell's parser
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
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
#![allow(clippy::byte_char_slices)]

use nu_parser::{LexState, Token, TokenContents, lex, lex_n_tokens, lex_signature};
use nu_protocol::{ParseError, Span};
use rstest::rstest;
use std::fmt::Write;

#[test]
fn lex_basic() {
    let file = b"let x = 4";

    let output = lex(file, 0, &[], &[], true);

    assert!(output.1.is_none());
}

#[test]
fn lex_newline() {
    let file = b"let x = 300\nlet y = 500;";

    let output = lex(file, 0, &[], &[], true);

    assert!(output.0.contains(&Token {
        contents: TokenContents::Eol,
        span: Span::new(11, 12)
    }));
}

#[test]
fn lex_annotations_list() {
    let file = b"items: list<string>";

    let (output, err) = lex_signature(file, 0, &[b'\n', b'\r'], &[b':', b'=', b','], false);

    assert!(err.is_none());
    assert_eq!(output.len(), 3);
}

#[test]
fn lex_annotations_record() {
    let file = b"config: record<name: string>";

    let (output, err) = lex_signature(file, 0, &[b'\n', b'\r'], &[b':', b'=', b','], false);

    assert!(err.is_none());
    assert_eq!(output.len(), 3);
}

#[test]
fn lex_annotations_empty() {
    let file = b"items: list<>";

    let (output, err) = lex_signature(file, 0, &[b'\n', b'\r'], &[b':', b'=', b','], false);

    assert!(err.is_none());
    assert_eq!(output.len(), 3);
}

#[test]
fn lex_annotations_space_before_annotations() {
    let file = b"items: list <string>";

    let (output, err) = lex_signature(file, 0, &[b'\n', b'\r'], &[b':', b'=', b','], false);

    assert!(err.is_none());
    assert_eq!(output.len(), 4);
}

#[test]
fn lex_annotations_space_within_annotations() {
    let file = b"items: list< string>";

    let (output, err) = lex_signature(file, 0, &[b'\n', b'\r'], &[b':', b'=', b','], false);

    assert!(err.is_none());
    assert_eq!(output.len(), 3);

    let file = b"items: list<string >";

    let (output, err) = lex_signature(file, 0, &[b'\n', b'\r'], &[b':', b'=', b','], false);

    assert!(err.is_none());
    assert_eq!(output.len(), 3);

    let file = b"items: list< string >";

    let (output, err) = lex_signature(file, 0, &[b'\n', b'\r'], &[b':', b'=', b','], false);

    assert!(err.is_none());
    assert_eq!(output.len(), 3);
}

#[test]
fn lex_annotations_nested() {
    let file = b"items: list<record<name: string>>";

    let (output, err) = lex_signature(file, 0, &[b'\n', b'\r'], &[b':', b'=', b','], false);

    assert!(err.is_none());
    assert_eq!(output.len(), 3);
}

#[test]
fn lex_annotations_nested_unterminated() {
    let file = b"items: list<record<name: string>";

    let (output, err) = lex_signature(file, 0, &[b'\n', b'\r'], &[b':', b'=', b','], false);

    assert!(matches!(
        err.unwrap(),
        ParseError::Unclosed(delim, ..) if delim == ">"
    ));
    assert_eq!(output.len(), 3);
}

#[test]
fn lex_annotations_unterminated() {
    let file = b"items: list<string";

    let (output, err) = lex_signature(file, 0, &[b'\n', b'\r'], &[b':', b'=', b','], false);

    assert!(matches!(
        err.unwrap(),
        ParseError::Unclosed(delim, ..) if delim == ">"
    ));
    assert_eq!(output.len(), 3);
}

#[test]
fn lex_empty() {
    let file = b"";

    let output = lex(file, 0, &[], &[], true);

    assert!(output.0.is_empty());
    assert!(output.1.is_none());
}

#[test]
fn lex_parenthesis() {
    // The whole parenthesis is an item for the lexer
    let file = b"let x = (300 + (322 * 444));";

    let output = lex(file, 0, &[], &[], true);

    assert_eq!(
        output.0.get(3).unwrap(),
        &Token {
            contents: TokenContents::Item,
            span: Span::new(8, 27)
        }
    );
}

#[test]
fn lex_comment() {
    let file = b"let x = 300 # a comment \n $x + 444";

    let output = lex(file, 0, &[], &[], false);

    assert_eq!(
        output.0.get(4).unwrap(),
        &Token {
            contents: TokenContents::Comment,
            span: Span::new(12, 24)
        }
    );
}

#[test]
fn lex_not_comment_needs_space_in_front_of_hashtag() {
    let file = b"1..10 | each {echo test#testing }";

    let output = lex(file, 0, &[], &[], false);

    assert!(output.1.is_none());
}

#[test]
fn lex_comment_with_space_in_front_of_hashtag() {
    let file = b"1..10 | each {echo test #testing }";

    let output = lex(file, 0, &[], &[], false);

    assert!(output.1.is_some());
    // Primary span is the opening `{` of the each block (innermost unclosed).
    assert!(matches!(
        output.1.unwrap(),
        ParseError::Unclosed(missing_token, open, ..) if missing_token == "}"
            && open == Span::new(13, 14)
    ));
}

#[test]
fn lex_comment_with_tab_in_front_of_hashtag() {
    let file = b"1..10 | each {echo test\t#testing }";

    let output = lex(file, 0, &[], &[], false);

    assert!(output.1.is_some());
    assert!(matches!(
        output.1.unwrap(),
        ParseError::Unclosed(missing_token, open, ..) if missing_token == "}"
            && open == Span::new(13, 14)
    ));
}

#[test]
fn lex_is_incomplete() {
    let file = b"let x = 300 | ;";

    let output = lex(file, 0, &[], &[], true);

    let err = output.1.unwrap();
    assert!(matches!(err, ParseError::ExtraTokens(_)));
}

#[test]
fn lex_incomplete_paren() {
    let file = b"let x = (300 + ( 4 + 1)";

    let output = lex(file, 0, &[], &[], true);

    let err = output.1.unwrap();
    // Inner `( 4 + 1)` is closed by the trailing `)`; remaining open is the outer `(`.
    assert!(matches!(
        err,
        ParseError::Unclosed(v, open, ..) if v == ")" && open == Span::new(8, 9)
    ));
}

#[test]
fn lex_incomplete_quote() {
    let file = b"let x = '300 + 4 + 1";

    let output = lex(file, 0, &[], &[], true);

    let err = output.1.unwrap();
    assert!(matches!(
        err,
        ParseError::Unclosed(v, open, ..) if v == "'" && open == Span::new(8, 9)
    ));
}

#[rstest]
#[case(br#"$"('" "')""#)] // https://github.com/nushell/nushell/issues/18807
#[case(br#"$"('a' + "b")""#)]
#[case(br#"$'("a b")'"#)]
#[case(br#"$"(1 + (2 * 3)) end""#)]
#[case(br#"$"\('not an expr'\)""#)]
#[case(br#"$"("a\"b")""#)] // escaped quote inside a nested double-quoted string
#[case(br#"$"a(1)b(2)""#)] // multiple sequential subexpressions
#[case(br#"$"($"in(2)ner")""#)] // nested interpolated string as the subexpression body
#[case(br#"$'($"a" + $'b')'"#)]
fn lex_interpolation_subexpression_is_one_token(#[case] file: &[u8]) {
    // A quote inside `(…)` of an interpolated string does not end the string,
    // and an escaped `\(` does not start a subexpression. The whole construct
    // must lex as a single item token.
    let (output, err) = lex(file, 0, &[], &[], true);
    assert!(err.is_none(), "expected clean lex, got {err:?}");
    let items: Vec<&Token> = output
        .iter()
        .filter(|t| t.contents == TokenContents::Item)
        .collect();
    assert_eq!(items.len(), 1, "expected a single item token: {items:?}");
    assert_eq!(items[0].span, Span::new(0, file.len()));
}

#[rstest]
#[case(br#"$"('"#, ")", 2)] // oldest open delimiter: the subexpression itself
#[case(br#"$"(1"#, ")", 2)] // subexpression still open at end of input
#[case(br#"$"foo (2 + 3""#, ")", 6)] // trailing quote opens a nested string; the mistake is the `(`
fn lex_interpolation_subexpression_unclosed(
    #[case] file: &[u8],
    #[case] delim: &str,
    #[case] open_at: usize,
) {
    let (_, err) = lex(file, 0, &[], &[], true);
    let err = err.expect("expected unclosed delimiter error");
    let (open, _end) = assert_unclosed(&err, delim);
    assert_eq!(open, Span::new(open_at, open_at + 1));
}

// ---------------------------------------------------------------------------
// Delimiter diagnostics regression suite
//
// Presentation heuristics may reshape *labels* when the delimiter stack proves
// a real failure.
// ---------------------------------------------------------------------------

fn parse_first_error(src: &[u8]) -> ParseError {
    let engine = nu_protocol::engine::EngineState::new();
    let mut ws = nu_protocol::engine::StateWorkingSet::new(&engine);
    nu_parser::parse(&mut ws, None, src, false);
    assert!(
        !ws.parse_errors.is_empty(),
        "expected a parse error for {:?}",
        std::str::from_utf8(src)
    );
    ws.parse_errors[0].clone()
}

fn line_of(src: &[u8], offset: usize) -> usize {
    src[..offset.min(src.len())]
        .iter()
        .filter(|&&b| b == b'\n')
        .count()
        + 1
}

/// Assert unbalanced delimiter; returns the closer span.
fn assert_unbalanced(err: &ParseError, open: &str, close: &str) -> Span {
    match err {
        ParseError::Unbalanced(o, c, span, help) => {
            assert_eq!(*o, open, "unexpected open kind in {err:?}");
            assert_eq!(*c, close, "unexpected close kind in {err:?}");
            assert!(
                help.contains("if it is extra") && help.contains("or add"),
                "help should be dual remove/add, got: {help}"
            );
            *span
        }
        other => panic!("expected Unbalanced({open}, {close}), got {other:?}"),
    }
}

fn assert_unclosed(err: &ParseError, delim: &str) -> (Span, Span) {
    match err {
        ParseError::Unclosed(d, open, expected, ..) => {
            assert_eq!(*d, delim, "unexpected unclosed delim in {err:?}");
            (*open, *expected)
        }
        other => panic!("expected Unclosed({delim}), got {other:?}"),
    }
}

#[test]
fn lex_unclosed_nested_brace_points_at_inner_open() {
    // Missing close for `ls: {` with no trailing closers — primary is that opener.
    let file = b"$env.config = {\n  ls: {\n    use_ls_colors: true\n";

    let output = lex(file, 0, &[], &[], true);
    let err = output.1.expect("expected unclosed delimiter error");
    let (open, _end) = assert_unclosed(&err, "}");
    assert_eq!(file[open.start], b'{');
    // Prefer the inner open: second `{` (the `ls` record).
    let second = file
        .iter()
        .enumerate()
        .filter(|(_, b)| **b == b'{')
        .nth(1)
        .map(|(i, _)| i)
        .unwrap();
    assert_eq!(open.start, second);
    if let ParseError::Unclosed(.., help) = &err {
        assert!(
            help.contains("ls") || help.contains("matching"),
            "help should mention structure or generic fix: {help}"
        );
    }
}

// Structure-hint unit tests live next to `delimiter_structure_hint` (crate-private).

/// These patterns previously caused *false positive* Unclosed errors when
/// indent heuristics invented failures. Stack-only rules must accept them.
#[rstest]
#[case::paren_wrapped_record_dedented_closer(
    b"def f [] {\n        let emoji_dict = ({\n        \"200\": \"x\",\n    })\n}\n"
)]
#[case::else_if_chain_dedented_closers(
    b"{||\n    if $in < 1hr {\n      'red'\n      } else if $in < 1wk {\n      'green'\n    } else if $in < 6wk {\n      'blue'\n    } else { 'gray' }\n  }\n"
)]
#[case::balanced_nested_records(
    b"$env.config = {\n  hooks: {\n    pre: 1\n  }\n  rm: {\n    x: 1\n  }\n}\n"
)]
#[case::balanced_list_paren_record_mix(b"let x = [1 (2 + 3) {a: 4}]\n")]
#[case::balanced_def_signature_and_parens(
    b"def f [a: int, b: string] { $a + ($b | str length) }\n"
)]
#[case::multiline_pipeline_no_invented_missing_brace(b"ls\n| where type == file\n| get name\n")]
#[case::continued_pipeline_after_pipe(b"ls |\n  get name\n")]
#[case::balanced_quoted_record_keys(b"{ \"type\": 1, name: 2 }\n")]
// Multi-line constructs with matching closers are valid in scripts and the REPL.
#[case::multiline_closed_double_quoted_string(b"let x = \"hello\nworld\"\n")]
#[case::multiline_closed_single_quoted_string(b"let x = 'hello\nworld'\n")]
#[case::multiline_closed_list(b"let y = [1, 2, 3\n4, 5, 6]\n")]
#[case::multiline_closed_list_with_nested(b"let y = [\n  1,\n  (2 + 3),\n  {a: 4}\n]\n")]
#[case::multiline_closed_record(b"let r = {\n  a: 1\n  b: 2\n}\n")]
#[case::multiline_closed_parens(b"let n = (\n  1 + 2\n)\n")]
fn lex_valid_code_never_errors_from_indent_style(#[case] file: &[u8]) {
    let output = lex(file, 0, &[], &[], true);
    assert!(
        output.1.is_none(),
        "valid input must not lex-error, got {:?} for {:?}",
        output.1,
        std::str::from_utf8(file)
    );
    let engine = nu_protocol::engine::EngineState::new();
    let mut ws = nu_protocol::engine::StateWorkingSet::new(&engine);
    nu_parser::parse(&mut ws, None, file, false);
    let delimiterish = ws
        .parse_errors
        .iter()
        .any(|e| matches!(e, ParseError::Unclosed(..) | ParseError::Unbalanced(..)));
    assert!(
        !delimiterish,
        "valid input must not get delimiter diagnostics, got {:?} for {:?}",
        ws.parse_errors,
        std::str::from_utf8(file)
    );
}

#[test]
fn lex_truly_unclosed_still_reports() {
    // Real stack failure: missing closers at end of input.
    let file = b"$env.config = {\n  ls: {\n    use_ls_colors: true\n";
    let output = lex(file, 0, &[], &[], true);
    let err = output.1.expect("expected real unclosed error");
    assert_unclosed(&err, "}");
}

#[rstest]
#[case::unclosed_paren(b"print (1 + 2", b'(', ")")]
#[case::unclosed_bracket(b"let x = [1, 2", b'[', "]")]
// Multi-line without a closer is still unclosed (not confused with valid multi-line forms).
#[case::multiline_unclosed_list(b"let y = [1, 2, 3\n4, 5, 6", b'[', "]")]
#[case::multiline_unclosed_paren(b"let n = (\n  1 + 2", b'(', ")")]
#[case::multiline_unclosed_record(b"let r = {\n  a: 1\n  b: 2", b'{', "}")]
fn lex_unclosed_paren_and_bracket_report_correct_delim(
    #[case] src: &[u8],
    #[case] open_byte: u8,
    #[case] expected_closer: &str,
) {
    let (open, _) = assert_unclosed(
        &lex(src, 0, &[], &[], true)
            .1
            .expect("expected unclosed delimiter"),
        expected_closer,
    );
    assert_eq!(src[open.start], open_byte);
}

#[test]
fn lex_missing_closure_brace_before_pipe_labels_near_pipe() {
    // Real bug pattern from defs.nu `startup-stats`: forgot `}` after a closure
    // body before the next `| upsert`. Stack still fails (outer `{` unclosed);
    // labels should point near the missing closer, not only at EOF.
    let file =
        b"def f [] {\n  ls | upsert a {|n|\n    $n | length\n  | upsert b {|x|\n    $x\n  }\n";
    // Missing `}` after `length` before `| upsert b`. Final braces incomplete.
    let output = lex(file, 0, &[], &[], true);
    let err = output.1.expect("expected real unclosed error");
    let (open, expected) = assert_unclosed(&err, "}");
    // Expected closer should be at the `|` that starts `| upsert b`, not far past it.
    let pipe_at = file
        .windows(10)
        .position(|w| w == b"| upsert b")
        .expect("| upsert b");
    assert_eq!(
        expected.start, pipe_at,
        "expected closer label at `| upsert b` (offset {pipe_at}), open={open:?} expected={expected:?}"
    );
}

/// Extra `}` after control-flow without a block reports plain Unbalanced at the closer.
/// (No lookback to invent a "missing `{` after if" secondary label.)
#[rstest]
#[case::if_missing_brace(
    b"def f [] {\n  each {|r|\n    if $x != string\n      $r\n    }\n  }\n}\n"
)]
#[case::while_missing_brace(b"def f [] {\n  while $true\n    1\n  }\n}\n")]
#[case::try_missing_brace(b"def f [] {\n  try\n    1\n  }\n}\n")]
#[case::for_missing_brace(b"def f [] {\n  for x in 1..2\n    $x\n  }\n}\n")]
#[case::match_missing_brace(b"def f [] {\n  match $x\n    1 => { 2 }\n  }\n}\n")]
#[case::else_if_missing_brace(b"def f [] {\n  if $true { 1 }\n  else if $false\n    2\n  }\n}\n")]
fn parse_extra_brace_after_control_flow_is_unbalanced(#[case] file: &[u8]) {
    let err = parse_first_error(file);
    let closer = assert_unbalanced(&err, "{", "}");
    assert_eq!(file[closer.start], b'}');
}

#[test]
fn parse_extra_brace_after_bare_record_is_unbalanced() {
    // `type: $lst.0}` forgot `{` before the field; stack still fails on a later `}`.
    let file = b"\
def f [] {\n\
    insert content {\n\
        each {|lst|\n\
            type: $lst.0}\n\
            | if $true {\n\
                merge {name: x}\n\
            } else {\n\
                merge {name: y}\n\
            }\n\
        }\n\
        | flatten\n\
    }\n\
}\n";
    let err = parse_first_error(file);
    let closer = assert_unbalanced(&err, "{", "}");
    assert_eq!(file[closer.start], b'}');
}

#[test]
fn parse_extra_brace_without_hint_stays_unbalanced() {
    let file = b"def f [] {\n  1\n}\n}\n";
    let err = parse_first_error(file);
    let closer = assert_unbalanced(&err, "{", "}");
    assert_eq!(file[closer.start], b'}');
}

/// Extra `}` with `if` / record-looking text only inside strings still reports
/// Unbalanced at the real closer (stack-only; no string-content lookback).
#[test]
fn parse_extra_brace_with_if_inside_multiline_string_is_unbalanced() {
    let file = b"\
def f [] {\n\
  let s = 'first\n\
if this is just string content\n\
last'\n\
}\n\
}\n";
    let err = parse_first_error(file);
    let closer = assert_unbalanced(&err, "{", "}");
    assert_eq!(file[closer.start], b'}');
    assert_eq!(line_of(file, closer.start), 6);
}

#[test]
fn parse_extra_brace_with_record_inside_raw_string_is_unbalanced() {
    let file = b"\
def f [] {\n\
  let s = r#'first\n\
type: text}\n\
last'#\n\
}\n\
}\n";
    let err = parse_first_error(file);
    let closer = assert_unbalanced(&err, "{", "}");
    assert_eq!(file[closer.start], b'}');
    assert_eq!(line_of(file, closer.start), 6);
}

#[test]
fn lex_mismatched_closer_list_closed_with_paren() {
    // `)` closing a `[` should name open kind `[`, not invent `(` .
    let file = b"[1, 2, 3)";
    let err = lex(file, 0, &[], &[], true)
        .1
        .expect("expected unbalanced error");
    assert_unbalanced(&err, "[", ")");
}

#[test]
fn lex_mismatched_closer_paren_closed_with_bracket() {
    // `]` closing a `(` must keep unbalanced-with-`(`.
    let file = b"(1, 2]";
    let err = lex(file, 0, &[], &[], true)
        .1
        .expect("expected unbalanced error");
    assert_unbalanced(&err, "(", "]");
}

#[test]
fn lex_mismatched_closer_brace_closed_with_paren() {
    let file = b"{ a: 1 )";
    let err = lex(file, 0, &[], &[], true)
        .1
        .expect("expected delimiter error");
    assert_unbalanced(&err, "{", ")");
}

#[test]
fn lex_mismatched_bracket_inside_block_with_sig_brackets() {
    // `def f [] { 1 ] }` — report unbalanced against stack top `{`.
    let file = b"def f [] { 1 ] }";
    let err = parse_first_error(file);
    let closer = assert_unbalanced(&err, "{", "]");
    assert_eq!(file[closer.start], b']');
}

#[test]
fn parse_extra_paren_is_unbalanced() {
    for file in [b"1 + 2)" as &[u8], b"let x = 1)", b"{ a: 1 ) }"] {
        let err = parse_first_error(file);
        let closer = assert_unbalanced(&err, if file.starts_with(b"{") { "{" } else { "(" }, ")");
        assert_eq!(file[closer.start], b')');
    }
}

#[test]
fn parse_missing_open_paren_orphan_closer_is_unbalanced() {
    // `print -n ansi green)` — stack reports unexpected `)` (no lookback insert site).
    let file = b"def f [] {\n  if $x {\n        print -n ansi green)\n  }\n}\n";
    let err = parse_first_error(file);
    let closer = assert_unbalanced(&err, "{", ")");
    assert_eq!(file[closer.start], b')');
}

#[test]
fn parse_balanced_parens_with_extra_close_stays_unbalanced() {
    let file = b"def f [] { print (ansi green)) }\n";
    let err = parse_first_error(file);
    let closer = assert_unbalanced(&err, "{", ")");
    assert_eq!(file[closer.start], b')');
}

#[test]
fn parse_missing_list_open_bracket_orphan_closer_is_unbalanced() {
    // `2 (x - 2) 0]` — unexpected `]` against stack top `{`.
    let file = b"def f [] {\n  if $x {\n      2 ($in_ten - 2) 0]\n  }\n}\n";
    let err = parse_first_error(file);
    let closer = assert_unbalanced(&err, "{", "]");
    assert_eq!(file[closer.start], b']');
}

#[test]
fn parse_missing_sig_close_before_body_brace() {
    // `def name [\n  param\n {` — missing `]` before body; label at body `{`.
    let file = b"def prepend-if-not-in [\n  value: string\n {\n  let list = $in\n}\n";
    let err = parse_first_error(file);
    let (open, expected) = assert_unclosed(&err, "]");
    assert_eq!(file[open.start], b'[');
    assert_eq!(file[expected.start], b'{');
    assert_eq!(line_of(file, expected.start), 3);
}

#[test]
fn parse_unclosed_quotes_still_report() {
    let file = b"let x = \"hello";
    let err = parse_first_error(file);
    // Quote failures may surface as Unclosed("\"") or similar string errors.
    let msg = format!("{err:?}");
    assert!(
        matches!(err, ParseError::Unclosed(d, ..) if d.contains('"') || d.contains('\''))
            || msg.to_lowercase().contains("quote")
            || msg.contains('\"'),
        "expected quote-related error, got {err:?}"
    );
}

#[rstest]
#[case::single_line(b"let x = \"hello")]
#[case::multiline(b"let x = \"hello\nworld")]
#[case::multiline_single_quotes(b"let x = 'hello\nworld")]
fn parse_unclosed_quotes_multiline_still_report(#[case] file: &[u8]) {
    // Multi-line string content is fine only when closed; missing closer is Unclosed.
    let err = parse_first_error(file);
    let msg = format!("{err:?}");
    assert!(
        matches!(err, ParseError::Unclosed(d, ..) if d.contains('"') || d.contains('\''))
            || msg.to_lowercase().contains("quote")
            || msg.contains('\"')
            || msg.contains('\''),
        "expected unclosed quote for {:?}, got {err:?}",
        std::str::from_utf8(file)
    );
}

#[test]
fn parse_multiline_unclosed_list_reports_unclosed() {
    // Same shape as a valid multi-line list, but without the closing `]`.
    let file = b"let y = [1, 2, 3\n4, 5, 6";
    let err = parse_first_error(file);
    let (open, _) = assert_unclosed(&err, "]");
    assert_eq!(file[open.start], b'[');
}

/// `}` and `)` always diagnose on empty stack. Bare `]` is not treated as a
/// closer when nothing is open (it can be an ordinary item character in some
/// positions), so it is intentionally omitted here.
#[rstest]
#[case::extra_brace(b"}", "}", "{")]
#[case::extra_paren(b")", ")", "(")]
fn lex_extra_closers_on_empty_stack(
    #[case] src: &[u8],
    #[case] close: &str,
    #[case] default_open: &str,
) {
    let err = lex(src, 0, &[], &[], true)
        .1
        .expect("expected unbalanced closer on empty stack");
    // Empty stack: unbalanced with the default opener for that closer.
    // (Missing-open presentation only kicks in with lookback context.)
    assert_unbalanced(&err, default_open, close);
}

#[test]
fn lex_comments_no_space() {
    // test for parses that contain tokens that normally introduce comments
    // Code:
    // let z = 42 #the comment
    // let x#y = 69 #hello
    // let flk = nixpkgs#hello #hello
    let file = b"let z = 42 #the comment \n let x#y = 69 #hello \n let flk = nixpkgs#hello #hello";
    let output = lex(file, 0, &[], &[], false);

    assert_eq!(
        output.0.get(4).unwrap(),
        &Token {
            contents: TokenContents::Comment,
            span: Span::new(11, 24)
        }
    );

    assert_eq!(
        output.0.get(7).unwrap(),
        &Token {
            contents: TokenContents::Item,
            span: Span::new(30, 33)
        }
    );

    assert_eq!(
        output.0.get(10).unwrap(),
        &Token {
            contents: TokenContents::Comment,
            span: Span::new(39, 46)
        }
    );

    assert_eq!(
        output.0.get(15).unwrap(),
        &Token {
            contents: TokenContents::Item,
            span: Span::new(58, 71)
        }
    );

    assert_eq!(
        output.0.get(16).unwrap(),
        &Token {
            contents: TokenContents::Comment,
            span: Span::new(72, 78)
        }
    );
}

#[test]
fn lex_comments() {
    // Comments should keep the end of line token
    // Code:
    // let z = 4
    // let x = 4 #comment
    // let y = 1 # comment
    let file = b"let z = 4 #comment \n let x = 4 # comment\n let y = 1 # comment";

    let output = lex(file, 0, &[], &[], false);

    assert_eq!(
        output.0.get(4).unwrap(),
        &Token {
            contents: TokenContents::Comment,
            span: Span::new(10, 19)
        }
    );
    assert_eq!(
        output.0.get(5).unwrap(),
        &Token {
            contents: TokenContents::Eol,
            span: Span::new(19, 20)
        }
    );

    // When there is no space between the comment and the new line the span
    // for the command and the EOL overlaps
    assert_eq!(
        output.0.get(10).unwrap(),
        &Token {
            contents: TokenContents::Comment,
            span: Span::new(31, 40)
        }
    );
    assert_eq!(
        output.0.get(11).unwrap(),
        &Token {
            contents: TokenContents::Eol,
            span: Span::new(40, 41)
        }
    );
}

#[test]
fn lex_manually() {
    let file = b"'a'\n#comment\n#comment again\n| continue";
    let mut lex_state = LexState {
        input: file,
        output: Vec::new(),
        error: None,
        span_offset: 10,
    };
    assert_eq!(lex_n_tokens(&mut lex_state, &[], &[], false, 1), 1);
    assert_eq!(lex_state.output.len(), 1);
    assert_eq!(lex_n_tokens(&mut lex_state, &[], &[], false, 5), 5);
    assert_eq!(lex_state.output.len(), 6);
    // Next token is the pipe.
    // This shortens the output because it exhausts the input before it can
    // compensate for the EOL tokens lost to the line continuation
    assert_eq!(lex_n_tokens(&mut lex_state, &[], &[], false, 1), -1);
    assert_eq!(lex_state.output.len(), 5);
    assert_eq!(file.len(), lex_state.span_offset - 10);
    let last_span = lex_state.output.last().unwrap().span;
    assert_eq!(&file[last_span.start - 10..last_span.end - 10], b"continue");
}

/// Large nested record must lex without error (regression for O(n²) indent
/// heuristics). Wall-clock bounds are intentionally not asserted — they flake
/// under CI load / debug builds.
#[test]
fn lex_large_nested_record_completes() {
    let mut src = String::from("$env.config = {\n");
    for i in 0..2000 {
        let _ = write!(src, "  key{i}: {{\n    nested: {i}\n  }}\n");
    }
    src.push('}');
    let (_tokens, err) = lex(src.as_bytes(), 0, &[], &[], true);
    assert!(err.is_none(), "unexpected lex error: {err:?}");
}

#[test]
fn parse_bare_string_interpolation_with_two_paren_groups() {
    // Regression: Missing-`(` presentation reshapes some `)` failures. That must
    // not block the paren-expr fallback to bare-word string interpolation
    // (e.g. `(100 + 20 + 3)/bar/(300 + 20 + 1)`).
    let file = b"(100 + 20 + 3)/bar/(300 + 20 + 1)";
    let engine = nu_protocol::engine::EngineState::new();
    let mut ws = nu_protocol::engine::StateWorkingSet::new(&engine);
    let block = nu_parser::parse(&mut ws, None, file, true);
    assert!(
        ws.parse_errors.is_empty(),
        "bare interpolation must parse cleanly, got {:?}",
        ws.parse_errors
    );
    let pipeline = &block.pipelines[0];
    let expr = &pipeline.elements[0].expr.expr;
    assert!(
        matches!(expr, nu_protocol::ast::Expr::StringInterpolation(_)),
        "expected StringInterpolation, got {expr:?}"
    );
}