Skip to main content

blue_lang_syntax/
parse.rs

1//! Precedence-climbing parser, lowering straight to the tatara-lisp
2//! quoted form.
3//!
4//! **The load-bearing design decision, made here and once:** the parser's
5//! output IS a `tatara_lisp::Sexp`. There is no private blue AST that later
6//! gets converted. That is Tenet 1 — *blue source parses to tatara-lisp* —
7//! and building it any other way would make homoiconicity a conversion step
8//! rather than an identity, which is the difference between blue's macro
9//! story working and merely being claimed.
10//!
11//! The consequence to keep in view: every surface construct must have a
12//! well-defined s-expression it means. Where the mapping is not obvious it
13//! is written down in the test module, because the tests are the
14//! specification of the surface until the mechanized spec exists.
15//!
16//! ## Spans are built, not bolted on
17//!
18//! Every production returns a [`Spanned`] — tatara-lisp's parallel tree where
19//! **each node** carries the byte range that produced it — and the spanless
20//! [`parse_program`] is a projection of it via `Spanned::to_sexp`.
21//!
22//! That direction is deliberate and it is the second time this file has learned
23//! the lesson: the spanless tree is derivable from the spanned one and the
24//! reverse is not. When the parser produced `Sexp` and callers wanted positions,
25//! the only answers available were *invent one* (a squiggle under unrelated
26//! code) or *use nothing* — and the type checker took the second, so every type
27//! error in the editor was reported at line 0, column 0 regardless of where it
28//! was. A span discarded at the parser cannot be recovered by anything
29//! downstream, exactly as [`crate::lex`] says of trivia.
30//!
31//! Two honest limits, both stated where they are caused rather than inferred:
32//!
33//! - A node blue **synthesizes** (the `define` a `def` lowers to, the `not` an
34//!   `unless` lowers to, the `equal?` a `case` arm compares with) has no source
35//!   text of its own. It is given the span of the token that *caused* it — the
36//!   `def`, the `unless`, the `when` — which is where a reader looks when told
37//!   something about it.
38//! - An interpolation's inner expression is parsed from a **sub-source** (the
39//!   text between `#{` and `}`), so its own offsets are relative to that
40//!   substring and would point into the wrong buffer. Those nodes are stamped
41//!   with the whole string literal's span instead. Sub-span accuracy inside an
42//!   interpolation needs the lexer to record each part's offset in the outer
43//!   source, which it does not.
44
45use tatara_lisp::{Atom, Sexp, Spanned, SpannedForm};
46
47use crate::lex::{lex, Span, Token, TokenKind};
48use crate::spelling::{Note, Spelling};
49
50/// Why a source did not parse, and where.
51///
52/// **Structured, and located at the boundary.** `message` says what was
53/// expected and what was found, in blue's own words; `span` is the byte range
54/// of the offending text; `line` and `col` are that range's start, 1-based,
55/// with the column counted in characters. Every public entry point fills
56/// `line`/`col` from the source it was given, so `Display` reads
57/// `3:7: expected `)`, found `]`` and never a byte offset.
58///
59/// The byte offsets used to BE the display (`… at 10..11`), and a Rust
60/// `Debug` rendering named the token (`found Ident("y")`). Both are facts
61/// about the implementation; neither is something an author can act on.
62/// The span stays, as data, for any consumer that needs to point into the
63/// buffer — an editor squiggle, a diagnostic code attached downstream.
64#[derive(Clone, Debug, PartialEq)]
65pub struct ParseError {
66    pub message: String,
67    pub span: Span,
68    /// 1-based line of `span.start`.
69    pub line: usize,
70    /// 1-based column of `span.start`, in characters.
71    pub col: usize,
72}
73
74impl ParseError {
75    /// An error at `span`, not yet located: the parser sees tokens, not text,
76    /// so the line and column are filled by [`Self::located`] at the entry
77    /// point that holds the source.
78    fn new(message: impl Into<String>, span: Span) -> Self {
79        Self {
80            message: message.into(),
81            span,
82            line: 0,
83            col: 0,
84        }
85    }
86
87    /// Resolve `line`/`col` against the source this error's span indexes.
88    fn located(mut self, src: &str) -> Self {
89        let (line, col) = Span::line_col(src, self.span.start.min(src.len()));
90        self.line = line;
91        self.col = col;
92        self
93    }
94}
95
96impl std::fmt::Display for ParseError {
97    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
98        write!(f, "{}:{}: {}", self.line, self.col, self.message)
99    }
100}
101
102impl std::error::Error for ParseError {}
103
104impl From<crate::lex::LexError> for ParseError {
105    fn from(e: crate::lex::LexError) -> Self {
106        Self::new(e.message, e.span)
107    }
108}
109
110thread_local! {
111    static PARSES: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
112}
113
114/// How many programs this thread has parsed: an independent count for a
115/// caller that promises to parse a text once, read off the parser itself
116/// rather than off the caller's own bookkeeping.
117#[must_use]
118pub fn parses_on_this_thread() -> u64 {
119    PARSES.with(std::cell::Cell::get)
120}
121
122/// Parse a blue program into a sequence of tatara-lisp forms.
123pub fn parse_program(src: &str) -> Result<Vec<Sexp>, ParseError> {
124    parse_program_with_depth(src, MAX_EXPR_DEPTH)
125}
126
127/// Parse a blue program keeping **every node's** source span.
128///
129/// This is the parser's real output; [`parse_program`] projects the spans away.
130/// Reach for this one whenever a position is going to be shown to a human — an
131/// editor squiggle, a `line:col` in a CLI error, a future debugger's breakpoint
132/// table. Reach for [`parse_program`] only when the consumer genuinely does not
133/// care where anything was.
134pub fn parse_program_tree(src: &str) -> Result<Vec<Spanned>, ParseError> {
135    parse_program_tree_with_depth(src, MAX_EXPR_DEPTH)
136}
137
138/// [`parse_program_tree`] with the nesting bound supplied by the caller.
139pub fn parse_program_tree_with_depth(
140    src: &str,
141    max_depth: usize,
142) -> Result<Vec<Spanned>, ParseError> {
143    PARSES.with(|n| n.set(n.get() + 1));
144    let mut p = Parser::over(
145        src,
146        lex(src).map_err(|e| ParseError::from(e).located(src))?,
147        max_depth,
148    );
149    p.program().map_err(|e| e.located(src))
150}
151
152pub fn spelling_notes(src: &str) -> Result<Vec<Note>, ParseError> {
153    let mut p = Parser::over(
154        src,
155        lex(src).map_err(|e| ParseError::from(e).located(src))?,
156        MAX_EXPR_DEPTH,
157    );
158    p.program().map_err(|e| e.located(src))?;
159    Ok(p.notes)
160}
161
162/// Parse a program written in a [`Yakugo`](crate::yakugo::Yakugo) surface.
163///
164/// The pack is applied to the TOKEN STREAM, between lexing and parsing, so the
165/// parser below is untouched and every keyword site works by construction.
166/// What comes out is the same `Sexp` the English surface produces — that is
167/// the invariant the pack tests assert, and the only thing that makes a
168/// surface a surface rather than a dialect.
169///
170/// # Errors
171///
172/// Lex and parse errors propagate unchanged. A pack cannot repair source that
173/// does not tokenize, and reporting otherwise would name the wrong problem.
174pub fn parse_program_in(src: &str, pack: &crate::yakugo::Yakugo) -> Result<Vec<Sexp>, ParseError> {
175    Ok(parse_program_tree_in(src, pack)?
176        .iter()
177        .map(Spanned::to_sexp)
178        .collect())
179}
180
181/// [`parse_program_in`] keeping **every node's** source span.
182///
183/// The same relation [`parse_program_tree`] has to [`parse_program`], and it
184/// exists for the same reason: a surface is a spelling, so a program written in
185/// one has positions exactly as real as an English-surface program's. Without
186/// this door, `pipeline::run_in_surface` would have to lift a spanless tree back
187/// up and every type error in a `yakugo` program would report `<synthetic>` —
188/// a position the parser HAD and threw away one line earlier, which is the
189/// class `Diagnostic::span`'s own docs name.
190///
191/// [`parse_program_in`] is now its projection, so the two cannot drift.
192///
193/// # Errors
194///
195/// As [`parse_program_in`].
196pub fn parse_program_tree_in(
197    src: &str,
198    pack: &crate::yakugo::Yakugo,
199) -> Result<Vec<Spanned>, ParseError> {
200    let toks =
201        crate::yakugo::canonical_tokens(src, pack).map_err(|e| ParseError::from(e).located(src))?;
202    PARSES.with(|n| n.set(n.get() + 1));
203    let mut p = Parser::over(src, toks, MAX_EXPR_DEPTH);
204    p.program().map_err(|e| e.located(src))
205}
206
207/// [`parse_program`] with the nesting bound supplied by the caller.
208///
209/// The bound is a **safety limit, not a dialect**: `max_depth` cannot change
210/// what any program means, only whether a pathological one is refused before
211/// the stack is at risk. That is why it is a parameter here and
212/// [`MAX_EXPR_DEPTH`] is only the default — a knob that could alter meaning
213/// would belong nowhere near a config file (see `blue-lang-cli`'s `config`
214/// module for the rule this obeys).
215pub fn parse_program_with_depth(src: &str, max_depth: usize) -> Result<Vec<Sexp>, ParseError> {
216    Ok(parse_program_tree_with_depth(src, max_depth)?
217        .iter()
218        .map(Spanned::to_sexp)
219        .collect())
220}
221
222/// Parse, keeping each top-level form's source span.
223///
224/// The spans are what let the formatter put comments back. A comment is not
225/// part of a program's *meaning*, so it has no place in the `Sexp` tree —
226/// putting it there would break canonicality, since two programs differing only
227/// in a comment would stop formatting identically. Instead the formatter renders
228/// the tree and re-interleaves comments by position, which needs to know where
229/// each form started and ended.
230///
231/// A projection of [`parse_program_tree`], which carries a span on every node
232/// rather than only the top-level ones. This entry point survives because the
233/// formatter wants exactly the top-level pairing and nothing deeper.
234pub fn parse_program_spanned(src: &str) -> Result<Vec<(Sexp, Span)>, ParseError> {
235    parse_program_spanned_with_depth(src, MAX_EXPR_DEPTH)
236}
237
238/// [`parse_program_spanned`] with the nesting bound supplied by the caller.
239pub fn parse_program_spanned_with_depth(
240    src: &str,
241    max_depth: usize,
242) -> Result<Vec<(Sexp, Span)>, ParseError> {
243    Ok(parse_program_tree_with_depth(src, max_depth)?
244        .iter()
245        .map(|f| (f.to_sexp(), f.span))
246        .collect())
247}
248
249/// Every comment in `src`, with its byte span and whether it sits alone on its
250/// line.
251///
252/// `own_line` is the distinction that decides placement: a comment alone on its
253/// line belongs *before* the form that follows, while one after code on the same
254/// line belongs *to* that line. Conflating them moves a trailing note onto the
255/// wrong row.
256pub fn comments(src: &str) -> Vec<Comment> {
257    lex(src)
258        .map(|toks| {
259            toks.iter()
260                .filter_map(|t| match &t.kind {
261                    TokenKind::Comment(text) => Some(Comment {
262                        text: text.clone(),
263                        span: t.span,
264                        own_line: line_before_is_blank(src, t.span.start),
265                    }),
266                    _ => None,
267                })
268                .collect()
269        })
270        .unwrap_or_default()
271}
272
273/// A comment, and where it sat.
274#[derive(Clone, Debug, PartialEq, Eq)]
275pub struct Comment {
276    /// The text, including the leading `#`.
277    pub text: String,
278    pub span: Span,
279    /// Nothing but whitespace precedes it on its line.
280    pub own_line: bool,
281}
282
283fn line_before_is_blank(src: &str, start: usize) -> bool {
284    src[..start.min(src.len())]
285        .rsplit('\n')
286        .next()
287        .is_some_and(|prefix| prefix.trim().is_empty())
288}
289
290/// Parse a single blue expression. Convenience for tests and the REPL.
291pub fn parse_expr(src: &str) -> Result<Sexp, ParseError> {
292    let forms = parse_program(src)?;
293    match forms.len() {
294        1 => Ok(forms.into_iter().next().expect("checked len")),
295        n => Err(ParseError::new(
296            format!("expected exactly one expression, found {n}"),
297            Span::new(0, src.len()),
298        )
299        .located(src)),
300    }
301}
302
303/// What an infix operator binds like, and what it lowers to.
304///
305/// **One row per operator, carrying both facts.** Precedence and callee
306/// used to live apart, and the cost was immediate: the surface spelling was
307/// emitted verbatim, so `a == b` lowered to `(== a b)` — a symbol no
308/// interpreter binds — and the program died at *runtime* with `unbound
309/// symbol ==`. Splitting the two invites exactly that: add an operator to
310/// the precedence table, forget the lowering, ship a parse that cannot run.
311/// Joined here, "has a precedence" and "has a callee" are the same fact.
312#[derive(Clone, Copy, Debug)]
313pub struct Infix {
314    /// Surface spelling.
315    pub op: &'static str,
316    /// Left and right binding power. Higher binds tighter.
317    pub power: (u8, u8),
318    /// The tatara-lisp callee this lowers to.
319    pub callee: &'static str,
320    /// One line for a reader: what the operator computes, and the trap in it
321    /// when there is one. `blue reference` prints it; a row without one fails
322    /// `every_operator_and_keyword_is_described`.
323    pub doc: &'static str,
324}
325
326/// The complete infix table. Precedence follows Ruby's where Ruby has an
327/// opinion; `|>` (below) sits under everything so `a |> f |> g` chains
328/// without parentheses.
329pub const INFIX: &[Infix] = &[
330    Infix {
331        op: "or",
332        power: (1, 2),
333        callee: "or",
334        doc: "Logical or: the left value if truthy, otherwise the right; the right is evaluated only when needed. `||` is read as `or` and written `or` (B0025).",
335    },
336    Infix {
337        op: "and",
338        power: (3, 4),
339        callee: "and",
340        doc: "Logical and: the right value if the left is truthy, otherwise the left; short-circuits. `&&` is read as `and` and written `and` (B0025).",
341    },
342    // `==` is STRUCTURAL equality, so it lowers to `equal?` and not to `=`.
343    //
344    // tatara's `=` is NUMERIC comparison: `"a" = "a"` is a type error, not
345    // false. Lowering `==` to it meant every string, list or nil comparison
346    // failed with "expected number, got string" — found by blue's own spec
347    // suite the moment a test compared two strings, which is the first thing
348    // anybody does.
349    //
350    // `equal?` is structural and total over the value domain: strings, lists
351    // and nil all compare, and numbers still compare as numbers.
352    Infix {
353        op: "==",
354        power: (5, 6),
355        callee: "equal?",
356        doc: "Structural equality over every value (okite D0001): 4 == 4.0, lists and maps by content, nil only equals nil.",
357    },
358    Infix {
359        op: "!=",
360        power: (5, 6),
361        callee: "not=",
362        doc: "Exactly not(a == b) (okite D0002).",
363    },
364    Infix {
365        op: "<",
366        power: (5, 6),
367        callee: "<",
368        doc: "Numeric less-than. A string operand is a type error: order text with compare(a, b).",
369    },
370    Infix {
371        op: "<=",
372        power: (5, 6),
373        callee: "<=",
374        doc: "Numeric less-than-or-equal. Numbers only, like <.",
375    },
376    Infix {
377        op: ">",
378        power: (5, 6),
379        callee: ">",
380        doc: "Numeric greater-than. Numbers only, like <.",
381    },
382    Infix {
383        op: ">=",
384        power: (5, 6),
385        callee: ">=",
386        doc: "Numeric greater-than-or-equal. Numbers only, like <.",
387    },
388    Infix {
389        op: "+",
390        power: (7, 8),
391        callee: "+",
392        doc: "Numeric addition; an Int and a Float give a Float. Not string concatenation: interpolate instead.",
393    },
394    Infix {
395        op: "-",
396        power: (7, 8),
397        callee: "-",
398        doc: "Numeric subtraction. Written before an operand with nothing on its left, it is unary minus: -n.",
399    },
400    Infix {
401        op: "*",
402        power: (9, 10),
403        callee: "*",
404        doc: "Numeric multiplication.",
405    },
406    Infix {
407        op: "/",
408        power: (9, 10),
409        callee: "/",
410        doc: "Division that never truncates (okite D0008): 7 / 2 is 3.5, while 6 / 2 stays the Int 3. Integer division is floor(a / b).",
411    },
412    Infix {
413        op: "%",
414        power: (9, 10),
415        callee: "mod",
416        doc: "Euclidean remainder: never negative, so (0 - 7) % 3 is 2, not -1. A zero divisor is a runtime error.",
417    },
418];
419
420/// Every surface keyword that BEGINS an expression.
421///
422/// Exists so the formatter's corpus can be checked against it. Three separate
423/// times a form was added to this parser, the formatter was not extended, and
424/// all three formatting laws still passed — because the corpus is
425/// hand-maintained and contained no example of the new form. The annotated
426/// `def` rendered as a method send; `defmacro` rendered as
427/// `defmacro(double, x(), …)`, which does not re-parse at all.
428///
429/// A law cannot notice a case nobody wrote down. `blue-lang-fmt`'s
430/// `every_surface_keyword_appears_in_the_corpus` closes that by making the
431/// omission itself the failure, so adding a keyword here forces the corpus
432/// entry that exercises the other three laws over it.
433///
434/// `do` and `end` are absent deliberately: they are block delimiters, not
435/// expression heads, and have no standalone rendering to test.
436/// The callee `assert e` lowers to.
437///
438/// **Owned here, by the lowering itself, and read by every consumer.** It was
439/// briefly a literal here and a separate `const` in `blue-lang-test`, and the
440/// shadowing gate then could not see a parser that lowered to the wrong name:
441/// the gate checked its own copy. Same duplication class as the operator table.
442pub const LOWERED_ASSERT: &str = "blue-assert";
443
444/// The callee a `{...}` map literal lowers to.
445///
446/// **`hash-map`, not `map`.** tatara binds `map` to the higher-order function,
447/// so a literal lowering to `(map …)` called the HOF with the key/value pairs
448/// as arguments and failed with "expected list, got int". Same shadowing class
449/// as [`LOWERED_ASSERT`], caught by the same gate once the name was listed
450/// there — it was not, which is why this shipped.
451pub const LOWERED_MAP: &str = "hash-map";
452
453/// The callee string interpolation lowers to.
454///
455/// blue's own `concat`, which renders either side through `to_s` — that is what
456/// lets `"n=#{42}"` interpolate a number. Not `+`, which is arithmetic.
457pub const LOWERED_CONCAT: &str = "concat";
458
459pub const LOWERED_FIELD: &str = "blue-field";
460
461pub const LOWERED_ERROR_MATCH: &str = "blue-rescues?";
462
463pub const LOWERED_ENSURE: &str = "blue-ensure";
464
465pub const LOWERED_RERAISE: &str = "blue-reraise";
466
467pub const LOWERED_ATTEMPTED: &str = "blue-attempted";
468
469pub const LOWERED_FAILED: &str = "blue-failed";
470
471pub const RESCUED: &str = "rescued-error";
472
473pub const SURFACE_KEYWORDS: &[&str] = &[
474    "if",
475    "begin",
476    "unless",
477    "def",
478    "defmacro",
479    "quote",
480    "unquote",
481    "unquote_splice",
482    "test",
483    "assert",
484    "fn",
485    "case",
486];
487
488/// The words that are reserved but are not heads of a `SURFACE_KEYWORDS` form
489/// — block delimiters and the three literals.
490///
491/// Split out as data rather than inlined in a `matches!` because a reserved
492/// word is not only the parser's business: a highlighter has to paint exactly
493/// this set, and one that reads `SURFACE_KEYWORDS` alone silently under-paints
494/// `do` / `end` / `else` / `elsif`. That was not hypothetical — escriba's
495/// `escriba-render/src/langs.rs` hand-transcribed "SURFACE_KEYWORDS plus the
496/// four block words `is_reserved_word` adds", because the union was
497/// unreachable from outside this module. A transcription is a second
498/// definition, and it drifts the first time this list changes.
499pub const BLOCK_KEYWORDS: &[&str] = &[
500    "do", "end", "else", "elsif", "rescue", "ensure", "true", "false", "nil",
501];
502
503pub const OPERATOR_KEYWORDS: &[&str] = &["and", "or", "not"];
504
505/// A surface keyword may not be rebound. `if = 1` is a mistake, not a binding,
506/// and letting it through would shadow the form for the rest of the file.
507///
508/// Public because it is the ONE answer to "is this identifier a keyword" —
509/// the parser's rebinding check and every downstream highlighter read it here
510/// rather than each carrying its own copy of the list.
511pub fn is_reserved_word(name: &str) -> bool {
512    SURFACE_KEYWORDS.contains(&name)
513        || BLOCK_KEYWORDS.contains(&name)
514        || OPERATOR_KEYWORDS.contains(&name)
515}
516
517/// Can `name` be written as the head of a call — `name(…)`?
518///
519/// True exactly when the lexer reads `name` as one identifier and it is not
520/// reserved. Answered BY the lexer rather than by a character class copied
521/// from it, so the two cannot disagree: the formatter asks this before
522/// printing a tree as `name(args)`, and a head such as `hash-map`, `not=` or
523/// `if` — which a tree can carry and a call cannot spell — must get a
524/// different rendering or none.
525pub fn is_callable_name(name: &str) -> bool {
526    // `retsu/first` is written `retsu::first(…)`.
527    if qualified(name).is_some() {
528        return true;
529    }
530    !is_reserved_word(name)
531        && matches!(
532            lex(name).as_deref(),
533            Ok([t, e]) if matches!(&t.kind, TokenKind::Ident(n) if n == name)
534                && e.kind == TokenKind::Eof
535        )
536}
537
538/// The character a qualified name's AST symbol joins its two halves with:
539/// `retsu::first` is the symbol `retsu/first`. The surface never writes it
540/// (`/` is division, and no identifier can contain it), which is what makes
541/// a symbol with text on both sides of one `/` unambiguously qualified.
542pub const QUALIFIER: char = '/';
543
544/// The qualifier that names a builtin: `blue::count` is the interpreter's
545/// `count`, whatever a program defines. No package may be named it.
546pub const BUILTIN_QUALIFIER: &str = "blue";
547
548/// The AST symbol for `pkg::name`.
549#[must_use]
550pub fn qualify(pkg: &str, name: &str) -> String {
551    format!("{pkg}{QUALIFIER}{name}")
552}
553
554/// `retsu/first` → `Some(("retsu", "first"))`: the package and the name of a
555/// qualified symbol. `None` for every other symbol, `/` (division) included.
556///
557/// The ONE reader of the shape, so the formatter, the name table and the
558/// runtime's renderer cannot disagree about what counts as qualified.
559#[must_use]
560pub fn qualified(sym: &str) -> Option<(&str, &str)> {
561    let (pkg, name) = sym.split_once(QUALIFIER)?;
562    let ident = |s: &str| {
563        matches!(
564            lex(s).as_deref(),
565            Ok([t, e]) if matches!(&t.kind, TokenKind::Ident(n) if n == s) && e.kind == TokenKind::Eof
566        )
567    };
568    let named = |s: &str| ident(s) || matches!(s, "true" | "false" | "nil");
569    (ident(pkg) && !is_reserved_word(pkg) && named(name)).then_some((pkg, name))
570}
571
572/// One line for a reader on each reserved word: what it writes, and the shape.
573///
574/// Beside the two tables rather than inside them, because both are `&[&str]`
575/// read by the formatter's corpus gate and by highlighters outside this repo;
576/// changing their type would break every one of those readers.
577/// `every_operator_and_keyword_is_described` makes a word without a line here a
578/// red build, so the omission cannot ship. `blue reference` prints these.
579#[must_use]
580pub fn keyword_doc(word: &str) -> &'static str {
581    match word {
582        "if" => "`if c … elsif d … else … end`: an expression; its value is the branch taken, nil when none is. An `else` that is empty or `nil` is dropped (B0035).",
583        "unless" => "`unless c … end` is read as `if not c` and written that way (B0026).",
584        "begin" => "`begin … rescue :kind as e … rescue e … ensure … end`: run the statements; a `rescue` clause catches an error by kind, binding it with `as`, and `rescue e` catches every error; `ensure` always runs. A def body takes the same clauses without `begin`.",
585        "rescue" => "A clause of `begin` or `def`: `rescue :a, :b as e`, `rescue e` (every error, last). Inline, `x rescue fallback` is fallback when x raises; it binds loosest, below `|>`.",
586        "ensure" => "The last clause of `begin` or `def`: statements that run whether the block returned or raised; the block's value or error is kept.",
587        "and" => "`a and b`: logical and; `&&` is read as `and` and written `and` (B0025).",
588        "or" => "`a or b`: logical or; `||` is read as `or` and written `or` (B0025).",
589        "not" => "`not x`: true when x is false or nil; `!x` is read as `not x` and written `not x` (B0025). `not (a == b)` is written `a != b` (B0034).",
590        "def" => "`def name(a, b) … end`: a named function; its value is the last expression. Parameters may be typed: `def f(n: Int) -> Int`.",
591        "defmacro" => "`defmacro name(x) quote … unquote(x) … end end`: a macro, a rewrite of the unevaluated form.",
592        "quote" => "`quote … end`: the form itself as data, not its value. Used inside `defmacro`.",
593        "unquote" => "`unquote(x)`: inside `quote`, splice in the value of `x`.",
594        "unquote_splice" => "`unquote_splice(xs)`: inside `quote`, splice the items of the form `xs` in place, not the form itself.",
595        "test" => "`test \"what must hold\" … end`: a test block, run by `blue test` and ignored by `blue run`.",
596        "assert" => "`assert expr`: inside a test, fail it unless `expr` is truthy; the failure quotes the expression.",
597        "fn" => "`fn(x) x * 2 end`: an anonymous function, a value. Blue has no brace or do blocks.",
598        "case" => "`case v when 1 … when 2 … else … end`: compares with `==`; it does not destructure.",
599        "do" => "Reserved and refused: blue has no do-blocks. Pass `fn(x) … end` instead.",
600        "end" => "Closes `def`, `defmacro`, `if`, `case`, `fn`, `test` and `quote`.",
601        "else" => "The branch of `if` or `case` taken when nothing before it matched.",
602        "elsif" => "A further condition in an `if` chain; an `else` holding only an `if` is written `elsif` (B0027).",
603        "true" => "The boolean true.",
604        "false" => "The boolean false. Only `false` and `nil` are falsy; 0, \"\" and [] are truthy.",
605        "nil" => "The absent value. It is not `[]` (okite D0004), and only `nil == nil`.",
606        _ => "",
607    }
608}
609
610fn infix(op: &str) -> Option<&'static Infix> {
611    INFIX.iter().find(|i| i.op == op)
612}
613
614pub const SYMBOL_SPELLINGS: &[(&str, &str)] = &[("&&", "and"), ("||", "or"), ("!", "not")];
615
616fn word_for(op: &str) -> &str {
617    SYMBOL_SPELLINGS
618        .iter()
619        .find(|(sym, _)| *sym == op)
620        .map_or(op, |(_, word)| word)
621}
622
623fn infix_of(kind: &TokenKind) -> Option<&'static Infix> {
624    match kind {
625        TokenKind::Op(o) => infix(word_for(o)),
626        TokenKind::Ident(w) if w == "and" || w == "or" => infix(w),
627        _ => None,
628    }
629}
630
631const PIPE_POWER: (u8, u8) = (0, 1);
632const RESCUE_POWER: (u8, u8) = (0, 1);
633
634struct Parser {
635    toks: Vec<Token>,
636    pos: usize,
637    /// Current expression-nesting depth, bounded by [`Self::max_depth`].
638    ///
639    /// Without this the parser does not fail on deep input — it **aborts the
640    /// process** with a stack overflow (SIGABRT), which `catch_unwind` cannot
641    /// catch. Measured 2026-08-01: `"(".repeat(2_000)` killed the test runner
642    /// outright. Every consumer inherited it — an LSP parsing a half-typed
643    /// line, a formatter, and shikumi loading a `.b` config off disk.
644    depth: usize,
645    /// The bound [`Self::depth`] is checked against.
646    ///
647    /// Defaults to [`MAX_EXPR_DEPTH`] on every entry point that does not name
648    /// one; `parse_program_with_depth` exists so an operator can raise it
649    /// without recompiling. It is a **bound**, so raising it changes no
650    /// program's meaning — only which pathological inputs are refused.
651    max_depth: usize,
652    src: String,
653    comment_spans: Vec<Span>,
654    notes: Vec<Note>,
655}
656
657/// Maximum expression nesting before the parser refuses.
658///
659/// Chosen well above anything human-written (blue's own `spec/*.b` peaks in
660/// single digits) and far below the measured overflow point, so the bound is
661/// hit as a typed `Err` long before the stack is at risk. A limit that is
662/// merely *near* the crash point is not a safety bound; it is a race.
663pub const MAX_EXPR_DEPTH: usize = 256;
664
665/// The parser's internal error: a [`ParseError`] behind a pointer.
666///
667/// Every production returns a `Result`, and a debug build keeps several per
668/// frame down the recursion, so the error's size is paid at every level of
669/// nesting whether or not anything fails. Measured 2026-09-29 on a 2 MiB
670/// debug thread, parsing `(((…1…)))` with no bound: the unboxed 40-byte error
671/// overflowed between 255 and 260 levels, barely past [`MAX_EXPR_DEPTH`];
672/// adding `line`/`col` unboxed moved it to 240–250, under the bound, and
673/// `depth_beyond_the_limit_is_an_err_not_an_abort` aborted. Boxed, the
674/// overflow is between 300 and 310. The error path is cold; the pointer is
675/// what keeps the bound a bound.
676type Fail = Box<ParseError>;
677
678fn fail(message: impl Into<String>, span: Span) -> Fail {
679    Box::new(ParseError::new(message, span))
680}
681
682impl Parser {
683    fn over(src: &str, all: Vec<Token>, max_depth: usize) -> Self {
684        let comment_spans = all
685            .iter()
686            .filter(|t| matches!(t.kind, TokenKind::Comment(_)))
687            .map(|t| t.span)
688            .collect();
689        let toks = all
690            .into_iter()
691            .filter(|t| !matches!(t.kind, TokenKind::Comment(_)))
692            .collect();
693        Parser {
694            toks,
695            pos: 0,
696            depth: 0,
697            max_depth,
698            src: src.to_string(),
699            comment_spans,
700            notes: Vec::new(),
701        }
702    }
703
704    fn note(&mut self, spelling: Spelling, span: Span, written: &str, canonical: &str) {
705        self.notes.push(Note {
706            spelling,
707            span,
708            written: written.to_string(),
709            canonical: canonical.to_string(),
710        });
711    }
712
713    fn text(&self, span: Span) -> &str {
714        self.src.get(span.start..span.end).unwrap_or("")
715    }
716
717    fn comment_within(&self, lo: usize, hi: usize) -> bool {
718        self.comment_spans
719            .iter()
720            .any(|c| c.start >= lo && c.start < hi)
721    }
722
723    fn touches_previous(&self) -> bool {
724        let here = self.peek_span();
725        self.toks
726            .get(self.pos.wrapping_sub(1))
727            .is_some_and(|prev| prev.span.end == here.start)
728    }
729
730    fn peek(&self) -> &TokenKind {
731        &self.toks[self.pos.min(self.toks.len() - 1)].kind
732    }
733
734    fn peek_span(&self) -> Span {
735        self.toks[self.pos.min(self.toks.len() - 1)].span
736    }
737
738    fn bump(&mut self) -> TokenKind {
739        let k = self.toks[self.pos.min(self.toks.len() - 1)].kind.clone();
740        if self.pos < self.toks.len() {
741            self.pos += 1;
742        }
743        k
744    }
745
746    fn at(&self, k: &TokenKind) -> bool {
747        self.peek() == k
748    }
749
750    fn eat(&mut self, k: &TokenKind) -> bool {
751        if self.at(k) {
752            self.bump();
753            true
754        } else {
755            false
756        }
757    }
758
759    fn expect(&mut self, k: &TokenKind, what: &str) -> Result<(), Fail> {
760        if self.eat(k) {
761            Ok(())
762        } else {
763            Err(self.error(format!("expected {what}, found {}", self.peek())))
764        }
765    }
766
767    fn error(&self, message: impl Into<String>) -> Fail {
768        fail(message, self.peek_span())
769    }
770
771    /// Skip statement separators (newlines and semicolon-free layout).
772    fn skip_newlines(&mut self) {
773        while matches!(self.peek(), TokenKind::Newline) {
774            self.bump();
775        }
776    }
777
778    fn at_ident(&self, name: &str) -> bool {
779        matches!(self.peek(), TokenKind::Ident(n) if n == name)
780    }
781
782    /// Byte offset where the next token begins — the anchor a production spans
783    /// *from*. Paired with [`Self::span_since`].
784    fn mark(&self) -> usize {
785        self.peek_span().start
786    }
787
788    /// The span from `start` to the end of the **last consumed** token.
789    ///
790    /// `pos` has already advanced past that token, so this looks one back. Call
791    /// it after a production has consumed everything it owns; calling it early
792    /// yields a span that stops short of the node it describes.
793    fn span_since(&self, start: usize) -> Span {
794        let end = self
795            .toks
796            .get(self.pos.saturating_sub(1))
797            .map_or(start, |t| t.span.end);
798        Span::new(start, end.max(start))
799    }
800
801    fn program(&mut self) -> Result<Vec<Spanned>, Fail> {
802        let mut out = Vec::new();
803        loop {
804            self.skip_newlines();
805            if matches!(self.peek(), TokenKind::Eof) {
806                break;
807            }
808            out.push(self.statement()?);
809            self.end_of_statement(&[])?;
810        }
811        Ok(out)
812    }
813
814    /// **A statement ends at the end of its line.** Refuse anything else.
815    ///
816    /// The lexer has always said newlines "are statement separators", and the
817    /// parser never asked for one: `program` and `body` looped over
818    /// `statement()` and started the next one wherever the last stopped. So a
819    /// form blue does not have became two forms it does, silently —
820    /// `y = xs[0]` parsed as `y = xs` then the list `[0]`, and Ruby's
821    /// `a = f x` as `a = f` then `x`. Both ran; both bound the wrong value.
822    ///
823    /// **Refused rather than given a meaning**, because each would be a second
824    /// spelling of a tree blue already spells one way (§0's FORM axis):
825    /// indexing is the call `nth(i, xs)`, and a call has parentheses. `;` is
826    /// refused by the lexer for the same reason — one separator, the newline.
827    ///
828    /// What may follow a statement: a newline, the end of the file, or — in a
829    /// body — the keyword that closes it, which is what keeps
830    /// `fn(x) x + 1 end` a one-line lambda.
831    fn end_of_statement(&self, terminators: &[&str]) -> Result<(), Fail> {
832        let next = self.peek();
833        if matches!(next, TokenKind::Newline | TokenKind::Eof)
834            || terminators.iter().any(|t| self.at_ident(t))
835        {
836            return Ok(());
837        }
838        let prev = self.toks.get(self.pos.wrapping_sub(1)).map(|t| &t.kind);
839        let prev_name = match prev {
840            Some(TokenKind::Ident(n)) => Some(n.as_str()),
841            _ => None,
842        };
843        let callee = prev_name.unwrap_or("f");
844        let why = match (prev, next) {
845            (Some(TokenKind::Ident(t)), _) if t == "then" => {
846                "blue's `if` has no `then`: put the branch on the next line".to_string()
847            }
848            (_, TokenKind::Ident(d)) if d == "do" => "blue has no `do` blocks: pass a function \
849                 as an argument, `f(xs, fn(x) … end)`"
850                .to_string(),
851            (_, TokenKind::LBracket) => {
852                let index = match (self.toks.get(self.pos + 1), self.toks.get(self.pos + 2)) {
853                    (Some(i), Some(close)) if close.kind == TokenKind::RBracket => match &i.kind {
854                        TokenKind::Int(n) => n.to_string(),
855                        TokenKind::Ident(n) => n.clone(),
856                        _ => "i".to_string(),
857                    },
858                    _ => "i".to_string(),
859                };
860                let list = prev_name.unwrap_or("xs");
861                format!(
862                    "blue has no `xs[i]` indexing: write `nth({index}, {list})` \
863                     (or `first({list})`, `last({list})`)"
864                )
865            }
866            (_, TokenKind::LBrace) => "blue has no brace blocks: pass a function as an \
867                 argument, `map(xs, fn(x) … end)`"
868                .to_string(),
869            (
870                Some(TokenKind::Ident(_)),
871                TokenKind::Ident(_)
872                | TokenKind::Int(_)
873                | TokenKind::Float(_)
874                | TokenKind::Str(_)
875                | TokenKind::InterpolatedStr { .. }
876                | TokenKind::Sym(_)
877                | TokenKind::True
878                | TokenKind::False
879                | TokenKind::Nil,
880            ) => {
881                let arg = match next {
882                    TokenKind::Ident(a) => a.clone(),
883                    _ => "x".to_string(),
884                };
885                format!("a call needs parentheses: write `{callee}({arg})`")
886            }
887            _ => "put each statement on its own line".to_string(),
888        };
889        Err(self.error(format!("expected the end of the line, found {next}: {why}")))
890    }
891
892    /// A statement: either a binding or an expression.
893    ///
894    /// `x = 5` lowers to `(define x 5)`. Blue had NO way to name a value — a
895    /// capability probe found `x = 5` was a parse error, which makes every
896    /// program a single expression. That is more fundamental than anything else
897    /// the probe found.
898    ///
899    /// Only at STATEMENT position, never inside an expression, so `f(x = 1)` is
900    /// still an error rather than a silent binding. Ruby allows assignment as an
901    /// expression and it is a well-known footgun — `if x = 1` where `==` was
902    /// meant. Blue declines it, and the cost is only that a walrus-style idiom
903    /// has to be two lines.
904    fn statement(&mut self) -> Result<Spanned, Fail> {
905        // The SECOND recursion cycle, and it needs the same guard as `expr`.
906        //
907        // Guarding `expr` alone was not enough — measured 2026-08-01: with the
908        // expression bound in place, `"def a\n".repeat(2_000)` STILL aborted
909        // the process. Block nesting (`def` opening a body that contains more
910        // statements) recurses through here, not through `expr`, so a fix
911        // applied to one cycle silently left the other reachable. Two paths to
912        // the same crash; one guard covered one of them.
913        //
914        // Shares `self.depth` with `expr` on purpose: what the stack cares
915        // about is TOTAL nesting, not which grammar production produced it, so
916        // two independent counters would each permit their own full budget and
917        // together exceed what the stack can hold.
918        let max = self.max_depth;
919        if self.depth >= max {
920            return Err(self.error(format!(
921                "statement nests deeper than {max}; refusing to \
922                 recurse further (this is a limit, not a syntax error)"
923            )));
924        }
925        self.depth += 1;
926        let r = self.statement_inner();
927        self.depth -= 1;
928        r
929    }
930
931    fn statement_inner(&mut self) -> Result<Spanned, Fail> {
932        if let TokenKind::Ident(name) = self.peek().clone() {
933            if self.peek_at(1) == "=" && !is_reserved_word(&name) {
934                let name_span = self.peek_span();
935                self.bump(); // name
936                self.bump(); // =
937                self.skip_newlines();
938                let value = self.expr(0)?;
939                // `define` is synthesized — `x = 5` contains no such word — so
940                // it takes the span of the name being bound, which is what a
941                // reader looks at when told something about the binding.
942                return Ok(list_at(
943                    self.span_since(name_span.start),
944                    vec![sym_at(name_span, "define"), sym_at(name_span, &name), value],
945                ));
946            }
947        }
948        let e = self.expr(0)?;
949        if let SpannedForm::List(items) = &e.form {
950            if items.len() == 3
951                && items[0].as_symbol() == Some("define")
952                && items[1].as_symbol().is_some()
953                && self.text(items[0].span) == "define"
954            {
955                self.note(Spelling::OperatorSpelling, e.span, "define(x, v)", "x = v");
956            }
957        }
958        Ok(e)
959    }
960
961    /// The token `n` positions ahead, for the two-token lookahead a binding
962    /// needs. Returns `Eof` past the end rather than panicking.
963    fn peek_at(&self, n: usize) -> String {
964        match self.toks.get(self.pos + n).map(|t| &t.kind) {
965            Some(TokenKind::Op(o)) => o.clone(),
966            _ => String::new(),
967        }
968    }
969
970    /// Pratt loop.
971    fn expr(&mut self, min_bp: u8) -> Result<Spanned, Fail> {
972        // Depth guard at the single recursion cycle (`expr` -> `prefix` ->
973        // `expr`). Returning an Err here converts an UNRECOVERABLE abort into
974        // an ordinary parse failure a caller can render — the difference
975        // between an LSP showing a squiggle and an LSP being gone.
976        //
977        // The decrement is deliberately not RAII: every exit from this
978        // function is via `?` or a normal return, and both are covered by the
979        // explicit decrements below. A guard object would be tidier but would
980        // also hide the invariant this comment is here to state.
981        let max = self.max_depth;
982        if self.depth >= max {
983            return Err(self.error(format!(
984                "expression nests deeper than {max}; refusing to \
985                 recurse further (this is a limit, not a syntax error)"
986            )));
987        }
988        self.depth += 1;
989        let r = self.expr_inner(min_bp);
990        self.depth -= 1;
991        r
992    }
993
994    #[inline(never)]
995    fn rescue_node(&self, start: usize, op_span: Span, lhs: Spanned, fallback: Spanned) -> Spanned {
996        list_at(
997            self.span_since(start),
998            vec![
999                sym_at(op_span, "try"),
1000                lhs,
1001                list_at(
1002                    op_span.merge(fallback.span),
1003                    vec![
1004                        sym_at(op_span, "catch"),
1005                        list_at(op_span, vec![sym_at(op_span, "_")]),
1006                        fallback,
1007                    ],
1008                ),
1009            ],
1010        )
1011    }
1012
1013    #[inline(never)]
1014    fn infix_spelling(&mut self, callee: Option<&str>, op_span: Span) {
1015        let written = self.text(op_span).to_string();
1016        match (callee, self.peek().clone()) {
1017            (None, _) => self.note(Spelling::CallForm, op_span, "|>", "a call"),
1018            (Some(c), TokenKind::Op(o)) if word_for(&o) != o => {
1019                self.note(Spelling::LogicalSymbol, op_span, &written, c);
1020            }
1021            (Some(_), TokenKind::Op(o)) if written != o => {
1022                self.note(Spelling::OperatorSpelling, op_span, &written, &o);
1023            }
1024            _ => {}
1025        }
1026    }
1027
1028    #[inline(never)]
1029    fn infix_node(
1030        &mut self,
1031        start: usize,
1032        callee: Option<&str>,
1033        op_span: Span,
1034        lhs: Spanned,
1035        rhs: Spanned,
1036    ) -> Spanned {
1037        if callee == Some("-") && matches!(lhs.form, SpannedForm::Atom(Atom::Int(0))) {
1038            self.note(
1039                Spelling::OperatorSpelling,
1040                lhs.span.merge(op_span),
1041                "0 - x",
1042                "-x",
1043            );
1044        }
1045
1046        if callee.is_none() {
1047            // `x |> f`      => (f x)
1048            // `x |> f(a)`   => (f x a)   — the pipeline threads into
1049            //                  the FIRST argument position, as Elixir's
1050            //                  does; that is what makes it composable.
1051            let rhs_span = rhs.span;
1052            let threaded = match rhs.form {
1053                SpannedForm::List(mut items) if !items.is_empty() => {
1054                    items.insert(1, lhs);
1055                    list_at(self.span_since(start), items)
1056                }
1057                callee => list_at(
1058                    self.span_since(start),
1059                    vec![Spanned::new(rhs_span, callee), lhs],
1060                ),
1061            };
1062            match &threaded.form {
1063                SpannedForm::List(items)
1064                    if items.len() == 2 && items[0].as_symbol() == Some("not") =>
1065                {
1066                    let (head, operand) = (items[0].span, items[1].clone());
1067                    self.negation(head, threaded.span, operand)
1068                }
1069                SpannedForm::List(items)
1070                    if items.len() == 4
1071                        && items[0].as_symbol() == Some("if")
1072                        && matches!(items[3].form, SpannedForm::Nil) =>
1073                {
1074                    list_at(threaded.span, items[..3].to_vec())
1075                }
1076                _ => threaded,
1077            }
1078        } else {
1079            // The callee symbol takes the OPERATOR's span, not the whole
1080            // expression's: `a + b` lowers to `(+ a b)` and the `+` node is
1081            // the one byte-range in the source that means it. Handing it the
1082            // whole expression's span would make a diagnostic about the
1083            // operator underline both operands too.
1084            list_at(
1085                self.span_since(start),
1086                vec![sym_at(op_span, callee.unwrap_or_default()), lhs, rhs],
1087            )
1088        }
1089    }
1090
1091    fn expr_inner(&mut self, min_bp: u8) -> Result<Spanned, Fail> {
1092        let start = self.mark();
1093        let mut lhs = self.prefix()?;
1094
1095        loop {
1096            // Postfix: `.name`, `.name(args)`, `(args)`
1097            match self.peek() {
1098                TokenKind::Dot => {
1099                    let dot = self.peek_span();
1100                    self.bump();
1101                    lhs = self.finish_send(start, lhs, dot)?;
1102                    continue;
1103                }
1104                TokenKind::LParen => {
1105                    let (args, splat) = self.call_args()?;
1106                    let call = self.call_node(self.span_since(start), vec![lhs], args, splat);
1107                    lhs = self.call_spelling(call);
1108                    continue;
1109                }
1110                _ => {}
1111            }
1112
1113            // Infix
1114            // `callee == None` marks the pipeline, which is a rewrite rather
1115            // than a call.
1116            let op_span = self.peek_span();
1117            if self.at_ident("rescue") {
1118                if RESCUE_POWER.0 < min_bp {
1119                    break;
1120                }
1121                self.bump();
1122                self.skip_newlines();
1123                let fallback = self.expr(RESCUE_POWER.1)?;
1124                lhs = self.rescue_node(start, op_span, lhs, fallback);
1125                continue;
1126            }
1127            let (callee, (lbp, rbp)) = match self.peek() {
1128                TokenKind::Pipe => (None, PIPE_POWER),
1129                other => match infix_of(other) {
1130                    Some(i) => (Some(i.callee), i.power),
1131                    None => break,
1132                },
1133            };
1134            if lbp < min_bp {
1135                break;
1136            }
1137            self.infix_spelling(callee, op_span);
1138            self.bump();
1139            self.skip_newlines();
1140            let rhs = self.expr(rbp)?;
1141            lhs = self.infix_node(start, callee, op_span, lhs, rhs);
1142        }
1143
1144        Ok(lhs)
1145    }
1146
1147    #[inline(never)]
1148    fn interpolation(span: Span, parts: &[String], exprs: &[String]) -> Result<Spanned, Fail> {
1149        // Every node here carries the WHOLE string literal's span.
1150        //
1151        // The inner expression is parsed from `raw` — a substring — so
1152        // its own offsets index that substring, not `src`. Carrying them
1153        // through would produce spans that look real and point into a
1154        // buffer nobody has: for `"a#{x}"` at offset 40, `x`'s span
1155        // would be 0..1, i.e. the start of the file. The lexer does not
1156        // record where each part sat in the outer source, so the string
1157        // literal is the most precise honest answer available.
1158        let mut acc = atom_at(span, Atom::Str(parts[0].clone()));
1159        for (i, raw) in exprs.iter().enumerate() {
1160            let inner = parse_expr(raw).map_err(|e| {
1161                fail(
1162                    format!("in interpolation `#{{{raw}}}`: {}", e.message),
1163                    span,
1164                )
1165            })?;
1166            let inner = Spanned::from_sexp_at(&inner, span);
1167            acc = list_at(span, vec![sym_at(span, LOWERED_CONCAT), acc, inner]);
1168            // `parts.len() == exprs.len() + 1` by construction, so this
1169            // index is always in range.
1170            acc = list_at(
1171                span,
1172                vec![
1173                    sym_at(span, LOWERED_CONCAT),
1174                    acc,
1175                    atom_at(span, Atom::Str(parts[i + 1].clone())),
1176                ],
1177            );
1178        }
1179        Ok(acc)
1180    }
1181
1182    #[inline(never)]
1183    fn not_prefix(&mut self, span: Span) -> Result<Spanned, Fail> {
1184        if self.at(&TokenKind::LParen) && self.touches_previous() {
1185            self.note(Spelling::OperatorSpelling, span, "not(x)", "not x");
1186            let (args, splat) = self.call_args()?;
1187            if args.len() != 1 || splat.is_some() {
1188                let call = self.call_node(
1189                    self.span_since(span.start),
1190                    vec![sym_at(span, "not")],
1191                    args,
1192                    splat,
1193                );
1194                return Ok(call);
1195            }
1196            let operand = args.into_iter().next().expect("one argument");
1197            return Ok(self.negation(span, self.span_since(span.start), operand));
1198        }
1199        let rhs = self.expr(11)?;
1200        Ok(self.negation(span, self.span_since(span.start), rhs))
1201    }
1202
1203    fn prefix(&mut self) -> Result<Spanned, Fail> {
1204        if self.at(&TokenKind::LParen) {
1205            let span = self.peek_span();
1206            self.bump();
1207            if self.arrow_lambda_ahead() {
1208                return self.arrow_lambda(span);
1209            }
1210            return self.group();
1211        }
1212        self.prefix_token()
1213    }
1214
1215    fn group(&mut self) -> Result<Spanned, Fail> {
1216        self.skip_newlines();
1217        let inner = self.expr(0)?;
1218        self.skip_newlines();
1219        self.expect(&TokenKind::RParen, "`)`")?;
1220        Ok(inner)
1221    }
1222
1223    #[inline(never)]
1224    fn prefix_token(&mut self) -> Result<Spanned, Fail> {
1225        let span = self.peek_span();
1226        match self.bump() {
1227            TokenKind::Int(v) => Ok(atom_at(span, Atom::Int(v))),
1228            TokenKind::Float(v) => Ok(atom_at(span, Atom::Float(v))),
1229            TokenKind::Str(s) => Ok(atom_at(span, Atom::Str(s))),
1230
1231            // `"a#{x}b"` → `(concat (concat "a" x) "b")`.
1232            //
1233            // Lowered to `concat`, not to `+`: blue's `+` is arithmetic (see
1234            // the INFIX table), and interpolation must render a value of ANY
1235            // type — `concat` goes through `to_s`, which is what makes
1236            // `"n=#{42}"` work.
1237            //
1238            // The expression source is parsed HERE with the ordinary parser
1239            // rather than lexed inside the string, so an interpolation can hold
1240            // anything an expression can and the two can never drift.
1241            TokenKind::InterpolatedStr { parts, exprs } => Self::interpolation(span, &parts, &exprs),
1242            TokenKind::Sym(s) => Ok(atom_at(span, Atom::Keyword(s))),
1243            TokenKind::True => Ok(atom_at(span, Atom::Bool(true))),
1244            TokenKind::False => Ok(atom_at(span, Atom::Bool(false))),
1245            TokenKind::Nil => Ok(Spanned::new(span, SpannedForm::Nil)),
1246
1247            TokenKind::Op(o) if o == "-" => {
1248                let rhs = self.expr(11)?; // binds tighter than `*`
1249                Ok(list_at(
1250                    self.span_since(span.start),
1251                    vec![sym_at(span, "-"), atom_at(span, Atom::Int(0)), rhs],
1252                ))
1253            }
1254            TokenKind::Op(o) if o == "!" => {
1255                let written = self.text(span).to_string();
1256                self.note(Spelling::LogicalSymbol, span, &written, "not");
1257                let rhs = self.expr(11)?;
1258                Ok(self.negation(span, self.span_since(span.start), rhs))
1259            }
1260            TokenKind::Ident(w) if w == "not" => self.not_prefix(span),
1261            TokenKind::Ident(w) if (w == "and" || w == "or") && self.at(&TokenKind::LParen) => {
1262                Ok(sym_at(span, &w))
1263            }
1264            TokenKind::Ident(w) if w == "and" || w == "or" => Err(fail(
1265                format!("`{w}` joins two expressions: `a {w} b`"),
1266                span,
1267            )),
1268
1269            TokenKind::LParen => {
1270                self.skip_newlines();
1271                let inner = self.expr(0)?;
1272                self.skip_newlines();
1273                self.expect(&TokenKind::RParen, "`)`")?;
1274                // The inner expression's own span, NOT one widened to include
1275                // the parentheses: grouping is not part of the expression's
1276                // meaning, and `(a + b)` should underline `a + b`.
1277                Ok(inner)
1278            }
1279
1280            TokenKind::LBracket => self.list_literal(span),
1281            TokenKind::LBrace => self.map_literal(span),
1282
1283            TokenKind::Ident(name) if self.at(&TokenKind::PathSep) => {
1284                self.qualified_name(span, &name)
1285            }
1286            TokenKind::PathSep => Err(fail(
1287                "a qualified name is `package::name`, with the package written before `::`",
1288                span,
1289            )),
1290            TokenKind::Ident(name) => match name.as_str() {
1291                "if" => self.if_form(span, false),
1292                "begin" if !(self.at(&TokenKind::LParen) && self.touches_previous()) => {
1293                    self.begin_form(span)
1294                }
1295                "rescue" | "ensure" => Err(fail(
1296                    format!("`{name}` belongs to a `begin … end` block or a `def` body, after the statements it protects"),
1297                    span,
1298                )),
1299                "unless" => {
1300                    self.note(Spelling::Unless, span, "unless", "if not");
1301                    self.if_form(span, true)
1302                }
1303                "def" => self.def_form(span),
1304                "defmacro" => self.defmacro_form(span),
1305                "case" => self.case_form(span),
1306                "fn" => self.lambda_form(span),
1307                "test" => self.test_form(span),
1308                "assert" => self.assert_form(span),
1309                "quote" => self.quote_form(span),
1310                "unquote" => self.unquote_form(span, false),
1311                "unquote_splice" => self.unquote_form(span, true),
1312                "do" => Err(fail(
1313                    "blue has no `do` blocks: pass a function as an argument, \
1314                     `f(xs, fn(x) … end)`",
1315                    span,
1316                )),
1317                "end" => Err(fail(
1318                    "unexpected `end`: nothing open here for it to close",
1319                    span,
1320                )),
1321                _ => Ok(sym_at(span, &name)),
1322            },
1323
1324            other => Err(fail(format!("expected an expression, found {other}"), span)),
1325        }
1326    }
1327
1328    /// After `package`, at `::`: `retsu::first` lowers to the ONE symbol
1329    /// `retsu/first` — tatara's `require` shape, so a qualified name means the
1330    /// same thing in both lisps.
1331    ///
1332    /// After `::` any identifier is a name, reserved words included:
1333    /// `kueri::if` names kueri's definition, never the `if` form, because a
1334    /// reserved word is reserved only where it could start a form. The
1335    /// package side is never a reserved word, and a name has exactly one
1336    /// qualifier (`a::b::c` is refused), since a package is one level deep.
1337    fn qualified_name(&mut self, pkg_span: Span, pkg: &str) -> Result<Spanned, Fail> {
1338        if is_reserved_word(pkg) {
1339            return Err(fail(
1340                format!("`{pkg}` is a reserved word and cannot name a package"),
1341                pkg_span,
1342            ));
1343        }
1344        self.bump(); // ::
1345        let name_span = self.peek_span();
1346        let name = match self.bump() {
1347            TokenKind::Ident(n) => n,
1348            // `true`, `false` and `nil` lex as their own tokens.
1349            TokenKind::True => "true".to_string(),
1350            TokenKind::False => "false".to_string(),
1351            TokenKind::Nil => "nil".to_string(),
1352            other => {
1353                return Err(fail(
1354                    format!("expected a name after `{pkg}::`, found {other}"),
1355                    name_span,
1356                ))
1357            }
1358        };
1359        if self.at(&TokenKind::PathSep) {
1360            return Err(self.error(format!(
1361                "a qualified name has one qualifier: `package::name`, and `{pkg}::{name}::…` has two"
1362            )));
1363        }
1364        Ok(sym_at(
1365            self.span_since(pkg_span.start),
1366            &qualify(pkg, &name),
1367        ))
1368    }
1369
1370    /// After a `.`: `recv.name` or `recv.name(args)`.
1371    ///
1372    /// **A bare `recv.name` is a SEND, not a field read.** Blue commits to
1373    /// the uniform access principle here: a structure exposes no public
1374    /// fields, so a field can later become a computed method without
1375    /// breaking a caller.
1376    #[inline(never)]
1377    fn negation(&mut self, head: Span, whole: Span, operand: Spanned) -> Spanned {
1378        if let SpannedForm::List(items) = &operand.form {
1379            let flipped = match items.first().and_then(Spanned::as_symbol) {
1380                Some("equal?") => Some("not="),
1381                Some("not=") => Some("equal?"),
1382                _ => None,
1383            };
1384            if let (Some(to), 3) = (flipped, items.len()) {
1385                let (written, canonical) = if to == "not=" {
1386                    ("not (a == b)", "a != b")
1387                } else {
1388                    ("not (a != b)", "a == b")
1389                };
1390                self.note(Spelling::NegatedComparison, whole, written, canonical);
1391                let mut out = items.clone();
1392                out[0] = sym_at(items[0].span, to);
1393                return list_at(whole, out);
1394            }
1395        }
1396        list_at(whole, vec![sym_at(head, "not"), operand])
1397    }
1398
1399    #[inline(never)]
1400    fn call_spelling(&mut self, call: Spanned) -> Spanned {
1401        let SpannedForm::List(items) = &call.form else {
1402            return call;
1403        };
1404        let Some(head) = items.first().and_then(Spanned::as_symbol) else {
1405            return call;
1406        };
1407        let argc = items.len() - 1;
1408        let operator = match (head, argc) {
1409            ("and" | "or" | "mod" | "equal?", 2) => Some(head),
1410            ("list", _) => Some("list"),
1411            _ => None,
1412        };
1413        if let Some(op) = operator {
1414            let canonical = match op {
1415                "list" => "[…]".to_string(),
1416                "mod" => "a % b".to_string(),
1417                "equal?" => "a == b".to_string(),
1418                other => format!("a {other} b"),
1419            };
1420            self.note(
1421                Spelling::OperatorSpelling,
1422                call.span,
1423                &format!("{head}(…)"),
1424                &canonical,
1425            );
1426            return call;
1427        }
1428        if head == "apply"
1429            && self.text(items[0].span) == "apply"
1430            && argc >= 2
1431            && items[1].as_symbol().is_some_and(is_callable_name)
1432        {
1433            self.note(Spelling::Apply, call.span, "apply(f, …, xs)", "f(…, *xs)");
1434            return call;
1435        }
1436        if head == "try" {
1437            return self.try_spelling(call);
1438        }
1439        call
1440    }
1441
1442    fn try_spelling(&mut self, call: Spanned) -> Spanned {
1443        let SpannedForm::List(items) = &call.form else {
1444            return call;
1445        };
1446        let Some((catch, body)) = items[1..].split_last() else {
1447            return call;
1448        };
1449        let Some(clause) = catch.as_list() else {
1450            return call;
1451        };
1452        let binder = match clause {
1453            [head, binding, handler @ ..]
1454                if head.as_symbol() == Some("catch") && !handler.is_empty() =>
1455            {
1456                match binding.as_list() {
1457                    Some([b]) => b.as_symbol().map(|n| (n.to_string(), b.span)),
1458                    _ => None,
1459                }
1460            }
1461            _ => None,
1462        };
1463        let Some((name, binder_span)) = binder else {
1464            return call;
1465        };
1466        if body.is_empty() {
1467            return call;
1468        }
1469        self.note(Spelling::TryCatch, call.span, "try(…, catch(…))", "rescue");
1470        let handler = &clause[2..];
1471        let rename =
1472            name.starts_with('_') && name != "_" && !handler.iter().any(|h| mentions(h, &name));
1473        let body = if body.len() == 1 {
1474            body[0].clone()
1475        } else {
1476            let span = cover(body, call.span);
1477            let mut list = vec![sym_at(Span::new(span.start, span.start), "begin")];
1478            list.extend(body.iter().cloned());
1479            list_at(span, list)
1480        };
1481        let handler = if handler.len() == 1 {
1482            handler[0].clone()
1483        } else {
1484            let span = cover(handler, catch.span);
1485            let mut list = vec![sym_at(Span::new(span.start, span.start), "begin")];
1486            list.extend(handler.iter().cloned());
1487            list_at(span, list)
1488        };
1489        let binder = sym_at(binder_span, if rename { "_" } else { &name });
1490        list_at(
1491            call.span,
1492            vec![
1493                items[0].clone(),
1494                body,
1495                list_at(
1496                    catch.span,
1497                    vec![
1498                        clause[0].clone(),
1499                        list_at(clause[1].span, vec![binder]),
1500                        handler,
1501                    ],
1502                ),
1503            ],
1504        )
1505    }
1506
1507    fn finish_send(&mut self, start: usize, recv: Spanned, dot: Span) -> Result<Spanned, Fail> {
1508        let name_span = self.peek_span();
1509        let name = match self.bump() {
1510            // A RESERVED WORD is not a method name.
1511            //
1512            // Found by the formatter property suite, minimal input `1.def`.
1513            // The send parsed happily into `(def 1)` — `def` is just an
1514            // identifier to the lexer — and the formatter then rendered that
1515            // as `def(1)`, which the parser rejects. So `format` turned valid
1516            // source into source that does not parse: corruption, not a style
1517            // choice, and silent until a round-trip property looked.
1518            //
1519            // Rejecting here rather than teaching the formatter to quote it
1520            // fixes the class instead of the symptom: `x.if`, `x.end` and
1521            // `x.case` all lower onto special forms the same way, and none of
1522            // them is a method anyone meant to call.
1523            TokenKind::Ident(n) if is_reserved_word(&n) => {
1524                return Err(self.error(format!(
1525                    "`{n}` is a reserved word and cannot be a method name — \
1526                     `recv.{n}` would lower onto the `{n}` form itself"
1527                )))
1528            }
1529            TokenKind::Ident(n) => n,
1530            other => {
1531                return Err(fail(
1532                    format!("expected a method name after `.`, found {other}"),
1533                    name_span,
1534                ))
1535            }
1536        };
1537        if !self.at(&TokenKind::LParen) {
1538            return Ok(list_at(
1539                self.span_since(start),
1540                vec![
1541                    sym_at(dot, LOWERED_FIELD),
1542                    recv,
1543                    atom_at(name_span, Atom::Keyword(name)),
1544                ],
1545            ));
1546        }
1547        self.note(
1548            Spelling::CallForm,
1549            dot.merge(name_span),
1550            &format!("x.{name}(…)"),
1551            &format!("{name}(x, …)"),
1552        );
1553        let (args, splat) = self.call_args()?;
1554        Ok(self.call_node(
1555            self.span_since(start),
1556            vec![sym_at(name_span, &name), recv],
1557            args,
1558            splat,
1559        ))
1560    }
1561
1562    fn call_node(
1563        &self,
1564        span: Span,
1565        head: Vec<Spanned>,
1566        args: Vec<Spanned>,
1567        splat: Option<Span>,
1568    ) -> Spanned {
1569        let mut list = Vec::with_capacity(head.len() + args.len() + 1);
1570        if let Some(at) = splat {
1571            list.push(sym_at(at, "apply"));
1572        }
1573        list.extend(head);
1574        list.extend(args);
1575        list_at(span, list)
1576    }
1577
1578    fn paren_args(&mut self) -> Result<Vec<Spanned>, Fail> {
1579        let (args, splat) = self.call_args()?;
1580        if let Some(at) = splat {
1581            return Err(fail(
1582                "a splat `*xs` passes a list as a call's last arguments, and this is not a call",
1583                at,
1584            ));
1585        }
1586        Ok(args)
1587    }
1588
1589    fn call_args(&mut self) -> Result<(Vec<Spanned>, Option<Span>), Fail> {
1590        self.expect(&TokenKind::LParen, "`(`")?;
1591        let mut args = Vec::new();
1592        let mut splat = None;
1593        self.skip_newlines();
1594        if self.eat(&TokenKind::RParen) {
1595            return Ok((args, splat));
1596        }
1597        loop {
1598            self.skip_newlines();
1599            if splat.is_some() {
1600                return Err(
1601                    self.error("a splat `*xs` passes a list as the LAST arguments: write it last")
1602                );
1603            }
1604            if matches!(self.peek(), TokenKind::Op(o) if o == "*") {
1605                splat = Some(self.peek_span());
1606                self.bump();
1607            }
1608            args.push(self.expr(0)?);
1609            self.skip_newlines();
1610            if self.eat(&TokenKind::Comma) {
1611                if self.trailing_comma(&TokenKind::RParen) {
1612                    break;
1613                }
1614                continue;
1615            }
1616            self.expect(&TokenKind::RParen, "`,` or `)`")?;
1617            break;
1618        }
1619        Ok((args, splat))
1620    }
1621
1622    fn trailing_comma(&mut self, closer: &TokenKind) -> bool {
1623        let comma = self
1624            .toks
1625            .get(self.pos.wrapping_sub(1))
1626            .map_or(self.peek_span(), |t| t.span);
1627        self.skip_newlines();
1628        if self.at(closer) {
1629            self.note(Spelling::TrailingComma, comma, ",", "");
1630            self.bump();
1631            return true;
1632        }
1633        false
1634    }
1635
1636    fn list_literal(&mut self, open: Span) -> Result<Spanned, Fail> {
1637        let mut items = vec![sym_at(open, "list")];
1638        self.skip_newlines();
1639        if self.eat(&TokenKind::RBracket) {
1640            return Ok(list_at(self.span_since(open.start), items));
1641        }
1642        loop {
1643            self.skip_newlines();
1644            items.push(self.expr(0)?);
1645            self.skip_newlines();
1646            if self.eat(&TokenKind::Comma) {
1647                if self.trailing_comma(&TokenKind::RBracket) {
1648                    break;
1649                }
1650                continue;
1651            }
1652            self.expect(&TokenKind::RBracket, "`,` or `]`")?;
1653            break;
1654        }
1655        Ok(list_at(self.span_since(open.start), items))
1656    }
1657
1658    /// `{a: 1, "k" => v}` — both spellings, one tree.
1659    ///
1660    /// This is §V.13's rendering law at the parser: `a: 1` and `:a => 1`
1661    /// produce the *same* s-expression, which is precisely why the
1662    /// formatter may always choose the shorthand. The rocket survives only
1663    /// where the key is not a plain symbol.
1664    fn map_literal(&mut self, open: Span) -> Result<Spanned, Fail> {
1665        let mut items = vec![sym_at(open, LOWERED_MAP)];
1666        self.skip_newlines();
1667        if self.eat(&TokenKind::RBrace) {
1668            return Ok(list_at(self.span_since(open.start), items));
1669        }
1670        loop {
1671            self.skip_newlines();
1672            match self.peek().clone() {
1673                TokenKind::Label(name) => {
1674                    let label_span = self.peek_span();
1675                    self.bump();
1676                    self.skip_newlines();
1677                    items.push(atom_at(label_span, Atom::Keyword(name.clone())));
1678                    if matches!(self.peek(), TokenKind::Comma | TokenKind::RBrace) {
1679                        items.push(sym_at(label_span, &name));
1680                    } else {
1681                        let value = self.expr(0)?;
1682                        if matches!(&value.form, SpannedForm::Atom(Atom::Symbol(s)) if *s == name)
1683                            && label_name(&name)
1684                        {
1685                            let written = format!("{name}: {name}");
1686                            let canonical = format!("{name}:");
1687                            self.note(
1688                                Spelling::MapPun,
1689                                label_span.merge(value.span),
1690                                &written,
1691                                &canonical,
1692                            );
1693                        }
1694                        items.push(value);
1695                    }
1696                }
1697                _ => {
1698                    let k = self.expr(0)?;
1699                    self.skip_newlines();
1700                    if let SpannedForm::Atom(Atom::Keyword(name)) = &k.form {
1701                        if blue_lang_name(name) {
1702                            let written = format!(":{name} =>");
1703                            let canonical = format!("{name}:");
1704                            self.note(Spelling::KeywordRocket, k.span, &written, &canonical);
1705                        }
1706                    }
1707                    self.expect(&TokenKind::Rocket, "`=>` in a map literal")?;
1708                    self.skip_newlines();
1709                    items.push(k);
1710                    items.push(self.expr(0)?);
1711                }
1712            }
1713            self.skip_newlines();
1714            if self.eat(&TokenKind::Comma) {
1715                if self.trailing_comma(&TokenKind::RBrace) {
1716                    break;
1717                }
1718                continue;
1719            }
1720            self.expect(&TokenKind::RBrace, "`,` or `}`")?;
1721            break;
1722        }
1723        Ok(list_at(self.span_since(open.start), items))
1724    }
1725
1726    /// `if c ... [else ...] end`, and `unless` as its negation.
1727    ///
1728    /// `unless` lowers to `(if (not c) ...)` rather than to a distinct
1729    /// form: one tree per meaning, so the formatter and every downstream
1730    /// tool see exactly one shape.
1731    /// An `elsif` arm: an `if` that does NOT consume the closing `end`.
1732    ///
1733    /// `if / elsif / elsif / else / end` is one `end` for the whole chain, so
1734    /// every arm but the first must leave it for its parent.
1735    fn if_chain(&mut self, head: Span) -> Result<Spanned, Fail> {
1736        let cond = self.expr(0)?;
1737        let then = self.body(&["elsif", "else", "end"])?;
1738        let els = self.else_part()?;
1739        let mut out = vec![sym_at(head, "if"), cond, then];
1740        if let Some(e) = els {
1741            out.push(e);
1742        }
1743        Ok(list_at(self.span_since(head.start), out))
1744    }
1745
1746    fn else_part(&mut self) -> Result<Option<Spanned>, Fail> {
1747        if self.at_ident("elsif") {
1748            let elsif = self.peek_span();
1749            self.bump();
1750            return Ok(Some(self.if_chain(elsif)?));
1751        }
1752        if !self.at_ident("else") {
1753            self.expect_ident("end")?;
1754            return Ok(None);
1755        }
1756        let else_span = self.peek_span();
1757        self.bump();
1758        let save = self.pos;
1759        self.skip_newlines();
1760        let leads_with_if = self.at_ident("if") || self.at_ident("unless");
1761        self.pos = save;
1762        let e = self.body(&["end"])?;
1763        let end_at = self.peek_span().start;
1764        self.expect_ident("end")?;
1765        if matches!(e.form, SpannedForm::Nil) && !self.comment_within(else_span.start, end_at) {
1766            self.note(Spelling::ElseNil, else_span, "else nil", "no else");
1767            return Ok(None);
1768        }
1769        let single_if = matches!(&e.form, SpannedForm::List(items)
1770            if (items.len() == 3 || items.len() == 4) && items[0].as_symbol() == Some("if"));
1771        if leads_with_if && single_if {
1772            self.note(Spelling::ElseIf, else_span, "else if", "elsif");
1773        }
1774        Ok(Some(e))
1775    }
1776
1777    fn if_form(&mut self, head: Span, negate: bool) -> Result<Spanned, Fail> {
1778        let cond = self.expr(0)?;
1779        let cond = if negate {
1780            // `unless c` lowers to `(if (not c) …)`. The synthesized `not` takes
1781            // the `unless` keyword's span — that word is what put it there.
1782            let inner = cond.span;
1783            self.negation(head, head.merge(inner), cond)
1784        } else {
1785            cond
1786        };
1787        // `elsif` terminates the then-body too, or the chain is swallowed.
1788        //
1789        // It used to be absent entirely — not a keyword, not reserved, nowhere
1790        // in this file — so `elsif cond` parsed as a stray expression inside
1791        // the then-body and the branch it guarded VANISHED. No error: the
1792        // program ran and returned the `else` value. Measured:
1793        //
1794        //   if a < 1 then 1 elsif a < 5 then 2 else 3 end   with a = 3
1795        //     wanted 2, returned 3
1796        //
1797        // A silent wrong answer in the most-used control form in the language,
1798        // reachable by writing the Ruby every blue user already knows.
1799        let then = self.body(&["elsif", "else", "end"])?;
1800        let els = self.else_part()?;
1801        let mut out = vec![sym_at(head, "if"), cond, then];
1802        if let Some(e) = els {
1803            out.push(e);
1804        }
1805        Ok(list_at(self.span_since(head.start), out))
1806    }
1807
1808    /// `def name(a, b) ... end`            => `(define (name a b) body)`
1809    /// `def name(a: T, b: T) -> R ... end`  => `(define-typed (name (a T) (b T)) R body)`
1810    ///
1811    /// **The two shapes are deliberately different heads.** §0 says an
1812    /// unannotated program gets ZERO analysis, and the cleanest way to
1813    /// mean that is for untyped code not to reach the typing machinery at
1814    /// all — not to reach it and be waved through. A checker that must
1815    /// walk every node to discover there is nothing to check has already
1816    /// paid the cost the ladder exists to avoid.
1817    ///
1818    /// Annotations are per-parameter, so a signature may be partially
1819    /// annotated. That is the ladder at its finest grain: `a: Int` is
1820    /// checked and a bare `b` stays `dyn`, in the same signature.
1821    /// `case subject / when a / … / else / … / end` => a `cond` over equality.
1822    ///
1823    /// **Value matching, not destructuring.** Elixir's `case` binds pattern
1824    /// variables; blue's compares with the same `equal?` the `==` operator uses,
1825    /// so `when [1, 2]` matches a list by value. Destructuring needs a pattern
1826    /// language and a binder, which blue does not have — and a `case` that
1827    /// *looked* like Elixir's while silently only comparing would be worse than
1828    /// one that plainly compares.
1829    ///
1830    /// The subject is evaluated ONCE, into a binding, so `case expensive()` does
1831    /// not re-run per arm. That is a correctness property, not an optimisation:
1832    /// a subject with a side effect would fire once per `when`.
1833    fn case_form(&mut self, head: Span) -> Result<Spanned, Fail> {
1834        let subject = self.expr(0)?;
1835        self.skip_newlines();
1836
1837        // A fresh name the surface cannot spell, so it cannot capture a user
1838        // binding of the same name.
1839        let subject_var = "case-subject";
1840        let mut arms: Vec<Spanned> = Vec::new();
1841        let mut otherwise: Option<Spanned> = None;
1842        // The `else` keyword's own span, so the synthesized `(else body)` clause
1843        // sits where the author wrote it rather than back at `case`.
1844        let mut else_kw: Option<Span> = None;
1845
1846        loop {
1847            self.skip_newlines();
1848            if self.at_ident("end") {
1849                break;
1850            }
1851            let maybe_else = self.peek_span();
1852            if self.eat_ident("else") {
1853                else_kw = Some(maybe_else);
1854                otherwise = Some(self.body(&["end"])?);
1855                continue;
1856            }
1857            let when = self.peek_span();
1858            if !self.eat_ident("when") {
1859                return Err(self.error(format!(
1860                    "expected `when`, `else` or `end` in a case, found {}",
1861                    self.peek()
1862                )));
1863            }
1864            self.skip_newlines();
1865            let pattern = self.expr(0)?;
1866            let body = self.body(&["when", "else", "end"])?;
1867            // The synthesized comparison takes the arm's `when` span: that word
1868            // is what put an `equal?` there, and it is where a reader looks when
1869            // told an arm cannot match.
1870            let test = list_at(
1871                when.merge(pattern.span),
1872                vec![sym_at(when, "equal?"), sym_at(when, subject_var), pattern],
1873            );
1874            arms.push(list_at(when.merge(body.span), vec![test, body]));
1875        }
1876        self.expect_ident("end")?;
1877
1878        if arms.is_empty() && otherwise.is_none() {
1879            return Err(self.error("a case needs at least one `when` or an `else`".to_string()));
1880        }
1881
1882        // A case with no matching arm and no else is NIL, matching Ruby. Elixir
1883        // raises CaseClauseError; blue follows Ruby because its `if` without an
1884        // else is already nil, and having two different answers to "no branch
1885        // taken" in one language is the inconsistency.
1886        let mut cond = vec![sym_at(head, "cond")];
1887        cond.extend(arms);
1888        // The `else` clause spans its own keyword through its body. Anchoring it
1889        // at `head` — the `case` keyword — put a node whose subtree sits at the
1890        // END of the form at a span near its START, so the else body escaped its
1891        // parent's range entirely. `every_node_sits_inside_its_parents_span`
1892        // caught that; it is the kind of mistake no output-shape test can see,
1893        // because the tree is correct and only the positions are wrong.
1894        let else_span = else_kw.unwrap_or(head);
1895        let else_body = otherwise.unwrap_or_else(|| Spanned::new(else_span, SpannedForm::Nil));
1896        cond.push(list_at(
1897            else_span.merge(else_body.span),
1898            vec![sym_at(else_span, "else"), else_body],
1899        ));
1900
1901        let whole = self.span_since(head.start);
1902        let binding = list_at(
1903            head.merge(subject.span),
1904            vec![sym_at(head, subject_var), subject],
1905        );
1906        Ok(list_at(
1907            whole,
1908            vec![
1909                sym_at(head, "let"),
1910                list_at(binding.span, vec![binding]),
1911                list_at(whole, cond),
1912            ],
1913        ))
1914    }
1915
1916    /// Is the `(` just consumed the head of `(a, b) -> expr`? Only bare names
1917    /// and commas between the parens, then `->`, all on one line.
1918    fn arrow_lambda_ahead(&self) -> bool {
1919        let mut i = self.pos;
1920        let kind = |i: usize| self.toks.get(i).map(|t| &t.kind);
1921        if !matches!(kind(i), Some(TokenKind::RParen)) {
1922            loop {
1923                if !matches!(kind(i), Some(TokenKind::Ident(_))) {
1924                    return false;
1925                }
1926                i += 1;
1927                match kind(i) {
1928                    Some(TokenKind::Comma) => i += 1,
1929                    Some(TokenKind::RParen) => break,
1930                    _ => return false,
1931                }
1932            }
1933        }
1934        matches!(kind(i + 1), Some(TokenKind::Op(o)) if o == "->")
1935    }
1936
1937    /// `(a, b) -> expr` => `(lambda (a b) expr)`: the inline spelling of a
1938    /// one-expression `fn`. `->` is unambiguous here: the return-type arrow
1939    /// only follows a `def` header, never a parenthesised name list in
1940    /// expression position. The formatter spells a lambda this way exactly
1941    /// when its body is one expression that fits the line, and as a broken
1942    /// `fn … end` otherwise.
1943    fn arrow_lambda(&mut self, head: Span) -> Result<Spanned, Fail> {
1944        let mut params: Vec<Spanned> = Vec::new();
1945        while !self.at(&TokenKind::RParen) {
1946            let p_span = self.peek_span();
1947            match self.bump() {
1948                TokenKind::Ident(p) => params.push(sym_at(p_span, &p)),
1949                other => {
1950                    return Err(fail(
1951                        format!("expected a parameter name, found {other}"),
1952                        p_span,
1953                    ))
1954                }
1955            }
1956            self.eat(&TokenKind::Comma);
1957        }
1958        self.bump();
1959        self.bump();
1960        self.skip_newlines();
1961        let params_span = cover(&params, head);
1962        let body = self.expr(0)?;
1963        Ok(list_at(
1964            self.span_since(head.start),
1965            vec![sym_at(head, "lambda"), list_at(params_span, params), body],
1966        ))
1967    }
1968
1969    /// `fn(a, b) ... end` => `(lambda (a b) body)`
1970    ///
1971    /// Without this the higher-order functions are unreachable in practice:
1972    /// `map(inc, xs)` works only because `inc` happens to be a named stdlib
1973    /// function, and there was no way to write the one-off the call site
1974    /// actually wants.
1975    ///
1976    /// `fn … end` is the block spelling and always breaks over lines; the
1977    /// inline spelling is `(x) -> expr` ([`Self::arrow_lambda`]).
1978    fn lambda_form(&mut self, head: Span) -> Result<Spanned, Fail> {
1979        let mut params: Vec<Spanned> = Vec::new();
1980        if !self.at(&TokenKind::LParen) {
1981            self.note(Spelling::ParameterList, head, "fn", "fn()");
1982        }
1983        if self.at(&TokenKind::LParen) {
1984            self.bump();
1985            self.skip_newlines();
1986            if !self.eat(&TokenKind::RParen) {
1987                loop {
1988                    self.skip_newlines();
1989                    let p_span = self.peek_span();
1990                    match self.bump() {
1991                        TokenKind::Ident(p) => params.push(sym_at(p_span, &p)),
1992                        other => {
1993                            return Err(fail(
1994                                format!("expected a parameter name, found {other}"),
1995                                p_span,
1996                            ))
1997                        }
1998                    }
1999                    self.skip_newlines();
2000                    if self.eat(&TokenKind::Comma) {
2001                        if self.trailing_comma(&TokenKind::RParen) {
2002                            break;
2003                        }
2004                        continue;
2005                    }
2006                    self.expect(&TokenKind::RParen, "`,` or `)`")?;
2007                    break;
2008                }
2009            }
2010        }
2011        let params_span = cover(&params, head);
2012        let body = self.body(&["end"])?;
2013        self.expect_ident("end")?;
2014        Ok(list_at(
2015            self.span_since(head.start),
2016            vec![sym_at(head, "lambda"), list_at(params_span, params), body],
2017        ))
2018    }
2019
2020    /// `test "name" ... end` => `(deftest "name" body)`
2021    ///
2022    /// A string, not an identifier: a test name is prose for a human report,
2023    /// and forcing it into an identifier is how test names become
2024    /// `test_adds_two_numbers_correctly`.
2025    fn test_form(&mut self, head: Span) -> Result<Spanned, Fail> {
2026        let name_span = self.peek_span();
2027        let name = match self.bump() {
2028            TokenKind::Str(s) => s,
2029            other => {
2030                return Err(fail(
2031                    format!("expected a string name after `test`, found {other}"),
2032                    name_span,
2033                ))
2034            }
2035        };
2036        let body = self.body(&["end"])?;
2037        self.expect_ident("end")?;
2038        Ok(list_at(
2039            self.span_since(head.start),
2040            vec![
2041                sym_at(head, "deftest"),
2042                atom_at(name_span, Atom::Str(name)),
2043                body,
2044            ],
2045        ))
2046    }
2047
2048    /// `assert expr` => `(blue-assert 'expr expr)`
2049    ///
2050    /// **`blue-assert`, not `assert`.** tatara-lisp's stdlib already defines
2051    /// `assert` as a macro — `(defmacro assert (pred message) …)` — and a macro
2052    /// in the expander is consulted before any primitive in the registry. So
2053    /// lowering to `assert` bound `pred` to the *quoted form*, which is
2054    /// truthy, and **every assertion silently passed**. A test framework whose
2055    /// assertions always pass is the worst defect it can have: every test in
2056    /// the suite goes green.
2057    ///
2058    /// The lesson generalizes: any name blue lowers to that tatara already
2059    /// binds is silently captured. `blue_lang_test`'s
2060    /// `no_lowered_name_is_shadowed_by_the_runtime` gates the whole class.
2061    ///
2062    /// **Both the form and the value.** A test framework whose failure says
2063    /// only "assertion failed" makes the author re-derive what they were
2064    /// checking; one that shows the expression does not. The quoted form is
2065    /// the expression as DATA, so the runner can render it — and it renders it
2066    /// through `blue-lang-fmt`, meaning the failure message is in canonical
2067    /// blue syntax rather than the underlying tatara-lisp.
2068    ///
2069    /// This is homoiconicity paying for itself: the capture needs no source
2070    /// map, no macro hygiene, and no string of the original text.
2071    fn assert_form(&mut self, head: Span) -> Result<Spanned, Fail> {
2072        let e = self.expr(0)?;
2073        let quoted = Spanned::new(e.span, SpannedForm::Quote(Box::new(e.clone())));
2074        Ok(list_at(
2075            self.span_since(head.start),
2076            vec![sym_at(head, LOWERED_ASSERT), quoted, e],
2077        ))
2078    }
2079
2080    /// `defmacro name(a, b) ... end` => `(defmacro name (a b) body)`
2081    ///
2082    /// **Deliberately untyped.** A macro's parameters are *source forms*, not
2083    /// values, so `a: Int` would be a category error: the argument at expansion
2084    /// time is a fragment of syntax. §IV's ladder types values; macro
2085    /// parameters are not on it. Annotating one is rejected rather than
2086    /// silently ignored — an ignored annotation is how an author comes to
2087    /// believe a check is running.
2088    fn defmacro_form(&mut self, head: Span) -> Result<Spanned, Fail> {
2089        let name_span = self.peek_span();
2090        let name = match self.bump() {
2091            TokenKind::Ident(n) => n,
2092            other => {
2093                return Err(fail(
2094                    format!("expected a name after `defmacro`, found {other}"),
2095                    name_span,
2096                ))
2097            }
2098        };
2099        let mut params: Vec<Spanned> = Vec::new();
2100        if self.at(&TokenKind::LParen) {
2101            self.bump();
2102            self.skip_newlines();
2103            if !self.eat(&TokenKind::RParen) {
2104                loop {
2105                    self.skip_newlines();
2106                    let p_span = self.peek_span();
2107                    match self.bump() {
2108                        TokenKind::Ident(p) => params.push(sym_at(p_span, &p)),
2109                        TokenKind::Label(p) => {
2110                            return Err(self.error(format!(
2111                                "macro parameter `{p}` cannot be typed: a macro receives \
2112                                 source forms, not values"
2113                            )))
2114                        }
2115                        other => {
2116                            return Err(fail(
2117                                format!("expected a parameter name, found {other}"),
2118                                p_span,
2119                            ))
2120                        }
2121                    }
2122                    self.skip_newlines();
2123                    if self.eat(&TokenKind::Comma) {
2124                        if self.trailing_comma(&TokenKind::RParen) {
2125                            break;
2126                        }
2127                        continue;
2128                    }
2129                    self.expect(&TokenKind::RParen, "`,` or `)`")?;
2130                    break;
2131                }
2132            }
2133        }
2134        if matches!(self.peek(), TokenKind::Op(o) if o == "->") {
2135            return Err(self.error(
2136                "a macro has no return type: it produces source forms, not values".to_string(),
2137            ));
2138        }
2139
2140        let params_span = cover(&params, name_span);
2141        let body = self.body(&["end"])?;
2142        self.expect_ident("end")?;
2143
2144        // `(defmacro name (params) body)` — tatara-lisp's own shape, so blue
2145        // registers into the SAME expander rather than a parallel one.
2146        Ok(list_at(
2147            self.span_since(head.start),
2148            vec![
2149                sym_at(head, "defmacro"),
2150                sym_at(name_span, &name),
2151                list_at(params_span, params),
2152                body,
2153            ],
2154        ))
2155    }
2156
2157    /// `quote ... end` => `` `body `` (a quasiquote).
2158    ///
2159    /// Quasiquote rather than plain quote, because a macro body that could not
2160    /// splice its arguments in would be useless — this is Elixir's `quote do`,
2161    /// which is likewise a template and not inert data.
2162    fn quote_form(&mut self, head: Span) -> Result<Spanned, Fail> {
2163        let body = self.body(&["end"])?;
2164        self.expect_ident("end")?;
2165        // `Sexp::Quasiquote`, NOT `(quasiquote body)` as a list.
2166        //
2167        // The list form Displays as the text `(quasiquote …)`, which the
2168        // tatara-lisp reader reads back as an ordinary list whose head happens
2169        // to be the symbol `quasiquote` — losing the structure. The evaluator
2170        // then reached the inner `,x` with no enclosing quasiquote and rejected
2171        // it: "unquote outside of quasiquote". Building the real variant makes
2172        // it Display as `` ` `` and survive the round trip.
2173        Ok(Spanned::new(
2174            self.span_since(head.start),
2175            SpannedForm::Quasiquote(Box::new(body)),
2176        ))
2177    }
2178
2179    /// `unquote(expr)` => `,expr`; `unquote_splice(expr)` => `,@expr`.
2180    fn unquote_form(&mut self, head: Span, splice: bool) -> Result<Spanned, Fail> {
2181        self.expect(&TokenKind::LParen, "`(` after unquote")?;
2182        self.skip_newlines();
2183        let inner = self.expr(0)?;
2184        self.skip_newlines();
2185        self.expect(&TokenKind::RParen, "`)`")?;
2186        let span = self.span_since(head.start);
2187        Ok(Spanned::new(
2188            span,
2189            if splice {
2190                SpannedForm::UnquoteSplice(Box::new(inner))
2191            } else {
2192                SpannedForm::Unquote(Box::new(inner))
2193            },
2194        ))
2195    }
2196
2197    fn protected_body(&mut self, head: Span) -> Result<Spanned, Fail> {
2198        let body = self.body(&["rescue", "ensure", "end"])?;
2199        if self.at_ident("rescue") || self.at_ident("ensure") {
2200            return self.rescue_tail(head, body);
2201        }
2202        Ok(body)
2203    }
2204
2205    fn begin_form(&mut self, head: Span) -> Result<Spanned, Fail> {
2206        let body = self.body(&["rescue", "ensure", "end"])?;
2207        if !(self.at_ident("rescue") || self.at_ident("ensure")) {
2208            return Err(self.error(
2209                "a `begin` block protects its statements with `rescue` or `ensure`; without either, write the statements directly",
2210            ));
2211        }
2212        let tree = self.rescue_tail(head, body)?;
2213        self.expect_ident("end")?;
2214        Ok(Spanned::new(self.span_since(head.start), tree.form))
2215    }
2216
2217    fn rescue_tail(&mut self, head: Span, body: Spanned) -> Result<Spanned, Fail> {
2218        let mut clauses: Vec<Spanned> = Vec::new();
2219        let mut catch_all: Option<(Spanned, Spanned, Span)> = None;
2220        while self.at_ident("rescue") {
2221            let kw = self.peek_span();
2222            self.bump();
2223            if catch_all.is_some() {
2224                return Err(fail(
2225                    "a `rescue NAME` catches every error, so it is the last `rescue`; put the kinds it would shadow above it",
2226                    kw,
2227                ));
2228            }
2229            match self.peek().clone() {
2230                TokenKind::Sym(_) => {
2231                    let mut kinds = Vec::new();
2232                    loop {
2233                        let k_span = self.peek_span();
2234                        match self.bump() {
2235                            TokenKind::Sym(k) => kinds.push(atom_at(k_span, Atom::Keyword(k))),
2236                            other => {
2237                                return Err(fail(
2238                                    format!("expected an error kind such as `:not_found`, found {other}"),
2239                                    k_span,
2240                                ))
2241                            }
2242                        }
2243                        if self.eat(&TokenKind::Comma) {
2244                            self.skip_newlines();
2245                            continue;
2246                        }
2247                        break;
2248                    }
2249                    let binder = if self.eat_ident("as") {
2250                        let b_span = self.peek_span();
2251                        match self.bump() {
2252                            TokenKind::Ident(b) if !is_reserved_word(&b) => Some(sym_at(b_span, &b)),
2253                            other => {
2254                                return Err(fail(
2255                                    format!("expected the name the error is bound to after `as`, found {other}"),
2256                                    b_span,
2257                                ))
2258                            }
2259                        }
2260                    } else {
2261                        None
2262                    };
2263                    let handler = self.body(&["rescue", "ensure", "end"])?;
2264                    let mut test = vec![sym_at(kw, LOWERED_ERROR_MATCH), sym_at(kw, RESCUED)];
2265                    test.extend(kinds);
2266                    let test = list_at(cover(&test, kw), test);
2267                    let handler = match binder {
2268                        Some(b) => let_bound(b, handler, kw),
2269                        None => handler,
2270                    };
2271                    clauses.push(list_at(kw.merge(handler.span), vec![test, handler]));
2272                }
2273                TokenKind::Ident(b) if !is_reserved_word(&b) => {
2274                    let b_span = self.peek_span();
2275                    self.bump();
2276                    let handler = self.body(&["rescue", "ensure", "end"])?;
2277                    catch_all = Some((sym_at(b_span, &b), handler, kw));
2278                }
2279                _ => {
2280                    return Err(fail(
2281                        "`rescue` names what it catches: `rescue :kind`, `rescue :a, :b as e`, or `rescue e` for every error (`rescue _` discards it)",
2282                        kw,
2283                    ))
2284                }
2285            }
2286        }
2287        let ensure = if self.at_ident("ensure") {
2288            self.bump();
2289            let cleanup = self.body(&["rescue", "end"])?;
2290            if self.at_ident("rescue") {
2291                return Err(self.error("`ensure` comes after every `rescue` of its block"));
2292            }
2293            Some(cleanup)
2294        } else {
2295            None
2296        };
2297        let whole = self.span_since(head.start);
2298        let protected = if clauses.is_empty() {
2299            match catch_all {
2300                None => body,
2301                Some((binder, handler, kw)) => list_at(
2302                    whole,
2303                    vec![
2304                        sym_at(head, "try"),
2305                        body,
2306                        list_at(
2307                            kw.merge(handler.span),
2308                            vec![
2309                                sym_at(kw, "catch"),
2310                                list_at(binder.span, vec![binder]),
2311                                handler,
2312                            ],
2313                        ),
2314                    ],
2315                ),
2316            }
2317        } else {
2318            let first_kw = clauses[0].span;
2319            let otherwise = match catch_all {
2320                Some((binder, handler, kw)) => {
2321                    let bound = let_bound(binder, handler, kw);
2322                    list_at(kw.merge(bound.span), vec![sym_at(kw, "else"), bound])
2323                }
2324                None => list_at(
2325                    head,
2326                    vec![
2327                        sym_at(head, "else"),
2328                        list_at(
2329                            head,
2330                            vec![sym_at(head, LOWERED_RERAISE), sym_at(head, RESCUED)],
2331                        ),
2332                    ],
2333                ),
2334            };
2335            let mut cond = vec![sym_at(first_kw, "cond")];
2336            cond.extend(clauses);
2337            cond.push(otherwise);
2338            let cond_span = cover(&cond, first_kw);
2339            list_at(
2340                whole,
2341                vec![
2342                    sym_at(head, "try"),
2343                    body,
2344                    list_at(
2345                        cond_span,
2346                        vec![
2347                            sym_at(first_kw, "catch"),
2348                            list_at(first_kw, vec![sym_at(first_kw, RESCUED)]),
2349                            list_at(cond_span, cond),
2350                        ],
2351                    ),
2352                ],
2353            )
2354        };
2355        Ok(match ensure {
2356            None => protected,
2357            Some(cleanup) => {
2358                let attempted = list_at(
2359                    protected.span,
2360                    vec![sym_at(head, LOWERED_ATTEMPTED), protected],
2361                );
2362                let failed = list_at(
2363                    head,
2364                    vec![sym_at(head, LOWERED_FAILED), sym_at(head, RESCUED)],
2365                );
2366                let caught = list_at(
2367                    whole,
2368                    vec![
2369                        sym_at(head, "try"),
2370                        attempted,
2371                        list_at(
2372                            head,
2373                            vec![
2374                                sym_at(head, "catch"),
2375                                list_at(head, vec![sym_at(head, RESCUED)]),
2376                                failed,
2377                            ],
2378                        ),
2379                    ],
2380                );
2381                list_at(
2382                    whole,
2383                    vec![sym_at(head, LOWERED_ENSURE), caught, thunk(cleanup, head)],
2384                )
2385            }
2386        })
2387    }
2388
2389    fn def_form(&mut self, head: Span) -> Result<Spanned, Fail> {
2390        let name_span = self.peek_span();
2391        let name = match self.bump() {
2392            TokenKind::Ident(n) => n,
2393            other => {
2394                return Err(fail(
2395                    format!("expected a name after `def`, found {other}"),
2396                    name_span,
2397                ))
2398            }
2399        };
2400        let mut params: Vec<(String, Span, Option<Spanned>)> = Vec::new();
2401        if !self.at(&TokenKind::LParen) {
2402            self.note(
2403                Spelling::ParameterList,
2404                name_span,
2405                &format!("def {name}"),
2406                &format!("def {name}()"),
2407            );
2408        }
2409        if self.at(&TokenKind::LParen) {
2410            self.bump();
2411            self.skip_newlines();
2412            if !self.eat(&TokenKind::RParen) {
2413                loop {
2414                    self.skip_newlines();
2415                    let p_span = self.peek_span();
2416                    match self.bump() {
2417                        // `a` — unannotated
2418                        TokenKind::Ident(p) => params.push((p, p_span, None)),
2419                        // `a:` came through as one token, so a type follows
2420                        TokenKind::Label(p) => {
2421                            self.skip_newlines();
2422                            let ty = self.type_expr()?;
2423                            params.push((p, p_span, Some(ty)));
2424                        }
2425                        other => {
2426                            return Err(fail(
2427                                format!("expected a parameter name, found {other}"),
2428                                p_span,
2429                            ))
2430                        }
2431                    }
2432                    self.skip_newlines();
2433                    if self.eat(&TokenKind::Comma) {
2434                        if self.trailing_comma(&TokenKind::RParen) {
2435                            break;
2436                        }
2437                        continue;
2438                    }
2439                    self.expect(&TokenKind::RParen, "`,` or `)`")?;
2440                    break;
2441                }
2442            }
2443        }
2444        // The signature list spans the name through the closing `)`.
2445        let sig_span = self.span_since(name_span.start);
2446
2447        // Optional `-> R`
2448        let ret = if matches!(self.peek(), TokenKind::Op(o) if o == "->") {
2449            self.bump();
2450            self.skip_newlines();
2451            Some(self.type_expr()?)
2452        } else {
2453            None
2454        };
2455
2456        let begins = {
2457            let save = self.pos;
2458            self.skip_newlines();
2459            let at = self.at_ident("begin").then(|| self.peek_span());
2460            self.pos = save;
2461            at
2462        };
2463        let body = self.protected_body(head)?;
2464        if let Some(b) = begins {
2465            let protects = matches!(&body.form, SpannedForm::List(items)
2466                if matches!(items.first().and_then(Spanned::as_symbol), Some("try") | Some(LOWERED_ENSURE)));
2467            if protects && body.span.start == b.start {
2468                self.note(
2469                    Spelling::TryCatch,
2470                    b,
2471                    "begin … end as a whole def body",
2472                    "the clauses on the def itself",
2473                );
2474            }
2475        }
2476        self.expect_ident("end")?;
2477        let whole = self.span_since(head.start);
2478
2479        let annotated = ret.is_some() || params.iter().any(|(_, _, t)| t.is_some());
2480        if !annotated {
2481            let mut sig = vec![sym_at(name_span, &name)];
2482            sig.extend(params.into_iter().map(|(p, s, _)| sym_at(s, &p)));
2483            return Ok(list_at(
2484                whole,
2485                vec![sym_at(head, "define"), list_at(sig_span, sig), body],
2486            ));
2487        }
2488
2489        // Typed shape. An un-annotated parameter in an otherwise annotated
2490        // signature is written `(p dyn)` so the checker sees the ladder
2491        // position explicitly rather than inferring it from absence.
2492        //
2493        // A `dyn` written by the parser rather than the author takes the
2494        // parameter's own span, so a diagnostic about it points at the
2495        // parameter — the only thing in the source it could mean.
2496        let mut sig = vec![sym_at(name_span, &name)];
2497        for (p, s, t) in params {
2498            let ty = t.unwrap_or_else(|| sym_at(s, "dyn"));
2499            sig.push(list_at(s.merge(ty.span), vec![sym_at(s, &p), ty]));
2500        }
2501        Ok(list_at(
2502            whole,
2503            vec![
2504                sym_at(head, "define-typed"),
2505                list_at(sig_span, sig),
2506                ret.unwrap_or_else(|| sym_at(head, "dyn")),
2507                body,
2508            ],
2509        ))
2510    }
2511
2512    /// A type expression. Currently a bare name (`Int`, `Str`, `dyn`) or a
2513    /// one-argument constructor (`List(Int)`).
2514    fn type_expr(&mut self) -> Result<Spanned, Fail> {
2515        let name_span = self.peek_span();
2516        let name = match self.bump() {
2517            TokenKind::Ident(n) => n,
2518            other => {
2519                return Err(fail(
2520                    format!("expected a type name, found {other}"),
2521                    name_span,
2522                ))
2523            }
2524        };
2525        if self.at(&TokenKind::LParen) {
2526            let args = self.paren_args()?;
2527            let mut list = vec![sym_at(name_span, &name)];
2528            list.extend(args);
2529            return Ok(list_at(self.span_since(name_span.start), list));
2530        }
2531        Ok(sym_at(name_span, &name))
2532    }
2533
2534    /// Consume `name` if it is the next token, else leave the position alone.
2535    fn eat_ident(&mut self, name: &str) -> bool {
2536        if self.at_ident(name) {
2537            self.bump();
2538            true
2539        } else {
2540            false
2541        }
2542    }
2543
2544    fn expect_ident(&mut self, name: &str) -> Result<(), Fail> {
2545        if self.at_ident(name) {
2546            self.bump();
2547            Ok(())
2548        } else {
2549            Err(self.error(format!("expected `{name}`, found {}", self.peek())))
2550        }
2551    }
2552
2553    /// A sequence of expressions up to one of `terminators`, wrapped in
2554    /// `(begin ...)` when there is more than one.
2555    fn body(&mut self, terminators: &[&str]) -> Result<Spanned, Fail> {
2556        // Anchored BEFORE the leading newlines are skipped, so an empty body's
2557        // span sits where the body would have started.
2558        let start = self.mark();
2559        let mut forms = Vec::new();
2560        loop {
2561            self.skip_newlines();
2562            if matches!(self.peek(), TokenKind::Eof) {
2563                return Err(self.error(format!(
2564                    "unterminated block: expected {} before the end of the file",
2565                    one_of(terminators)
2566                )));
2567            }
2568            if terminators.iter().any(|t| self.at_ident(t)) {
2569                break;
2570            }
2571            forms.push(self.statement()?);
2572            self.end_of_statement(terminators)?;
2573        }
2574        Ok(match forms.len() {
2575            0 => Spanned::new(Span::new(start, start), SpannedForm::Nil),
2576            1 => forms.into_iter().next().expect("checked len"),
2577            _ => {
2578                // `cover`, NOT `span_since`: the loop breaks *before* consuming
2579                // the terminator but *after* `skip_newlines`, so the last token
2580                // behind `pos` can be a newline and `span_since` would stretch
2581                // the body across trailing blank lines. The forms know exactly
2582                // where they are.
2583                let span = cover(&forms, Span::new(start, start));
2584                // The synthesized `begin` is zero-width at the FIRST FORM's
2585                // start, not at `start`. `start` is anchored before the leading
2586                // newlines are skipped, so it sits earlier than the body itself
2587                // — and a head node outside its own list's span is a span bug
2588                // that `every_node_sits_inside_its_parents_span` reports.
2589                let head = Span::new(span.start, span.start);
2590                let mut list = vec![sym_at(head, "begin")];
2591                list.extend(forms);
2592                list_at(span, list)
2593            }
2594        })
2595    }
2596}
2597
2598/// `` `a` ``, `` `a` or `b` ``, `` `a`, `b` or `c` `` — a list of keywords as
2599/// a sentence says it.
2600fn one_of(words: &[&str]) -> String {
2601    let quoted: Vec<String> = words.iter().map(|w| format!("`{w}`")).collect();
2602    match quoted.split_last() {
2603        None => String::new(),
2604        Some((last, [])) => last.clone(),
2605        Some((last, rest)) => format!("{} or {last}", rest.join(", ")),
2606    }
2607}
2608
2609fn let_bound(binder: Spanned, body: Spanned, at: Span) -> Spanned {
2610    let span = at.merge(body.span);
2611    let pair = list_at(binder.span, vec![binder, sym_at(at, RESCUED)]);
2612    list_at(
2613        span,
2614        vec![sym_at(at, "let"), list_at(pair.span, vec![pair]), body],
2615    )
2616}
2617
2618fn thunk(body: Spanned, at: Span) -> Spanned {
2619    let span = at.merge(body.span);
2620    list_at(
2621        span,
2622        vec![sym_at(at, "lambda"), list_at(at, Vec::new()), body],
2623    )
2624}
2625
2626/// Whether `name:` lexes as exactly one label spelling `name`: the parser's and
2627/// the formatter's shared test for a key that can be punned (`{name:}`). It asks
2628/// the lexer, so a name ending in `?` or `!` puns too and no second list of
2629/// "plain names" can drift from what a label is.
2630#[must_use]
2631pub fn label_name(name: &str) -> bool {
2632    matches!(
2633        lex(&format!("{name}:")).as_deref(),
2634        Ok([t, e]) if matches!(&t.kind, TokenKind::Label(n) if n == name) && e.kind == TokenKind::Eof
2635    )
2636}
2637
2638fn blue_lang_name(name: &str) -> bool {
2639    matches!(
2640        lex(name).as_deref(),
2641        Ok([t, e]) if matches!(&t.kind, TokenKind::Ident(n) if n == name) && e.kind == TokenKind::Eof
2642    ) && !name.ends_with(['?', '!'])
2643}
2644
2645fn mentions(form: &Spanned, name: &str) -> bool {
2646    match &form.form {
2647        SpannedForm::Atom(Atom::Symbol(s)) => s == name,
2648        SpannedForm::List(items) => items.iter().any(|i| mentions(i, name)),
2649        SpannedForm::Quote(i)
2650        | SpannedForm::Quasiquote(i)
2651        | SpannedForm::Unquote(i)
2652        | SpannedForm::UnquoteSplice(i) => mentions(i, name),
2653        _ => false,
2654    }
2655}
2656
2657/// A symbol node at `span`.
2658fn sym_at(span: Span, s: &str) -> Spanned {
2659    Spanned::new(span, SpannedForm::Atom(Atom::Symbol(s.to_string())))
2660}
2661
2662/// An atom node at `span`.
2663fn atom_at(span: Span, a: Atom) -> Spanned {
2664    Spanned::new(span, SpannedForm::Atom(a))
2665}
2666
2667/// A list node at `span`.
2668fn list_at(span: Span, items: Vec<Spanned>) -> Spanned {
2669    Spanned::new(span, SpannedForm::List(items))
2670}
2671
2672/// The smallest span covering every node in `items`, or `fallback` when there
2673/// are none.
2674///
2675/// The fallback is a parameter rather than `Span::synthetic()` because a
2676/// synthetic span is a claim that a node has no source origin at all — and an
2677/// empty parameter list in `def f()` very much has one. Defaulting to synthetic
2678/// here is how "no span" leaks into a tree where every node does have a place.
2679fn cover(items: &[Spanned], fallback: Span) -> Span {
2680    let merged = items
2681        .iter()
2682        .fold(Span::synthetic(), |acc, s| acc.merge(s.span));
2683    if merged.is_synthetic() {
2684        fallback
2685    } else {
2686        merged
2687    }
2688}
2689
2690#[cfg(test)]
2691mod tests {
2692    use super::*;
2693
2694    /// Render an `Sexp` to canonical text so tests can state the expected
2695    /// quoted form as a string. This is `Display`, which tatara-lisp owns —
2696    /// blue does not build Lisp syntax by concatenation.
2697    fn q(src: &str) -> String {
2698        parse_expr(src)
2699            .map(|s| s.to_string())
2700            .unwrap_or_else(|e| panic!("{src:?}: {e}"))
2701    }
2702
2703    // ---- the thesis: Ruby-shaped source becomes tatara-lisp ----------
2704
2705    #[test]
2706    fn arithmetic_respects_precedence() {
2707        assert_eq!(q("1 + 2 * 3"), "(+ 1 (* 2 3))");
2708        assert_eq!(q("(1 + 2) * 3"), "(* (+ 1 2) 3)");
2709    }
2710
2711    #[test]
2712    fn comparison_binds_looser_than_arithmetic() {
2713        assert_eq!(q("a + 1 < b"), "(< (+ a 1) b)");
2714    }
2715
2716    /// The expected tree names `or`/`and`, not `||`/`&&`: the surface
2717    /// spelling is the SURFACE's, and lowering renames it to the form
2718    /// tatara-lisp actually has. This test previously asserted the verbatim
2719    /// spelling, which is how `(== a b)` — a symbol nothing binds — shipped.
2720    #[test]
2721    fn logical_operators_bind_loosest_and_lower_to_tataras_names() {
2722        assert_eq!(q("a && b || c"), "(or (and a b) c)");
2723    }
2724
2725    #[test]
2726    fn left_associativity() {
2727        assert_eq!(q("1 - 2 - 3"), "(- (- 1 2) 3)");
2728    }
2729
2730    #[test]
2731    fn a_dot_without_parentheses_reads_a_field() {
2732        assert_eq!(q("user.name"), "(blue-field user :name)");
2733    }
2734
2735    #[test]
2736    fn method_call_with_args() {
2737        assert_eq!(q("user.greet(1, 2)"), "(greet user 1 2)");
2738    }
2739
2740    #[test]
2741    fn chained_fields_read_left_to_right() {
2742        assert_eq!(q("a.b.c"), "(blue-field (blue-field a :b) :c)");
2743        assert_eq!(q("a.b(1).c"), "(blue-field (b a 1) :c)");
2744    }
2745
2746    #[test]
2747    fn plain_call() {
2748        assert_eq!(q("f(1, 2)"), "(f 1 2)");
2749    }
2750
2751    /// The pipeline threads into the FIRST argument, as Elixir's does —
2752    /// that is what makes `|>` composable rather than decorative.
2753    #[test]
2754    fn pipeline_threads_into_first_argument() {
2755        assert_eq!(q("x |> f"), "(f x)");
2756        assert_eq!(q("x |> f(1)"), "(f x 1)");
2757        assert_eq!(q("x |> f |> g"), "(g (f x))");
2758    }
2759
2760    #[test]
2761    fn pipeline_binds_looser_than_arithmetic() {
2762        assert_eq!(q("1 + 2 |> f"), "(f (+ 1 2))");
2763    }
2764
2765    // ---- §V.13's rendering law, enforced at the parser ---------------
2766
2767    /// `a: 1` and `:a => 1` are the SAME TREE. That is exactly why the
2768    /// formatter may always render the shorthand: they are not two
2769    /// spellings of two things, they are two spellings of one thing.
2770    #[test]
2771    fn label_and_rocket_produce_the_same_tree_for_a_symbol_key() {
2772        assert_eq!(q("{a: 1}"), q("{:a => 1}"));
2773        assert_eq!(q("{a: 1}"), "(hash-map :a 1)");
2774    }
2775
2776    /// And where the key is NOT a plain symbol, the rocket is the only
2777    /// spelling — so it survives because it must, never as a style choice.
2778    #[test]
2779    fn a_string_key_has_no_shorthand() {
2780        assert_eq!(q(r#"{"k" => 1}"#), r#"(hash-map "k" 1)"#);
2781    }
2782
2783    #[test]
2784    fn list_literal() {
2785        assert_eq!(q("[1, 2, 3]"), "(list 1 2 3)");
2786        assert_eq!(q("[]"), "(list)");
2787    }
2788
2789    // ---- blocks ------------------------------------------------------
2790
2791    #[test]
2792    fn if_else_end() {
2793        assert_eq!(q("if a\n  1\nelse\n  2\nend"), "(if a 1 2)");
2794    }
2795
2796    #[test]
2797    fn if_without_else() {
2798        assert_eq!(q("if a\n  1\nend"), "(if a 1)");
2799    }
2800
2801    /// `unless` lowers to `(if (not c) …)` — one tree per meaning, so
2802    /// every downstream tool sees exactly one shape.
2803    #[test]
2804    fn unless_is_a_negated_if() {
2805        assert_eq!(q("unless a\n  1\nend"), "(if (not a) 1)");
2806    }
2807
2808    #[test]
2809    fn multi_statement_body_becomes_begin() {
2810        assert_eq!(q("if a\n  1\n  2\nend"), "(if a (begin 1 2))");
2811    }
2812
2813    #[test]
2814    fn def_lowers_to_define() {
2815        assert_eq!(
2816            q("def add(a, b)\n  a + b\nend"),
2817            "(define (add a b) (+ a b))"
2818        );
2819    }
2820
2821    #[test]
2822    fn def_with_no_params() {
2823        assert_eq!(q("def zero()\n  0\nend"), "(define (zero) 0)");
2824    }
2825
2826    // ---- literals ----------------------------------------------------
2827
2828    #[test]
2829    fn literals_lower_to_atoms() {
2830        assert_eq!(q("42"), "42");
2831        assert_eq!(q("true"), "#t");
2832        assert_eq!(q(":ok"), ":ok");
2833        assert_eq!(q(r#""hi""#), r#""hi""#);
2834    }
2835
2836    #[test]
2837    fn unary_minus_and_not() {
2838        assert_eq!(q("-x"), "(- 0 x)");
2839        assert_eq!(q("!x"), "(not x)");
2840    }
2841
2842    // ---- programs and errors -----------------------------------------
2843
2844    #[test]
2845    fn a_program_is_a_sequence_of_forms() {
2846        let forms = parse_program("def f()\n  1\nend\nf()").expect("parse");
2847        assert_eq!(forms.len(), 2);
2848        assert_eq!(forms[1].to_string(), "(f)");
2849    }
2850
2851    #[test]
2852    fn unterminated_block_is_an_error_naming_what_was_expected() {
2853        let e = parse_program("if a\n  1").expect_err("must fail");
2854        assert!(e.message.contains("unterminated"), "{}", e.message);
2855    }
2856
2857    #[test]
2858    fn a_parse_error_carries_a_span_into_the_source() {
2859        let src = "1 + )";
2860        let e = parse_program(src).expect_err("must fail");
2861        assert!(e.span.start < src.len(), "span {:?} outside source", e.span);
2862    }
2863
2864    // ---- one statement per line -------------------------------------
2865
2866    /// `y = xs[0]` was TWO statements, `(define y xs)` and `(list 0)`: nothing
2867    /// required a separator between statements, so the parser finished `xs`,
2868    /// saw a list literal and started a new statement on the same line. The
2869    /// program ran and `y` was the whole list. Found by the 2026-09-29 audit.
2870    #[test]
2871    fn postfix_indexing_is_refused_naming_nth() {
2872        let e = parse_program("y = xs[0]").expect_err("must not split into two statements");
2873        assert!(e.message.contains("`nth(0, xs)`"), "{}", e.message);
2874        assert_eq!((e.line, e.col), (1, 7), "{e}");
2875    }
2876
2877    /// `a = f x` — Ruby's command call — parsed as `a = f` and then `x`.
2878    #[test]
2879    fn a_call_without_parentheses_is_refused_naming_the_call() {
2880        let e = parse_program("a = f x").expect_err("must not split into two statements");
2881        assert!(e.message.contains("`f(x)`"), "{}", e.message);
2882        assert_eq!((e.line, e.col), (1, 7), "{e}");
2883    }
2884
2885    /// The same rule inside a block body, and in every body shape.
2886    #[test]
2887    fn two_statements_on_one_line_are_refused_in_a_body() {
2888        for src in [
2889            "def f(xs)\n  xs[0]\nend",
2890            "if a\n  f x\nend",
2891            "fn(x) x 1 end",
2892            "test \"t\"\n  assert a b\nend",
2893            "1 2",
2894        ] {
2895            assert!(parse_program(src).is_err(), "{src:?} must not parse");
2896        }
2897    }
2898
2899    /// `(x) -> e` is the inline spelling of `fn(x) e end`: one tree.
2900    #[test]
2901    fn an_arrow_lambda_lowers_to_the_same_tree_as_fn() {
2902        for (arrow, block) in [
2903            ("(x) -> x * 2", "fn(x) x * 2 end"),
2904            ("(a, b) -> a + b", "fn(a, b) a + b end"),
2905            ("() -> 1", "fn() 1 end"),
2906            ("map(xs, (x) -> x + 1)", "map(xs, fn(x) x + 1 end)"),
2907        ] {
2908            let a = parse_program(arrow).expect(arrow);
2909            let b = parse_program(block).expect(block);
2910            assert_eq!(format!("{a:?}"), format!("{b:?}"), "{arrow}");
2911        }
2912        assert_eq!(
2913            parse_program("(x)").map(|p| p.len()).ok(),
2914            Some(1),
2915            "a parenthesised name is still a group"
2916        );
2917    }
2918
2919    /// The controls: every place a statement legitimately ends on its line.
2920    /// A block keyword closing the body on the same line is one of them, which
2921    /// is what keeps a one-line lambda one line.
2922    #[test]
2923    fn a_statement_may_end_at_a_newline_the_end_or_a_closing_keyword() {
2924        for src in [
2925            "fn(x) x + 1 end",
2926            "map(fn(x) x end, xs)",
2927            "if a\n  1\nelse\n  2\nend",
2928            "if a 1 else 2 end",
2929            "case x\nwhen 1 :a\nwhen 2 :b\nelse :c\nend",
2930            "x = 1\ny = 2",
2931            "x = 1",
2932        ] {
2933            assert!(
2934                parse_program(src).is_ok(),
2935                "{src:?}: {:?}",
2936                parse_program(src)
2937            );
2938        }
2939    }
2940
2941    /// `if c then a else b end` read `then` as a statement and `a` as another.
2942    #[test]
2943    fn then_is_refused_by_name() {
2944        let e = parse_program("x = if c then 1 else 2 end").expect_err("then is not a keyword");
2945        assert!(e.message.contains("`then`"), "{}", e.message);
2946    }
2947
2948    /// A brace block after a send is not a map literal the author meant.
2949    #[test]
2950    fn a_brace_block_is_refused_naming_fn() {
2951        let e = parse_program("xs.map { |x| x }").expect_err("no brace blocks");
2952        assert!(e.message.contains("fn(x)"), "{}", e.message);
2953    }
2954
2955    // ---- qualified names ------------------------------------------------
2956
2957    /// `retsu::first(xs)` is one symbol, tatara's `require` shape.
2958    ///
2959    /// Red run (2026-09-29), before the `::` token: `expected an expression,
2960    /// found `:``.
2961    #[test]
2962    fn a_qualified_name_is_one_symbol() {
2963        assert_eq!(q("retsu::first(xs)"), "(retsu/first xs)");
2964        assert_eq!(q("retsu::first"), "retsu/first");
2965        assert_eq!(q("xs |> retsu::first"), "(retsu/first xs)");
2966        assert_eq!(q("blue::length(xs)"), "(blue/length xs)");
2967    }
2968
2969    /// Whitespace around `::` is layout, not meaning.
2970    #[test]
2971    fn whitespace_around_the_qualifier_is_accepted() {
2972        assert_eq!(q("retsu :: first(xs)"), "(retsu/first xs)");
2973    }
2974
2975    /// After `::` a reserved word is a name: `kueri::if` is kueri's
2976    /// definition, not the `if` form.
2977    #[test]
2978    fn a_reserved_word_after_the_qualifier_is_a_name() {
2979        assert_eq!(q("kueri::if(a, b)"), "(kueri/if a b)");
2980        assert_eq!(q("heni::test(x)"), "(heni/test x)");
2981        assert_eq!(q("sabi::fn"), "sabi/fn");
2982    }
2983
2984    #[test]
2985    fn a_qualified_name_has_exactly_one_qualifier() {
2986        for (src, says) in [
2987            ("::first(xs)", "`package::name`"),
2988            ("a::b::c", "one qualifier"),
2989            ("if::x", "reserved word"),
2990            ("retsu::1", "expected a name after `retsu::`"),
2991        ] {
2992            let e = parse_program(src).expect_err(src);
2993            assert!(e.message.contains(says), "{src}: {}", e.message);
2994        }
2995    }
2996
2997    /// The one reader of the shape: division is not qualified, and neither
2998    /// is anything the surface could not have written.
2999    #[test]
3000    fn qualified_reads_only_the_package_name_shape() {
3001        assert_eq!(qualified("retsu/first"), Some(("retsu", "first")));
3002        assert_eq!(qualified("kueri/if"), Some(("kueri", "if")));
3003        assert_eq!(qualified("nisshi/pair?"), Some(("nisshi", "pair?")));
3004        for not in ["/", "a/", "/a", "a/b/c", "if/x", "hash-map", "a"] {
3005            assert_eq!(qualified(not), None, "{not}");
3006        }
3007        assert!(is_callable_name("kueri/if"));
3008    }
3009
3010    /// `:pair?` is the symbol `pair?`: an import list can name a predicate.
3011    ///
3012    /// Red run (2026-09-29), before: `unexpected character `?``, found by
3013    /// `blue migrate` listing nisshi's `pair?`.
3014    #[test]
3015    fn a_symbol_may_end_in_a_question_or_bang() {
3016        assert_eq!(q("[:pair?, :set!]"), "(list :pair? :set!)");
3017        assert_eq!(q("use(\"n\", [:empty?])"), "(use \"n\" (list :empty?))");
3018        assert_eq!(q(":a != :b"), "(not= :a :b)");
3019    }
3020
3021    // ---- errors a person can read -------------------------------------
3022
3023    /// `line:col` and blue's words, never `at 10..11` and `Ident("y")`.
3024    #[test]
3025    fn a_parse_error_displays_line_col_and_the_source_text() {
3026        let e = parse_program("x = 1\ny = [1, 2 y]").expect_err("must fail");
3027        assert_eq!(e.to_string(), "2:11: expected `,` or `]`, found `y`");
3028        assert_eq!(e.span, Span::new(16, 17), "the span stays, as data");
3029    }
3030
3031    /// Every token kind renders without Rust's `Debug` shape.
3032    #[test]
3033    fn no_error_message_names_a_rust_variant() {
3034        for src in [
3035            "f(1 2)",
3036            "{a: 1 b}",
3037            "def 1\nend",
3038            "x.1",
3039            "if a\n",
3040            "fn(1) 2 end",
3041            "[1 :b]",
3042            "1 +",
3043            "(1",
3044        ] {
3045            let e = parse_program(src).expect_err(src);
3046            let shown = e.to_string();
3047            for tell in [
3048                "Ident(", "Int(", "Op(", "Label(", "Sym(", "Str(", "LParen", "RBracket", "Eof",
3049                "Newline", "..",
3050            ] {
3051                assert!(!shown.contains(tell), "{src:?} -> {shown}");
3052            }
3053            assert!(e.line >= 1 && e.col >= 1, "{src:?} not located: {shown}");
3054        }
3055    }
3056
3057    /// A lex error reaches the author through the same door, located.
3058    #[test]
3059    fn a_lex_error_is_located_too() {
3060        let e = parse_program("x = 1\ny = 2; z = 3").expect_err("no semicolons");
3061        assert_eq!((e.line, e.col), (2, 6), "{e}");
3062        assert!(e.message.contains("new line"), "{e}");
3063    }
3064
3065    /// Anti-vacuity: `q` must be able to FAIL. If every input parsed, the
3066    /// assertions above would be worthless.
3067    #[test]
3068    fn the_parser_rejects_garbage() {
3069        assert!(parse_program("def").is_err());
3070        assert!(parse_program("(1").is_err());
3071        assert!(parse_program("end").is_err());
3072    }
3073}