doge-compiler 0.2.0

Compiler for the Doge programming language — lexer, parser, semantic checks, and Rust codegen.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
use super::*;

impl Parser {
    // ----- statements -----

    pub(super) fn parse_statement(&mut self) -> Result<Stmt, Diagnostic> {
        match self.peek() {
            TokenKind::Such => self.parse_such(),
            TokenKind::So => self.parse_so(),
            TokenKind::Many => self.parse_many(),
            TokenKind::Very => self.parse_very(),
            TokenKind::Pls => self.parse_pls(),
            TokenKind::If => self.parse_if(),
            TokenKind::For => self.parse_for(),
            TokenKind::While => self.parse_while(),
            TokenKind::Bark => self.parse_bark(),
            TokenKind::Bonk => self.parse_bonk(),
            TokenKind::Return => self.parse_return(),
            TokenKind::Bork => {
                let span = self.current_span();
                self.advance();
                self.eat(TokenKind::Newline)?;
                Ok(Stmt::Bork { span })
            }
            TokenKind::Continue => {
                let span = self.current_span();
                self.advance();
                self.eat(TokenKind::Newline)?;
                Ok(Stmt::Continue { span })
            }
            TokenKind::Def => {
                Err(self.python_habit("no def here", "such greet much name: is the way"))
            }
            TokenKind::Class => Err(self.python_habit("no class here", "many Name: is the way")),
            TokenKind::Amaze => self.parse_amaze(),
            _ => self.parse_expr_or_assign(),
        }
    }

    /// `such` is contextual: `such NAME =` is a variable, `such NAME :` or
    /// `such NAME much …:` is a function definition (docs/GRAMMAR.md).
    pub(super) fn parse_such(&mut self) -> Result<Stmt, Diagnostic> {
        let span = self.current_span();
        self.eat(TokenKind::Such)?;
        let (name, _) = self.eat_ident("a name after such")?;

        // `such a, b = …` — a destructuring declaration. A name list is always a
        // declaration (a function has a single name), so `=` and values follow.
        if self.is(&TokenKind::Comma) {
            let (names, rest) = self.parse_target_names(name)?;
            self.expect_destructure_eq()?;
            let expr = self.parse_assign_rhs(true)?;
            self.eat(TokenKind::Newline)?;
            return Ok(Stmt::Decl {
                names,
                rest,
                expr,
                span,
            });
        }

        match self.peek() {
            TokenKind::Eq => {
                self.advance();
                let expr = self.parse_assign_rhs(false)?;
                self.eat(TokenKind::Newline)?;
                Ok(Stmt::Decl {
                    names: vec![name],
                    rest: None,
                    expr,
                    span,
                })
            }
            TokenKind::Much | TokenKind::Colon => {
                let mut params = Params::default();
                if self.is(&TokenKind::Much) {
                    self.advance();
                    params = self.parse_params()?;
                }
                self.eat(TokenKind::Colon)?;
                let body = self.parse_block()?;
                if !self.is(&TokenKind::Wow) {
                    return Err(self.missing_wow(self.current_span(), "function"));
                }
                self.advance(); // wow
                self.eat(TokenKind::Newline)?;
                Ok(Stmt::FuncDef {
                    name,
                    params,
                    body,
                    span,
                })
            }
            _ => {
                let bad = self.current_span();
                Err(self
                    .diag(
                        bad,
                        format!("after such {name}, doge expects = (a variable) or : (a function)"),
                    )
                    .with_hint("such x = 1  —  or  —  such greet much name:"))
            }
        }
    }

    /// `so` is contextual: `so NAME =` is a constant, bare `so NAME` is an
    /// import, and `so "path/to/mod.doge"` is a string-path import
    /// (docs/GRAMMAR.md).
    pub(super) fn parse_so(&mut self) -> Result<Stmt, Diagnostic> {
        let span = self.current_span();
        self.eat(TokenKind::So)?;

        if let TokenKind::Str(_) | TokenKind::StrInterp(_) = self.peek() {
            return self.parse_path_import(span);
        }

        let (name, _) = self.eat_ident("a name after so")?;
        match self.peek() {
            TokenKind::Eq => {
                self.advance();
                let expr = self.parse_expr()?;
                self.eat(TokenKind::Newline)?;
                Ok(Stmt::ConstDecl { name, expr, span })
            }
            TokenKind::Newline => {
                self.advance();
                Ok(Stmt::Import {
                    module: name,
                    path: None,
                    span,
                })
            }
            _ => {
                let bad = self.current_span();
                Err(self
                    .diag(
                        bad,
                        format!("after so {name}, doge expects = (a constant) or a line end (an import)"),
                    )
                    .with_hint("so PI = 3.14  —  so math  —  or  —  so \"lib/utils.doge\""))
            }
        }
    }

    /// `so "path/to/mod.doge"` — a string-path import. The path is relative to the
    /// importing file, must end in `.doge`, use `/` separators, and have a stem
    /// that is a valid identifier (which becomes the module's binding name).
    fn parse_path_import(&mut self, span: Span) -> Result<Stmt, Diagnostic> {
        let str_span = self.current_span();
        if let TokenKind::StrInterp(_) = self.peek() {
            self.advance();
            return Err(self
                .diag(
                    str_span,
                    "an import path is a plain string, not an interpolated one",
                )
                .with_headline("very import. much confuse.")
                .with_hint("write a fixed path — so \"lib/utils.doge\""));
        }
        let TokenKind::Str(raw) = self.advance().kind else {
            unreachable!("parse_path_import entered only on a string token");
        };
        self.eat(TokenKind::Newline)?;

        let module = self.import_path_stem(&raw, str_span)?;
        Ok(Stmt::Import {
            module,
            path: Some(raw),
            span,
        })
    }

    /// Validate a string import path and return the stem it binds. The rules keep
    /// paths portable and the binding name usable: forward slashes only, relative,
    /// ends in `.doge`, and a stem that is a legal identifier.
    fn import_path_stem(&self, raw: &str, span: Span) -> Result<String, Diagnostic> {
        let bad = |message: &str, hint: &str| {
            self.diag(span, message.to_string())
                .with_headline("very path. much confuse.")
                .with_hint(hint.to_string())
        };

        if raw.contains('\\') {
            return Err(bad(
                "an import path uses / separators, not \\",
                "write forward slashes — so \"lib/utils.doge\"",
            ));
        }
        if raw.starts_with('/') {
            return Err(bad(
                "an import path is relative to this file, so it cannot start with /",
                "drop the leading / — so \"lib/utils.doge\"",
            ));
        }
        let Some(file) = raw.rsplit('/').next().filter(|f| !f.is_empty()) else {
            return Err(bad(
                "an import path needs a file name",
                "point at a .doge file — so \"lib/utils.doge\"",
            ));
        };
        let Some(stem) = file.strip_suffix(".doge") else {
            return Err(bad(
                "an import path names a .doge file",
                "add the extension — so \"lib/utils.doge\"",
            ));
        };
        if stem.is_empty() || !is_ident(stem) {
            return Err(bad(
                "the file name in an import path must be a plain name (so it can bind the module)",
                "rename the file to a plain name — so \"lib/utils.doge\"",
            ));
        }
        Ok(stem.to_string())
    }

    /// `many NAME [much PARENT]: … wow` — an object definition whose body is only
    /// functions. The optional `much PARENT` names the class it inherits from.
    pub(super) fn parse_many(&mut self) -> Result<Stmt, Diagnostic> {
        let span = self.current_span();
        self.eat(TokenKind::Many)?;
        let (name, _) = self.eat_ident("an object name after many")?;
        let parent = if self.is(&TokenKind::Much) {
            self.advance();
            let (parent, _) = self.eat_ident("a parent object name after much")?;
            Some(parent)
        } else {
            None
        };
        self.eat(TokenKind::Colon)?;
        let body = self.parse_block()?;
        let mut methods = Vec::new();
        for stmt in body {
            match stmt {
                Stmt::FuncDef { .. } => methods.push(stmt),
                other => {
                    let bad = other.span();
                    return Err(self
                        .diag(bad, "an object body may only hold methods (such …:)")
                        .with_headline("very object. much confuse.")
                        .with_hint("move this out of the object, or make it a method"));
                }
            }
        }
        if !self.is(&TokenKind::Wow) {
            return Err(self.missing_wow(self.current_span(), "object"));
        }
        self.advance(); // wow
        self.eat(TokenKind::Newline)?;
        Ok(Stmt::ObjDef {
            name,
            parent,
            methods,
            span,
        })
    }

    pub(super) fn parse_very(&mut self) -> Result<Stmt, Diagnostic> {
        let span = self.current_span();
        self.eat(TokenKind::Very)?;
        let first = self.parse_expr()?;
        if self.is(&TokenKind::Comma) {
            return self.finish_multi_assign(first, true, span);
        }
        let op = match self.peek() {
            TokenKind::Eq => None,
            TokenKind::AugAssign(op) => Some(*op),
            _ => {
                return Err(self
                    .diag(
                        first.span(),
                        "very always reassigns — doge expects = after the target",
                    )
                    .with_hint("very age = 9"))
            }
        };
        self.advance();
        let expr = self.parse_assign_rhs(false)?;
        self.eat(TokenKind::Newline)?;
        self.require_target(&first)?;
        Ok(Stmt::Assign {
            targets: vec![first],
            rest: None,
            expr,
            op,
            flavored: true,
            span,
        })
    }

    pub(super) fn parse_pls(&mut self) -> Result<Stmt, Diagnostic> {
        let span = self.current_span();
        self.eat(TokenKind::Pls)?;
        let body = self.parse_block()?;
        self.eat(TokenKind::OhNo)?;
        let (err_name, _) = self.eat_ident("the caught error's name after oh no")?;
        self.eat(TokenKind::Bang)?;
        let handler = self.parse_block()?;
        Ok(Stmt::Try {
            body,
            err_name,
            handler,
            span,
        })
    }

    pub(super) fn parse_if(&mut self) -> Result<Stmt, Diagnostic> {
        let span = self.current_span();
        self.eat(TokenKind::If)?;
        let mut branches = Vec::new();
        let cond = self.parse_expr()?;
        self.eat(TokenKind::Colon)?;
        let body = self.parse_block()?;
        branches.push((cond, body));

        while self.is(&TokenKind::Elif) {
            self.advance();
            let cond = self.parse_expr()?;
            self.eat(TokenKind::Colon)?;
            let body = self.parse_block()?;
            branches.push((cond, body));
        }

        let else_body = if self.is(&TokenKind::Else) {
            self.advance();
            self.eat(TokenKind::Colon)?;
            Some(self.parse_block()?)
        } else {
            None
        };

        Ok(Stmt::If {
            branches,
            else_body,
            span,
        })
    }

    pub(super) fn parse_for(&mut self) -> Result<Stmt, Diagnostic> {
        let span = self.current_span();
        self.eat(TokenKind::For)?;
        let (first, _) = self.eat_ident("a loop variable after for")?;
        let (vars, rest) = if self.is(&TokenKind::Comma) {
            self.parse_target_names(first)?
        } else {
            (vec![first], None)
        };
        self.eat(TokenKind::In)?;
        let iter = self.parse_expr()?;
        self.eat(TokenKind::Colon)?;
        let body = self.parse_block()?;
        Ok(Stmt::For {
            vars,
            rest,
            iter,
            body,
            span,
        })
    }

    pub(super) fn parse_while(&mut self) -> Result<Stmt, Diagnostic> {
        let span = self.current_span();
        self.eat(TokenKind::While)?;
        let cond = self.parse_expr()?;
        self.eat(TokenKind::Colon)?;
        let body = self.parse_block()?;
        Ok(Stmt::While { cond, body, span })
    }

    pub(super) fn parse_bark(&mut self) -> Result<Stmt, Diagnostic> {
        let span = self.current_span();
        self.eat(TokenKind::Bark)?;
        let expr = self.parse_expr()?;
        self.eat(TokenKind::Newline)?;
        Ok(Stmt::Bark { expr, span })
    }

    pub(super) fn parse_bonk(&mut self) -> Result<Stmt, Diagnostic> {
        let span = self.current_span();
        self.eat(TokenKind::Bonk)?;
        let expr = self.parse_expr()?;
        self.eat(TokenKind::Newline)?;
        Ok(Stmt::Bonk { expr, span })
    }

    /// `amaze cond` or `amaze cond, message` — assert. The optional message
    /// follows a comma; `parse_expr` stops at the comma, so the split is clean.
    pub(super) fn parse_amaze(&mut self) -> Result<Stmt, Diagnostic> {
        let span = self.current_span();
        self.eat(TokenKind::Amaze)?;
        let cond = self.parse_expr()?;
        let message = if self.is(&TokenKind::Comma) {
            self.advance();
            Some(self.parse_expr()?)
        } else {
            None
        };
        self.eat(TokenKind::Newline)?;
        Ok(Stmt::Amaze {
            cond,
            message,
            span,
        })
    }

    pub(super) fn parse_return(&mut self) -> Result<Stmt, Diagnostic> {
        let span = self.current_span();
        self.eat(TokenKind::Return)?;
        let expr = if self.is(&TokenKind::Newline) {
            None
        } else {
            Some(self.parse_expr()?)
        };
        self.eat(TokenKind::Newline)?;
        Ok(Stmt::Return { expr, span })
    }

    /// A leading expression: an assignment if `=` follows a valid target,
    /// otherwise a bare expression statement.
    pub(super) fn parse_expr_or_assign(&mut self) -> Result<Stmt, Diagnostic> {
        let span = self.current_span();
        let expr = self.parse_expr()?;
        if self.is(&TokenKind::Comma) {
            return self.finish_multi_assign(expr, false, span);
        }
        // `Some(None)` is a plain `=`; `Some(Some(op))` is an augmented `op=`.
        let assign = match self.peek() {
            TokenKind::Eq => Some(None),
            TokenKind::AugAssign(op) => Some(Some(*op)),
            _ => None,
        };
        if let Some(op) = assign {
            self.advance();
            let value = self.parse_assign_rhs(false)?;
            self.eat(TokenKind::Newline)?;
            self.require_target(&expr)?;
            Ok(Stmt::Assign {
                targets: vec![expr],
                rest: None,
                expr: value,
                op,
                flavored: false,
                span,
            })
        } else {
            self.eat(TokenKind::Newline)?;
            Ok(Stmt::ExprStmt { expr })
        }
    }

    /// Finish a destructuring assignment once a `,` after the first target has
    /// revealed it: collect the remaining targets (with an optional trailing
    /// `many` collector), reject an augmented operator (destructuring is `=`
    /// only), then bind the comma-list right-hand side. `flavored` records a
    /// leading `very`.
    fn finish_multi_assign(
        &mut self,
        first: Expr,
        flavored: bool,
        span: Span,
    ) -> Result<Stmt, Diagnostic> {
        let (targets, rest) = self.parse_target_exprs(first)?;
        if let TokenKind::AugAssign(_) = self.peek() {
            return Err(self
                .diag(
                    self.current_span(),
                    "augmented assignment takes a single target",
                )
                .with_headline("very many. much augment.")
                .with_hint("split it up, e.g. a = a + 1 on its own line"));
        }
        self.expect_destructure_eq()?;
        let value = self.parse_assign_rhs(true)?;
        self.eat(TokenKind::Newline)?;
        for target in &targets {
            self.require_target(target)?;
        }
        if let Some(rest) = &rest {
            self.require_target(rest)?;
        }
        Ok(Stmt::Assign {
            targets,
            rest,
            expr: value,
            op: None,
            flavored,
            span,
        })
    }

    /// A block: `NEWLINE INDENT { statement } DEDENT` (docs/GRAMMAR.md).
    pub(super) fn parse_block(&mut self) -> Result<Vec<Stmt>, Diagnostic> {
        self.eat(TokenKind::Newline)?;
        self.eat(TokenKind::Indent).map_err(|_| {
            let span = self.current_span();
            self.diag(span, "doge expected an indented block here")
                .with_headline("very flat. much empty.")
                .with_hint("indent the body with spaces")
        })?;
        let mut stmts = Vec::new();
        while !self.is(&TokenKind::Dedent) {
            if self.is(&TokenKind::Eof) {
                // The lexer balances every Indent with a Dedent, so this only
                // fires on a genuinely truncated stream — treat it as end early.
                return Err(self.missing_wow(self.current_span(), "block"));
            }
            stmts.push(self.parse_statement()?);
        }
        self.eat(TokenKind::Dedent)?;
        Ok(stmts)
    }

    /// A function header's parameter list (after the `much`): comma-separated
    /// parameters, each optionally `name = literal`, with an optional trailing
    /// `many rest` variadic. Required parameters must precede defaulted ones, and
    /// the variadic — if present — must come last (docs/GRAMMAR.md).
    pub(super) fn parse_params(&mut self) -> Result<Params, Diagnostic> {
        let mut params: Vec<Param> = Vec::new();
        let mut vararg: Option<String> = None;
        loop {
            if self.is(&TokenKind::Many) {
                self.advance();
                let (name, _) = self.eat_ident("a name after many")?;
                vararg = Some(name);
                if self.is(&TokenKind::Comma) {
                    let comma = self.current_span();
                    return Err(self
                        .diag(comma, "the many parameter must be the last one")
                        .with_headline("very rest. much greedy.")
                        .with_hint("put many rest at the end of the parameter list"));
                }
                break;
            }
            let (name, span) = self.eat_ident("a parameter name")?;
            let default = if self.is(&TokenKind::Eq) {
                self.advance();
                let expr = self.parse_expr()?;
                self.require_literal(&expr)?;
                Some(expr)
            } else {
                if params.last().is_some_and(|p| p.default.is_some()) {
                    return Err(self
                        .diag(
                            span,
                            "a parameter with no default cannot follow one with a default",
                        )
                        .with_headline("very order. much default.")
                        .with_hint("move defaulted parameters to the end of the list"));
                }
                None
            };
            params.push(Param {
                name,
                default,
                span,
            });
            if self.is(&TokenKind::Comma) {
                self.advance();
            } else {
                break;
            }
        }
        Ok(Params { params, vararg })
    }

    /// A default parameter value must be a literal: a number, string, bool,
    /// `none`, a unary minus on a number, or a list/dict of literals. This keeps a
    /// default free of names and side effects, so it is evaluated fresh at every
    /// call (docs/SYNTAX.md §6).
    pub(super) fn require_literal(&self, expr: &Expr) -> Result<(), Diagnostic> {
        let ok = match expr {
            Expr::Int { .. }
            | Expr::Float { .. }
            | Expr::Str { .. }
            | Expr::Bool { .. }
            | Expr::None { .. } => true,
            Expr::Unary {
                op: UnOp::Neg,
                operand,
                ..
            } => matches!(operand.as_ref(), Expr::Int { .. } | Expr::Float { .. }),
            Expr::List { items, .. } => {
                for item in items {
                    self.require_literal(item)?;
                }
                true
            }
            Expr::Dict { entries, .. } => {
                for (key, value) in entries {
                    self.require_literal(key)?;
                    self.require_literal(value)?;
                }
                true
            }
            _ => false,
        };
        if ok {
            Ok(())
        } else {
            Err(self
                .diag(expr.span(), "a default parameter value must be a literal")
                .with_headline("very default. much dynamic.")
                .with_hint("use a fixed value like 0, \"hi\", true, none, or [ ]"))
        }
    }

    pub(super) fn require_target(&self, expr: &Expr) -> Result<(), Diagnostic> {
        match expr {
            Expr::Ident { .. } | Expr::Index { .. } | Expr::Attr { .. } => Ok(()),
            other => Err(self
                .diag(other.span(), "doge cannot assign to this")
                .with_hint("assign to a name, an item like xs[0], or a field like x.name")),
        }
    }

    /// The remaining names of a destructuring `such`/`for` header, given the
    /// already-consumed `first` name and the `,` that follows it: each further
    /// comma-separated name, then an optional trailing `many rest` collector that
    /// must be the last target. Used where every target is a plain binding name.
    fn parse_target_names(
        &mut self,
        first: String,
    ) -> Result<(Vec<String>, Option<String>), Diagnostic> {
        let mut names = vec![first];
        let mut rest = None;
        while self.is(&TokenKind::Comma) {
            self.advance();
            if self.is(&TokenKind::Many) {
                self.advance();
                let (name, _) = self.eat_ident("a name after many")?;
                rest = Some(name);
                self.reject_target_after_collector()?;
                break;
            }
            let (name, _) = self.eat_ident("a name after the comma")?;
            names.push(name);
        }
        Ok((names, rest))
    }

    /// Like [`parse_target_names`] but for a reassignment, where each target is a
    /// full assignable expression (`a`, `xs[0]`, `dog.name`) rather than a plain
    /// name. `require_target` validates each one at the call site.
    fn parse_target_exprs(&mut self, first: Expr) -> Result<(Vec<Expr>, Option<Expr>), Diagnostic> {
        let mut targets = vec![first];
        let mut rest = None;
        while self.is(&TokenKind::Comma) {
            self.advance();
            if self.is(&TokenKind::Many) {
                self.advance();
                rest = Some(self.parse_expr()?);
                self.reject_target_after_collector()?;
                break;
            }
            targets.push(self.parse_expr()?);
        }
        Ok((targets, rest))
    }

    /// A `many rest` collector must be the final target: a `,` after it is an
    /// error, mirroring the same rule on a `much rest` function parameter.
    fn reject_target_after_collector(&self) -> Result<(), Diagnostic> {
        if self.is(&TokenKind::Comma) {
            return Err(self
                .diag(
                    self.current_span(),
                    "the many collector must be the last target",
                )
                .with_headline("very rest. much greedy.")
                .with_hint("put many rest at the end of the target list"));
        }
        Ok(())
    }

    /// The right-hand side of an assignment. In a multiple-assignment position
    /// (`multi`), a comma-separated list of values builds an implicit List, so
    /// `a, b = b, a` swaps; a single value is returned as-is (unpacked at
    /// runtime). Outside multiple assignment a trailing comma is an error, so
    /// `such x = 1, 2` stays rejected rather than silently building a List.
    fn parse_assign_rhs(&mut self, multi: bool) -> Result<Expr, Diagnostic> {
        let first = self.parse_expr()?;
        if !self.is(&TokenKind::Comma) {
            return Ok(first);
        }
        if !multi {
            return Err(self
                .diag(
                    self.current_span(),
                    "doge sees extra values but only one name to hold them",
                )
                .with_headline("very many. much value.")
                .with_hint("give the left side matching names, or wrap the values in [ ]"));
        }
        let span = first.span();
        let mut items = vec![first];
        while self.is(&TokenKind::Comma) {
            self.advance();
            items.push(self.parse_expr()?);
        }
        Ok(Expr::List { items, span })
    }

    /// A destructuring header's target list must be followed by `=` and values.
    fn expect_destructure_eq(&mut self) -> Result<(), Diagnostic> {
        if !self.is(&TokenKind::Eq) {
            return Err(self
                .diag(
                    self.current_span(),
                    "a destructuring assignment needs = and values",
                )
                .with_headline("very unpack. much incomplete.")
                .with_hint("give matching values, e.g. a, b = [1, 2]"));
        }
        self.advance();
        Ok(())
    }
}

/// Whether `s` is a legal Doge identifier: a leading letter or `_`, then letters,
/// digits, or `_`. Mirrors the lexer's word rule ([`crate::lexer`]) so an import
/// path's stem can only bind a name the lexer would accept.
fn is_ident(s: &str) -> bool {
    let mut chars = s.chars();
    match chars.next() {
        Some(c) if c.is_ascii_alphabetic() || c == '_' => {}
        _ => return false,
    }
    chars.all(|c| c.is_ascii_alphanumeric() || c == '_')
}