rsemu 0.0.4

A multiplatform emulator in pure Rust, built bottom-up on a generic framework.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
//! The recursive-descent parser for `.machine` files.
//!
//! Hand-written, like the lexer: zero dependencies (`ROADMAP.md` §0), and a
//! generator would not produce the error messages §5 demands anyway.
//!
//! # Shape of the grammar
//!
//! A file is a flat list of statements, and so is every block. Each statement
//! begins with a word that says what it is, which is what keeps the graph
//! scannable and what makes a wrong word a one-line diagnostic:
//!
//! ```text
//! unit      := stmt* EOF
//! stmt      := machine | param | osc | space | object | map | wire
//!            | include | template | instance | for
//! machine   := "machine" STRING block
//! param     := "param" name ("=" expr)?
//! osc       := "osc" name "=" expr freq-unit
//! space     := "space" name props
//! object    := "object" name STRING props?
//! map       := "map" name expr "size" expr "=" expr props?
//! wire      := "wire" path "->" path
//! include   := "include" STRING
//! template  := "template" name ("(" param-list ")")? block
//! instance  := "instance" name "=" name ("(" arg-list ")")?
//! for       := "for" name "in" expr (".." | "..=") expr block
//! props     := "{" (name "=" expr ","?)* "}"
//! expr      := additive
//! additive  := multiplicative (("+"|"-") multiplicative)*
//! multiplicative := unary (("*"|"/"|"%") unary)*
//! unary     := "-" unary | primary
//! primary   := NUMBER | STRING | "true" | "false" | path | call
//!            | "(" expr ")" | "[" expr,* "]" | "{" (name "=" expr ","?)* "}"
//! call      := path "(" expr,* ")"          -- "(" must touch the path
//! path      := name ("." name)*
//! name      := (IDENT | "$" IDENT | "$" "{" expr "}")+   -- parts must touch
//! ```
//!
//! # Decisions §5 does not make for us
//!
//! * **Separators are optional.** `{ width = 16, unassigned = open-bus }` and a
//!   comma-less multi-line block are both in §5, so commas are permitted and
//!   never required. Newlines are not tokens; instead no expression can extend
//!   past its line by accident, because a call's `(` must touch its callee and
//!   there is no juxtaposition operator.
//! * **`instance`** is this parser's spelling of "instantiate a template". §5
//!   requires templates "instantiated N times" but shows no syntax; a named
//!   instantiation is chosen because a template declares several objects and
//!   the instance name is the natural namespace for them.
//! * **One error, then stop.** No resynchronisation: a recovering parser
//!   invents cascades, and §5 asks for a message a person can act on.
//!
//! # Robustness
//!
//! Nesting is capped at [`MAX_DEPTH`], which bounds both parser recursion and
//! the recursion in dropping the tree, so no input — truncated, adversarial, or
//! merely enthusiastic — can overflow the stack. Nothing here indexes, slices
//! or does unchecked arithmetic on untrusted values.

use alloc::boxed::Box;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;

use crate::machine::ast::{
    Arg, BinOp, Expr, ForStmt, FreqUnit, IncludeStmt, InstanceStmt, MachineDecl, MapStmt, Name,
    NamePart, ObjectDecl, OscDecl, ParamDecl, Path, Property, SourceUnit, SpaceDecl, Stmt,
    TemplateDecl, TemplateParam, UnOp, WireStmt,
};
use crate::machine::diag::Diagnostic;
use crate::machine::lexer::{Token, TokenKind, tokenize};
use crate::machine::span::{SourceFile, Span, Spanned};

/// How deeply blocks and expressions may nest.
///
/// A machine description does not legitimately nest anywhere near this far; the
/// limit exists so that `((((((…))))))` is an error message rather than a stack
/// overflow. It bounds tree depth, so dropping the result is bounded too.
pub const MAX_DEPTH: u32 = 64;

/// Every word that starts a statement, in the order the error message lists
/// them.
const STATEMENT_KEYWORDS: &[&str] = &[
    "machine", "param", "osc", "space", "object", "map", "wire", "include", "template", "instance",
    "for",
];

/// Parse a whole file into an AST.
///
/// The file is not read from disk — `no_std` has no filesystem and the include
/// search path belongs to the caller (`ROADMAP.md` §5). Returns the first
/// error; see [`Diagnostic::render`](crate::machine::diag::Diagnostic::render)
/// for turning it into something a person can read.
pub fn parse(src: &SourceFile<'_>) -> Result<SourceUnit, Diagnostic> {
    let tokens = tokenize(src)?;
    let eof = Token {
        kind: TokenKind::Eof,
        span: Span::at(tokens.last().map_or(0, |t| t.span.end)),
    };
    Parser {
        tokens,
        eof,
        pos: 0,
        depth: 0,
    }
    .unit()
}

/// Where a statement appears, which decides whether `machine` is legal there.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Scope {
    /// Top level of a file.
    File,
    /// Inside a `machine`, `template` or `for` body.
    Block,
}

struct Parser {
    tokens: Vec<Token>,
    /// Returned by [`Parser::cur`] past the end of the stream. The lexer always
    /// emits an `Eof` token and `advance` stops on it, so this is unreachable —
    /// it exists so that the parser contains no indexing operation at all, and
    /// therefore no way for a bug to become a panic on user input.
    eof: Token,
    pos: usize,
    depth: u32,
}

impl Parser {
    /// The current token.
    fn cur(&self) -> &Token {
        self.tokens.get(self.pos).unwrap_or(&self.eof)
    }

    fn span(&self) -> Span {
        self.cur().span
    }

    fn advance(&mut self) -> Token {
        let tok = self.cur().clone();
        if !matches!(tok.kind, TokenKind::Eof) {
            self.pos += 1;
        }
        tok
    }

    /// Whether the current token is of this kind, ignoring any payload.
    fn at(&self, kind: &TokenKind) -> bool {
        core::mem::discriminant(&self.cur().kind) == core::mem::discriminant(kind)
    }

    fn eat(&mut self, kind: &TokenKind) -> bool {
        if self.at(kind) {
            self.advance();
            true
        } else {
            false
        }
    }

    /// The current token's text, if it is an identifier.
    fn ident_text(&self) -> Option<&str> {
        match &self.cur().kind {
            TokenKind::Ident(text) => Some(text),
            _ => None,
        }
    }

    fn at_ident(&self, word: &str) -> bool {
        self.ident_text() == Some(word)
    }

    /// `expected X, found Y` at the current token.
    fn expected(&self, what: &str) -> Diagnostic {
        Diagnostic::new(
            self.span(),
            format!("expected {what}, found {}", self.cur().kind.describe()),
        )
    }

    fn expect(&mut self, kind: &TokenKind, what: &str) -> Result<Token, Diagnostic> {
        if self.at(kind) {
            Ok(self.advance())
        } else {
            Err(self.expected(what))
        }
    }

    /// Consume a contextual keyword such as `size` or `in`.
    fn expect_keyword(&mut self, word: &str) -> Result<Token, Diagnostic> {
        if self.at_ident(word) {
            Ok(self.advance())
        } else {
            Err(self.expected(&format!("`{word}`")))
        }
    }

    fn expect_string(&mut self, what: &str) -> Result<Spanned<String>, Diagnostic> {
        match &self.cur().kind {
            TokenKind::Str(text) => {
                let value = text.clone();
                let tok = self.advance();
                Ok(Spanned::new(value, tok.span))
            }
            _ => Err(self.expected(what)),
        }
    }

    /// Enter one level of nesting.
    fn enter(&mut self, span: Span) -> Result<(), Diagnostic> {
        self.depth += 1;
        if self.depth > MAX_DEPTH {
            return Err(Diagnostic::new(
                span,
                format!("nested more than {MAX_DEPTH} levels deep"),
            ));
        }
        Ok(())
    }

    fn leave(&mut self) {
        self.depth = self.depth.saturating_sub(1);
    }

    // ---- statements ------------------------------------------------------

    fn unit(mut self) -> Result<SourceUnit, Diagnostic> {
        let start = self.span().start;
        let mut stmts = Vec::new();
        while !self.at(&TokenKind::Eof) {
            stmts.push(self.stmt(Scope::File)?);
        }
        let end = self.span().end;
        Ok(SourceUnit {
            stmts,
            span: Span::new(start, end),
        })
    }

    /// A `{ … }` body of statements. `open` is the brace's span, so that an
    /// unclosed block can point at it.
    fn block(&mut self, open: Span) -> Result<Vec<Stmt>, Diagnostic> {
        self.enter(open)?;
        let mut stmts = Vec::new();
        loop {
            if self.eat(&TokenKind::RBrace) {
                self.leave();
                return Ok(stmts);
            }
            if self.at(&TokenKind::Eof) {
                return Err(self
                    .expected("`}`")
                    .with_note(open, "this `{` is never closed"));
            }
            stmts.push(self.stmt(Scope::Block)?);
        }
    }

    fn stmt(&mut self, scope: Scope) -> Result<Stmt, Diagnostic> {
        let Some(word) = self.ident_text() else {
            return Err(self.expected("a statement"));
        };
        let word = word.to_string();
        let start = self.span();

        match word.as_str() {
            "machine" if scope == Scope::File => self.machine_stmt(start),
            "machine" => Err(Diagnostic::new(
                start,
                "a `machine` block cannot be nested inside another block",
            )),
            "param" => self.param_stmt(start),
            "osc" => self.osc_stmt(start),
            "space" => self.space_stmt(start),
            "object" => self.object_stmt(start),
            "map" => self.map_stmt(start),
            "wire" => self.wire_stmt(start),
            "include" => self.include_stmt(start),
            "template" => self.template_stmt(start),
            "instance" => self.instance_stmt(start),
            "for" => self.for_stmt(start),
            other => Err(Diagnostic::new(
                start,
                format!(
                    "unknown statement `{other}`; expected one of {}",
                    keyword_list()
                ),
            )),
        }
    }

    fn machine_stmt(&mut self, start: Span) -> Result<Stmt, Diagnostic> {
        self.advance();
        let name = self.expect_string("a quoted machine name")?;
        let open = self.expect(&TokenKind::LBrace, "`{`")?.span;
        let body = self.block(open)?;
        Ok(Stmt::Machine(MachineDecl {
            name,
            body,
            span: start.join(self.prev_span()),
        }))
    }

    fn param_stmt(&mut self, start: Span) -> Result<Stmt, Diagnostic> {
        self.advance();
        let name = self.name()?;
        let default = if self.eat(&TokenKind::Eq) {
            Some(self.expr()?)
        } else {
            None
        };
        Ok(Stmt::Param(ParamDecl {
            name,
            default,
            span: start.join(self.prev_span()),
        }))
    }

    fn osc_stmt(&mut self, start: Span) -> Result<Stmt, Diagnostic> {
        self.advance();
        let name = self.name()?;
        self.expect(&TokenKind::Eq, "`=`")?;
        let freq = self.expr()?;
        let unit_tok = self.cur().clone();
        let unit = match self.ident_text().and_then(FreqUnit::from_spelling) {
            Some(unit) => {
                self.advance();
                Spanned::new(unit, unit_tok.span)
            }
            None => {
                return Err(self.expected("a frequency unit (`Hz`, `kHz`, `MHz` or `GHz`)"));
            }
        };
        Ok(Stmt::Osc(OscDecl {
            name,
            freq,
            unit,
            span: start.join(self.prev_span()),
        }))
    }

    fn space_stmt(&mut self, start: Span) -> Result<Stmt, Diagnostic> {
        self.advance();
        let name = self.name()?;
        let open = self.expect(&TokenKind::LBrace, "`{`")?.span;
        let props = self.props(open)?;
        Ok(Stmt::Space(SpaceDecl {
            name,
            props,
            span: start.join(self.prev_span()),
        }))
    }

    fn object_stmt(&mut self, start: Span) -> Result<Stmt, Diagnostic> {
        self.advance();
        let name = self.name()?;
        let class = self.expect_string("a quoted device class")?;
        // The block is optional: a device with no properties is legitimate.
        let props = if self.at(&TokenKind::LBrace) {
            let open = self.advance().span;
            self.props(open)?
        } else {
            Vec::new()
        };
        Ok(Stmt::Object(ObjectDecl {
            name,
            class,
            props,
            span: start.join(self.prev_span()),
        }))
    }

    fn map_stmt(&mut self, start: Span) -> Result<Stmt, Diagnostic> {
        self.advance();
        let space = self.name()?;
        let base = self.expr()?;
        self.expect_keyword("size")?;
        let size = self.expr()?;
        self.expect(&TokenKind::Eq, "`=`")?;
        let target = self.expr()?;
        // An optional trailing block carries per-mapping attributes such as
        // priority and endianness (§4.1). Nothing consumes them yet.
        let props = if self.at(&TokenKind::LBrace) {
            let open = self.advance().span;
            self.props(open)?
        } else {
            Vec::new()
        };
        Ok(Stmt::Map(MapStmt {
            space,
            base,
            size,
            target,
            props,
            span: start.join(self.prev_span()),
        }))
    }

    fn wire_stmt(&mut self, start: Span) -> Result<Stmt, Diagnostic> {
        self.advance();
        let from = self.path()?;
        self.expect(&TokenKind::Arrow, "`->`")?;
        let to = self.path()?;
        Ok(Stmt::Wire(WireStmt {
            from,
            to,
            span: start.join(self.prev_span()),
        }))
    }

    fn include_stmt(&mut self, start: Span) -> Result<Stmt, Diagnostic> {
        self.advance();
        let path = self.expect_string("a quoted path")?;
        Ok(Stmt::Include(IncludeStmt {
            path,
            span: start.join(self.prev_span()),
        }))
    }

    fn template_stmt(&mut self, start: Span) -> Result<Stmt, Diagnostic> {
        self.advance();
        let name = self.name()?;
        let mut params = Vec::new();
        if self.eat(&TokenKind::LParen) {
            loop {
                if self.eat(&TokenKind::RParen) {
                    break;
                }
                let pname = self.name()?;
                let default = if self.eat(&TokenKind::Eq) {
                    Some(self.expr()?)
                } else {
                    None
                };
                let span = pname.span.join(self.prev_span());
                params.push(TemplateParam {
                    name: pname,
                    default,
                    span,
                });
                if !self.eat(&TokenKind::Comma) && !self.at(&TokenKind::RParen) {
                    return Err(self.expected("`,` or `)`"));
                }
            }
        }
        let open = self.expect(&TokenKind::LBrace, "`{`")?.span;
        let body = self.block(open)?;
        Ok(Stmt::Template(TemplateDecl {
            name,
            params,
            body,
            span: start.join(self.prev_span()),
        }))
    }

    fn instance_stmt(&mut self, start: Span) -> Result<Stmt, Diagnostic> {
        self.advance();
        let name = self.name()?;
        self.expect(&TokenKind::Eq, "`=`")?;
        let template = self.name()?;
        let mut args = Vec::new();
        if self.eat(&TokenKind::LParen) {
            loop {
                if self.eat(&TokenKind::RParen) {
                    break;
                }
                let arg_start = self.span();
                // `id = 0` binds a parameter; a bare expression is positional.
                let arg_name = if self.ident_text().is_some()
                    && matches!(self.peek_kind(1), Some(TokenKind::Eq))
                {
                    let n = self.name()?;
                    self.advance(); // `=`
                    Some(n)
                } else {
                    None
                };
                let value = self.expr()?;
                args.push(Arg {
                    name: arg_name,
                    value,
                    span: arg_start.join(self.prev_span()),
                });
                if !self.eat(&TokenKind::Comma) && !self.at(&TokenKind::RParen) {
                    return Err(self.expected("`,` or `)`"));
                }
            }
        }
        Ok(Stmt::Instance(InstanceStmt {
            name,
            template,
            args,
            span: start.join(self.prev_span()),
        }))
    }

    fn for_stmt(&mut self, start: Span) -> Result<Stmt, Diagnostic> {
        self.advance();
        let var = self.name()?;
        self.expect_keyword("in")?;
        let from = self.expr()?;
        let inclusive = if self.eat(&TokenKind::DotDotEq) {
            true
        } else {
            self.expect(&TokenKind::DotDot, "`..` or `..=`")?;
            false
        };
        let to = self.expr()?;
        let open = self.expect(&TokenKind::LBrace, "`{`")?.span;
        let body = self.block(open)?;
        Ok(Stmt::For(ForStmt {
            var,
            start: from,
            end: to,
            inclusive,
            body,
            span: start.join(self.prev_span()),
        }))
    }

    /// A `name = value` block, the opening brace already consumed.
    fn props(&mut self, open: Span) -> Result<Vec<Property>, Diagnostic> {
        self.enter(open)?;
        let mut props = Vec::new();
        loop {
            // Commas are separators, and optional; extra ones are harmless.
            while self.eat(&TokenKind::Comma) {}
            if self.eat(&TokenKind::RBrace) {
                self.leave();
                return Ok(props);
            }
            if self.at(&TokenKind::Eof) {
                return Err(self
                    .expected("`}`")
                    .with_note(open, "this `{` is never closed"));
            }
            if self.ident_text().is_none() && !self.at(&TokenKind::Dollar) {
                return Err(self.expected("a property name or `}`"));
            }
            let name = self.name()?;
            self.expect(&TokenKind::Eq, "`=`")?;
            let value = self.expr()?;
            let span = name.span.join(value.span());
            props.push(Property { name, value, span });
        }
    }

    // ---- names, paths, expressions ---------------------------------------

    /// The span of the token just consumed, for closing a statement's span.
    fn prev_span(&self) -> Span {
        match self.pos.checked_sub(1).and_then(|i| self.tokens.get(i)) {
            Some(tok) => tok.span,
            None => self.span(),
        }
    }

    fn peek_kind(&self, ahead: usize) -> Option<&TokenKind> {
        self.tokens.get(self.pos + ahead).map(|t| &t.kind)
    }

    /// A name, possibly with `$` substitutions. Parts must be adjacent in the
    /// source: `cpu$i` is one name, `cpu $i` is two things.
    fn name(&mut self) -> Result<Name, Diagnostic> {
        let start = self.span();
        let mut parts = Vec::new();
        let mut end = start.start;
        loop {
            let tok_span = self.span();
            if !parts.is_empty() && tok_span.start != end {
                break;
            }
            match self.cur().kind.clone() {
                TokenKind::Ident(text) => {
                    self.advance();
                    parts.push(NamePart::Literal(text));
                    end = tok_span.end;
                }
                TokenKind::Dollar => {
                    let dollar = self.advance().span;
                    if self.span().start != dollar.end {
                        return Err(self.expected("a name or `{` directly after `$`"));
                    }
                    match self.cur().kind.clone() {
                        TokenKind::Ident(text) => {
                            let span = self.advance().span;
                            parts.push(NamePart::Substitution(Expr::Path(Path {
                                segments: alloc::vec![Name {
                                    parts: alloc::vec![NamePart::Literal(text)],
                                    span,
                                }],
                                span,
                            })));
                            end = span.end;
                        }
                        TokenKind::LBrace => {
                            self.advance();
                            let expr = self.expr()?;
                            let close = self.expect(&TokenKind::RBrace, "`}`")?.span;
                            parts.push(NamePart::Substitution(expr));
                            end = close.end;
                        }
                        _ => return Err(self.expected("a name or `{` directly after `$`")),
                    }
                }
                _ => break,
            }
        }
        if parts.is_empty() {
            return Err(self.expected("a name"));
        }
        Ok(Name {
            parts,
            span: Span::new(start.start, end),
        })
    }

    fn path(&mut self) -> Result<Path, Diagnostic> {
        let first = self.name()?;
        let start = first.span;
        let mut segments = alloc::vec![first];
        let mut end = start.end;
        while self.at(&TokenKind::Dot) {
            self.advance();
            let seg = self.name()?;
            end = seg.span.end;
            segments.push(seg);
        }
        Ok(Path {
            segments,
            span: Span::new(start.start, end),
        })
    }

    fn expr(&mut self) -> Result<Expr, Diagnostic> {
        self.additive()
    }

    fn additive(&mut self) -> Result<Expr, Diagnostic> {
        let mut lhs = self.multiplicative()?;
        loop {
            let op = if self.at(&TokenKind::Plus) {
                BinOp::Add
            } else if self.at(&TokenKind::Minus) {
                BinOp::Sub
            } else {
                return Ok(lhs);
            };
            self.advance();
            let rhs = self.multiplicative()?;
            let span = lhs.span().join(rhs.span());
            lhs = Expr::Binary {
                op,
                lhs: Box::new(lhs),
                rhs: Box::new(rhs),
                span,
            };
        }
    }

    fn multiplicative(&mut self) -> Result<Expr, Diagnostic> {
        let mut lhs = self.unary()?;
        loop {
            let op = if self.at(&TokenKind::Star) {
                BinOp::Mul
            } else if self.at(&TokenKind::Slash) {
                BinOp::Div
            } else if self.at(&TokenKind::Percent) {
                BinOp::Rem
            } else {
                return Ok(lhs);
            };
            self.advance();
            let rhs = self.unary()?;
            let span = lhs.span().join(rhs.span());
            lhs = Expr::Binary {
                op,
                lhs: Box::new(lhs),
                rhs: Box::new(rhs),
                span,
            };
        }
    }

    /// The one place every expression nesting level passes through, and so the
    /// place the depth guard lives.
    fn unary(&mut self) -> Result<Expr, Diagnostic> {
        self.enter(self.span())?;
        let out = self.unary_inner();
        self.leave();
        out
    }

    fn unary_inner(&mut self) -> Result<Expr, Diagnostic> {
        if self.at(&TokenKind::Minus) {
            let op_span = self.advance().span;
            let operand = self.unary()?;
            let span = op_span.join(operand.span());
            return Ok(Expr::Unary {
                op: UnOp::Neg,
                operand: Box::new(operand),
                span,
            });
        }
        self.primary()
    }

    fn primary(&mut self) -> Result<Expr, Diagnostic> {
        let span = self.span();
        match self.cur().kind.clone() {
            TokenKind::Num(lit) => {
                self.advance();
                Ok(Expr::Num(Spanned::new(lit, span)))
            }
            TokenKind::Str(text) => {
                self.advance();
                Ok(Expr::Str(Spanned::new(text, span)))
            }
            TokenKind::LParen => {
                self.advance();
                let inner = self.expr()?;
                self.expect(&TokenKind::RParen, "`)`")?;
                Ok(inner)
            }
            TokenKind::LBracket => {
                self.advance();
                let mut items = Vec::new();
                loop {
                    while self.eat(&TokenKind::Comma) {}
                    if self.at(&TokenKind::RBracket) {
                        break;
                    }
                    if self.at(&TokenKind::Eof) {
                        return Err(self
                            .expected("`]`")
                            .with_note(span, "this `[` is never closed"));
                    }
                    items.push(self.expr()?);
                }
                let close = self.expect(&TokenKind::RBracket, "`]`")?.span;
                Ok(Expr::List {
                    items,
                    span: span.join(close),
                })
            }
            TokenKind::LBrace => {
                self.advance();
                let entries = self.props(span)?;
                Ok(Expr::Map {
                    entries,
                    span: span.join(self.prev_span()),
                })
            }
            TokenKind::Ident(word) if word == "true" || word == "false" => {
                // `true`/`false` are values, not names — but only when they
                // stand alone, so an object may still be called `true.thing`.
                if matches!(self.peek_kind(1), Some(TokenKind::Dot)) {
                    return self.path_or_call();
                }
                self.advance();
                Ok(Expr::Bool(Spanned::new(word == "true", span)))
            }
            TokenKind::Ident(_) | TokenKind::Dollar => self.path_or_call(),
            _ => Err(self.expected("a value")),
        }
    }

    fn path_or_call(&mut self) -> Result<Expr, Diagnostic> {
        let path = self.path()?;
        let start = path.span;
        // A call's parenthesis must touch its callee. That is what keeps
        // `clock = master` followed on the next line by `(something)` from
        // silently becoming a call, without making newlines significant.
        if self.at(&TokenKind::LParen) && self.span().start == path.span.end {
            let open = self.advance().span;
            let mut args = Vec::new();
            loop {
                while self.eat(&TokenKind::Comma) {}
                if self.at(&TokenKind::RParen) {
                    break;
                }
                if self.at(&TokenKind::Eof) {
                    return Err(self
                        .expected("`)`")
                        .with_note(open, "this `(` is never closed"));
                }
                args.push(self.expr()?);
            }
            let close = self.expect(&TokenKind::RParen, "`)`")?.span;
            return Ok(Expr::Call {
                callee: path,
                args,
                span: start.join(close),
            });
        }
        Ok(Expr::Path(path))
    }
}

/// The statement keywords, formatted for a diagnostic.
fn keyword_list() -> String {
    let mut out = String::new();
    for (i, word) in STATEMENT_KEYWORDS.iter().enumerate() {
        if i > 0 {
            out.push_str(", ");
        }
        out.push('`');
        out.push_str(word);
        out.push('`');
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::machine::ast::NamePart;

    fn dump(text: &str) -> String {
        let src = SourceFile::new("t.machine", text);
        parse(&src).expect("should parse").dump()
    }

    fn err(text: &str) -> String {
        let src = SourceFile::new("t.machine", text);
        parse(&src).expect_err("should fail").message
    }

    #[test]
    fn statements_round_trip_through_the_dumper() {
        assert_eq!(
            dump("param region = \"ntsc\"\n"),
            "param region = \"ntsc\"\n"
        );
        assert_eq!(dump("param ram\n"), "param ram\n");
        assert_eq!(
            dump("osc master = 236250000/11 Hz\n"),
            "osc master = (236250000 / 11) Hz\n"
        );
        assert_eq!(
            dump("space cpubus { width = 16, unassigned = open-bus }\n"),
            "space cpubus { width = 16, unassigned = open-bus }\n"
        );
        assert_eq!(
            dump("object ram \"wram\" { size = 2K }\n"),
            "object ram \"wram\" { size = 2048 }\n"
        );
        assert_eq!(dump("object ram \"wram\"\n"), "object ram \"wram\" {}\n");
        assert_eq!(
            dump("map cpubus 0x0000 size 0x2000 = mirror(wram)\n"),
            "map cpubus 0 size 8192 = mirror(wram)\n"
        );
        assert_eq!(
            dump("wire ppu.nmi -> cpu.nmi\n"),
            "wire ppu.nmi -> cpu.nmi\n"
        );
        assert_eq!(
            dump("include \"pci-common.machine\"\n"),
            "include \"pci-common.machine\"\n"
        );
    }

    #[test]
    fn precedence_is_the_usual_one() {
        assert_eq!(dump("param x = 1 + 2 * 3\n"), "param x = (1 + (2 * 3))\n");
        assert_eq!(dump("param x = (1 + 2) * 3\n"), "param x = ((1 + 2) * 3)\n");
        assert_eq!(dump("param x = 8 / 2 / 2\n"), "param x = ((8 / 2) / 2)\n");
        assert_eq!(dump("param x = -3 + 1\n"), "param x = ((-3) + 1)\n");
        assert_eq!(dump("param x = 7 % 2\n"), "param x = (7 % 2)\n");
    }

    #[test]
    fn values_cover_the_property_types() {
        assert_eq!(dump("param x = true\n"), "param x = true\n");
        assert_eq!(dump("param x = false\n"), "param x = false\n");
        assert_eq!(dump("param x = [1, 2, 3]\n"), "param x = [1, 2, 3]\n");
        assert_eq!(dump("param x = []\n"), "param x = []\n");
        assert_eq!(
            dump("param x = { a = 1, b = \"two\" }\n"),
            "param x = { a = 1, b = \"two\" }\n"
        );
        assert_eq!(dump("param x = 10ms\n"), "param x = 10000000\n");
        assert_eq!(dump("param x = cpu.irq\n"), "param x = cpu.irq\n");
    }

    #[test]
    fn commas_between_properties_are_optional() {
        let with = dump("object cpu \"mos6502\" { clock = master / 12, space = cpubus }\n");
        let without =
            dump("object cpu \"mos6502\" {\n  clock = master / 12\n  space = cpubus\n}\n");
        assert_eq!(with, without);
        // A trailing comma is fine too.
        assert_eq!(dump("space s { width = 8, }\n"), "space s { width = 8 }\n");
    }

    #[test]
    fn a_bare_expression_never_swallows_the_next_statement() {
        // `master` must not absorb the `(` on the following line as a call.
        let text = "object cpu \"c\" { clock = master }\nparam x = 1\n";
        assert_eq!(
            dump(text),
            "object cpu \"c\" { clock = master }\nparam x = 1\n"
        );
    }

    #[test]
    fn templates_loops_and_instances_parse() {
        let text = "\
template cpu_complex(id, clock = master / 12) {
  object cpu$id \"mos6502\" { clock = clock }
  wire cpu$id.irq -> pic.in$id
}
for i in 0..4 {
  instance core$i = cpu_complex(id = i, clock = master / 12)
}
for j in 0..=3 { object bank${j + 1} \"ram\" }
";
        assert_eq!(
            dump(text),
            "\
template cpu_complex(id, clock = (master / 12)) {
  object cpu$id \"mos6502\" { clock = clock }
  wire cpu$id.irq -> pic.in$id
}
for i in 0..4 {
  instance core$i = cpu_complex(id = i, clock = (master / 12))
}
for j in 0..=3 {
  object bank${(j + 1)} \"ram\" {}
}
"
        );
    }

    #[test]
    fn a_template_may_take_no_parameters() {
        assert_eq!(
            dump("template t { object a \"b\" }\ninstance x = t\n"),
            "template t() {\n  object a \"b\" {}\n}\ninstance x = t()\n"
        );
    }

    #[test]
    fn positional_instance_arguments_are_allowed() {
        assert_eq!(dump("instance x = t(1, 2)\n"), "instance x = t(1, 2)\n");
    }

    #[test]
    fn substituted_names_keep_their_parts() {
        let src = SourceFile::new("t", "for i in 0..2 { object cpu$i \"c\" }\n");
        let unit = parse(&src).expect("should parse");
        let Stmt::For(f) = &unit.stmts[0] else {
            panic!("not a for");
        };
        let Stmt::Object(obj) = &f.body[0] else {
            panic!("not an object");
        };
        assert_eq!(obj.name.parts.len(), 2);
        assert!(matches!(&obj.name.parts[0], NamePart::Literal(t) if t == "cpu"));
        assert!(matches!(&obj.name.parts[1], NamePart::Substitution(_)));
        assert_eq!(obj.name.as_literal(), None);
        // The span covers `cpu$i`, not just `cpu`.
        assert_eq!(
            &src.text()[obj.name.span.start as usize..obj.name.span.end as usize],
            "cpu$i"
        );
    }

    #[test]
    fn spans_point_at_what_was_written() {
        let text = "machine \"nes\" {\n  object cpu \"mos6502\" { clock = master / 12 }\n}\n";
        let src = SourceFile::new("t", text);
        let unit = parse(&src).expect("should parse");
        let Stmt::Machine(m) = &unit.stmts[0] else {
            panic!("not a machine");
        };
        assert_eq!(src.location(m.span.start).line, 1);
        assert_eq!(src.location(m.span.end).line, 3);
        let Stmt::Object(obj) = &m.body[0] else {
            panic!("not an object");
        };
        assert_eq!(
            &text[obj.class.span.start as usize..obj.class.span.end as usize],
            "\"mos6502\""
        );
        let value = &obj.props[0].value;
        assert_eq!(
            &text[value.span().start as usize..value.span().end as usize],
            "master / 12"
        );
    }

    #[test]
    fn parse_errors_are_specific() {
        assert_eq!(
            err("machine \"nes\" {\n"),
            "expected `}`, found end of file"
        );
        assert!(
            err("frobnicate x\n")
                .starts_with("unknown statement `frobnicate`; expected one of `machine`")
        );
        assert_eq!(
            err("machine nes {}\n"),
            "expected a quoted machine name, found `nes`"
        );
        assert_eq!(
            err("osc master = 12\n"),
            "expected a frequency unit (`Hz`, `kHz`, `MHz` or `GHz`), found end of file"
        );
        assert_eq!(
            err("osc master = 12 Hertz\n"),
            "expected a frequency unit (`Hz`, `kHz`, `MHz` or `GHz`), found `Hertz`"
        );
        assert_eq!(
            err("map cpubus 0 0x20 = ram\n"),
            "expected `size`, found a number"
        );
        assert_eq!(err("wire a b\n"), "expected `->`, found `b`");
        assert_eq!(
            err("space s { 12 = 4 }\n"),
            "expected a property name or `}`, found a number"
        );
        assert_eq!(err("space s { width = }\n"), "expected a value, found `}`");
        assert_eq!(
            err("param x = 1 +\n"),
            "expected a value, found end of file"
        );
        assert_eq!(
            err("machine \"a\" { machine \"b\" {} }\n"),
            "a `machine` block cannot be nested inside another block"
        );
        assert_eq!(
            err("for i in 0 4 {}\n"),
            "expected `..` or `..=`, found a number"
        );
        assert_eq!(err("}\n"), "expected a statement, found `}`");
        assert_eq!(
            err("param x = $ i\n"),
            "expected a name or `{` directly after `$`, found `i`"
        );
    }

    #[test]
    fn nesting_is_capped_rather_than_overflowing() {
        let mut text = String::from("param x = ");
        for _ in 0..500 {
            text.push('(');
        }
        assert!(err(&text).starts_with("nested more than 64 levels deep"));

        let mut blocks = String::new();
        for _ in 0..500 {
            blocks.push_str("template t { ");
        }
        assert!(err(&blocks).starts_with("nested more than 64 levels deep"));

        // Unary chains recurse too.
        let mut minus = String::from("param x = ");
        for _ in 0..500 {
            minus.push('-');
        }
        minus.push('1');
        assert!(err(&minus).starts_with("nested more than 64 levels deep"));
    }

    #[test]
    fn depth_is_released_so_wide_files_are_fine() {
        // 200 sibling blocks, each shallow: the guard must not accumulate.
        let mut text = String::new();
        for _ in 0..200 {
            text.push_str("space s { a = (1 + (2 * 3)) }\n");
        }
        let src = SourceFile::new("t", &text);
        assert!(parse(&src).is_ok());
    }
}