macroforge_ts_syn 0.4.0

TypeScript syntax types for build-time macro code generation
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
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
//! Hand-written recursive-descent parser for declarative macro template bodies.
//!
//! The template body is its own mini-language, not TypeScript. It consists of:
//!
//! - Multiple **arms** separated by blank lines (or adjacent lines each starting
//!   with `(` at the outermost level).
//! - Each arm is `pattern "=>" body`.
//! - A pattern is `"(" elements? ")"`.
//! - An element is a literal, a fragment (`$ident:Kind`), or a repetition
//!   (`$( inner )<sep?><repkind>`).
//! - A body is a sequence of literal chunks, `$ident` substitutions, and
//!   `$( inner )<sep?><repkind>` repetitions.
//!
//! Spans attached to parsed nodes are **relative to the original source file**
//! — the caller passes the template body's origin span so we can map byte
//! offsets correctly.

use crate::abi::SpanIR;

use super::errors::DeclarativeError;
use super::types::{
    Body, BodyToken, FragmentKind, MacroArm, MacroDef, MacroMode, Pattern, PatternElement,
    RepetitionKind,
};

/// Metavariable name reserved by the cluster-aware runtime-name
/// template feature (Phase E of the production-hardening plan).
///
/// When the body parser sees `$__cluster__` it always emits a
/// [`BodyToken::Substitution`], even if the identifier is followed by
/// `(` — normally that combination is a macro-call token, but
/// `__cluster__` is special so users can write the natural form
/// `__helper_$__cluster__($args)` in a `call_arms` body without it
/// being mis-parsed as a call to a macro named `__cluster__`. The
/// double-underscore suffix matches the conventional "system reserved"
/// naming convention and is vanishingly unlikely to collide with a
/// user-defined metavariable name or a user-declared macro called
/// `$cluster` (which IS now allowed — the earlier total reservation
/// of the short `cluster` name was dropped in PR 12 of the
/// production-hardening plan).
const RESERVED_CLUSTER_NAME: &str = "__cluster__";

/// Parse a macro template body into a [`MacroDef`].
///
/// `template_body` is the static text between the `` macro` `` and the
/// closing backtick (without the backticks themselves). `template_span`
/// is that text's span in the original source file — byte offsets inside
/// `template_body` are remapped by adding `template_span.start`.
///
/// The returned [`MacroDef`] has `name` left empty; the host sets it during
/// discovery since the parser can't see the surrounding const binding.
pub fn parse_macro_def(
    template_body: &str,
    template_span: SpanIR,
) -> Result<MacroDef, DeclarativeError> {
    let arms = split_arms(template_body, template_span)?;
    let mut parsed_arms = Vec::with_capacity(arms.len());
    for arm in arms {
        parsed_arms.push(parse_arm(arm.text, arm.span)?);
    }

    if parsed_arms.is_empty() {
        return Err(DeclarativeError::new(
            template_span,
            "macro definition has no arms",
        ));
    }

    Ok(MacroDef::from_arms(
        String::new(),
        parsed_arms,
        MacroMode::ExpandOnly,
        template_span,
    ))
}

// ---------------------------------------------------------------------------
// Arm splitting
// ---------------------------------------------------------------------------

struct RawArm<'a> {
    text: &'a str,
    span: SpanIR,
}

/// Split a template body into raw arm slices.
///
/// An arm starts at the first non-whitespace character of a line whose first
/// non-whitespace character is `(`, provided we're at depth 0 (no enclosing
/// braces or parens from a previous arm's body). The first arm additionally
/// starts at the first `(` in the template body regardless of leading newlines.
fn split_arms<'a>(
    template_body: &'a str,
    template_span: SpanIR,
) -> Result<Vec<RawArm<'a>>, DeclarativeError> {
    let bytes = template_body.as_bytes();
    let len = bytes.len();
    let mut arms = Vec::new();

    // Find arm start positions: byte offsets inside template_body at which a
    // new arm's `(` lives.
    let mut starts: Vec<usize> = Vec::new();

    // Depth counters, tracking nesting inside arm bodies so that a `(` inside
    // a body doesn't get mistaken for the start of a new arm.
    let mut paren: i32 = 0;
    let mut brace: i32 = 0;
    let mut bracket: i32 = 0;

    // `at_line_start` is true when the next non-whitespace character is the
    // first one of a logical line.
    let mut at_line_start = true;
    let mut i = 0;
    while i < len {
        let c = bytes[i];
        match c {
            b'\n' => {
                at_line_start = true;
                i += 1;
                continue;
            }
            b' ' | b'\t' | b'\r' => {
                i += 1;
                continue;
            }
            b'(' => {
                if at_line_start && paren == 0 && brace == 0 && bracket == 0 {
                    starts.push(i);
                }
                paren += 1;
                at_line_start = false;
            }
            b')' => {
                paren -= 1;
                at_line_start = false;
            }
            b'{' => {
                brace += 1;
                at_line_start = false;
            }
            b'}' => {
                brace -= 1;
                at_line_start = false;
            }
            b'[' => {
                bracket += 1;
                at_line_start = false;
            }
            b']' => {
                bracket -= 1;
                at_line_start = false;
            }
            _ => {
                at_line_start = false;
            }
        }
        i += 1;
    }

    if paren != 0 || brace != 0 || bracket != 0 {
        return Err(DeclarativeError::new(
            template_span,
            "unbalanced delimiters in macro template",
        ));
    }

    if starts.is_empty() {
        return Err(DeclarativeError::new(
            template_span,
            "macro template contains no arms (expected at least one `(...)=> ...`)",
        ));
    }

    // Build slices from each start to the next start (or end of template).
    for (idx, &start) in starts.iter().enumerate() {
        let end = starts.get(idx + 1).copied().unwrap_or(len);
        let slice = &template_body[start..end];
        let trimmed = slice.trim_end();
        let trimmed_end = start + trimmed.len();
        arms.push(RawArm {
            text: trimmed,
            span: SpanIR::new(
                template_span.start + start as u32,
                template_span.start + trimmed_end as u32,
            ),
        });
    }

    Ok(arms)
}

// ---------------------------------------------------------------------------
// Arm parsing
// ---------------------------------------------------------------------------

fn parse_arm(text: &str, span: SpanIR) -> Result<MacroArm, DeclarativeError> {
    // The arm must start with `(`.
    let bytes = text.as_bytes();
    if bytes.is_empty() || bytes[0] != b'(' {
        return Err(DeclarativeError::new(span, "arm does not start with `(`"));
    }

    // Find the matching `)` that closes the top-level pattern parentheses.
    let pattern_end = match_balanced(bytes, 0)?;
    let pattern_src = &text[1..pattern_end];
    let pattern_span = SpanIR::new(span.start + 1, span.start + pattern_end as u32);

    // After the `)`, expect `=>`.
    let mut i = pattern_end + 1;
    i = skip_ws(bytes, i);
    if i + 1 >= bytes.len() || bytes[i] != b'=' || bytes[i + 1] != b'>' {
        return Err(DeclarativeError::new(
            SpanIR::new(span.start + i as u32, span.end),
            "expected `=>` after pattern",
        ));
    }
    i += 2;
    i = skip_ws(bytes, i);

    let body_src = &text[i..];
    let body_span = SpanIR::new(span.start + i as u32, span.end);

    let pattern = parse_pattern(pattern_src, pattern_span)?;
    let body = parse_body(body_src, body_span)?;

    Ok(MacroArm {
        pattern,
        body,
        span,
    })
}

/// Find the byte offset of the `)` that closes the `(` at `open_idx`.
fn match_balanced(bytes: &[u8], open_idx: usize) -> Result<usize, DeclarativeError> {
    debug_assert_eq!(bytes[open_idx], b'(');
    let mut depth = 0i32;
    let mut i = open_idx;
    while i < bytes.len() {
        match bytes[i] {
            b'(' => depth += 1,
            b')' => {
                depth -= 1;
                if depth == 0 {
                    return Ok(i);
                }
            }
            _ => {}
        }
        i += 1;
    }
    Err(DeclarativeError::new(
        SpanIR::new(open_idx as u32, bytes.len() as u32),
        "unbalanced `(` in pattern",
    ))
}

fn skip_ws(bytes: &[u8], mut i: usize) -> usize {
    while i < bytes.len() && matches!(bytes[i], b' ' | b'\t' | b'\r' | b'\n') {
        i += 1;
    }
    i
}

// ---------------------------------------------------------------------------
// Pattern parsing
// ---------------------------------------------------------------------------

fn parse_pattern(src: &str, span: SpanIR) -> Result<Pattern, DeclarativeError> {
    let trimmed = src.trim();
    if trimmed.is_empty() {
        return Ok(Pattern::Empty);
    }
    let mut parser = PatternParser::new(src, span);
    let elements = parser.parse_elements(None)?;
    Ok(Pattern::Sequence(elements))
}

struct PatternParser<'a> {
    src: &'a [u8],
    // Absolute span base: adding this to a byte index inside `src` yields the
    // span position in the original source file.
    base: u32,
    pos: usize,
}

impl<'a> PatternParser<'a> {
    fn new(src: &'a str, span: SpanIR) -> Self {
        Self {
            src: src.as_bytes(),
            base: span.start,
            pos: 0,
        }
    }

    fn span_at(&self, start: usize, end: usize) -> SpanIR {
        SpanIR::new(self.base + start as u32, self.base + end as u32)
    }

    fn peek(&self) -> Option<u8> {
        self.src.get(self.pos).copied()
    }

    fn skip_ws(&mut self) {
        while let Some(c) = self.peek() {
            if matches!(c, b' ' | b'\t' | b'\r' | b'\n') {
                self.pos += 1;
            } else {
                break;
            }
        }
    }

    /// Parse a sequence of elements until `stop` (if any) or end of input.
    fn parse_elements(
        &mut self,
        stop: Option<u8>,
    ) -> Result<Vec<PatternElement>, DeclarativeError> {
        let mut elements = Vec::new();
        loop {
            self.skip_ws();
            match self.peek() {
                None => break,
                Some(c) if Some(c) == stop => break,
                Some(b'$') => {
                    // Fragment or repetition.
                    self.pos += 1; // consume `$`
                    match self.peek() {
                        Some(b'(') => {
                            self.pos += 1; // consume `(`
                            let inner = self.parse_elements(Some(b')'))?;
                            match self.peek() {
                                Some(b')') => self.pos += 1,
                                _ => {
                                    return Err(DeclarativeError::new(
                                        self.span_at(self.pos, self.pos),
                                        "expected `)` closing repetition pattern",
                                    ));
                                }
                            }
                            // After `)`, an optional separator, then a repkind.
                            let (separator, kind) = self.parse_rep_suffix()?;
                            elements.push(PatternElement::Repetition {
                                pattern: Box::new(Pattern::Sequence(inner)),
                                separator,
                                kind,
                            });
                        }
                        Some(c) if is_ident_start(c) => {
                            let name = self.consume_ident();
                            self.skip_ws();
                            if self.peek() != Some(b':') {
                                return Err(DeclarativeError::new(
                                    self.span_at(self.pos, self.pos),
                                    format!("expected `:<Kind>` after fragment name `${}`", name),
                                ));
                            }
                            self.pos += 1; // consume `:`
                            self.skip_ws();
                            let kind_start = self.pos;
                            let kind_name = self.consume_ident();
                            if kind_name.is_empty() {
                                return Err(DeclarativeError::new(
                                    self.span_at(kind_start, kind_start),
                                    format!(
                                        "expected fragment kind after `${}:` (known: {})",
                                        name,
                                        FragmentKind::known_names(),
                                    ),
                                ));
                            }
                            let kind = FragmentKind::from_name(&kind_name).ok_or_else(|| {
                                DeclarativeError::new(
                                    self.span_at(kind_start, self.pos),
                                    format!(
                                        "unknown fragment kind `{}` (known: {})",
                                        kind_name,
                                        FragmentKind::known_names(),
                                    ),
                                )
                            })?;
                            elements.push(PatternElement::Fragment { name, kind });
                        }
                        Some(other) => {
                            return Err(DeclarativeError::new(
                                self.span_at(self.pos, self.pos + 1),
                                format!(
                                    "unexpected character `{}` after `$` (expected identifier or `(`)",
                                    other as char
                                ),
                            ));
                        }
                        None => {
                            return Err(DeclarativeError::new(
                                self.span_at(self.pos, self.pos),
                                "unexpected end of pattern after `$`",
                            ));
                        }
                    }
                }
                Some(b',') | Some(b';') => {
                    // Separator — emit as literal element so matchers can
                    // consume it between positional fragments.
                    let c = self.src[self.pos];
                    self.pos += 1;
                    elements.push(PatternElement::Literal((c as char).to_string()));
                }
                Some(_) => {
                    // A literal run (e.g., a bare comma or semicolon handled above;
                    // anything else we treat as an error for MVP).
                    let start = self.pos;
                    let c = self.src[self.pos];
                    self.pos += 1;
                    return Err(DeclarativeError::new(
                        self.span_at(start, self.pos),
                        format!(
                            "unexpected character `{}` in pattern (expected `$name:Kind`, `$( ... )`, `,` or `;`)",
                            c as char
                        ),
                    ));
                }
            }
        }
        Ok(elements)
    }

    /// Parse the suffix after `$(...)`: an optional separator followed by a
    /// repetition kind marker (`*`, `+`, `?`). Common forms:
    ///
    /// - `$(...),+`  — comma-separated, one-or-more
    /// - `$(...),*`  — comma-separated, zero-or-more
    /// - `$(...);+`  — semicolon-separated, one-or-more
    /// - `$(...)?`   — zero-or-one (no separator)
    /// - `$(...)+`   — no separator, one-or-more
    /// - `$(...)*`   — no separator, zero-or-more
    fn parse_rep_suffix(&mut self) -> Result<(Option<String>, RepetitionKind), DeclarativeError> {
        self.skip_ws();
        let separator = match self.peek() {
            Some(c @ (b',' | b';')) => {
                self.pos += 1;
                Some((c as char).to_string())
            }
            _ => None,
        };
        self.skip_ws();
        let kind = match self.peek() {
            Some(b'*') => {
                self.pos += 1;
                RepetitionKind::ZeroOrMore
            }
            Some(b'+') => {
                self.pos += 1;
                RepetitionKind::OneOrMore
            }
            Some(b'?') => {
                self.pos += 1;
                RepetitionKind::ZeroOrOne
            }
            _ => {
                return Err(DeclarativeError::new(
                    self.span_at(self.pos, self.pos),
                    "expected `*`, `+`, or `?` after repetition",
                ));
            }
        };
        Ok((separator, kind))
    }

    fn consume_ident(&mut self) -> String {
        let start = self.pos;
        if let Some(c) = self.peek() {
            if !is_ident_start(c) {
                return String::new();
            }
        } else {
            return String::new();
        }
        while let Some(c) = self.peek() {
            if is_ident_continue(c) {
                self.pos += 1;
            } else {
                break;
            }
        }
        std::str::from_utf8(&self.src[start..self.pos])
            .expect("ident is ASCII")
            .to_string()
    }
}

fn is_ident_start(c: u8) -> bool {
    c.is_ascii_alphabetic() || c == b'_'
}

fn is_ident_continue(c: u8) -> bool {
    is_ident_start(c) || c.is_ascii_digit()
}

// ---------------------------------------------------------------------------
// Body parsing
// ---------------------------------------------------------------------------

fn parse_body(src: &str, span: SpanIR) -> Result<Body, DeclarativeError> {
    let mut parser = BodyParser::new(src, span);
    let tokens = parser.parse_until(None)?;
    Ok(Body(tokens))
}

/// Which kind of string literal, if any, the parser is currently inside.
///
/// The body parser is lexically shallow — it only understands three things
/// it needs to avoid eating: single-quoted strings, double-quoted strings,
/// and template literals. Inside `Single`, `Double`, or the text portion
/// of `Template`, a bare `$` is always literal text, never the start of a
/// substitution or macro escape. Template-expression slots (`${...}`) are
/// a return-trip: we push the enclosing `Template` onto the stack and
/// parse the expression in `Code` mode, so `${$x}` still sees `$x` as a
/// substitution.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum StringCtx {
    /// Normal macro-body code. `$ident` is a substitution/call, `$$` is
    /// an escape.
    Code,
    /// Inside `'...'`. `$` is literal.
    Single,
    /// Inside `"..."`. `$` is literal.
    Double,
    /// Inside `` `...` `` template literal, in the string-text portion
    /// (not currently inside a `${}` expression slot).
    Template,
}

/// Incremental string-context tracker. The three body-parsing loops
/// (`parse_until`, `parse_call_args`, `parse_repetition_inner`) each
/// instantiate one of these and call [`StringState::advance`] on every
/// byte as the cursor walks forward. Between advances, callers consult
/// [`StringState::in_string`] to decide whether to treat `$` as special
/// at the current position.
///
/// The state machine handles:
///
/// - `\\` escapes inside single/double/template strings (next byte is
///   literal regardless of what it is).
/// - Entry to and exit from each string kind.
/// - Template-expression slots: when a `$` immediately followed by `{`
///   appears in `Template` context, the state pushes `Template` onto
///   `stack`, switches to `Code`, and resets `brace_depth`. The first
///   `{` seen in the new `Code` region is special-cased (it's the
///   opener of the `${...}` slot, not a nested block), so a
///   `pending_open_brace` flag absorbs it. Subsequent `{` / `}` inside
///   the slot increment and decrement `brace_depth` normally; when
///   `brace_depth` is 0 and a `}` arrives, the stack is popped and we
///   return to `Template`.
#[derive(Debug, Clone)]
struct StringState {
    ctx: StringCtx,
    /// Next byte is an escape target (already saw `\` inside a string).
    escape_next: bool,
    /// Stack of saved contexts — one entry per nested template-expression
    /// slot. Each entry is the context to return to (always `Template`
    /// in practice, since only template literals have expression slots).
    stack: Vec<StringCtx>,
    /// `{`/`}` nesting depth within the current template-expression slot.
    /// Only meaningful when `stack` is non-empty (i.e., we're inside one).
    brace_depth: u32,
    /// The next `{` seen in `Code` context is the opener of a
    /// `${...}` slot — absorb it without touching `brace_depth`.
    pending_open_brace: bool,
}

impl StringState {
    fn new() -> Self {
        Self {
            ctx: StringCtx::Code,
            escape_next: false,
            stack: Vec::new(),
            brace_depth: 0,
            pending_open_brace: false,
        }
    }

    /// Returns `true` iff the position **before** the next `advance` call
    /// is inside a string literal text region where `$` must be treated
    /// as a literal character. Template-expression slots return `false`
    /// because they're code, not string text.
    fn in_string(&self) -> bool {
        matches!(
            self.ctx,
            StringCtx::Single | StringCtx::Double | StringCtx::Template
        )
    }

    /// Walk the state forward by one byte. `b` is the byte at the
    /// current cursor position; `next` is the byte immediately after
    /// (used to detect `${` in template context — the `$` needs to know
    /// whether a `{` follows before it decides to open an expression
    /// slot).
    fn advance(&mut self, b: u8, next: Option<u8>) {
        if self.escape_next {
            self.escape_next = false;
            return;
        }
        match self.ctx {
            StringCtx::Code => match b {
                b'\'' => self.ctx = StringCtx::Single,
                b'"' => self.ctx = StringCtx::Double,
                b'`' => self.ctx = StringCtx::Template,
                b'{' if !self.stack.is_empty() => {
                    if self.pending_open_brace {
                        self.pending_open_brace = false;
                    } else {
                        self.brace_depth += 1;
                    }
                }
                b'}' if !self.stack.is_empty() => {
                    if self.brace_depth == 0 {
                        // Closing `}` of the surrounding `${...}` slot —
                        // pop back to the enclosing template literal.
                        self.ctx = self.stack.pop().unwrap_or(StringCtx::Code);
                    } else {
                        self.brace_depth -= 1;
                    }
                }
                _ => {}
            },
            StringCtx::Single => match b {
                b'\\' => self.escape_next = true,
                b'\'' => self.ctx = StringCtx::Code,
                _ => {}
            },
            StringCtx::Double => match b {
                b'\\' => self.escape_next = true,
                b'"' => self.ctx = StringCtx::Code,
                _ => {}
            },
            StringCtx::Template => match b {
                b'\\' => self.escape_next = true,
                b'`' => self.ctx = StringCtx::Code,
                b'$' if next == Some(b'{') => {
                    // Enter an expression slot. The next byte will be
                    // the `{`, which `pending_open_brace` causes us to
                    // absorb without a depth bump.
                    self.stack.push(StringCtx::Template);
                    self.ctx = StringCtx::Code;
                    self.brace_depth = 0;
                    self.pending_open_brace = true;
                }
                _ => {}
            },
        }
    }
}

struct BodyParser<'a> {
    src: &'a [u8],
    base: u32,
    pos: usize,
}

impl<'a> BodyParser<'a> {
    fn new(src: &'a str, span: SpanIR) -> Self {
        Self {
            src: src.as_bytes(),
            base: span.start,
            pos: 0,
        }
    }

    fn span_at(&self, start: usize, end: usize) -> SpanIR {
        SpanIR::new(self.base + start as u32, self.base + end as u32)
    }

    fn peek(&self) -> Option<u8> {
        self.src.get(self.pos).copied()
    }

    fn peek_n(&self, n: usize) -> Option<u8> {
        self.src.get(self.pos + n).copied()
    }

    /// Parse body tokens until we hit the byte `stop` (if any) at depth 0
    /// relative to the repetition nesting. Literal parens/braces inside the
    /// body don't cause early termination — only the matching `)` of a
    /// repetition does, and we rely on a separate depth counter for that.
    fn parse_until(&mut self, _stop: Option<u8>) -> Result<Vec<BodyToken>, DeclarativeError> {
        let mut tokens: Vec<BodyToken> = Vec::new();
        let mut literal_start = self.pos;
        let mut str_state = StringState::new();

        while self.pos < self.src.len() {
            let c = self.src[self.pos];
            // Inside a string literal, `$` is always literal text — skip
            // special handling and let the cursor advance naturally.
            if !str_state.in_string() && c == b'$' {
                // Could be `$ident` substitution, `$(...)rep` repetition, or `$$` escape.
                match self.peek_n(1) {
                    Some(b'$') => {
                        // `$$` escape — emit literal `$`, skip both.
                        // Flush literal up to here, then push `$` as its own literal.
                        self.flush_literal(&mut tokens, literal_start, self.pos);
                        tokens.push(BodyToken::Literal("$".to_string()));
                        // Tick the state machine past both consumed bytes
                        // so the context reflects two forward steps.
                        str_state.advance(c, self.peek_n(1));
                        str_state.advance(b'$', self.peek_n(2));
                        self.pos += 2;
                        literal_start = self.pos;
                        continue;
                    }
                    Some(b'(') => {
                        self.flush_literal(&mut tokens, literal_start, self.pos);
                        self.pos += 2; // consume `$(`
                        let inner = self.parse_repetition_inner()?;
                        match self.peek() {
                            Some(b')') => self.pos += 1,
                            _ => {
                                return Err(DeclarativeError::new(
                                    self.span_at(self.pos, self.pos),
                                    "expected `)` closing repetition in body",
                                ));
                            }
                        }
                        let (separator, kind) = self.parse_rep_suffix()?;
                        tokens.push(BodyToken::Repetition {
                            body: inner,
                            separator,
                            kind,
                        });
                        // The repetition sub-parser maintains its own
                        // string state; we treat the whole `$(...)+`
                        // as one opaque step and reset ours to Code.
                        str_state = StringState::new();
                        literal_start = self.pos;
                        continue;
                    }
                    Some(c) if is_ident_start(c) => {
                        self.flush_literal(&mut tokens, literal_start, self.pos);
                        self.pos += 1; // consume `$`
                        let name = self.consume_ident();
                        // Phase 12: if the next non-whitespace char on the
                        // SAME LINE is `(`, this is a macro call, not a
                        // substitution. Newlines break the association so
                        // `$foo\n(stmt)` stays a substitution followed by a
                        // parenthesized expression — the same rule Rust's
                        // `macro_rules!` uses.
                        //
                        // Phase E exception: `$__cluster__` is a reserved
                        // substitution name used by the cluster-aware
                        // runtime-name template feature. It must always
                        // parse as a substitution so `__h_$__cluster__($y)`
                        // renders correctly as `__h_<id>($y)` instead of
                        // being misread as a call to a macro named
                        // `__cluster__`.
                        //
                        // Type-position composition: `$ident<args>` with
                        // **strict adjacency** (no whitespace between
                        // ident and `<`) is parsed as a macro call too,
                        // so type macros can naturally compose:
                        // `$Result<T, E> => $Box<T> | E`. The strict-
                        // adjacency rule prevents value-position bodies
                        // from misreading `$x < $y` (with whitespace) as
                        // a call.
                        let is_value_call = name != RESERVED_CLUSTER_NAME
                            && self.peek_after_horizontal_ws() == Some(b'(');
                        let is_type_call = name != RESERVED_CLUSTER_NAME
                            && !is_value_call
                            && self.peek() == Some(b'<')
                            && self.angle_args_balance_ok();
                        if is_value_call {
                            self.skip_horizontal_ws();
                            self.pos += 1; // consume `(`
                            let args = self.parse_call_args()?;
                            match self.peek() {
                                Some(b')') => self.pos += 1,
                                _ => {
                                    return Err(DeclarativeError::new(
                                        self.span_at(self.pos, self.pos),
                                        "expected `)` closing macro call in body",
                                    ));
                                }
                            }
                            tokens.push(BodyToken::MacroCall { name, args });
                        } else if is_type_call {
                            self.pos += 1; // consume `<`
                            let args = self.parse_type_call_args()?;
                            match self.peek() {
                                Some(b'>') => self.pos += 1,
                                _ => {
                                    return Err(DeclarativeError::new(
                                        self.span_at(self.pos, self.pos),
                                        "expected `>` closing type-position macro call in body",
                                    ));
                                }
                            }
                            tokens.push(BodyToken::MacroCall { name, args });
                        } else {
                            tokens.push(BodyToken::Substitution(name));
                        }
                        // The sub-parse may have walked through strings
                        // itself; reset our tracker to Code for whatever
                        // follows.
                        str_state = StringState::new();
                        literal_start = self.pos;
                        continue;
                    }
                    _ => {
                        // Bare `$` in body — treat as literal.
                    }
                }
            }
            // Default fall-through: advance the cursor and the string
            // state machine by one byte.
            str_state.advance(c, self.peek_n(1));
            self.pos += 1;
        }

        self.flush_literal(&mut tokens, literal_start, self.pos);
        Ok(tokens)
    }

    /// Peek past **horizontal** ASCII whitespace (spaces and tabs only) and
    /// return the next byte. Used by `$ident` → macro-call disambiguation
    /// so that a newline between the identifier and a following `(` breaks
    /// the association — `$foo\n(stmt)` parses as substitution + paren
    /// expression, not as `$foo(stmt)`.
    fn peek_after_horizontal_ws(&self) -> Option<u8> {
        let mut j = self.pos;
        while j < self.src.len() && matches!(self.src[j], b' ' | b'\t') {
            j += 1;
        }
        self.src.get(j).copied()
    }

    /// Skip only spaces and tabs — not newlines. Paired with
    /// [`Self::peek_after_horizontal_ws`] for the newline-sensitive
    /// `$ident ( args )` disambiguation.
    fn skip_horizontal_ws(&mut self) {
        while self.pos < self.src.len() && matches!(self.src[self.pos], b' ' | b'\t') {
            self.pos += 1;
        }
    }

    /// Look ahead from `self.pos` (which must point at `<`) to verify
    /// that the angle-bracket span balances cleanly — i.e. there's a
    /// matching `>` at the same depth, no unbalanced parens / braces
    /// inside, and the contents look like a comma-separated argument
    /// list rather than a chained comparison expression.
    ///
    /// Used by the body parser to disambiguate `$ident<args>` (a
    /// type-position macro call) from `$x < $y > z` (a chain of
    /// comparison operators that should fall through to substitution
    /// + literal). The lookahead is read-only and does not advance
    ///   `self.pos`.
    ///
    /// Conservative: returns `false` whenever the lookahead is
    /// ambiguous, so the parser falls back to the substitution path
    /// and the user can always disambiguate by inserting whitespace
    /// (`$x < $y` is never a macro call because it has space before
    /// the `<`).
    fn angle_args_balance_ok(&self) -> bool {
        if self.peek() != Some(b'<') {
            return false;
        }
        let mut depth: i32 = 0;
        let mut paren: i32 = 0;
        let mut brace: i32 = 0;
        let mut bracket: i32 = 0;
        let mut j = self.pos;
        let mut in_string: Option<u8> = None;
        let mut escape_next = false;
        while j < self.src.len() {
            let c = self.src[j];
            if let Some(quote) = in_string {
                if escape_next {
                    escape_next = false;
                } else if c == b'\\' {
                    escape_next = true;
                } else if c == quote {
                    in_string = None;
                }
                j += 1;
                continue;
            }
            match c {
                b'\'' | b'"' | b'`' => {
                    in_string = Some(c);
                }
                b'(' => paren += 1,
                b')' => paren -= 1,
                b'{' => brace += 1,
                b'}' => brace -= 1,
                b'[' => bracket += 1,
                b']' => bracket -= 1,
                b'<' => depth += 1,
                b'>' => {
                    depth -= 1;
                    if depth == 0 {
                        // Balanced — make sure paren/brace/bracket are
                        // also balanced inside the angle span.
                        return paren == 0 && brace == 0 && bracket == 0;
                    }
                }
                // A semicolon or end-of-line inside the angles
                // strongly suggests we're not in a type argument
                // list. Bail out conservatively.
                b';' | b'\n' => return false,
                _ => {}
            }
            j += 1;
        }
        false
    }

    /// Parse the contents of a type-position macro call's argument
    /// list — the body tokens between `<` and the matching `>`.
    /// Tracks `<`/`>` depth so nested generic args (e.g.
    /// `$Outer<$Inner<$x>>`) parse correctly. Body substitutions
    /// (`$ident`) and nested macro calls (`$other(...)` or
    /// `$other<...>`) are recognized inside the type-arg list the
    /// same way they are inside parenthesized arg lists.
    fn parse_type_call_args(&mut self) -> Result<Vec<BodyToken>, DeclarativeError> {
        let mut tokens: Vec<BodyToken> = Vec::new();
        let mut literal_start = self.pos;
        let mut depth: i32 = 0;

        while self.pos < self.src.len() {
            let c = self.src[self.pos];
            if c == b'>' && depth == 0 {
                self.flush_literal(&mut tokens, literal_start, self.pos);
                return Ok(tokens);
            }
            if c == b'<' {
                depth += 1;
                self.pos += 1;
                continue;
            }
            if c == b'>' {
                depth -= 1;
                self.pos += 1;
                continue;
            }
            if c == b'$' {
                match self.peek_n(1) {
                    Some(b'$') => {
                        self.flush_literal(&mut tokens, literal_start, self.pos);
                        tokens.push(BodyToken::Literal("$".to_string()));
                        self.pos += 2;
                        literal_start = self.pos;
                        continue;
                    }
                    Some(c) if is_ident_start(c) => {
                        self.flush_literal(&mut tokens, literal_start, self.pos);
                        self.pos += 1; // consume `$`
                        let name = self.consume_ident();
                        // Inside a type-arg list we ALSO recognize
                        // nested type-call shapes for the natural
                        // form `$Outer<$Inner<$x>>`. Strict
                        // adjacency on `<` to avoid false positives.
                        let nested_value_call = name != RESERVED_CLUSTER_NAME
                            && self.peek_after_horizontal_ws() == Some(b'(');
                        let nested_type_call = name != RESERVED_CLUSTER_NAME
                            && !nested_value_call
                            && self.peek() == Some(b'<')
                            && self.angle_args_balance_ok();
                        if nested_value_call {
                            self.skip_horizontal_ws();
                            self.pos += 1;
                            let nested_args = self.parse_call_args()?;
                            match self.peek() {
                                Some(b')') => self.pos += 1,
                                _ => {
                                    return Err(DeclarativeError::new(
                                        self.span_at(self.pos, self.pos),
                                        "expected `)` closing nested macro call in type arg list",
                                    ));
                                }
                            }
                            tokens.push(BodyToken::MacroCall {
                                name,
                                args: nested_args,
                            });
                        } else if nested_type_call {
                            self.pos += 1; // consume `<`
                            let nested_args = self.parse_type_call_args()?;
                            match self.peek() {
                                Some(b'>') => self.pos += 1,
                                _ => {
                                    return Err(DeclarativeError::new(
                                        self.span_at(self.pos, self.pos),
                                        "expected `>` closing nested type-macro call in type arg list",
                                    ));
                                }
                            }
                            tokens.push(BodyToken::MacroCall {
                                name,
                                args: nested_args,
                            });
                        } else {
                            tokens.push(BodyToken::Substitution(name));
                        }
                        literal_start = self.pos;
                        continue;
                    }
                    _ => {}
                }
            }
            self.pos += 1;
        }

        Err(DeclarativeError::new(
            self.span_at(self.pos, self.pos),
            "unterminated type-position macro call in body (no matching `>`)",
        ))
    }

    /// Parse the contents of a macro call's argument list — the body
    /// tokens between `(` and the matching `)`. Nested `()` are tracked
    /// so e.g. `$outer(foo($x))` is parsed as one `MacroCall` containing
    /// a literal `foo(` + substitution + literal `)`.
    fn parse_call_args(&mut self) -> Result<Vec<BodyToken>, DeclarativeError> {
        let mut tokens: Vec<BodyToken> = Vec::new();
        let mut literal_start = self.pos;
        let mut paren_depth: i32 = 0;
        let mut str_state = StringState::new();

        while self.pos < self.src.len() {
            let c = self.src[self.pos];
            // `)` and `(` inside string literals don't affect call
            // argument balancing — they're just text.
            if !str_state.in_string() {
                if c == b')' && paren_depth == 0 {
                    self.flush_literal(&mut tokens, literal_start, self.pos);
                    return Ok(tokens);
                }
                if c == b'(' {
                    paren_depth += 1;
                    str_state.advance(c, self.peek_n(1));
                    self.pos += 1;
                    continue;
                }
                if c == b')' {
                    paren_depth -= 1;
                    str_state.advance(c, self.peek_n(1));
                    self.pos += 1;
                    continue;
                }
                if c == b'$' {
                    match self.peek_n(1) {
                        Some(b'$') => {
                            self.flush_literal(&mut tokens, literal_start, self.pos);
                            tokens.push(BodyToken::Literal("$".to_string()));
                            str_state.advance(c, self.peek_n(1));
                            str_state.advance(b'$', self.peek_n(2));
                            self.pos += 2;
                            literal_start = self.pos;
                            continue;
                        }
                        Some(c) if is_ident_start(c) => {
                            self.flush_literal(&mut tokens, literal_start, self.pos);
                            self.pos += 1; // consume `$`
                            let name = self.consume_ident();
                            // Nested macro call inside arg list. Newlines
                            // between the identifier and `(` break the
                            // association, mirroring `parse_until`.
                            // `$cluster` is reserved — see the
                            // [`RESERVED_CLUSTER_NAME`] constant.
                            if name != RESERVED_CLUSTER_NAME
                                && self.peek_after_horizontal_ws() == Some(b'(')
                            {
                                self.skip_horizontal_ws();
                                self.pos += 1;
                                let nested_args = self.parse_call_args()?;
                                match self.peek() {
                                    Some(b')') => self.pos += 1,
                                    _ => {
                                        return Err(DeclarativeError::new(
                                            self.span_at(self.pos, self.pos),
                                            "expected `)` closing nested macro call in arg list",
                                        ));
                                    }
                                }
                                tokens.push(BodyToken::MacroCall {
                                    name,
                                    args: nested_args,
                                });
                            } else {
                                tokens.push(BodyToken::Substitution(name));
                            }
                            str_state = StringState::new();
                            literal_start = self.pos;
                            continue;
                        }
                        _ => {}
                    }
                }
            }
            str_state.advance(c, self.peek_n(1));
            self.pos += 1;
        }

        Err(DeclarativeError::new(
            self.span_at(self.pos, self.pos),
            "unterminated macro call in body (no matching `)`)",
        ))
    }

    fn parse_repetition_inner(&mut self) -> Result<Vec<BodyToken>, DeclarativeError> {
        // Track `(` depth so nested `()` inside the repetition body don't
        // cause early termination. The opening `$(` consumed one paren's
        // worth of "depth context" already; we stop at the matching `)`.
        let mut tokens: Vec<BodyToken> = Vec::new();
        let mut literal_start = self.pos;
        let mut paren_depth: i32 = 0;
        let mut str_state = StringState::new();

        while self.pos < self.src.len() {
            let c = self.src[self.pos];
            if !str_state.in_string() {
                if c == b')' && paren_depth == 0 {
                    self.flush_literal(&mut tokens, literal_start, self.pos);
                    return Ok(tokens);
                }
                if c == b'(' {
                    paren_depth += 1;
                    str_state.advance(c, self.peek_n(1));
                    self.pos += 1;
                    continue;
                }
                if c == b')' {
                    paren_depth -= 1;
                    str_state.advance(c, self.peek_n(1));
                    self.pos += 1;
                    continue;
                }
                if c == b'$' {
                    match self.peek_n(1) {
                        Some(b'$') => {
                            self.flush_literal(&mut tokens, literal_start, self.pos);
                            tokens.push(BodyToken::Literal("$".to_string()));
                            str_state.advance(c, self.peek_n(1));
                            str_state.advance(b'$', self.peek_n(2));
                            self.pos += 2;
                            literal_start = self.pos;
                            continue;
                        }
                        Some(b'(') => {
                            self.flush_literal(&mut tokens, literal_start, self.pos);
                            self.pos += 2;
                            let inner = self.parse_repetition_inner()?;
                            match self.peek() {
                                Some(b')') => self.pos += 1,
                                _ => {
                                    return Err(DeclarativeError::new(
                                        self.span_at(self.pos, self.pos),
                                        "expected `)` closing nested repetition in body",
                                    ));
                                }
                            }
                            let (separator, kind) = self.parse_rep_suffix()?;
                            tokens.push(BodyToken::Repetition {
                                body: inner,
                                separator,
                                kind,
                            });
                            str_state = StringState::new();
                            literal_start = self.pos;
                            continue;
                        }
                        Some(c) if is_ident_start(c) => {
                            self.flush_literal(&mut tokens, literal_start, self.pos);
                            self.pos += 1;
                            let name = self.consume_ident();
                            // Phase 12: macro call inside a repetition
                            // body — `$( $double($x); )+`. Newline-
                            // sensitive disambiguation matches the
                            // top-level rule. `$cluster` is reserved —
                            // see [`RESERVED_CLUSTER_NAME`].
                            if name != RESERVED_CLUSTER_NAME
                                && self.peek_after_horizontal_ws() == Some(b'(')
                            {
                                self.skip_horizontal_ws();
                                self.pos += 1;
                                // Note: we re-use parse_call_args here
                                // even though we're inside a repetition
                                // body. The call args are balanced on
                                // their own `()`; the repetition's outer
                                // `)` is handled by the paren_depth
                                // counter above.
                                let call_args = self.parse_call_args()?;
                                match self.peek() {
                                    Some(b')') => self.pos += 1,
                                    _ => {
                                        return Err(DeclarativeError::new(
                                            self.span_at(self.pos, self.pos),
                                            "expected `)` closing macro call in repetition body",
                                        ));
                                    }
                                }
                                tokens.push(BodyToken::MacroCall {
                                    name,
                                    args: call_args,
                                });
                            } else {
                                tokens.push(BodyToken::Substitution(name));
                            }
                            str_state = StringState::new();
                            literal_start = self.pos;
                            continue;
                        }
                        _ => {}
                    }
                }
            }
            str_state.advance(c, self.peek_n(1));
            self.pos += 1;
        }

        Err(DeclarativeError::new(
            self.span_at(self.pos, self.pos),
            "unterminated repetition body (no matching `)`)",
        ))
    }

    fn parse_rep_suffix(&mut self) -> Result<(Option<String>, RepetitionKind), DeclarativeError> {
        let separator = match self.peek() {
            Some(c @ (b',' | b';')) => {
                // Lookahead: only treat as separator if followed by a rep
                // kind marker (with optional whitespace). Otherwise it's part
                // of the subsequent literal.
                let mut probe = self.pos + 1;
                while probe < self.src.len()
                    && matches!(self.src[probe], b' ' | b'\t' | b'\r' | b'\n')
                {
                    probe += 1;
                }
                if probe < self.src.len() && matches!(self.src[probe], b'*' | b'+' | b'?') {
                    self.pos += 1;
                    Some((c as char).to_string())
                } else {
                    None
                }
            }
            _ => None,
        };
        let kind = match self.peek() {
            Some(b'*') => {
                self.pos += 1;
                RepetitionKind::ZeroOrMore
            }
            Some(b'+') => {
                self.pos += 1;
                RepetitionKind::OneOrMore
            }
            Some(b'?') => {
                self.pos += 1;
                RepetitionKind::ZeroOrOne
            }
            _ => {
                return Err(DeclarativeError::new(
                    self.span_at(self.pos, self.pos),
                    "expected `*`, `+`, or `?` after repetition in body",
                ));
            }
        };
        Ok((separator, kind))
    }

    fn flush_literal(&self, tokens: &mut Vec<BodyToken>, start: usize, end: usize) {
        if end > start {
            let text = std::str::from_utf8(&self.src[start..end])
                .expect("body source is valid UTF-8")
                .to_string();
            tokens.push(BodyToken::Literal(text));
        }
    }

    fn consume_ident(&mut self) -> String {
        let start = self.pos;
        while let Some(c) = self.peek() {
            if is_ident_continue(c) {
                self.pos += 1;
            } else {
                break;
            }
        }
        std::str::from_utf8(&self.src[start..self.pos])
            .expect("ident is ASCII")
            .to_string()
    }
}