fatou 0.3.0

A language server, formatter, and linter for Julia
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
use crate::parser::events::Event;
use crate::parser::expr::parse_stmt;
use crate::parser::lexer::{TokKind, Token, lex};
use crate::parser::tree_builder::{build_tree, syntax_kind_for};
use crate::syntax::{SyntaxKind, SyntaxNode};

pub use crate::parser::diagnostics::ParseDiagnostic;
use crate::parser::diagnostics::{DiagnosticKind, push_diagnostic};

/// The result of a parse: the lossless CST plus any diagnostics.
#[derive(Debug, Clone)]
pub struct ParseOutput {
    pub cst: SyntaxNode,
    pub diagnostics: Vec<ParseDiagnostic>,
}

/// Parse `text` into a lossless `rowan` CST. The drive loop works one logical
/// line at a time: leading trivia (including newlines) is emitted directly at
/// the root, then the line's statements are parsed with the Pratt parser. A line
/// that carries a top-level `;` groups its statements into a `TOPLEVEL_SEMICOLON`
/// node (mirroring JuliaSyntax's `toplevel-;`); a plain line stays bare. An
/// unparseable token is consumed as one error token so the loop always makes
/// progress.
pub fn parse(text: &str) -> ParseOutput {
    let tokens = lex(text);
    let mut diagnostics = Vec::new();
    let mut events = Vec::new();

    let mut i = 0usize;
    while i < tokens.len() {
        // Leading trivia and blank/newline runs belong directly at the root.
        if tokens[i].kind.is_trivia() {
            events.push(Event::Tok(i));
            i += 1;
            continue;
        }

        // Collect a single logical line (up to the next newline or EOF),
        // tracking whether it carries a `;` separator. `leftover_mark` records
        // the event offset right after the line's first complete statement so a
        // trailing junk run on a separator-less line can be wrapped in an
        // `ERROR` node and flagged with a `TrailingJunk` diagnostic (projected
        // `(error-t …)`: `x y` ⇒ `x (error-t y)`).
        let mut line = Vec::new();
        let mut has_semicolon = false;
        let mut leftover_mark: Option<usize> = None;
        let mut first_is_doc_string = false;
        // A doc-eligible string is only a docstring when a *real* statement
        // follows it. If the leftover after the string can't start a statement
        // (a stray closer/keyword: `"doc" ]`), the string is a plain statement
        // and the remainder is junk — un-gate the raw trailing-junk collection.
        let mut doc_no_target = false;
        // A separator-less line: after the first complete statement, JuliaSyntax
        // bumps the remainder as flat error tokens rather than re-parsing it. A
        // line that carries a `;` keeps the per-segment behavior (deferred), so
        // flat-junk collection is gated off when one is present.
        let line_has_semicolon = rest_of_line_has_semicolon(&tokens, i);
        while i < tokens.len() {
            match tokens[i].kind {
                TokKind::Newline => break,
                TokKind::Semicolon => {
                    has_semicolon = true;
                    line.push(Event::Tok(i));
                    i += 1;
                }
                k if k.is_trivia() => {
                    line.push(Event::Tok(i));
                    i += 1;
                }
                _ => {
                    if !line_has_semicolon
                        && leftover_mark.is_some()
                        && (!first_is_doc_string || doc_no_target)
                    {
                        // Trailing junk after the first statement: collect raw
                        // (no structural re-parse) so the wrapping below puts the
                        // whole run in one ERROR node and the projector renders
                        // delimiters, commas, and `@` as `✘` (`x y, z` ⇒
                        // `x (error-t y ✘ z)`, `x@y` ⇒ `x (error-t ✘ y)`).
                        line.push(Event::Tok(i));
                        i += 1;
                    } else if let Some(expr) = parse_stmt(&tokens, i, &mut diagnostics) {
                        if leftover_mark.is_none() {
                            first_is_doc_string = stmt_is_doc_string(&expr.events, &tokens);
                        }
                        line.extend(expr.events);
                        i = expr.end;
                        leftover_mark.get_or_insert(line.len());
                    } else if leftover_mark.is_none()
                        && is_close_delimiter_tok(tokens[i].kind)
                        && !rest_of_line_has_semicolon(&tokens, i)
                    {
                        // A stray *closing* delimiter at statement start (no
                        // preceding statement) is JuliaSyntax's leading empty
                        // `(error)` plus an `(error-t ✘ …)` that swallows the
                        // rest of the line: `)` ⇒ `(error) (error-t ✘)`,
                        // `) x` ⇒ `(error) (error-t ✘ x)`, `)))` ⇒ `… ✘ ✘ ✘`.
                        // The `;`-segment forms emit a subtler double marker, so
                        // they are left to the loose-token fallback below.
                        line.push(Event::Start(SyntaxKind::ERROR));
                        line.push(Event::Finish);
                        let run_start = tokens[i].start;
                        line.push(Event::Start(SyntaxKind::ERROR));
                        while i < tokens.len() && tokens[i].kind != TokKind::Newline {
                            line.push(Event::Tok(i));
                            i += 1;
                        }
                        line.push(Event::Finish);
                        push_diagnostic(
                            &mut diagnostics,
                            DiagnosticKind::StrayCloser,
                            "stray closing delimiter",
                            run_start,
                            tokens[i - 1].end,
                        );
                    } else if leftover_mark.is_none() && is_stray_block_keyword_tok(tokens[i].kind)
                    {
                        // A middle/closing block keyword (`end`, `else`,
                        // `elseif`, `catch`, `finally`) where a statement is
                        // expected is not a block opener; JuliaSyntax wraps it
                        // alone in `(error <kw>)` and bumps the rest of the line
                        // as a separate trailing-junk run (`end y z` ⇒
                        // `(error end) (error-t y z)`). Emit the wrapped keyword
                        // as the line's first "statement" and set `leftover_mark`
                        // so the existing trailing-junk machinery handles the
                        // remainder.
                        line.push(Event::Start(SyntaxKind::ERROR));
                        line.push(Event::Tok(i));
                        line.push(Event::Finish);
                        push_diagnostic(
                            &mut diagnostics,
                            DiagnosticKind::StrayKeyword,
                            "unexpected block keyword",
                            tokens[i].start,
                            tokens[i].end,
                        );
                        i += 1;
                        leftover_mark = Some(line.len());
                    } else {
                        // A leftover token that can't start a statement. After a
                        // doc-eligible string this means the string has no
                        // documentable target, so the rest of the line is junk.
                        if first_is_doc_string && leftover_mark.is_some() {
                            doc_no_target = true;
                        }
                        line.push(Event::Tok(i));
                        i += 1;
                    }
                }
            }
        }

        if has_semicolon {
            events.push(Event::Start(SyntaxKind::TOPLEVEL_SEMICOLON));
            events.extend(line);
            events.push(Event::Finish);
        } else if let Some(mark) = leftover_mark.filter(|&m| {
            // Junk after the first statement on a separator-less line — but a
            // bare docstring (`"a"\nfoo`) owns its trailing statement, so leave
            // that to `fold_docstrings`. A docstring defers only when its
            // leftover actually begins with a documentable statement subtree
            // (`"doc" foo`); a leftover that opens with junk (`"doc" ]`) is
            // recovered here so the stray closer becomes `(error-t ✘)`.
            let tail = &line[m..];
            let defer_to_doc = first_is_doc_string && leftover_starts_with_subtree(tail, &tokens);
            !defer_to_doc && tail.iter().any(|e| is_significant_event(e, &tokens))
        }) {
            events.extend(line[..mark].iter().cloned());
            let tail = &line[mark..];
            // Leading trivia stays outside the error node; everything from the
            // first significant leftover token onward is the recovered run.
            let lead = tail
                .iter()
                .take_while(|e| !is_significant_event(e, &tokens))
                .count();
            events.extend(tail[..lead].iter().cloned());
            let first_junk = tail[lead..].iter().find_map(|e| match e {
                Event::Tok(idx) => Some(*idx),
                _ => None,
            });
            events.push(Event::Start(SyntaxKind::ERROR));
            events.extend(tail[lead..].iter().cloned());
            events.push(Event::Finish);
            if let Some(idx) = first_junk {
                push_diagnostic(
                    &mut diagnostics,
                    DiagnosticKind::TrailingJunk,
                    "trailing tokens after statement",
                    tokens[idx].start,
                    tokens[idx].end,
                );
            }
        } else {
            events.extend(line);
        }
    }

    let events = fold_docstrings(&events, &tokens, true);

    let cst = build_tree(&tokens, &events);
    flag_invalid_const_decls(&cst, &mut diagnostics);
    flag_invalid_function_signatures(&cst, &mut diagnostics);
    flag_invalid_catch_vars(&cst, &mut diagnostics);
    flag_invalid_export_items(&cst, &mut diagnostics);
    ParseOutput { cst, diagnostics }
}

/// Flag each parenthesized `export` item that is not a single symbol. An export
/// item may be parenthesized, but only around a single identifier, operator,
/// `var"…"`, or `$`-interpolation (`export (x)`/`export (+)`/`export ($a)`),
/// which JuliaSyntax unwraps to the bare symbol. Any other parenthesized form
/// (`export (x::T)`, `export (x, y)`, `export ()`, `export ((x))`) is invalid and
/// JuliaSyntax error-wraps the parsed expression (`(export (error (::-i x T)))`).
/// The diagnostic is a zero-width point at the parenthesized node's start; the
/// projector reconstructs the error wrapper from it (the CST stays faithful).
fn flag_invalid_export_items(cst: &SyntaxNode, diagnostics: &mut Vec<ParseDiagnostic>) {
    for stmt in cst
        .descendants()
        .filter(|n| n.kind() == SyntaxKind::EXPORT_STMT)
    {
        for item in stmt
            .children()
            .filter(|c| matches!(c.kind(), SyntaxKind::PAREN_EXPR | SyntaxKind::TUPLE_EXPR))
        {
            if !export_paren_is_symbol(&item) {
                let pos = usize::from(item.text_range().start());
                push_diagnostic(
                    diagnostics,
                    DiagnosticKind::InvalidExportItem,
                    "invalid `export` item: expected a single symbol",
                    pos,
                    pos,
                );
            }
        }
    }
}

/// Whether a parenthesized `export` item wraps a single symbol that JuliaSyntax
/// unwraps: a lone identifier (`(x)`), `var"…"` (`(var"x")`), `$`-interpolation
/// (`($a)`), or operator (`(+)`). A `(x, y)` tuple, an empty `()`, a nested paren
/// (`((x))`), a literal (`(1)`), or any compound expression (`(x::T)`, `(a+b)`)
/// is not. A `TUPLE_EXPR` (a comma/empty paren) is always invalid.
fn export_paren_is_symbol(item: &SyntaxNode) -> bool {
    if item.kind() != SyntaxKind::PAREN_EXPR {
        return false;
    }
    let mut significant = item.children_with_tokens().filter(|el| {
        !matches!(
            el.kind(),
            SyntaxKind::WHITESPACE
                | SyntaxKind::NEWLINE
                | SyntaxKind::COMMENT
                | SyntaxKind::BLOCK_COMMENT
                | SyntaxKind::LPAREN
                | SyntaxKind::RPAREN
        )
    });
    let Some(first) = significant.next() else {
        return false;
    };
    if significant.next().is_some() {
        return false;
    }
    match first {
        // A symbol-shaped node: a bare name, `var"…"`, or `$`-interpolation. A
        // `LITERAL`, `TYPE_ANNOTATION`, nested `PAREN_EXPR`, etc. is not.
        rowan::NodeOrToken::Node(n) => matches!(
            n.kind(),
            SyntaxKind::NAME | SyntaxKind::NONSTANDARD_IDENTIFIER | SyntaxKind::INTERPOLATION
        ),
        // The only bare token a `PAREN_EXPR` carries is the lone operator-value
        // form (`(+)`, `(:)`), which `parse_paren` builds for `is_paren_value_op`.
        rowan::NodeOrToken::Token(_) => true,
    }
}

/// Flag each `const` whose declaration is not the plain `=` assignment
/// JuliaSyntax requires. A bare declaration (`const x`), a non-`=` assignment
/// (`const x += 1`, `const x .= 1`), or a `global`/`local` declaration without an
/// `=` (`const global x`) is invalid, so JuliaSyntax wraps the whole `const` in
/// `(error …)` (`const x` ⇒ `(error (const x))`). A valid `const x = 1` — or a
/// `global`/`local`-wrapped `=` (`const global x = 1`) — is left alone. The
/// diagnostic is a zero-width point at the `const` keyword start; the projector
/// reconstructs the error wrapper from it (the CST topology stays faithful).
fn flag_invalid_const_decls(cst: &SyntaxNode, diagnostics: &mut Vec<ParseDiagnostic>) {
    for node in cst
        .descendants()
        .filter(|n| n.kind() == SyntaxKind::CONST_STMT)
    {
        if !const_decl_is_assignment(&node) && !is_struct_const_field(&node) {
            let pos = usize::from(node.text_range().start());
            push_diagnostic(
                diagnostics,
                DiagnosticKind::ConstNotAssignment,
                "expected assignment after `const`",
                pos,
                pos,
            );
        }
    }
}

/// Flag each `function`/`macro` whose signature is a bare identifier name but
/// which carries a non-empty body. A header like `function f end` (a bare name
/// with a truly empty body) is the valid forward-declaration form `(function f)`,
/// but once a body is present (`function f body end`) or the body block is
/// explicitly opened with a `;` (`function f; end`), the bare name is no longer a
/// valid signature: JuliaSyntax error-wraps it (`(function (error f) (block
/// body))`). The diagnostic is a zero-width point at the `SIGNATURE` node's start;
/// the projector reconstructs the error wrapper from it (the CST stays faithful).
fn flag_invalid_function_signatures(cst: &SyntaxNode, diagnostics: &mut Vec<ParseDiagnostic>) {
    for node in cst
        .descendants()
        .filter(|n| matches!(n.kind(), SyntaxKind::FUNCTION_DEF | SyntaxKind::MACRO_DEF))
    {
        let Some(sig) = node.children().find(|c| c.kind() == SyntaxKind::SIGNATURE) else {
            continue;
        };
        let sig_is_bare_name = sig.first_child().is_some_and(|inner| {
            matches!(inner.kind(), SyntaxKind::NAME | SyntaxKind::INTERPOLATION)
        });
        if sig_is_bare_name && !function_body_is_empty(&node) {
            let pos = usize::from(sig.text_range().start());
            push_diagnostic(
                diagnostics,
                DiagnosticKind::InvalidFunctionSignature,
                "invalid function signature: expected a call, not a bare name",
                pos,
                pos,
            );
        }
    }
}

/// Flag each `catch` clause whose variable is not a plain identifier. The catch
/// variable must be a bare identifier (`catch e`), a `$`-interpolation
/// (`catch $e`), or a `var"…"` non-standard identifier (`catch var"e"`); any
/// other expression (`catch e+3`, `catch e.f`, `catch f(e)`, `catch 3`) is
/// invalid and JuliaSyntax wraps it in `(error …)` (`catch e+3` ⇒ `(catch
/// (error (call-i e + 3)) …)`). The diagnostic is a zero-width point at the
/// catch-variable node's start; the projector reconstructs the error wrapper
/// from it (the CST topology stays faithful).
fn flag_invalid_catch_vars(cst: &SyntaxNode, diagnostics: &mut Vec<ParseDiagnostic>) {
    for node in cst
        .descendants()
        .filter(|n| n.kind() == SyntaxKind::CATCH_CLAUSE)
    {
        let Some(var) = node.children().find(|c| c.kind() != SyntaxKind::BLOCK) else {
            continue;
        };
        if !matches!(
            var.kind(),
            SyntaxKind::NAME | SyntaxKind::INTERPOLATION | SyntaxKind::NONSTANDARD_IDENTIFIER
        ) {
            let pos = usize::from(var.text_range().start());
            push_diagnostic(
                diagnostics,
                DiagnosticKind::CatchVarNotIdentifier,
                "catch variable must be an identifier",
                pos,
                pos,
            );
        }
    }
}

/// Whether a `function`/`macro`'s body is syntactically empty — no statement
/// nodes and no `;` separator opening the block. A bare-name header keeps the
/// forward-declaration form only while this holds; a `;` (`function f; end`) or
/// any body statement marks the block as genuinely opened.
fn function_body_is_empty(node: &SyntaxNode) -> bool {
    match node.children().find(|c| c.kind() == SyntaxKind::BLOCK) {
        Some(block) => {
            block.first_child().is_none()
                && !block
                    .children_with_tokens()
                    .any(|el| el.kind() == SyntaxKind::SEMICOLON)
        }
        None => true,
    }
}

/// Whether `node` is a `const` field declaration directly inside a struct body
/// (`struct A const a end` ⇒ `(const a)`, not wrapped). Such a bare `const` is
/// valid; the exemption is narrow — a `const` nested inside an `if`/`begin`
/// within the struct (its block's parent is not the `STRUCT_DEF`) is still an
/// error.
fn is_struct_const_field(node: &SyntaxNode) -> bool {
    node.parent()
        .filter(|b| b.kind() == SyntaxKind::BLOCK)
        .and_then(|b| b.parent())
        .is_some_and(|p| p.kind() == SyntaxKind::STRUCT_DEF)
}

/// Whether a `const`'s declaration is a plain `=` assignment. The body is the
/// `const`'s first child node, unwrapping any `global`/`local` declaration; it is
/// valid only when it is an `ASSIGNMENT_EXPR` headed by a plain `=` (not
/// `.=`/`+=`/…).
fn const_decl_is_assignment(node: &SyntaxNode) -> bool {
    let mut body = node.first_child();
    while let Some(n) = body {
        match n.kind() {
            SyntaxKind::GLOBAL_STMT | SyntaxKind::LOCAL_STMT => body = n.first_child(),
            SyntaxKind::ASSIGNMENT_EXPR => {
                return n
                    .children_with_tokens()
                    .filter_map(|el| el.into_token())
                    .any(|t| t.kind() == SyntaxKind::EQ);
            }
            _ => return false,
        }
    }
    false
}

/// Whether `kind` is a closing delimiter (`)`, `]`, `}`), which when stray at
/// statement start drives JuliaSyntax's leading-`(error)` recovery.
fn is_close_delimiter_tok(kind: TokKind) -> bool {
    matches!(kind, TokKind::RParen | TokKind::RBracket | TokKind::RBrace)
}

/// Whether `kind` is a middle/closing block keyword (`end`, `else`, `elseif`,
/// `catch`, `finally`) — a token that only closes or continues an enclosing
/// block. Where a statement is expected one of these is stray, so JuliaSyntax
/// wraps it alone in `(error <kw>)` (`@doc x\nend` ⇒ `(macrocall @doc x) (error
/// end)`, `end y z` ⇒ `(error end) (error-t y z)`).
fn is_stray_block_keyword_tok(kind: TokKind) -> bool {
    matches!(
        kind,
        TokKind::EndKw
            | TokKind::ElseKw
            | TokKind::ElseifKw
            | TokKind::CatchKw
            | TokKind::FinallyKw
    )
}

/// Whether the logical line starting at `start` (up to the next newline or EOF)
/// carries a top-level `;`. The stray-closer recovery only applies to the clean
/// separator-less form; `;`-segment lines keep the loose-token fallback.
fn rest_of_line_has_semicolon(tokens: &[Token], start: usize) -> bool {
    tokens[start..]
        .iter()
        .take_while(|t| t.kind != TokKind::Newline)
        .any(|t| t.kind == TokKind::Semicolon)
}

/// Whether an event carries significant (non-trivia) content: any node opener,
/// or a non-trivia leaf token.
fn is_significant_event(event: &Event, tokens: &[Token]) -> bool {
    match event {
        Event::Start(_) => true,
        Event::Tok(idx) => !tokens[*idx].kind.is_trivia(),
        Event::Finish => false,
    }
}

/// Whether the first significant event in `tail` opens a node (a real statement
/// subtree). A docstring's trailing leftover defers to `fold_docstrings` only
/// when it begins with such a subtree (its documentable target); a leftover that
/// opens with a bare junk token is recovered as trailing junk instead.
fn leftover_starts_with_subtree(tail: &[Event], tokens: &[Token]) -> bool {
    matches!(
        tail.iter().find(|e| is_significant_event(e, tokens)),
        Some(Event::Start(_))
    )
}

/// Whether a statement's events open a bare (doc-eligible) `STRING_LITERAL` —
/// the first inner token is not a `STRING_PREFIX`. Such a statement starts a
/// potential docstring, so a trailing statement on the same logical line is left
/// to `fold_docstrings` rather than wrapped as junk.
fn stmt_is_doc_string(events: &[Event], tokens: &[Token]) -> bool {
    matches!(
        events.first(),
        Some(Event::Start(SyntaxKind::STRING_LITERAL))
    ) && string_is_doc_eligible(&events[1..], tokens)
}

/// One child of a statement container: either a leaf token or a fully-formed
/// subtree (its node kind plus the *inner* events between its `Start`/`Finish`).
enum Item {
    Leaf(usize),
    Subtree(SyntaxKind, Vec<Event>),
}

/// Whether a node of `kind` is a statement container in which the docstring fold
/// applies (a string literal statement directly followed by another statement).
fn is_doc_container(kind: SyntaxKind) -> bool {
    matches!(kind, SyntaxKind::BLOCK | SyntaxKind::TOPLEVEL_SEMICOLON)
}

/// Whether a `STRING_LITERAL` subtree is a plain string (doc-eligible) rather
/// than a prefixed string macro (`r"…"`, `b"…"`), which is a macro call in
/// JuliaSyntax and never a docstring. Eligible iff its first token is not a
/// `STRING_PREFIX`.
fn string_is_doc_eligible(inner: &[Event], tokens: &[Token]) -> bool {
    match inner.first() {
        Some(Event::Tok(idx)) => syntax_kind_for(tokens[*idx].kind) != SyntaxKind::STRING_PREFIX,
        _ => true,
    }
}

/// Recursively fold docstrings in a statement container's child sequence.
/// `inner` is the flat event list of one node's children (the whole event
/// stream, for the implicit `ROOT`). A bare unprefixed `STRING_LITERAL`
/// statement immediately followed by another statement — at most one newline of
/// intervening trivia, no `;` — folds into a `DOC` node `(doc str target)`,
/// mirroring JuliaSyntax's `parse_docstring`. The pass descends into every
/// subtree so nested blocks (function/module/begin bodies) fold too.
fn fold_docstrings(inner: &[Event], tokens: &[Token], is_container: bool) -> Vec<Event> {
    // Split the flat event list into this level's direct children.
    let mut items: Vec<Item> = Vec::new();
    let mut k = 0;
    while k < inner.len() {
        match inner[k] {
            Event::Tok(idx) => {
                items.push(Item::Leaf(idx));
                k += 1;
            }
            Event::Start(kind) => {
                let mut depth = 1usize;
                let mut j = k + 1;
                while j < inner.len() {
                    match inner[j] {
                        Event::Start(_) => depth += 1,
                        Event::Finish => {
                            depth -= 1;
                            if depth == 0 {
                                break;
                            }
                        }
                        Event::Tok(_) => {}
                    }
                    j += 1;
                }
                // Recurse into the subtree's inner events.
                let child = fold_docstrings(&inner[k + 1..j], tokens, is_doc_container(kind));
                items.push(Item::Subtree(kind, child));
                k = j + 1;
            }
            Event::Finish => k += 1,
        }
    }

    let mut out: Vec<Event> = Vec::new();
    let mut idx = 0;
    while idx < items.len() {
        if is_container
            && let Item::Subtree(SyntaxKind::STRING_LITERAL, str_inner) = &items[idx]
            && string_is_doc_eligible(str_inner, tokens)
            && let Some(target) = doc_target(&items, idx, tokens)
        {
            // Wrap the string, the intervening trivia, and the target in a `DOC`.
            out.push(Event::Start(SyntaxKind::DOC));
            emit_items(&items[idx..=target], &mut out);
            out.push(Event::Finish);
            idx = target + 1;
            continue;
        }
        emit_items(&items[idx..=idx], &mut out);
        idx += 1;
    }
    out
}

/// Given a string statement at `start`, find the index of the statement it
/// documents: the next subtree child, reachable across at most one newline of
/// trivia and no `;`. Returns `None` if no eligible target follows.
fn doc_target(items: &[Item], start: usize, tokens: &[Token]) -> Option<usize> {
    let mut newlines = 0;
    let mut j = start + 1;
    while j < items.len() {
        match &items[j] {
            // An error-recovery node is never a documentable target (`"doc"\n]`
            // ⇒ `(string) (error) (error-t ✘)`, not a `(doc …)`); the string is
            // a plain statement.
            Item::Subtree(SyntaxKind::ERROR, _) => return None,
            Item::Subtree(..) => return Some(j),
            Item::Leaf(t) => {
                let kind = tokens[*t].kind;
                if kind == TokKind::Newline {
                    newlines += 1;
                    if newlines > 1 {
                        return None;
                    }
                } else if !kind.is_trivia() {
                    return None;
                }
                j += 1;
            }
        }
    }
    None
}

/// Append the events for `items` to `out`, rebuilding each subtree from its
/// recorded kind and inner events.
fn emit_items(items: &[Item], out: &mut Vec<Event>) {
    for item in items {
        match item {
            Item::Leaf(idx) => out.push(Event::Tok(*idx)),
            Item::Subtree(kind, inner) => {
                out.push(Event::Start(*kind));
                out.extend(inner.iter().cloned());
                out.push(Event::Finish);
            }
        }
    }
}

/// Round-trip the input through the parser: concatenating every token in the CST
/// must reproduce the original text exactly (the losslessness invariant).
pub fn reconstruct(text: &str) -> String {
    parse(text)
        .cst
        .descendants_with_tokens()
        .filter_map(|el| el.into_token())
        .map(|tok| tok.text().to_string())
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    fn assert_lossless(input: &str) {
        assert_eq!(reconstruct(input), input, "not lossless: {input:?}");
    }

    #[test]
    fn lossless_corpus() {
        for input in [
            "",
            "x = 1\n",
            "y = a + b * c\n",
            "f(a, b, c)\n",
            "v[1]\n",
            "a[end]\n",
            "a[end - 1]\n",
            "a[2:end]\n",
            "m[end, end]\n",
            "z = (a + b) / 2\n",
            "function g(x)\n    x ^ 2\nend\n",
            "if a >= b\n    a\nelseif c\n    c\nelse\n    b\nend\n",
            "begin\n    a\n    b\nend\n",
            "map(xs) do x\n    x + 1\nend\n",
            "open(\"f\") do\n    read()\nend\n",
            "# a comment\nx = 1  # trailing\n",
            "#= block =#\nq = 2\n",
            "obj.field\n",
            "x::Int\n",
            "Vector{T}\n",
            "foo(x::T) where {T<:Number} = x\n",
            "f(a, b; c=1, d=2)\n",
            "g(args...; kwargs...)\n",
            "y = a .+ b .* c\n",
            "z = .-x\n",
            "a .= f.(b, c)\n",
            "t = (a, b)\n",
            "u = (a,)\n",
            "e = ()\n",
            "nt = (x = 1, y = 2)\n",
            "y = a ? b : c\n",
            "z = a == b ? x + 1 : x - 1\n",
            "w = a ? b : c ? d : e\n",
            "v = [1, 2, 3]\n",
            "e = []\n",
            "m = [1 2; 3 4]\n",
            "s = [1 +2]\n",
            "c = [1; 2; 3]\n",
            "q = [x for x in xs]\n",
            "r = [x^2 for x in 1:10 if x > 2]\n",
            "g = (x for x in xs)\n",
            "a; b\n",
            "a;;;b;;\n",
            ";a\n",
        ] {
            assert_lossless(input);
        }
    }

    #[test]
    fn groups_top_level_semicolon_line() {
        let out = parse("a; b\nc\n");
        let kinds: Vec<_> = out.cst.children().map(|n| n.kind()).collect();
        use crate::syntax::SyntaxKind::{NAME, TOPLEVEL_SEMICOLON};
        assert_eq!(kinds, vec![TOPLEVEL_SEMICOLON, NAME]);
    }

    #[test]
    fn builds_expected_top_level_shape() {
        let out = parse("x = 1 + 2\n");
        let kinds: Vec<_> = out.cst.children().map(|n| n.kind()).collect();
        assert_eq!(kinds, vec![crate::syntax::SyntaxKind::ASSIGNMENT_EXPR]);
        assert!(out.diagnostics.is_empty());
    }
}