lini 0.1.0

A small, human-readable language for plain-text diagrams that compiles to clean SVG
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
use crate::ast::*;
use crate::error::Error;
use crate::lexer::{TokKind, Token};
use crate::span::Span;

pub fn parse(tokens: &[Token]) -> Result<File, Error> {
    let mut p = Parser {
        toks: tokens,
        pos: 0,
    };
    p.skip_newlines();

    // Optional defs block: must be the very first non-trivia token.
    let defs = if matches!(p.peek_kind(), Some(TokKind::LBrace)) {
        Some(p.parse_defs_block()?)
    } else {
        None
    };
    p.skip_newlines();

    let mut stmts = Vec::new();
    while p.peek().is_some() {
        stmts.push(p.parse_stmt()?);
        p.skip_newlines();
    }
    Ok(File { defs, stmts })
}

/// Parse a single Lini value from a complete token stream. Used by the theme
/// loader to interpret `--lini-NAME: VALUE;` declarations as Lini values.
pub fn parse_value_only(tokens: &[Token]) -> Result<Value, Error> {
    let mut p = Parser {
        toks: tokens,
        pos: 0,
    };
    p.skip_newlines();
    let v = p.parse_value()?;
    p.skip_newlines();
    if p.peek().is_some() {
        return Err(Error::at(p.next_span(), "trailing tokens after value"));
    }
    Ok(v)
}

struct Parser<'a> {
    toks: &'a [Token],
    pos: usize,
}

impl<'a> Parser<'a> {
    // ───────────────────────── Cursor helpers ─────────────────────────

    fn peek(&self) -> Option<&Token> {
        self.toks.get(self.pos)
    }

    fn peek_kind(&self) -> Option<&TokKind> {
        self.peek().map(|t| &t.kind)
    }

    fn next_span(&self) -> Span {
        self.peek()
            .map(|t| t.span)
            .unwrap_or_else(|| self.last_span())
    }

    fn last_span(&self) -> Span {
        self.toks
            .get(self.pos.saturating_sub(1))
            .map(|t| t.span)
            .unwrap_or_default()
    }

    fn prev_end(&self) -> Option<usize> {
        if self.pos == 0 {
            None
        } else {
            self.toks.get(self.pos - 1).map(|t| t.span.end)
        }
    }

    /// True iff the current token is glued to the previous one (no whitespace).
    fn current_glued_to_prev(&self) -> bool {
        match (self.prev_end(), self.peek()) {
            (Some(prev_end), Some(tok)) => prev_end == tok.span.start,
            _ => false,
        }
    }

    fn skip_newlines(&mut self) {
        while matches!(self.peek_kind(), Some(TokKind::Newline)) {
            self.pos += 1;
        }
    }

    fn consume_terminator(&mut self) -> Result<(), Error> {
        match self.peek_kind() {
            Some(TokKind::Newline) | Some(TokKind::Semi) => {
                self.pos += 1;
                self.skip_newlines();
                Ok(())
            }
            Some(TokKind::RBrace) | None => Ok(()),
            Some(other) => Err(Error::at(
                self.next_span(),
                format!("expected newline, ';' or '}}', found {}", tok_desc(other)),
            )),
        }
    }

    fn expect_kind(&mut self, pred: impl Fn(&TokKind) -> bool, what: &str) -> Result<Span, Error> {
        match self.peek() {
            Some(t) if pred(&t.kind) => {
                let span = t.span;
                self.pos += 1;
                Ok(span)
            }
            Some(t) => Err(Error::at(
                t.span,
                format!("expected {}, found {}", what, tok_desc(&t.kind)),
            )),
            None => Err(Error::at(
                self.last_span(),
                format!("expected {}, found end of file", what),
            )),
        }
    }

    fn expect_lbrace(&mut self) -> Result<Span, Error> {
        self.expect_kind(|k| matches!(k, TokKind::LBrace), "'{'")
    }
    fn expect_rbrace(&mut self) -> Result<Span, Error> {
        self.expect_kind(|k| matches!(k, TokKind::RBrace), "'}'")
    }
    fn expect_lparen(&mut self) -> Result<Span, Error> {
        self.expect_kind(|k| matches!(k, TokKind::LParen), "'('")
    }
    fn expect_rparen(&mut self) -> Result<Span, Error> {
        self.expect_kind(|k| matches!(k, TokKind::RParen), "')'")
    }
    fn expect_lbracket(&mut self) -> Result<Span, Error> {
        self.expect_kind(|k| matches!(k, TokKind::LBracket), "'['")
    }
    fn expect_rbracket(&mut self) -> Result<Span, Error> {
        self.expect_kind(|k| matches!(k, TokKind::RBracket), "']'")
    }
    fn expect_pipe(&mut self) -> Result<Span, Error> {
        self.expect_kind(|k| matches!(k, TokKind::Pipe), "'|'")
    }
    fn expect_dot(&mut self) -> Result<Span, Error> {
        self.expect_kind(|k| matches!(k, TokKind::Dot), "'.'")
    }

    fn expect_ident(&mut self) -> Result<(String, Span), Error> {
        match self.peek() {
            Some(Token {
                kind: TokKind::Ident(name),
                span,
            }) => {
                let out = (name.clone(), *span);
                self.pos += 1;
                Ok(out)
            }
            Some(t) => Err(Error::at(
                t.span,
                format!("expected identifier, found {}", tok_desc(&t.kind)),
            )),
            None => Err(Error::at(
                self.last_span(),
                "expected identifier, found end of file",
            )),
        }
    }

    fn expect_string(&mut self) -> Result<String, Error> {
        match self.peek() {
            Some(Token {
                kind: TokKind::String(s),
                ..
            }) => {
                let out = s.clone();
                self.pos += 1;
                Ok(out)
            }
            Some(t) => Err(Error::at(
                t.span,
                format!("expected string, found {}", tok_desc(&t.kind)),
            )),
            None => Err(Error::at(
                self.last_span(),
                "expected string, found end of file",
            )),
        }
    }

    fn eat_string(&mut self) -> Option<String> {
        if let Some(Token {
            kind: TokKind::String(s),
            ..
        }) = self.peek()
        {
            let out = s.clone();
            self.pos += 1;
            Some(out)
        } else {
            None
        }
    }

    // ───────────────────────── Values ─────────────────────────

    fn parse_value(&mut self) -> Result<Value, Error> {
        let tok = match self.peek() {
            Some(t) => t,
            None => {
                return Err(Error::at(
                    self.last_span(),
                    "expected value, found end of file",
                ));
            }
        };
        match &tok.kind {
            TokKind::Number(n) => {
                let v = *n;
                self.pos += 1;
                Ok(Value::Number(v))
            }
            TokKind::String(s) => {
                let v = s.clone();
                self.pos += 1;
                Ok(Value::String(v))
            }
            TokKind::Hex(h) => {
                let v = h.clone();
                self.pos += 1;
                Ok(Value::Hex(v))
            }
            TokKind::Ident(_) => {
                let (name, name_span) = self.expect_ident()?;
                if matches!(self.peek_kind(), Some(TokKind::LParen)) {
                    Ok(Value::Call(self.parse_call(name, name_span)?))
                } else {
                    Ok(Value::Ident(name))
                }
            }
            TokKind::LParen => self.parse_tuple_value(),
            TokKind::LBracket => self.parse_list_value(),
            TokKind::RawCssVar(name) => {
                let v = name.clone();
                self.pos += 1;
                Ok(Value::RawCssVar(v))
            }
            other => Err(Error::at(
                tok.span,
                format!("expected value, found {}", tok_desc(other)),
            )),
        }
    }

    fn parse_call(&mut self, name: String, name_span: Span) -> Result<FnCall, Error> {
        self.expect_lparen()?;
        let mut args = Vec::new();
        if !matches!(self.peek_kind(), Some(TokKind::RParen)) {
            args.push(self.parse_value()?);
            while matches!(self.peek_kind(), Some(TokKind::Comma)) {
                self.pos += 1;
                args.push(self.parse_value()?);
            }
        }
        let end = self.expect_rparen()?;
        Ok(FnCall {
            name,
            args,
            span: Span::new(name_span.start, end.end),
        })
    }

    fn parse_tuple_value(&mut self) -> Result<Value, Error> {
        self.expect_lparen()?;
        let mut items = Vec::new();
        if !matches!(self.peek_kind(), Some(TokKind::RParen)) {
            items.push(self.parse_value()?);
            while matches!(self.peek_kind(), Some(TokKind::Comma)) {
                self.pos += 1;
                items.push(self.parse_value()?);
            }
        }
        self.expect_rparen()?;
        Ok(Value::Tuple(items))
    }

    fn parse_list_value(&mut self) -> Result<Value, Error> {
        self.expect_lbracket()?;
        let mut items = Vec::new();
        if !matches!(self.peek_kind(), Some(TokKind::RBracket)) {
            items.push(self.parse_value()?);
            while matches!(self.peek_kind(), Some(TokKind::Comma)) {
                self.pos += 1;
                items.push(self.parse_value()?);
            }
        }
        self.expect_rbracket()?;
        Ok(Value::List(items))
    }

    // ───────────────────────── Attr items ─────────────────────────

    fn parse_attr_items(&mut self) -> Result<Vec<AttrItem>, Error> {
        let mut items = Vec::new();
        loop {
            match self.peek_kind() {
                Some(TokKind::Dot) => {
                    // SPEC section 2: style refs require whitespace before `.`. A no-WS
                    // dot would mean an endpoint side, which is wire-only — stop
                    // and let the caller decide.
                    if self.current_glued_to_prev() {
                        break;
                    }
                    items.push(AttrItem::Style(self.parse_style_ref()?));
                }
                Some(TokKind::Ident(_)) => items.push(AttrItem::Attr(self.parse_attr()?)),
                _ => break,
            }
        }
        Ok(items)
    }

    fn parse_style_ref(&mut self) -> Result<StyleRef, Error> {
        let start = self.expect_dot()?;
        let (name, end) = self.expect_ident()?;
        Ok(StyleRef {
            name,
            span: Span::new(start.start, end.end),
        })
    }

    fn parse_attr(&mut self) -> Result<Attr, Error> {
        let (name, name_span) = self.expect_ident()?;
        // SPEC section 2: `name:value` — binding `:` has no whitespace on either side.
        let next_is_colon = matches!(self.peek_kind(), Some(TokKind::Colon));
        if !next_is_colon {
            return Err(Error::at(
                name_span,
                format!("attr '{}' requires a value: write '{}:VALUE'", name, name),
            ));
        }
        if !self.current_glued_to_prev() {
            return Err(Error::at(
                self.next_span(),
                "binding ':' must have no whitespace before it",
            ));
        }
        let colon_span = self.next_span();
        self.pos += 1;
        // Check no whitespace after the colon (value must be glued).
        if !self.current_glued_to_prev() {
            return Err(Error::at(
                colon_span,
                "binding ':' must have no whitespace after it",
            ));
        }
        let value = self.parse_value()?;
        let end = self.last_span();
        Ok(Attr {
            name,
            value,
            span: Span::new(name_span.start, end.end),
        })
    }

    // ───────────────────────── Type refs (`|name|`) ─────────────────────────

    fn parse_type_use(&mut self) -> Result<TypeRef, Error> {
        let start = self.expect_pipe()?;
        let (name, _) = self.expect_ident()?;
        // Disallow `name:base` inside a type-use ref (that's defs syntax).
        if matches!(self.peek_kind(), Some(TokKind::Colon)) {
            return Err(Error::at(
                self.next_span(),
                format!(
                    "type-use ref '|{}|' cannot carry ':base' (only valid in the defs block)",
                    name
                ),
            ));
        }
        let end = self.expect_pipe()?;
        Ok(TypeRef {
            name,
            span: Span::new(start.start, end.end),
        })
    }

    // ───────────────────────── Defs block ─────────────────────────

    fn parse_defs_block(&mut self) -> Result<DefsBlock, Error> {
        let start = self.expect_lbrace()?;
        let mut entries = Vec::new();
        self.skip_newlines();
        while !matches!(self.peek_kind(), Some(TokKind::RBrace) | None) {
            entries.push(self.parse_defs_entry()?);
            self.consume_terminator()?;
        }
        let end = self.expect_rbrace()?;
        Ok(DefsBlock {
            entries,
            span: Span::new(start.start, end.end),
        })
    }

    fn parse_defs_entry(&mut self) -> Result<DefsEntry, Error> {
        let start = self.next_span();
        match self.peek_kind() {
            // `--name:value`
            Some(TokKind::RawCssVar(_)) => {
                let name = match self.peek_kind() {
                    Some(TokKind::RawCssVar(n)) => n.clone(),
                    _ => unreachable!(),
                };
                self.pos += 1;
                if !matches!(self.peek_kind(), Some(TokKind::Colon)) {
                    return Err(Error::at(
                        self.next_span(),
                        format!("expected ':' after --{}", name),
                    ));
                }
                if !self.current_glued_to_prev() {
                    return Err(Error::at(
                        self.next_span(),
                        "binding ':' must have no whitespace before it",
                    ));
                }
                let colon_span = self.next_span();
                self.pos += 1;
                if !self.current_glued_to_prev() {
                    return Err(Error::at(
                        colon_span,
                        "binding ':' must have no whitespace after it",
                    ));
                }
                let value = self.parse_value()?;
                let end = self.last_span();
                Ok(DefsEntry::VarOverride(VarOverride {
                    name,
                    value,
                    span: Span::new(start.start, end.end),
                }))
            }
            // `.style attrs...`
            Some(TokKind::Dot) => {
                let _ = self.expect_dot()?;
                let (name, _) = self.expect_ident()?;
                let items = self.parse_attr_items()?;
                let end = self.last_span();
                Ok(DefsEntry::StyleDef(StyleDef {
                    name,
                    items,
                    span: Span::new(start.start, end.end),
                }))
            }
            // `|scene| ...`, `|name:base| ...`
            Some(TokKind::Pipe) => self.parse_pipe_defs_entry(start),
            other => Err(Error::at(
                self.next_span(),
                format!(
                    "expected defs entry (|scene|, |name:base|, .style, or --name:value), found {}",
                    other.map_or("end of file".to_string(), tok_desc)
                ),
            )),
        }
    }

    fn parse_pipe_defs_entry(&mut self, start: Span) -> Result<DefsEntry, Error> {
        // Peek inside the pipes to decide between scene config / shape def.
        // Expected: `|` Ident (`:` Ident)? `|` …
        // After parsing the first ident, if next is Pipe → scene config (or
        // shorthand). If next is Colon → shape def.
        self.expect_pipe()?;
        let (first, _first_span) = self.expect_ident()?;
        match self.peek_kind() {
            Some(TokKind::Pipe) => {
                // `|first|` — three roles, dispatched by name:
                //   `|scene|`  — root container config
                //   `|wire|`   — global wire defaults
                //   `|name|`   — type-defaults for any other recognised type
                // (Validation that `name` is a known type happens in resolve;
                // the parser accepts any identifier here.)
                let close = self.expect_pipe()?;
                let items = self.parse_attr_items()?;
                let end = if items.is_empty() {
                    close
                } else {
                    self.last_span()
                };
                let span = Span::new(start.start, end.end);
                Ok(match first.as_str() {
                    "scene" => DefsEntry::SceneConfig(SceneConfig { items, span }),
                    "wire" => DefsEntry::WireConfig(WireConfig { items, span }),
                    _ => DefsEntry::TypeDefaults(TypeDefaults {
                        name: first,
                        items,
                        span,
                    }),
                })
            }
            Some(TokKind::Colon) => {
                if !self.current_glued_to_prev() {
                    return Err(Error::at(
                        self.next_span(),
                        "binding ':' must have no whitespace before it",
                    ));
                }
                let colon_span = self.next_span();
                self.pos += 1;
                if !self.current_glued_to_prev() {
                    return Err(Error::at(
                        colon_span,
                        "binding ':' must have no whitespace after it",
                    ));
                }
                let (base_name, base_span) = self.expect_ident()?;
                let end_pipe = self.expect_pipe()?;
                let items = self.parse_attr_items()?;
                let body = self.parse_optional_body()?;
                let end = self.last_span();
                Ok(DefsEntry::ShapeDef(ShapeDef {
                    name: first,
                    base: TypeRef {
                        name: base_name,
                        span: base_span,
                    },
                    items,
                    body,
                    span: Span::new(start.start, end.end.max(end_pipe.end)),
                }))
            }
            other => Err(Error::at(
                self.next_span(),
                format!(
                    "expected '|' or ':' after '|{}', found {}",
                    first,
                    other.map_or("end of file".to_string(), tok_desc)
                ),
            )),
        }
    }

    // ───────────────────────── Statements (scene root + bodies) ─────────────────────────

    fn parse_stmt(&mut self) -> Result<Stmt, Error> {
        // Anonymous primitive: `|type| …`
        if matches!(self.peek_kind(), Some(TokKind::Pipe)) {
            let inst = self.parse_anonymous_inst()?;
            return Ok(Stmt::Node(inst));
        }
        // Otherwise must start with an ident.
        if !matches!(self.peek_kind(), Some(TokKind::Ident(_))) {
            return Err(Error::at(
                self.next_span(),
                format!(
                    "expected statement, found {}",
                    self.peek_kind().map_or("end of file".to_string(), tok_desc)
                ),
            ));
        }

        // We need lookahead to decide between a node decl and a wire decl.
        // The deciding token: WireOp/Amp/glued-Dot → wire; everything else → node.
        let save = self.pos;
        let (id, id_span) = self.expect_ident()?;

        let next = self.peek_kind();
        let is_glued_dot = matches!(next, Some(TokKind::Dot)) && self.current_glued_to_prev();

        if matches!(next, Some(TokKind::WireOp(_)) | Some(TokKind::Amp)) || is_glued_dot {
            // Wire: rewind and let parse_wire_decl re-parse the first endpoint.
            self.pos = save;
            let wire = self.parse_wire_decl()?;
            return Ok(Stmt::Wire(wire));
        }

        // Node decl continuing from this id.
        let inst = self.parse_node_inst_after_id(Some(id), id_span)?;
        Ok(Stmt::Node(inst))
    }

    fn parse_body(&mut self) -> Result<Vec<BodyItem>, Error> {
        self.expect_lbrace()?;
        let mut items = Vec::new();
        self.skip_newlines();
        while !matches!(self.peek_kind(), Some(TokKind::RBrace) | None) {
            items.push(self.parse_body_item()?);
            self.consume_terminator()?;
        }
        self.expect_rbrace()?;
        Ok(items)
    }

    fn parse_body_item(&mut self) -> Result<BodyItem, Error> {
        let stmt = self.parse_stmt()?;
        Ok(match stmt {
            Stmt::Node(n) => BodyItem::Inst(n),
            Stmt::Wire(w) => BodyItem::Wire(w),
        })
    }

    fn parse_optional_body(&mut self) -> Result<Option<Vec<BodyItem>>, Error> {
        if matches!(self.peek_kind(), Some(TokKind::LBrace)) {
            Ok(Some(self.parse_body()?))
        } else {
            Ok(None)
        }
    }

    // ─────────── Node decls ───────────

    /// Parse an anonymous primitive: `|type| [label [href]] (attr|style)* [{body}]`.
    fn parse_anonymous_inst(&mut self) -> Result<ShapeInst, Error> {
        let start = self.next_span();
        let ty = self.parse_type_use()?;
        let (label, href) = self.parse_label_and_href();
        let items = self.parse_attr_items()?;
        let body = self.parse_optional_body()?;
        let end = self.last_span();
        Ok(ShapeInst {
            id: None,
            ty,
            label,
            href,
            items,
            body,
            span: Span::new(start.start, end.end),
        })
    }

    /// Parse a node decl after the id has been consumed. The id may be `None`
    /// only when called from `parse_anonymous_inst` — here always Some.
    fn parse_node_inst_after_id(
        &mut self,
        id: Option<String>,
        id_span: Span,
    ) -> Result<ShapeInst, Error> {
        // Optional `|type|` next.
        let ty = if matches!(self.peek_kind(), Some(TokKind::Pipe)) {
            self.parse_type_use()?
        } else {
            // SPEC section 1 default: omitted type → |rect|.
            TypeRef {
                name: "rect".to_string(),
                span: id_span,
            }
        };
        let (label, href) = self.parse_label_and_href();
        let items = self.parse_attr_items()?;
        let body = self.parse_optional_body()?;
        let end = self.last_span();
        Ok(ShapeInst {
            id,
            ty,
            label,
            href,
            items,
            body,
            span: Span::new(id_span.start, end.end),
        })
    }

    fn parse_label_and_href(&mut self) -> (Option<String>, Option<String>) {
        let label = self.eat_string();
        let href = if label.is_some() {
            self.eat_string()
        } else {
            None
        };
        (label, href)
    }

    // ─────────── Wire decls ───────────

    fn parse_wire_decl(&mut self) -> Result<WireDecl, Error> {
        let start = self.next_span();
        let first = self.parse_endpoint_group()?;
        let op = self.expect_wire_op()?;
        let mut chain = vec![first];
        chain.push(self.parse_endpoint_group()?);

        while let Some(TokKind::WireOp(next_op)) = self.peek_kind() {
            let next_op = *next_op;
            if next_op == op {
                self.pos += 1;
                chain.push(self.parse_endpoint_group()?);
            } else {
                return Err(Error::at(
                    self.next_span(),
                    format!(
                        "wire chain mixes operators '{}' and '{}'",
                        wire_op_str(op),
                        wire_op_str(next_op)
                    ),
                ));
            }
        }

        let label = self.eat_string();
        let items = self.parse_attr_items()?;
        let body = if matches!(self.peek_kind(), Some(TokKind::LBrace)) {
            Some(self.parse_wire_text_body()?)
        } else {
            None
        };
        let end = self.last_span();
        Ok(WireDecl {
            chain,
            op,
            label,
            items,
            body,
            span: Span::new(start.start, end.end),
        })
    }

    fn parse_endpoint_group(&mut self) -> Result<EndpointGroup, Error> {
        let mut endpoints = vec![self.parse_endpoint()?];
        while matches!(self.peek_kind(), Some(TokKind::Amp)) {
            self.pos += 1;
            endpoints.push(self.parse_endpoint()?);
        }
        Ok(EndpointGroup { endpoints })
    }

    fn parse_endpoint(&mut self) -> Result<WireEndpoint, Error> {
        let (first, first_span) = self.expect_ident()?;
        let mut path = vec![first];
        let mut end_span = first_span;

        // Consume any number of `.ident` segments — but only if glued (no WS).
        while matches!(self.peek_kind(), Some(TokKind::Dot)) && self.current_glued_to_prev() {
            self.pos += 1; // dot
            // Next must be a glued ident.
            if !matches!(self.peek_kind(), Some(TokKind::Ident(_))) {
                return Err(Error::at(
                    self.next_span(),
                    "expected identifier after '.' in endpoint path",
                ));
            }
            if !self.current_glued_to_prev() {
                return Err(Error::at(
                    self.next_span(),
                    "endpoint '.' must have no whitespace after it",
                ));
            }
            let (seg, seg_span) = self.expect_ident()?;
            path.push(seg);
            end_span = seg_span;
        }

        // Per SPEC section 10: if the LAST segment matches a side name, peel it off.
        let side = if path.len() > 1 {
            if let Some(s) = Side::parse(path.last().unwrap()) {
                path.pop();
                Some(s)
            } else {
                None
            }
        } else {
            None
        };

        Ok(WireEndpoint {
            path,
            side,
            span: Span::new(first_span.start, end_span.end),
        })
    }

    fn expect_wire_op(&mut self) -> Result<WireOp, Error> {
        match self.peek_kind() {
            Some(TokKind::WireOp(op)) => {
                let op = *op;
                self.pos += 1;
                Ok(op)
            }
            _ => Err(Error::at(
                self.next_span(),
                format!(
                    "expected wire operator, found {}",
                    self.peek_kind().map_or("end of file".to_string(), tok_desc)
                ),
            )),
        }
    }

    fn parse_wire_text_body(&mut self) -> Result<Vec<TextDecl>, Error> {
        self.expect_lbrace()?;
        self.skip_newlines();
        let mut texts = Vec::new();
        while !matches!(self.peek_kind(), Some(TokKind::RBrace) | None) {
            let start = self.next_span();
            // Must be `|text| "string" attrs...`.
            self.expect_pipe()?;
            let (kw, kw_span) = self.expect_ident()?;
            if kw != "text" {
                return Err(Error::at(
                    kw_span,
                    "wire body may only contain |text| primitives",
                ));
            }
            self.expect_pipe()?;
            let text = self.expect_string()?;
            let items = self.parse_attr_items()?;
            let end = self.last_span();
            texts.push(TextDecl {
                text,
                items,
                span: Span::new(start.start, end.end),
            });
            self.consume_terminator()?;
        }
        self.expect_rbrace()?;
        Ok(texts)
    }
}

fn wire_op_str(op: WireOp) -> String {
    format!(
        "{}{}{}",
        op.start.start_str(),
        op.line.as_str(),
        op.end.end_str()
    )
}

fn tok_desc(k: &TokKind) -> String {
    match k {
        TokKind::Ident(s) => format!("identifier '{}'", s),
        TokKind::String(_) => "string".to_string(),
        TokKind::Number(_) => "number".to_string(),
        TokKind::Hex(_) => "hex color".to_string(),
        TokKind::RawCssVar(s) => format!("'--{}'", s),
        TokKind::Pipe => "'|'".to_string(),
        TokKind::Colon => "':'".to_string(),
        TokKind::Dot => "'.'".to_string(),
        TokKind::Amp => "'&'".to_string(),
        TokKind::Semi => "';'".to_string(),
        TokKind::Comma => "','".to_string(),
        TokKind::LBrace => "'{'".to_string(),
        TokKind::RBrace => "'}'".to_string(),
        TokKind::LParen => "'('".to_string(),
        TokKind::RParen => "')'".to_string(),
        TokKind::LBracket => "'['".to_string(),
        TokKind::RBracket => "']'".to_string(),
        TokKind::WireOp(op) => format!("'{}'", wire_op_str(*op)),
        TokKind::Newline => "newline".to_string(),
    }
}