fatou-parser 0.10.0

Lossless CST parser, typed AST wrappers, and incremental reparser for the Julia language
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
//! Macro calls: `@m`, `@m(a, b)`, `@m a b`, `@Mod.mac x`, and the `var"..."` macro-name form.
//!
//! Split out of `expr.rs`; see that module's docs for the parser as a whole.

use super::*;

/// Parse a macro call introduced by a leading `@` (`@m`, `@m(a, b)`, `@m a b`,
/// `@.`, `@Mod.mac x`) into a `MACRO_CALL` wrapping a `MACRO_NAME` and the
/// arguments. The `@` sits at `at_idx`.
pub(super) fn parse_macro(
    ctx: &ParserCtx<'_>,
    at_idx: usize,
    diagnostics: &mut Vec<ParseDiagnostic>,
    flags: ExprFlags,
) -> ExprParse {
    let mut events = vec![Event::Start(SyntaxKind::MACRO_CALL)];
    events.push(Event::Start(SyntaxKind::MACRO_NAME));
    events.push(Event::Tok(at_idx)); // `@`
    let name_end = parse_macro_name_body(ctx, &mut events, at_idx + 1, diagnostics);
    finish(&mut events, SyntaxKind::MACRO_NAME);

    let end = parse_macro_args(ctx, &mut events, name_end, diagnostics, flags, at_idx);
    finish(&mut events, SyntaxKind::MACRO_CALL);
    ExprParse {
        start: at_idx,
        end,
        events,
    }
}

/// Parse a qualified macro call `lhs.@mac args` (`Base.@time f()`). `lhs` is the
/// already-parsed module path; `dot_idx` is the `.` before the `@`. The
/// `MACRO_NAME` spans `lhs`, the `.`, the `@`, and the macro name body.
pub(super) fn parse_qualified_macro(
    ctx: &ParserCtx<'_>,
    lhs: ExprParse,
    dot_idx: usize,
    diagnostics: &mut Vec<ParseDiagnostic>,
    flags: ExprFlags,
) -> ExprParse {
    let mut events = vec![Event::Start(SyntaxKind::MACRO_CALL)];
    events.push(Event::Start(SyntaxKind::MACRO_NAME));
    events.extend(lhs.events);
    push_range(&mut events, lhs.end, dot_idx);
    events.push(Event::Tok(dot_idx)); // `.`
    let at_idx = ctx.skip_trivia(dot_idx + 1);
    push_range(&mut events, dot_idx + 1, at_idx);
    events.push(Event::Tok(at_idx)); // `@`
    // A misplaced macro sigil: the `@` names a non-final component with a glued
    // `.ident` continuation (`A.@B.x`). JuliaSyntax relocates the sigil to the
    // final component and splices a zero-width marker; record it for the
    // projector to replay.
    if ctx.token(at_idx + 1).map(|t| t.kind) == Some(TokKind::Ident)
        && ctx.token(at_idx + 2).map(|t| t.kind) == Some(TokKind::Dot)
        && ctx.token(at_idx + 3).map(|t| t.kind) == Some(TokKind::Ident)
    {
        let at_start = ctx.tokens()[at_idx].start;
        push_diagnostic(
            diagnostics,
            DiagnosticKind::MacroSigilTrailing,
            "misplaced macro sigil",
            at_start,
            at_start,
        );
    }
    let name_end = parse_macro_name_body(ctx, &mut events, at_idx + 1, diagnostics);
    finish(&mut events, SyntaxKind::MACRO_NAME);

    let end = parse_macro_args(ctx, &mut events, name_end, diagnostics, flags, lhs.start);
    finish(&mut events, SyntaxKind::MACRO_CALL);
    ExprParse {
        start: lhs.start,
        end,
        events,
    }
}

/// Whether `i` begins a `var"…"` single-quoted non-standard identifier. Julia
/// models these as `(var name)` names rather than `@var_str` string macros;
/// triple-quoted `var"""…"""` is an ordinary string macro and is excluded.
pub(crate) fn is_var_identifier_start(ctx: &ParserCtx<'_>, i: usize) -> bool {
    ctx.token(i)
        .is_some_and(|t| t.kind == TokKind::StringPrefix && t.text == "var")
        && matches!(
            ctx.token(i + 1),
            Some(t) if t.kind == TokKind::StringDelimOpen && t.text.len() == 1
        )
}

/// If `i` begins a `var"…"` single-quoted non-standard identifier — the macro
/// name in `@var"#"` (`(var @#)`) — append its `NONSTANDARD_IDENTIFIER` node to
/// `events` and return the index past it. Otherwise return `None`. Triple-quoted
/// `var"""…"""` is an ordinary `@var_str` macro, not a name, so it is excluded.
pub(crate) fn push_var_macro_name(
    ctx: &ParserCtx<'_>,
    events: &mut Vec<Event>,
    i: usize,
    diagnostics: &mut Vec<ParseDiagnostic>,
) -> Option<usize> {
    if is_var_identifier_start(ctx, i) {
        let lit = parse_string_literal(ctx, i, diagnostics);
        let end = lit.end;
        events.extend(lit.events);
        Some(end)
    } else {
        None
    }
}

/// Emit the tokens of a macro name following the `@` sigil, starting at `start`:
/// either a lone `.` (the broadcast macro `@.`), an identifier followed by a
/// trailing adjacent `.ident` chain (`@Mod.mac`), or a `var"…"` non-standard
/// identifier (`@var"#"`). Returns the index just past the name.
fn parse_macro_name_body(
    ctx: &ParserCtx<'_>,
    events: &mut Vec<Event>,
    start: usize,
    diagnostics: &mut Vec<ParseDiagnostic>,
) -> usize {
    if let Some(end) = push_var_macro_name(ctx, events, start, diagnostics) {
        return end;
    }
    match ctx.token(start).map(|t| t.kind) {
        // The broadcast macro `@.` — the name is the single `.` token.
        Some(TokKind::Dot) => {
            events.push(Event::Tok(start));
            start + 1
        }
        Some(TokKind::Ident) => {
            events.push(Event::Tok(start));
            let mut i = start + 1;
            // Adjacent dotted-path continuation, no whitespace skipping (a space
            // before a `.` makes it a broadcast argument, not the name). Each step
            // is `.ident` (`@Mod.mac`), `.$ident` (an interpolated component, valid
            // when non-final), or `.@ident` (a misplaced inner sigil). The final
            // component carries the macro sigil; a `$` final or any inner/extra `@`
            // makes the path invalid, recovered by JuliaSyntax with zero-width
            // markers — recorded below and replayed by the projector.
            let mut saw_dollar_final = false;
            let mut saw_inner_at = false;
            while ctx.token(i).map(|t| t.kind) == Some(TokKind::Dot) {
                match ctx.token(i + 1).map(|t| t.kind) {
                    Some(TokKind::Ident) => {
                        events.push(Event::Tok(i)); // `.`
                        events.push(Event::Tok(i + 1)); // ident
                        i += 2;
                        saw_dollar_final = false;
                    }
                    Some(TokKind::Dollar)
                        if ctx.token(i + 2).map(|t| t.kind) == Some(TokKind::Ident) =>
                    {
                        events.push(Event::Tok(i)); // `.`
                        events.push(Event::Tok(i + 1)); // `$`
                        events.push(Event::Tok(i + 2)); // ident
                        i += 3;
                        saw_dollar_final = true;
                    }
                    Some(TokKind::At)
                        if ctx.token(i + 2).map(|t| t.kind) == Some(TokKind::Ident) =>
                    {
                        events.push(Event::Tok(i)); // `.`
                        events.push(Event::Tok(i + 1)); // `@`
                        events.push(Event::Tok(i + 2)); // ident
                        i += 3;
                        saw_dollar_final = false;
                        saw_inner_at = true;
                    }
                    _ => break,
                }
            }
            if saw_dollar_final || saw_inner_at {
                let at_start = ctx.tokens()[start - 1].start;
                push_diagnostic(
                    diagnostics,
                    DiagnosticKind::MacroSigilLeading,
                    "invalid qualified macro name",
                    at_start,
                    at_start,
                );
            }
            i
        }
        // A parenthesized macro name `@(A)`: a single identifier wrapped in
        // parens unwraps to the bare name `@A` (interior whitespace is allowed:
        // `@( A )`). The parens are kept in the CST for losslessness; the
        // projector reads only the identifier component. Anything other than a
        // lone identifier (`@(A.b)`, `@(f(x))`) is left for the paren-arg form to
        // handle (it stays a divergence, matching Julia's error recovery).
        Some(TokKind::LParen) => {
            let inner = ctx.skip_ws(start + 1);
            if ctx.token(inner).map(|t| t.kind) == Some(TokKind::Ident) {
                let after = ctx.skip_ws(inner + 1);
                if ctx.token(after).map(|t| t.kind) == Some(TokKind::RParen) {
                    push_range(events, start, after + 1);
                    return after + 1;
                }
            }
            start
        }
        // A `[`/`{` directly after `@` is an invalid macro name: JuliaSyntax
        // parses the bracketed expression and error-wraps it as the name
        // (`@[x] y z` ⇒ `(macrocall (error (vect x)) y z)`, `@{x} y` ⇒
        // `(macrocall (error (braces x)) y)`). Parse it as the name body, record
        // `InvalidMacroName`, and let the space-form arguments follow. (A name
        // identifier before the bracket — `@m[a]` — never reaches here; the
        // `Ident` arm consumes the name and `[a]` becomes the argument.)
        Some(TokKind::LBracket | TokKind::LBrace) => {
            if let Some(expr) = parse_prefix(ctx, start, diagnostics, ExprFlags::default()) {
                let name_start = ctx.tokens()[start].start;
                let end = expr.end;
                events.extend(expr.events);
                push_diagnostic(
                    diagnostics,
                    DiagnosticKind::InvalidMacroName,
                    "invalid macro name",
                    name_start,
                    name_start,
                );
                end
            } else {
                start
            }
        }
        // An operator, `$`, or keyword directly after `@` names the macro
        // (`@+`, `@!`, `@..`, `@$`, `@end`). A lone `:` (`@:`) is left to error.
        Some(k) if is_macro_name_token(k) => {
            events.push(Event::Tok(start));
            start + 1
        }
        // A bare `@` with no name — emit nothing more; the MACRO_NAME holds just
        // the sigil (still lossless).
        _ => start,
    }
}

/// Whether `kind`, directly after `@`, names the macro: any operator name, the
/// `$` sigil, or a keyword (`@+`, `@!`, `@..`, `@$`, `@end`). `.` is excluded —
/// it is the broadcast macro `@.`, handled before this — and `:` is excluded so
/// `@:` falls through to error recovery (Julia rejects it).
fn is_macro_name_token(kind: TokKind) -> bool {
    !matches!(kind, TokKind::Dot | TokKind::Colon)
        && (is_op_name(kind)
            || is_value_operator(kind)
            || kind == TokKind::Dollar
            || kind.is_keyword())
}

/// Parse the arguments of a macro call after its name (which ends at `name_end`)
/// into `events`, returning the index just past the last argument. Two forms: a
/// `(` adjacent to the name opens a comma-separated `ARG_LIST` (call-like);
/// otherwise the arguments are space-separated expressions. A macro starting
/// the first expression in bare parentheses may continue onto following lines.
fn parse_macro_args(
    ctx: &ParserCtx<'_>,
    events: &mut Vec<Event>,
    name_end: usize,
    diagnostics: &mut Vec<ParseDiagnostic>,
    flags: ExprFlags,
    macro_start: usize,
) -> usize {
    let inside_brackets = flags.inside_brackets;
    let generator_for_ends = flags.generator_for_ends;
    let macro_newlines = ctx.options.syntax_edition >= crate::parser::SyntaxEdition::Julia114
        && flags.paren_content_start == Some(macro_start);
    // Paren form `@m(a, b)`: the `(` must be adjacent (no whitespace), otherwise
    // `@m (a, b)` is the space form with a single parenthesized argument.
    if ctx.token(name_end).map(|t| t.kind) == Some(TokKind::LParen) {
        // Marker propagation into macro-call arguments (`a[@m(end)]`) is deferred.
        let (list_events, end) = parse_arg_list(
            ctx,
            name_end,
            TokKind::RParen,
            SyntaxKind::ARG_LIST,
            false,
            true,
            diagnostics,
        );
        events.extend(list_events);
        return end;
    }

    // Bracket form `@m[a]`/`@m{a}`: a `[`/`{` adjacent to the macro name (no
    // whitespace, else `name_end` points at the whitespace token) is the single
    // argument. Postfix operators chain onto the whole macrocall, not the bracket
    // (`@m[a].b` ⇒ `(. (macrocall @m (vect a)) (quote b))`, `@m[a](x)` ⇒
    // `(call (macrocall @m (vect a)) x)`), so parse only the bracket prefix here
    // and let the outer postfix chain attach any suffix.
    if matches!(
        ctx.token(name_end).map(|t| t.kind),
        Some(TokKind::LBracket | TokKind::LBrace)
    ) {
        let arg_flags = ExprFlags {
            inside_brackets,
            ..ExprFlags::default()
        };
        if let Some(arg) = parse_prefix(ctx, name_end, diagnostics, arg_flags) {
            events.extend(arg.events);
            return arg.end;
        }
    }

    // Space form `@m a b`: each argument is a full expression parsed
    // space-sensitively (`array_mode`), so a whitespace-preceded `(`/`[`/`{` or a
    // space-glued prefix operator begins the next argument (`@m f (x)` is two
    // arguments, `@m a +b` likewise). Stop at a newline, a line comment (which
    // runs to end of line, so no argument can follow it), end of input, or a
    // delimiter that closes/separates an enclosing list. The terminating trivia
    // is left for the caller to attach, so it must not be consumed here. A
    // `#= … =#` block comment is horizontal whitespace between arguments, not a
    // terminator like the line comment it sits beside here (`@test #=T=# f(x)`,
    // `@newinterp Interp #=ephemeral_cache=#true`).
    let mut pos = name_end;
    let mut n_args = 0;
    loop {
        let mut next = ctx.skip_ws_and_block_comments(pos);
        if macro_newlines
            && matches!(
                ctx.token(next).map(|t| t.kind),
                Some(TokKind::Newline | TokKind::Comment)
            )
        {
            let after = ctx.skip_trivia(next);
            // Operators on a following line apply to the completed macrocall.
            // A following `for` likewise belongs to an enclosing generator.
            // Leave all separating trivia with that enclosing expression.
            if ctx.token(after).is_none_or(|t| {
                t.kind == TokKind::ForKw
                    || is_operator(t.kind)
                    || is_quotable_operator(t.kind)
                    || is_value_operator(t.kind)
                    || is_unary_prefix_op(t.kind, t.text)
                    || t.kind == TokKind::DotDotDot
                    || word_operator(ctx, after, false).is_some()
            }) {
                break;
            }
            next = after;
        }
        match ctx.token(next).map(|t| t.kind) {
            None
            | Some(
                TokKind::Newline
                | TokKind::Comment
                | TokKind::Comma
                | TokKind::RParen
                | TokKind::RBracket
                | TokKind::RBrace
                | TokKind::Semicolon,
            ) => break,
            // In a generator-bearing position, a `for` after the arguments opens a
            // generator (`[@inbounds f(x) for x in xs]`, `g(@m a for a in as)`), so
            // it ends the macro's space-args. At statement scope `for` is instead a
            // for-loop argument (`@time for i in xs … end`). Plain parentheses
            // inherit that distinction independently of newline sensitivity.
            Some(TokKind::ForKw) if generator_for_ends => break,
            // A dot followed by `(` belongs to the completed macro call's
            // postfix chain. Let it build the dedicated invalid broadcast-macro
            // recovery (`@M.(x)`), rather than parsing the newly supported lone
            // `.` error atom as the macro's first space argument.
            Some(TokKind::Dot)
                if ctx
                    .token(ctx.skip_ws_and_block_comments(next + 1))
                    .is_some_and(|t| t.kind == TokKind::LParen) =>
            {
                break;
            }
            _ => {
                // A bare comma after a space-form argument folds it into a
                // bare-tuple argument rather than separating arguments
                // (`@show a, b` ⇒ `(macrocall @show (tuple a b))`, `@show a, b =
                // c` ⇒ `(macrocall @show (= (tuple a b) c))`). The macro grabs
                // the comma greedily even inside an enclosing list, so it wins
                // over the container's separator (`[@m a, b]` ⇒ `(vect (macrocall
                // @m (tuple a b)))`, `f(@m a, b)` ⇒ `(call f (macrocall @m (tuple
                // a b)))`). `stmt_comma` here drives that; the container never
                // sees the comma because the argument consumes it.
                let arg_flags = ExprFlags {
                    inside_brackets,
                    array_mode: true,
                    generator_for_ends,
                    stmt_comma: true,
                    ..ExprFlags::default()
                };
                match parse_expr_in(ctx.tokens(), next, 0, diagnostics, arg_flags) {
                    Some(arg) => {
                        // Commit the inter-argument trivia only once an argument
                        // actually parses. When nothing does (a block-closing
                        // `end`/`else`/`catch`/… where an argument was expected),
                        // the whitespace is terminating trivia the caller attaches;
                        // pushing it here would duplicate it (once in the macrocall,
                        // once in the enclosing block) and break losslessness.
                        push_range(events, pos, next);
                        events.extend(arg.events);
                        pos = arg.end;
                        n_args += 1;
                    }
                    None => break,
                }
            }
        }
    }

    // `@doc` extension: when the doc macro takes exactly one space-separated
    // argument and the next line carries another expression, it is consumed as a
    // second argument (`@doc x\ny` ⇒ `(macrocall @doc x y)`). A blank line, a
    // closing token, or end of input on the next line stops it. Matches
    // JuliaSyntax's doc-macro rule; the name's leaf identifier must be `doc`
    // (`@doc`, `A.@doc`, `@A.doc`).
    if n_args == 1 && macro_leaf_is_doc(ctx, name_end) {
        let nl = ctx.skip_ws(pos);
        if ctx.token(nl).map(|t| t.kind) == Some(TokKind::Newline) {
            let after = ctx.skip_ws(nl + 1);
            let extend = !matches!(
                ctx.token(after).map(|t| t.kind),
                None | Some(
                    TokKind::Newline
                        | TokKind::Comma
                        | TokKind::Semicolon
                        | TokKind::RParen
                        | TokKind::RBracket
                        | TokKind::RBrace
                        | TokKind::EndKw
                        | TokKind::ElseKw
                        | TokKind::ElseifKw
                        | TokKind::CatchKw
                        | TokKind::FinallyKw
                )
            );
            if extend {
                push_range(events, pos, after);
                let arg_flags = ExprFlags {
                    inside_brackets,
                    array_mode: true,
                    generator_for_ends,
                    stmt_comma: true,
                    ..ExprFlags::default()
                };
                if let Some(arg) = parse_expr_in(ctx.tokens(), after, 0, diagnostics, arg_flags) {
                    events.extend(arg.events);
                    pos = arg.end;
                }
            }
        }
    }
    pos
}

/// Whether the macro name ending at `name_end` has the leaf identifier `doc` —
/// the special doc macro (`@doc`, `A.@doc`, `@A.doc`), whose single-argument
/// form is extended with the next line's expression in [`parse_macro_args`].
fn macro_leaf_is_doc(ctx: &ParserCtx<'_>, name_end: usize) -> bool {
    name_end > 0
        && ctx
            .token(name_end - 1)
            .is_some_and(|t| t.kind == TokKind::Ident && t.text == "doc")
}