macro-lang 1.2.0

Hygienic macro expansion: pattern match + template substitution.
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
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
# macro-lang — API Reference

> Complete reference for every public item in `macro-lang`, with examples.
> **Status: stable (1.x).** The surface below is the `1.0` contract plus the
> additive `1.1` items (marked *Added in 1.1.0*); it follows
> [Semantic Versioning]#stability and will not change in a breaking way before
> `2.0`. See [`../dev/ROADMAP.md`]../dev/ROADMAP.md.

<sub>Copyright &copy; 2026 <strong>James Gober</strong>.</sub>

## Table of contents

- [Overview]#overview
- [Installation]#installation
- [Quick start]#quick-start
- [Concepts]#concepts
  - [Token trees]#token-trees
  - [Rules, patterns, and templates]#rules-patterns-and-templates
  - [How matching works]#how-matching-works
  - [Repetition depth and lockstep]#repetition-depth-and-lockstep
  - [Hygiene]#hygiene
  - [Termination and budgets]#termination-and-budgets
- [`Tree`]#tree
  - [`Tree::token`]#treetoken
  - [`Tree::span`]#treespan
  - [`Tree::dismantle`]#treedismantle
- [`Pattern`]#pattern
- [`Fragment`]#fragment
- [`Kleene`]#kleene
  - [`Kleene::allows`]#kleeneallows
- [`Template`]#template
  - [`Template::token`]#templatetoken
- [`Rule`]#rule
- [`Macro`]#macro
  - [`Macro::new`]#macronew
  - [`Macro::name`]#macroname
- [`Expander`]#expander
  - [`Expander::DEFAULT_LIMIT`]#expanderdefault_limit
  - [`Expander::new`]#expandernew
  - [`Expander::with_limit`]#expanderwith_limit
  - [`Expander::limit`]#expanderlimit
  - [`Expander::with_budget`]#expanderwith_budget
  - [`Expander::budget`]#expanderbudget
  - [`Expander::set_budget`]#expanderset_budget
  - [`Expander::usage`]#expanderusage
  - [`Expander::reset_usage`]#expanderreset_usage
  - [`Expander::expand`]#expanderexpand
  - [`Expander::expand_at`]#expanderexpand_at
  - [`Expander::origin`]#expanderorigin
- [`Budget`]#budget
  - [`Budget::DEFAULT`]#budgetdefault
  - [`Budget::with_max_depth`, `with_max_expansions`, `with_max_tokens`]#budgetwith_max_depth-with_max_expansions-with_max_tokens
- [`Limit`]#limit
- [`Usage`]#usage
- [`Context`]#context
- [`Origin`]#origin
- [`MacroError`]#macroerror
- [`ExpandError`]#expanderror
- [Feature flags]#feature-flags
- [Guide: writing a front end]#guide-writing-a-front-end
- [Guide: resolving names with contexts]#guide-resolving-names-with-contexts
- [Guide: bounding untrusted macros]#guide-bounding-untrusted-macros
- [Stability]#stability

---

## Overview

macro-lang is a hygienic macro expander for language implementations — the
`macro_rules!` / Scheme `syntax-rules` model, generic over a language's token
kind. It matches an invocation's token trees against a macro's rules, writes out
the template of the first rule that matches, and gives every token the template
introduces a fresh hygiene context.

| Item | Role |
|---|---|
| [`Tree`]#tree | A token tree: one token, or a delimited group. Input and output of every expansion. |
| [`Pattern`]#pattern | One element of a rule's match side. |
| [`Fragment`]#fragment | What a metavariable binding may capture. |
| [`Kleene`]#kleene | How many times a pattern repetition may match. |
| [`Template`]#template | One element of a rule's output side. |
| [`Rule`]#rule | A pattern paired with a template. |
| [`Macro`]#macro | A validated, compiled list of rules under a name. |
| [`Expander`]#expander | Runs expansions; owns the hygiene table, the budget, and the scratch buffers. |
| [`Budget`]#budget | Limits on nesting depth, expansions, and tokens written. |
| [`Limit`]#limit | Which part of a budget an expansion ran out of. |
| [`Usage`]#usage | The work charged against a budget since the last reset. |
| [`Context`]#context | The hygiene context of a token. |
| [`Origin`]#origin | Where a minted context came from. |
| [`MacroError`]#macroerror | Why a definition was rejected. |
| [`ExpandError`]#expanderror | Why an invocation could not be expanded. |

The crate is `#![forbid(unsafe_code)]`, `no_std`-compatible (needs only
`alloc`), and depends on [`token-lang`](https://crates.io/crates/token-lang)
(`Token`, `Span`) and [`intern-lang`](https://crates.io/crates/intern-lang)
(`Symbol`).

---

## Installation

```toml
[dependencies]
macro-lang = "1"
token-lang = "1"
intern-lang = "1"
```

Or from the terminal:

```bash
cargo add macro-lang token-lang intern-lang
```

MSRV: Rust 1.85 (Rust 2024 edition).

---

## Quick start

`twice!($x:tt) => $x $x`, built and expanded. The token kind here is `char`;
a real language uses its own kind enum.

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Fragment, Macro, Pattern, Rule, Template, Tree};
use token_lang::Span;

let mut names = Interner::new();
let x = names.intern("x");

let twice = Macro::new(names.intern("twice"), vec![Rule {
    pattern: vec![Pattern::Bind { name: x, fragment: Fragment::Tree }],
    template: vec![Template::Var(x), Template::Var(x)],
}])?;

let mut expander = Expander::new();
let input = [Tree::token('a', Span::new(7, 8))];
let out = expander.expand(&twice, &input, Span::new(0, 9), Context::ROOT)?;
assert_eq!(out, vec![input[0].clone(), input[0].clone()]);
# Ok::<(), Box<dyn std::error::Error>>(())
```

---

## Concepts

### Token trees

Expansion works on **token trees**: each tree is a single token or a delimited
group of trees. Grouping by delimiters is the only structure every language has
before parsing, and it is the structure patterns need — a group is matched as a
unit, and a metavariable can capture an entire bracketed expression without
knowing the grammar inside it. Expansion runs before parsing; the parser consumes
the expanded trees.

Tokens are [`token_lang::Token<K>`](https://docs.rs/token-lang): a kind `K` of
your choosing and a source span. macro-lang only clones kinds and compares them
for equality, so a small `Copy` kind — an enum whose identifiers and literals
carry interned `Symbol`s — is the fast path.

### Rules, patterns, and templates

A [`Macro`](#macro) is an ordered list of [`Rule`](#rule)s. Each rule has a
**pattern**, matched against the invocation, and a **template**, written out when
the pattern matches.

| `macro_rules!` syntax | Pattern element | Template element |
|---|---|---|
| a literal token `else` | `Pattern::Token(kind)` | `Template::Token { token, ctx }` |
| a group `( ... )` | `Pattern::Group { open, close, body }` | `Template::Group { open, close, body }` |
| `$x:tt` / `$x` | `Pattern::Bind { name, fragment }` | `Template::Var(name)` |
| `$( ... ),*` | `Pattern::Repeat { body, separator, kleene }` | `Template::Repeat { body, separator }` |

Patterns and templates are plain data. A language's front end parses its own
macro-definition syntax and lowers it into these elements (see
[writing a front end](#guide-writing-a-front-end)).

### How matching works

- Rules are tried **in order**; the first rule whose pattern matches the
  **entire** invocation is used.
- Literal tokens compare by **kind only** — spans and hygiene contexts never
  affect a match.
- A pattern is compiled into a nondeterministic automaton and simulated in one
  left-to-right pass, merging parses that reach the same point. There is no
  backtracking: cost grows linearly with the input even for adversarial
  patterns.
- If a rule matches in **more than one way**, expansion stops with
  [`ExpandError::Ambiguous`]#expanderror rather than guessing. An apparent
  ambiguity that the rest of the input resolves — `$($t:tt)* ;` followed by
  input ending in `;` — is not an error.
- If no rule matches, [`ExpandError::NoMatch`]#expanderror points at the token
  the furthest-reaching rule could not get past.

### Repetition depth and lockstep

A metavariable bound inside `n` pattern repetitions has **depth** `n` and
captures one tree per repetition. In the template:

- it must be used inside at least `n` template repetitions
  ([`MacroError::StillRepeating`]#macroerror otherwise);
- used inside more than `n`, it is repeated as a whole across the extra levels;
- every template repetition needs at least one metavariable that repeats at its
  depth, which decides the iteration count
  ([`MacroError::NoRepeatingVariable`]#macroerror otherwise);
- all metavariables that repeat at a template repetition's depth advance in
  **lockstep** and must have captured the same number of trees
  ([`ExpandError::RepetitionMismatch`]#expanderror otherwise).

### Hygiene

Every token in a tree carries a [`Context`](#context). Source tokens carry
[`Context::ROOT`](#context). Each expansion mints a context for the **literal**
tokens its template writes, while tokens **substituted** from the invocation keep
the context they arrived with. A name resolver that compares spelling *and*
context keeps a macro's internal names and its caller's names apart.
[`Expander::origin`](#expanderorigin) reports, for any minted context, the
context the token had in the macro definition (for definition-site fallback),
the macro, and the call site. See
[resolving names with contexts](#guide-resolving-names-with-contexts).

### Termination and budgets

Macros are untrusted input: a two-line macro can describe unbounded work. Every
[`Expander`](#expander) therefore enforces a [`Budget`](#budget), on by default:

- **Depth** (`max_depth`, default 128). An invocation in source runs at depth 1;
  an invocation runs one level deeper than the deepest expansion that produced
  its name token or any token of its input, and — through
  [`expand_at`]#expanderexpand_at — one level deeper than the code the driver
  found it in. Exceeding it is [`RecursionLimit`]#expanderror.
- **Expansions** (`max_expansions`, default `2^20`) and **tokens**
  (`max_tokens`, default `2^22`), counted in the expander's
  [`Usage`]#usage until [`reset_usage`]#expanderreset_usage zeroes it —
  never, for a batch compiler; once per unit of work, for a long-lived host.
  Exceeding either is [`ExpandError::Budget`]#expanderror. A macro that
  doubles its argument every round, or that expands into several calls of
  itself, is stopped here long before it reaches depth 128.

Hygiene cannot see the depth of an invocation rebuilt entirely from captured
source tokens: a macro that writes `$name ! ( $args )` from its own input
reproduces a call identical to the one it came from. A driver that knows where
it found each invocation passes that depth to `expand_at`, which bounds such a
macro by the depth limit; with plain [`expand`](#expanderexpand), the
expansion and token budgets bound it instead. See
[bounding untrusted macros](#guide-bounding-untrusted-macros).

---

## `Tree`

```rust,ignore
pub enum Tree<K> {
    Token { token: Token<K>, ctx: Context },
    Group { open: Token<K>, close: Token<K>, trees: Vec<Tree<K>> },
}
```

A token tree: a single token with its hygiene context, or a delimited group.

**Variants**

- `Token { token, ctx }` — one token (`token.kind`, `token.span`) and the
  [`Context`]#context it belongs to.
- `Group { open, close, trees }` — an opening delimiter token, the trees between
  the delimiters in source order, and the closing delimiter token. Delimiters
  carry no context; hygiene applies to the tokens inside.

**Trait implementations:** `Clone`, `Debug`, `PartialEq`, `Eq`, `Hash` (each
where `K` implements it).

**Deep trees.** Since 1.2.0, `Clone`, `PartialEq` and `Hash` walk a tree with a
heap stack, so they work at any nesting depth, and so does everything the
expander does with the trees it is given and returns. They behave exactly as the
derived impls did, and `Hash` feeds the hasher the same values. Dropping a tree
and `Debug` still recurse once per level, as for any recursive Rust type: the
fields are public and callers move them out by value, so `Tree` can't have a
custom `Drop` in 1.x. A front end that builds trees from untrusted text should
cap group nesting while building them, or pass finished trees to
[`Tree::dismantle`](#treedismantle) instead of dropping them.

Building the trees for `f(a, b)`:

```rust
use macro_lang::Tree;
use token_lang::{Span, Token};

let call: Vec<Tree<char>> = vec![
    Tree::token('f', Span::new(0, 1)),
    Tree::Group {
        open: Token::new('(', Span::new(1, 2)),
        close: Token::new(')', Span::new(6, 7)),
        trees: vec![
            Tree::token('a', Span::new(2, 3)),
            Tree::token(',', Span::new(3, 4)),
            Tree::token('b', Span::new(5, 6)),
        ],
    },
];
assert_eq!(call.len(), 2);
```

Grouping a flat token stream from a lexer — the step every front end performs
before expansion:

```rust
use macro_lang::{Context, Tree};
use token_lang::{Span, Token};

fn group(tokens: Vec<Token<char>>) -> Option<Vec<Tree<char>>> {
    let mut stack: Vec<(Token<char>, Vec<Tree<char>>)> = Vec::new();
    let mut top = Vec::new();
    for token in tokens {
        match token.kind {
            '(' => stack.push((token, std::mem::take(&mut top))),
            ')' => {
                let (open, parent) = stack.pop()?; // unbalanced input
                let trees = std::mem::replace(&mut top, parent);
                top.push(Tree::Group { open, close: token, trees });
            }
            _ => top.push(Tree::Token { token, ctx: Context::ROOT }),
        }
    }
    stack.is_empty().then_some(top)
}

let tokens = "a(bc)d"
    .char_indices()
    .map(|(i, c)| Token::new(c, Span::new(i as u32, i as u32 + 1)))
    .collect();
let trees = group(tokens).ok_or("unbalanced")?;
assert_eq!(trees.len(), 3);
assert!(matches!(&trees[1], Tree::Group { trees, .. } if trees.len() == 2));
# Ok::<(), Box<dyn std::error::Error>>(())
```

### `Tree::token`

```rust,ignore
pub const fn token(kind: K, span: Span) -> Tree<K>
```

Builds a single-token tree in the root context — a token written directly in
source.

**Parameters**

- `kind` — the token kind.
- `span` — the token's source span.

```rust
use macro_lang::{Context, Tree};
use token_lang::{Span, Token};

let tree = Tree::token('x', Span::new(4, 5));
assert_eq!(tree, Tree::Token { token: Token::new('x', Span::new(4, 5)), ctx: Context::ROOT });
```

### `Tree::span`

```rust,ignore
pub const fn span(&self) -> Span
```

Returns the source span the tree covers: the token's span, or for a group, from
the start of the opening delimiter to the end of the closing one. Useful for
pointing a diagnostic at a captured argument.

```rust
use macro_lang::Tree;
use token_lang::{Span, Token};

let group: Tree<char> = Tree::Group {
    open: Token::new('[', Span::new(10, 11)),
    close: Token::new(']', Span::new(14, 15)),
    trees: vec![Tree::token('x', Span::new(12, 13))],
};
assert_eq!(group.span(), Span::new(10, 15));
assert_eq!(Tree::token('x', Span::new(2, 3)).span(), Span::new(2, 3));
```

### `Tree::dismantle`

```rust,ignore
pub fn dismantle(trees: impl IntoIterator<Item = Tree<K>>)
```

*Added in 1.2.0.* Drops `trees` with a heap stack instead of recursion, so a
tree nested any number of levels deep can be thrown away without overflowing
the stack. Pass one tree as `[tree]`, or the `Vec` an expansion returns.

```rust
use macro_lang::Tree;
use token_lang::{Span, Token};

let mut tree = Tree::token('x', Span::new(0, 1));
for _ in 0..100_000 {
    tree = Tree::Group {
        open: Token::new('(', Span::new(0, 1)),
        close: Token::new(')', Span::new(0, 1)),
        trees: vec![tree],
    };
}
Tree::dismantle([tree]);
```

---

## `Pattern`

```rust,ignore
#[non_exhaustive]
pub enum Pattern<K> {
    Token(K),
    Group { open: K, close: K, body: Vec<Pattern<K>> },
    Bind { name: Symbol, fragment: Fragment<K> },
    Repeat { body: Vec<Pattern<K>>, separator: Option<K>, kleene: Kleene },
}
```

One element of a rule's pattern. A pattern is a `Vec<Pattern<K>>` matched against
the invocation left to right; it must consume the whole invocation.

**Variants**

- `Token(kind)` — matches one token whose kind equals `kind`.
- `Group { open, close, body }` — matches one group whose delimiter kinds equal
  `open` and `close` and whose contents match `body` in full.
- `Bind { name, fragment }` — the metavariable `$name:fragment`: captures one
  tree that `fragment` allows. `name` must be unique within the rule.
- `Repeat { body, separator, kleene }` — the repetition `$( body ) sep kleene`:
  matches `body` repeatedly, with a `separator` token between repetitions if one
  is given. The body must consume input on every path
  ([`MacroError::EmptyRepetition`]#macroerror), and a
  [`Kleene::ZeroOrOne`]#kleene repetition cannot have a separator
  ([`MacroError::OptionalSeparator`]#macroerror).

`#[non_exhaustive]`: new kinds of pattern element may be added in a minor
release. Constructing a variant is unaffected; a `match` on `Pattern` needs a
wildcard arm.

**Trait implementations:** `Clone`, `Debug`.

A literal keyword followed by a parenthesized argument — `when ( $cond:tt )`:

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Fragment, Macro, Pattern, Rule, Template, Tree};
use token_lang::{Span, Token};

let mut names = Interner::new();
let cond = names.intern("cond");
let s = Span::new(0, 1);
let when = Macro::new(names.intern("when"), vec![Rule {
    pattern: vec![
        Pattern::Token('w'),
        Pattern::Group {
            open: '(',
            close: ')',
            body: vec![Pattern::Bind { name: cond, fragment: Fragment::Tree }],
        },
    ],
    template: vec![Template::Var(cond)],
}])?;

let input = [
    Tree::token('w', s),
    Tree::Group { open: Token::new('(', s), close: Token::new(')', s), trees: vec![Tree::token('c', s)] },
];
let out = Expander::new().expand(&when, &input, s, Context::ROOT)?;
assert_eq!(out, vec![Tree::token('c', s)]);
# Ok::<(), Box<dyn std::error::Error>>(())
```

A comma-separated list of at least one element — `$($item:tt),+`:

```rust
use intern_lang::Interner;
use macro_lang::{Context, ExpandError, Expander, Fragment, Kleene, Macro, Pattern, Rule, Template, Tree};
use token_lang::Span;
# fn trees(src: &str) -> Vec<Tree<char>> {
#     src.char_indices().map(|(i, c)| Tree::token(c, Span::new(i as u32, i as u32 + 1))).collect()
# }

let mut names = Interner::new();
let item = names.intern("item");
let items = Macro::new(names.intern("items"), vec![Rule {
    pattern: vec![Pattern::Repeat {
        body: vec![Pattern::Bind { name: item, fragment: Fragment::Tree }],
        separator: Some(','),
        kleene: Kleene::OneOrMore,
    }],
    template: vec![Template::Repeat { body: vec![Template::Var(item)], separator: None }],
}])?;

let mut expander = Expander::new();
assert_eq!(expander.expand(&items, &trees("a,b,c"), Span::new(0, 5), Context::ROOT)?.len(), 3);
// `+` needs at least one element.
assert!(matches!(
    expander.expand(&items, &[], Span::new(0, 0), Context::ROOT),
    Err(ExpandError::NoMatch { .. })
));
# Ok::<(), Box<dyn std::error::Error>>(())
```

---

## `Fragment`

```rust,ignore
#[non_exhaustive]
pub enum Fragment<K> {
    Tree,
    Kind(fn(&K) -> bool),
}
```

What a [`Pattern::Bind`](#pattern) may capture — the fragment specifier.
macro-lang knows no grammar, so it cannot offer `expr` or `ty`; it offers the
structural specifier every macro system has, plus a token-level test the
language supplies.

**Variants**

- `Tree` — any single token tree: one token, or one whole group. The `tt`
  specifier.
- `Kind(test)` — a single token (never a group) for which `test(&kind)` returns
  `true`. Covers specifiers such as `ident` and `literal`:
  `Fragment::Kind(Kind::is_ident)`.

`#[non_exhaustive]`: new fragment kinds may be added in a minor release; a
`match` on `Fragment` needs a wildcard arm.

**Trait implementations:** `Clone`, `Copy`, `Debug`.

An `ident`-style fragment that only accepts letters:

```rust
use intern_lang::Interner;
use macro_lang::{Context, ExpandError, Expander, Fragment, Macro, Pattern, Rule, Template, Tree};
use token_lang::Span;

let mut names = Interner::new();
let name = names.intern("name");
let ident = Macro::new(names.intern("ident"), vec![Rule {
    pattern: vec![Pattern::Bind { name, fragment: Fragment::Kind(|c: &char| c.is_alphabetic()) }],
    template: vec![Template::Var(name)],
}])?;

let mut expander = Expander::new();
let ok = expander.expand(&ident, &[Tree::token('q', Span::new(0, 1))], Span::new(0, 1), Context::ROOT);
assert!(ok.is_ok());

let err = expander.expand(&ident, &[Tree::token('7', Span::new(0, 1))], Span::new(0, 1), Context::ROOT);
assert_eq!(err, Err(ExpandError::NoMatch { span: Span::new(0, 1) }));
# Ok::<(), Box<dyn std::error::Error>>(())
```

`Fragment::Tree` captures a whole group, delimiters included:

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Fragment, Macro, Pattern, Rule, Template, Tree};
use token_lang::{Span, Token};

let mut names = Interner::new();
let t = names.intern("t");
let id = Macro::new(names.intern("id"), vec![Rule {
    pattern: vec![Pattern::Bind { name: t, fragment: Fragment::Tree }],
    template: vec![Template::Var(t)],
}])?;

let s = Span::new(0, 1);
let group = Tree::Group {
    open: Token::new('(', s),
    close: Token::new(')', s),
    trees: vec![Tree::token('a', s), Tree::token('b', s)],
};
let out = Expander::new().expand(&id, &[group.clone()], s, Context::ROOT)?;
assert_eq!(out, vec![group]);
# Ok::<(), Box<dyn std::error::Error>>(())
```

---

## `Kleene`

```rust,ignore
pub enum Kleene {
    ZeroOrMore, // *
    OneOrMore,  // +
    ZeroOrOne,  // ?
}
```

How many times a [`Pattern::Repeat`](#pattern) may match.

**Trait implementations:** `Clone`, `Copy`, `Debug`, `PartialEq`, `Eq`, `Hash`.

### `Kleene::allows`

```rust,ignore
pub const fn allows(self, count: usize) -> bool
```

Returns `true` if a repetition may match exactly `count` times. Handy for a
front end validating its own syntax, or for tests.

**Parameters**

- `count` — a number of repetitions.

```rust
use macro_lang::Kleene;

assert!(Kleene::ZeroOrMore.allows(0));
assert!(Kleene::OneOrMore.allows(5));
assert!(!Kleene::OneOrMore.allows(0));
assert!(Kleene::ZeroOrOne.allows(1));
assert!(!Kleene::ZeroOrOne.allows(2));
```

---

## `Template`

```rust,ignore
#[non_exhaustive]
pub enum Template<K> {
    Token { token: Token<K>, ctx: Context },
    Group { open: Token<K>, close: Token<K>, body: Vec<Template<K>> },
    Var(Symbol),
    Repeat { body: Vec<Template<K>>, separator: Option<Token<K>> },
    RepeatSeparated { body: Vec<Template<K>>, separator: Token<K>, ctx: Context }, // 1.1.0
}
```

One element of a rule's template. A template is a `Vec<Template<K>>` written out
in order when the rule matches.

**Variants**

- `Token { token, ctx }` — a literal token. It is written with a context minted
  for the expansion; `ctx` is the context the token has in the macro definition
  — [`Context::ROOT`]#context for a macro written in source, or the context an
  earlier expansion gave it for a macro defined by a macro.
- `Group { open, close, body }` — a literal group, written with its delimiters
  and its expanded body.
- `Var(name)` — the tree captured by metavariable `name`, substituted with its
  contexts unchanged. Must be bound by the rule's pattern
  ([`MacroError::UnboundVariable`]#macroerror).
- `Repeat { body, separator }` — `body` written once per captured repetition,
  with `separator` written between repetitions. The separator is marked as a
  token defined in the root context — exact for a macro written in source. The
  iteration count comes from the metavariables used inside (see
  [lockstep]#repetition-depth-and-lockstep).
- `RepeatSeparated { body, separator, ctx }` — *Added in 1.1.0.* The same, with
  the separator's definition context given explicitly, like
  `Token { ctx, .. }`. The separator is marked exactly like a literal token
  defined in `ctx`: it shares the context the expansion mints for every other
  literal defined there, and [`Origin::parent`]#origin reports `ctx`. Use it
  when lowering a macro definition that an earlier expansion produced, so its
  separators bind and resolve like the tokens around them. With `ctx` equal to
  `Context::ROOT` it behaves exactly like `Repeat` with `Some(separator)`.

`#[non_exhaustive]`: new kinds of template element may be added in a minor
release. Constructing a variant is unaffected; a `match` on `Template` needs a
wildcard arm.

**Trait implementations:** `Clone`, `Debug`, `PartialEq`, `Eq`, `Hash`.

A template mixing literals, a group, and a separated repetition —
`( $($arg)+* )`:

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Fragment, Kleene, Macro, Pattern, Rule, Template, Tree};
use token_lang::{Span, Token};
# fn trees(src: &str) -> Vec<Tree<char>> {
#     src.char_indices().map(|(i, c)| Tree::token(c, Span::new(i as u32, i as u32 + 1))).collect()
# }
# fn text(trees: &[Tree<char>]) -> String {
#     trees.iter().map(|t| match t {
#         Tree::Token { token, .. } => token.kind.to_string(),
#         Tree::Group { open, close, trees } => format!("{}{}{}", open.kind, text(trees), close.kind),
#     }).collect()
# }

let mut names = Interner::new();
let arg = names.intern("arg");
let s = Span::new(0, 1);
let sum = Macro::new(names.intern("sum"), vec![Rule {
    pattern: vec![Pattern::Repeat {
        body: vec![Pattern::Bind { name: arg, fragment: Fragment::Tree }],
        separator: None,
        kleene: Kleene::ZeroOrMore,
    }],
    template: vec![Template::Group {
        open: Token::new('(', s),
        close: Token::new(')', s),
        body: vec![Template::Repeat {
            body: vec![Template::Var(arg)],
            separator: Some(Token::new('+', s)),
        }],
    }],
}])?;

let out = Expander::new().expand(&sum, &trees("abc"), s, Context::ROOT)?;
assert_eq!(text(&out), "(a+b+c)");
# Ok::<(), Box<dyn std::error::Error>>(())
```

A metavariable of depth 0 used inside a repetition is repeated with it —
`$head:tt $($x:tt)*  =>  $( $head $x )*`:

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Fragment, Kleene, Macro, Pattern, Rule, Template, Tree};
use token_lang::Span;
# fn trees(src: &str) -> Vec<Tree<char>> {
#     src.char_indices().map(|(i, c)| Tree::token(c, Span::new(i as u32, i as u32 + 1))).collect()
# }
# fn text(trees: &[Tree<char>]) -> String {
#     trees.iter().map(|t| match t {
#         Tree::Token { token, .. } => token.kind.to_string(),
#         Tree::Group { open, close, trees } => format!("{}{}{}", open.kind, text(trees), close.kind),
#     }).collect()
# }

let mut names = Interner::new();
let (head, x) = (names.intern("head"), names.intern("x"));
let distribute = Macro::new(names.intern("distribute"), vec![Rule {
    pattern: vec![
        Pattern::Bind { name: head, fragment: Fragment::Tree },
        Pattern::Repeat {
            body: vec![Pattern::Bind { name: x, fragment: Fragment::Tree }],
            separator: None,
            kleene: Kleene::ZeroOrMore,
        },
    ],
    template: vec![Template::Repeat {
        body: vec![Template::Var(head), Template::Var(x)],
        separator: None,
    }],
}])?;

let out = Expander::new().expand(&distribute, &trees("fabc"), Span::new(0, 4), Context::ROOT)?;
assert_eq!(text(&out), "fafbfc");
# Ok::<(), Box<dyn std::error::Error>>(())
```

A separator carrying its definition context — here a context an earlier
expansion minted, as it would be for a macro defined by a macro:

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Fragment, Kleene, Macro, Pattern, Rule, Template, Tree};
use token_lang::{Span, Token};

let mut names = Interner::new();
let s = Span::new(0, 1);
let mut expander = Expander::new();

// A context from an earlier expansion, standing in for the definition's.
let maker = Macro::new(names.intern("maker"), vec![Rule {
    pattern: vec![],
    template: vec![Template::token('k', s)],
}])?;
let Some(Tree::Token { ctx: def, .. }) = expander.expand(&maker, &[], s, Context::ROOT)?.first().cloned() else {
    return Err("expected a token".into());
};

// join!($($x:tt)*) => t $($x);*   with `t` and `;` both defined in `def`
let x = names.intern("x");
let join = Macro::new(names.intern("join"), vec![Rule {
    pattern: vec![Pattern::Repeat {
        body: vec![Pattern::Bind { name: x, fragment: Fragment::Tree }],
        separator: None,
        kleene: Kleene::ZeroOrMore,
    }],
    template: vec![
        Template::Token { token: Token::new('t', s), ctx: def },
        Template::RepeatSeparated {
            body: vec![Template::Var(x)],
            separator: Token::new(';', s),
            ctx: def,
        },
    ],
}])?;

let input = [Tree::token('a', s), Tree::token('b', s)];
let out = expander.expand(&join, &input, s, Context::ROOT)?;
let ctx_of = |i: usize| match out.get(i) {
    Some(Tree::Token { ctx, .. }) => Some(*ctx),
    _ => None,
};
// `t` (index 0) and `;` (index 2) share one minted context, parented by `def`.
assert_eq!(ctx_of(0), ctx_of(2));
let minted = ctx_of(2).ok_or("expected the separator")?;
assert_eq!(expander.origin(minted).ok_or("not minted")?.parent, def);
# Ok::<(), Box<dyn std::error::Error>>(())
```

### `Template::token`

```rust,ignore
pub const fn token(kind: K, span: Span) -> Template<K>
```

Builds a literal template token defined in the root context — the common case
for a macro written directly in source.

**Parameters**

- `kind` — the token kind to write.
- `span` — the token's span in the macro definition. Output tokens keep it, so a
  diagnostic on macro-introduced code can point into the definition.

```rust
use macro_lang::{Context, Template};
use token_lang::{Span, Token};

assert_eq!(
    Template::token(';', Span::new(8, 9)),
    Template::Token { token: Token::new(';', Span::new(8, 9)), ctx: Context::ROOT },
);
```

---

## `Rule`

```rust,ignore
pub struct Rule<K> {
    pub pattern: Vec<Pattern<K>>,
    pub template: Vec<Template<K>>,
}
```

One arm of a macro: a pattern and the template it expands to. Plain data;
[`Macro::new`](#macronew) validates and compiles it.

**Fields**

- `pattern` — what an invocation must look like; it must match all of the
  invocation's trees.
- `template` — what the invocation expands to.

**Trait implementations:** `Clone`, `Debug`.

`($x:tt) => { $x $x }`:

```rust
use intern_lang::Interner;
use macro_lang::{Fragment, Pattern, Rule, Template};

let mut names = Interner::new();
let x = names.intern("x");
let rule: Rule<char> = Rule {
    pattern: vec![Pattern::Bind { name: x, fragment: Fragment::Tree }],
    template: vec![Template::Var(x), Template::Var(x)],
};
assert_eq!(rule.pattern.len(), 1);
assert_eq!(rule.template.len(), 2);
```

An empty rule — `() => {}` — matches only an empty invocation and expands to
nothing:

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Macro, Rule};
use token_lang::Span;

let mut names = Interner::new();
let nothing = Macro::new(names.intern("nothing"), vec![Rule { pattern: vec![], template: vec![] }])?;
let out = Expander::<char>::new().expand(&nothing, &[], Span::new(0, 10), Context::ROOT)?;
assert!(out.is_empty());
# Ok::<(), Box<dyn std::error::Error>>(())
```

---

## `Macro`

```rust,ignore
pub struct Macro<K> { /* private */ }
```

A validated, compiled macro: a name and an ordered list of rules. Building one
does all the work that does not depend on an invocation — every static check
(see [`MacroError`](#macroerror)) and the compilation of each pattern into a
matching automaton and each template into a flat program. A macro is immutable
once built and can be expanded any number of times by any number of expanders.

**Trait implementations:** `Clone` (where `K: Clone`), `Debug` (prints the name
and rule count), `Send` and `Sync` (where `K` is).

### `Macro::new`

```rust,ignore
pub fn new(name: Symbol, rules: Vec<Rule<K>>) -> Result<Macro<K>, MacroError>
```

Validates and compiles a macro.

**Parameters**

- `name` — the macro's name, recorded in the [`Origin`]#origin of every
  context its expansions mint.
- `rules` — the arms, in the order they are tried.

**Errors** — the first problem found, checking rules in order:

| Error | Cause |
|---|---|
| [`NoRules`]#macroerror | `rules` is empty. |
| [`DuplicateBinding`]#macroerror | A pattern binds a name twice. |
| [`EmptyRepetition`]#macroerror | A pattern repetition can match nothing. |
| [`OptionalSeparator`]#macroerror | A `?` repetition has a separator. |
| [`UnboundVariable`]#macroerror | A template uses a name its pattern does not bind. |
| [`StillRepeating`]#macroerror | A template uses a repeating metavariable outside enough repetitions. |
| [`NoRepeatingVariable`]#macroerror | A template repetition has nothing to repeat over. |

A two-rule macro — `()` expands to `0`, anything else passes through:

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Fragment, Kleene, Macro, Pattern, Rule, Template, Tree};
use token_lang::Span;

let mut names = Interner::new();
let t = names.intern("t");
let or_zero = Macro::new(names.intern("or_zero"), vec![
    Rule { pattern: vec![], template: vec![Template::token('0', Span::new(0, 1))] },
    Rule {
        pattern: vec![Pattern::Repeat {
            body: vec![Pattern::Bind { name: t, fragment: Fragment::Tree }],
            separator: None,
            kleene: Kleene::OneOrMore,
        }],
        template: vec![Template::Repeat { body: vec![Template::Var(t)], separator: None }],
    },
])?;

let mut expander = Expander::new();
let zero = expander.expand(&or_zero, &[], Span::new(0, 0), Context::ROOT)?;
assert!(matches!(&zero[..], [Tree::Token { token, .. }] if token.kind == '0'));
let kept = expander.expand(&or_zero, &[Tree::token('k', Span::new(0, 1))], Span::new(0, 1), Context::ROOT)?;
assert_eq!(kept, vec![Tree::token('k', Span::new(0, 1))]);
# Ok::<(), Box<dyn std::error::Error>>(())
```

A definition error, caught before any invocation:

```rust
use intern_lang::Interner;
use macro_lang::{Fragment, Kleene, Macro, MacroError, Pattern, Rule, Template};

let mut names = Interner::new();
let x = names.intern("x");

// `$x` repeats in the pattern but is used bare in the template.
let err = Macro::<char>::new(names.intern("m"), vec![Rule {
    pattern: vec![Pattern::Repeat {
        body: vec![Pattern::Bind { name: x, fragment: Fragment::Tree }],
        separator: None,
        kleene: Kleene::ZeroOrMore,
    }],
    template: vec![Template::Var(x)],
}]).unwrap_err();
assert_eq!(err, MacroError::StillRepeating { rule: 0, name: x });
```

### `Macro::name`

```rust,ignore
pub const fn name(&self) -> Symbol
```

Returns the macro's name — the key an expansion driver usually stores the macro
under.

```rust
use intern_lang::Interner;
use macro_lang::{Macro, Rule};

let mut names = Interner::new();
let mac: Macro<char> = Macro::new(names.intern("empty"), vec![Rule { pattern: vec![], template: vec![] }])?;
assert_eq!(names.resolve(mac.name()), Some("empty"));
# Ok::<(), macro_lang::MacroError>(())
```

---

## `Expander`

```rust,ignore
pub struct Expander<K> { /* private */ }
```

Runs expansions and keeps the hygiene record of every one. An expander owns the
**hygiene table** — every [`Context`](#context) it has minted and the expansion
that minted it — a **[`Budget`](#budget)** bounding the work it may do (see
[termination and budgets](#termination-and-budgets)), and **pooled scratch
buffers** reused by every call, so steady-state expansion allocates only the
output trees.

Use one expander for a whole compilation (or one per thread), so every context in
the program comes from one table. The expander does not find invocations or look
macros up by name; that is the expansion driver's job (see
[writing a front end](#guide-writing-a-front-end)).

**Trait implementations:** `Default` (same as [`new`](#expandernew)), `Debug`
(a summary: the limit and the number of contexts and expansions recorded; the
exact format is not part of the stability contract), `Send` and `Sync` (where
`K` is).

### `Expander::DEFAULT_LIMIT`

```rust,ignore
pub const DEFAULT_LIMIT: u32 = 128;
```

The recursion limit [`Expander::new`](#expandernew) uses: 128 nested expansions,
the same default as rustc's `recursion_limit`. Equal to
[`Budget::DEFAULT`](#budgetdefault)`.max_depth`.

```rust
use macro_lang::Expander;

assert_eq!(Expander::<char>::DEFAULT_LIMIT, 128);
```

### `Expander::new`

```rust,ignore
pub fn new() -> Expander<K>
```

Creates an expander with the [default budget](#budgetdefault) — depth 128,
`2^20` expansions, `2^22` tokens — and an empty hygiene table.

```rust
use macro_lang::Expander;

let expander: Expander<char> = Expander::new();
assert_eq!(expander.limit(), Expander::<char>::DEFAULT_LIMIT);
```

### `Expander::with_limit`

```rust,ignore
pub fn with_limit(limit: u32) -> Expander<K>
```

Creates an expander that allows invocations nested at most `limit` expansions
deep, with the rest of the [default budget](#budgetdefault). Equivalent to
`Expander::with_budget(Budget::DEFAULT.with_max_depth(limit))`.

**Parameters**

- `limit` — the maximum nesting depth. An invocation runs one level deeper than
  the deepest expansion that produced its name token (`call_context`) or any
  token of its input, and, through [`expand_at`]#expanderexpand_at, one level
  deeper than the code the driver found it in. A limit of `0` rejects every
  expansion.

A macro with no base case is stopped instead of looping:

```rust
use intern_lang::Interner;
use macro_lang::{Context, ExpandError, Expander, Macro, Rule, Template, Tree};
use token_lang::Span;

let mut names = Interner::new();
// again!() => again — the output is one token, which the loop below treats
// as a fresh invocation of the same macro.
let again = Macro::new(names.intern("again"), vec![Rule {
    pattern: vec![],
    template: vec![Template::token('g', Span::new(0, 1))],
}])?;

let mut expander = Expander::with_limit(4);
let mut call_context = Context::ROOT;
let err = loop {
    match expander.expand(&again, &[], Span::new(0, 1), call_context) {
        Ok(out) => match out.first() {
            Some(Tree::Token { ctx, .. }) => call_context = *ctx,
            _ => break None,
        },
        Err(err) => break Some(err),
    }
};
assert_eq!(err, Some(ExpandError::RecursionLimit { limit: 4 }));
# Ok::<(), Box<dyn std::error::Error>>(())
```

### `Expander::limit`

```rust,ignore
pub const fn limit(&self) -> u32
```

Returns the recursion limit — the budget's `max_depth`.

```rust
use macro_lang::Expander;

assert_eq!(Expander::<char>::with_limit(16).limit(), 16);
```

### `Expander::with_budget`

```rust,ignore
pub fn with_budget(budget: Budget) -> Expander<K>
```

*Added in 1.1.0.* Creates an expander that enforces `budget`.

```rust
use macro_lang::{Budget, Expander};

let budget = Budget::DEFAULT.with_max_expansions(10_000).with_max_tokens(100_000);
let expander: Expander<char> = Expander::with_budget(budget);
assert_eq!(expander.budget(), budget);
assert_eq!(expander.limit(), 128);
```

### `Expander::budget`

```rust,ignore
pub const fn budget(&self) -> Budget
```

*Added in 1.1.0.* Returns the budget the expander enforces.

```rust
use macro_lang::{Budget, Expander};

assert_eq!(Expander::<char>::new().budget(), Budget::DEFAULT);
assert_eq!(Expander::<char>::with_limit(9).budget(), Budget::DEFAULT.with_max_depth(9));
```

### `Expander::set_budget`

```rust,ignore
pub fn set_budget(&mut self, budget: Budget)
```

*Added in 1.1.0.* Replaces the budget from now on. Work already done stays
counted — the expansion and token limits apply to the [`usage`](#expanderusage)
since the last [reset](#expanderreset_usage) — so raising them lets the current
unit of work continue, and lowering them below what it has already done makes
every further expansion fail with `ExpandError::Budget`.

```rust
use intern_lang::Interner;
use macro_lang::{Budget, Context, ExpandError, Expander, Limit, Macro, Rule};
use token_lang::Span;

let mut names = Interner::new();
let empty = Macro::<char>::new(names.intern("empty"), vec![Rule { pattern: vec![], template: vec![] }])?;
let s = Span::new(0, 1);

let mut expander = Expander::with_budget(Budget::DEFAULT.with_max_expansions(1));
assert!(expander.expand(&empty, &[], s, Context::ROOT).is_ok());
assert_eq!(expander.expand(&empty, &[], s, Context::ROOT),
           Err(ExpandError::Budget(Limit::Expansions { max: 1 })));

expander.set_budget(Budget::DEFAULT.with_max_expansions(2));
assert!(expander.expand(&empty, &[], s, Context::ROOT).is_ok());
# Ok::<(), Box<dyn std::error::Error>>(())
```

### `Expander::usage`

```rust,ignore
pub const fn usage(&self) -> Usage
```

*Added in 1.1.0.* Returns the work charged against the budget since the
expander was created or [`reset_usage`](#expanderreset_usage) was last called:
successful expansions and the tokens they wrote. Failed expansions are never
charged.

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Macro, Rule, Template, Usage};
use token_lang::Span;

let mut names = Interner::new();
let s = Span::new(0, 1);
let three = Macro::new(names.intern("three"), vec![Rule {
    pattern: vec![],
    template: "abc".chars().map(|c| Template::token(c, s)).collect(),
}])?;

let mut expander = Expander::new();
assert_eq!(expander.usage(), Usage::NONE);
expander.expand(&three, &[], s, Context::ROOT)?;
assert_eq!((expander.usage().expansions, expander.usage().tokens), (1, 3));
# Ok::<(), Box<dyn std::error::Error>>(())
```

### `Expander::reset_usage`

```rust,ignore
pub fn reset_usage(&mut self)
```

*Added in 1.1.0.* Zeroes the [`usage`](#expanderusage) counters, so the next
unit of work gets the full budget again. Nothing else changes: the budget,
every minted context, and every [`Origin`](#origin) stay as they were, so trees
produced before the reset remain valid, and nesting depth (which is hygiene,
not usage) is not reset either. This is how a long-lived host — a language
server, a REPL, a watch-mode compiler — keeps the budget per unit of work
rather than per process: it calls `reset_usage()` once per document parse, per
REPL entry, or per compilation (see
[bounding untrusted macros](#guide-bounding-untrusted-macros)).

Because minted contexts stay valid, the hygiene table is not shrunk: it keeps a
record of every expansion and context since the expander was created, and its
memory grows with every expansion whether or not usage is reset. A long-lived
host should use one expander per compilation (per document version in a
language server) and drop it with the trees it produced, rather than keep one
expander for the whole session. An expander kept for billions of expansions
eventually reports `ExpandError::ContextOverflow`.

```rust
use intern_lang::Interner;
use macro_lang::{Budget, Context, ExpandError, Expander, Limit, Macro, Rule, Template, Usage};
use token_lang::Span;

let mut names = Interner::new();
let s = Span::new(0, 1);
let one = Macro::new(names.intern("one"), vec![Rule {
    pattern: vec![],
    template: vec![Template::token('k', s)],
}])?;

// A REPL that allows two expansions per entry, on one expander for the session.
let mut expander = Expander::with_budget(Budget::DEFAULT.with_max_expansions(2));
for _entry in 0..3 {
    expander.reset_usage();
    expander.expand(&one, &[], s, Context::ROOT)?;
    expander.expand(&one, &[], s, Context::ROOT)?;
    assert_eq!(expander.expand(&one, &[], s, Context::ROOT),
               Err(ExpandError::Budget(Limit::Expansions { max: 2 })));
}
expander.reset_usage();
assert_eq!(expander.usage(), Usage::NONE);
# Ok::<(), Box<dyn std::error::Error>>(())
```

### `Expander::expand`

```rust,ignore
pub fn expand(
    &mut self,
    mac: &Macro<K>,
    input: &[Tree<K>],
    call_site: Span,
    call_context: Context,
) -> Result<Vec<Tree<K>>, ExpandError>
where
    K: Clone + PartialEq,
```

Expands one invocation of `mac`. The rules are tried in order against `input`;
the first rule whose pattern matches all of it is transcribed. Every literal
token the template writes receives a context minted for this expansion; every
captured tree is substituted with its contexts untouched.

**Parameters**

- `mac` — the macro being invoked.
- `input` — the invocation's argument trees: the contents between its
  delimiters, without the delimiters.
- `call_site` — the span of the whole invocation. Recorded in the
  [`Origin`]#origin of every minted context, and reported by
  [`ExpandError::NoMatch`]#expanderror for an empty invocation.
- `call_context` — the context of the invocation itself, normally that of the
  macro-name token. [`Context::ROOT`]#context for an invocation written in
  source; the name token's context for one produced by an earlier expansion, so
  nesting depth is tracked.

`expand` is [`expand_at`](#expanderexpand_at) with a depth of `0`: the
invocation's depth is inferred from hygiene alone — from `call_context` and the
contexts of the input tokens. An invocation rebuilt entirely from captured
source tokens looks exactly like the original call, so for that shape the
[expansion and token budgets](#termination-and-budgets), not the depth limit,
are what stop a runaway macro. A driver that knows where it found each
invocation should use `expand_at`.

**Returns** the expanded trees.

**Errors**

| Error | Cause |
|---|---|
| [`RecursionLimit`]#expanderror | The invocation is nested deeper than the [limit]#expanderlimit. |
| [`Budget`]#expanderror | The expander has already performed `max_expansions` expansions, or this one would take the tokens written past `max_tokens`. The token check is made before each token is written, so an oversized expansion is refused without building its output. |
| [`NoMatch`]#expanderror | No rule matches. |
| [`Ambiguous`]#expanderror | A rule matches in more than one way. |
| [`RepetitionMismatch`]#expanderror | Lockstep metavariables captured different numbers of trees. |
| [`ContextOverflow`]#expanderror | The context space is exhausted. |

On error the expander is left exactly as it was: no contexts are minted and no
expansion is recorded.

Rule selection — rules are tried in order:

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Fragment, Macro, Pattern, Rule, Template, Tree};
use token_lang::Span;
# fn trees(src: &str) -> Vec<Tree<char>> {
#     src.char_indices().map(|(i, c)| Tree::token(c, Span::new(i as u32, i as u32 + 1))).collect()
# }
# fn text(trees: &[Tree<char>]) -> String {
#     trees.iter().map(|t| match t {
#         Tree::Token { token, .. } => token.kind.to_string(),
#         Tree::Group { open, close, trees } => format!("{}{}{}", open.kind, text(trees), close.kind),
#     }).collect()
# }

let mut names = Interner::new();
let x = names.intern("x");
let s = Span::new(0, 1);
let pick = Macro::new(names.intern("pick"), vec![
    Rule { pattern: vec![Pattern::Token('a')], template: vec![Template::token('A', s)] },
    Rule {
        pattern: vec![Pattern::Bind { name: x, fragment: Fragment::Tree }],
        template: vec![Template::Var(x), Template::Var(x)],
    },
])?;

let mut expander = Expander::new();
assert_eq!(text(&expander.expand(&pick, &trees("a"), s, Context::ROOT)?), "A");
assert_eq!(text(&expander.expand(&pick, &trees("b"), s, Context::ROOT)?), "bb");
# Ok::<(), Box<dyn std::error::Error>>(())
```

Re-expanding output that contains another invocation — pass the context of the
inner invocation's name token as `call_context`:

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Macro, Rule, Template, Tree};
use token_lang::Span;

let mut names = Interner::new();
let s = Span::new(0, 1);
// outer!() => inner      inner!() => done
let outer = Macro::new(names.intern("outer"), vec![Rule { pattern: vec![], template: vec![Template::token('i', s)] }])?;
let inner = Macro::new(names.intern("inner"), vec![Rule { pattern: vec![], template: vec![Template::token('d', s)] }])?;

let mut expander = Expander::new();
let first = expander.expand(&outer, &[], s, Context::ROOT)?;
let Some(Tree::Token { ctx: inner_call, .. }) = first.first() else {
    return Err("expected one token".into());
};
let second = expander.expand(&inner, &[], s, *inner_call)?;
let Some(Tree::Token { ctx, .. }) = second.first() else {
    return Err("expected one token".into());
};
// The second expansion records that it was invoked from code the first produced.
assert_eq!(expander.origin(*ctx).ok_or("unknown context")?.call_context, *inner_call);
# Ok::<(), Box<dyn std::error::Error>>(())
```

### `Expander::expand_at`

```rust,ignore
pub fn expand_at(
    &mut self,
    mac: &Macro<K>,
    input: &[Tree<K>],
    call_site: Span,
    call_context: Context,
    depth: u32,
) -> Result<Vec<Tree<K>>, ExpandError>
where
    K: Clone + PartialEq,
```

*Added in 1.1.0.* [`expand`](#expanderexpand), with the driver also saying
where it found the invocation. The invocation runs one level deeper than the
deepest of `depth`, the expansion that produced `call_context`, and the
expansions that produced any input token; so the recursion limit bounds every
chain of nested expansions the driver performs, whatever the macros write —
including a macro that rebuilds its own invocation from captured tokens.

**Parameters**

As for [`expand`](#expanderexpand), plus:

- `depth` — the nesting depth of the code the invocation was found in: `0` for
  source, and `d + 1` for an invocation found in the output of a call made with
  depth `d`.

**Errors** as for [`expand`](#expanderexpand); on error the expander is left
exactly as it was.

`again!($n:tt $b:tt) => $n $b ($n $b)` rebuilds its own invocation from
captured tokens, so hygiene sees no nesting at all; the tracked depth still
stops it:

```rust
use intern_lang::Interner;
use macro_lang::{Context, ExpandError, Expander, Fragment, Macro, Pattern, Rule, Template, Tree};
use token_lang::{Span, Token};

let mut names = Interner::new();
let (n, b) = (names.intern("n"), names.intern("b"));
let tt = |name| Pattern::Bind { name, fragment: Fragment::Tree };
let s = Span::new(0, 1);
let again = Macro::new(names.intern("again"), vec![Rule {
    pattern: vec![tt(n), tt(b)],
    template: vec![
        Template::Var(n),
        Template::Var(b),
        Template::Group {
            open: Token::new('(', s),
            close: Token::new(')', s),
            body: vec![Template::Var(n), Template::Var(b)],
        },
    ],
}])?;

let mut expander = Expander::with_limit(8);
// `a ! (a !)`, with `a` naming `again`: its arguments are `a !`.
let mut args = vec![Tree::token('a', s), Tree::token('!', s)];
let mut depth = 0;
let err = loop {
    match expander.expand_at(&again, &args, s, Context::ROOT, depth) {
        Ok(out) => match out.get(2) {
            // The output invokes `again` once more: recurse into it.
            Some(Tree::Group { trees, .. }) => { args = trees.clone(); depth += 1; }
            _ => break None,
        },
        Err(err) => break Some(err),
    }
};
assert_eq!(err, Some(ExpandError::RecursionLimit { limit: 8 }));
# Ok::<(), Box<dyn std::error::Error>>(())
```

### `Expander::origin`

```rust,ignore
pub fn origin(&self, ctx: Context) -> Option<Origin>
```

Reports which expansion minted `ctx`, or `None` for [`Context::ROOT`](#context)
and for contexts this expander did not mint.

**Parameters**

- `ctx` — the context to look up.

**Returns** the [`Origin`](#origin): the context the token had in the macro
definition, the macro's name, the call site, and the call's own context.

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Macro, Rule, Template, Tree};
use token_lang::Span;

let mut names = Interner::new();
let make_tmp = names.intern("make_tmp");
let mac = Macro::new(make_tmp, vec![Rule {
    pattern: vec![],
    template: vec![Template::token('t', Span::new(40, 41))],
}])?;

let mut expander = Expander::new();
let out = expander.expand(&mac, &[], Span::new(3, 14), Context::ROOT)?;
let Some(Tree::Token { ctx, .. }) = out.first() else {
    return Err("expected one token".into());
};

let origin = expander.origin(*ctx).ok_or("not minted here")?;
assert_eq!(origin.macro_name, make_tmp);
assert_eq!(origin.call_site, Span::new(3, 14));
assert_eq!(origin.parent, Context::ROOT);
assert_eq!(expander.origin(Context::ROOT), None);
# Ok::<(), Box<dyn std::error::Error>>(())
```

Building an expansion backtrace by following `call_context` outward:

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Macro, Rule, Template, Tree};
use token_lang::Span;

let mut names = Interner::new();
let s = Span::new(0, 1);
let step = Macro::new(names.intern("step"), vec![Rule { pattern: vec![], template: vec![Template::token('s', s)] }])?;

// Expand three levels deep, each call made from the previous output.
let mut expander = Expander::new();
let mut call = Context::ROOT;
for _ in 0..3 {
    let out = expander.expand(&step, &[], s, call)?;
    let Some(Tree::Token { ctx, .. }) = out.first() else {
        return Err("expected one token".into());
    };
    call = *ctx;
}

let mut depth = 0;
let mut ctx = call;
while let Some(origin) = expander.origin(ctx) {
    depth += 1;
    ctx = origin.call_context;
}
assert_eq!(depth, 3);
# Ok::<(), Box<dyn std::error::Error>>(())
```

---

## `Budget`

```rust,ignore
#[non_exhaustive]
pub struct Budget {
    pub max_depth: u32,
    pub max_expansions: usize,
    pub max_tokens: usize,
}
```

*Added in 1.1.0.* The limits an [`Expander`](#expander) enforces on the work it
does. Every expander has one; [`Expander::new`](#expandernew) uses
[`Budget::DEFAULT`](#budgetdefault).

| Field | Limits | Default | Exceeding it reports |
|---|---|---:|---|
| `max_depth` | How deeply invocations may nest (source is depth 1). | 128 | [`ExpandError::RecursionLimit`]#expanderror |
| `max_expansions` | Successful expansions since creation or the last `reset_usage`. | 1 048 576 (`2^20`) | [`ExpandError::Budget`]#expanderror`(Limit::Expansions { .. })` |
| `max_tokens` | Tokens written since creation or the last `reset_usage`. | 4 194 304 (`2^22`) | [`ExpandError::Budget`]#expanderror`(Limit::Tokens { .. })` |

- **Cumulative until reset.** The expansion and token counts accumulate in
  the expander's [`Usage`]#usage. A batch compiler (one expander per
  compilation) never resets, and the budget bounds the compilation's total
  macro output. A long-lived host — language server, REPL, watch-mode
  compiler — calls [`reset_usage`]#expanderreset_usage once per unit of work,
  so the budget bounds each document, entry, or rebuild rather than the
  process. A unit that legitimately needs more raises the budget with
  [`set_budget`]#expanderset_budget.
- **How tokens are counted.** One per token written, two per group (its
  delimiters), and every token of a substituted capture. Each token is charged
  *before* it is written, so an expansion that would pass `max_tokens` stops at
  the limit instead of first building its output — even a single enormous
  capture is refused without being copied.
- **Failures are free.** A failed expansion is not charged; like every error,
  it leaves the expander exactly as it was.
- **What the defaults cost.** They are well above what typical programs
  expand to, and they stop a runaway macro quickly. Measured with
  `examples/budget` (release build, Windows x86_64, an 8-byte token kind): a
  doubling macro stops in about 0.2 s with a whole-process peak of about
  400 MB, and a self-rebuilding macro under plain `expand` in about 0.15 s.
  Memory grows in proportion to `max_tokens` and the size of the token kind;
  lower `max_tokens` where that is too much.

`#[non_exhaustive]`: further limits may be added in a minor release. Start from
`Budget::DEFAULT` (or `Budget::default()`) and adjust it with the `with_*`
methods, or assign the public fields.

**Trait implementations:** `Clone`, `Copy`, `Debug`, `Default` (=
`Budget::DEFAULT`), `PartialEq`, `Eq`, `Hash`.

```rust
use macro_lang::{Budget, Expander};

let mut budget = Budget::DEFAULT.with_max_depth(32);
budget.max_tokens = 1 << 16;
let expander: Expander<char> = Expander::with_budget(budget);
assert_eq!(expander.limit(), 32);
assert_eq!(expander.budget().max_tokens, 65_536);
```

### `Budget::DEFAULT`

```rust,ignore
pub const DEFAULT: Budget = Budget { max_depth: 128, max_expansions: 1 << 20, max_tokens: 1 << 22 };
```

The budget [`Expander::new`](#expandernew) uses.

```rust
use macro_lang::Budget;

assert_eq!(Budget::DEFAULT.max_depth, 128);
assert_eq!(Budget::DEFAULT.max_expansions, 1 << 20);
assert_eq!(Budget::DEFAULT.max_tokens, 1 << 22);
assert_eq!(Budget::default(), Budget::DEFAULT);
```

### `Budget::with_max_depth`, `with_max_expansions`, `with_max_tokens`

```rust,ignore
pub const fn with_max_depth(self, max_depth: u32) -> Budget
pub const fn with_max_expansions(self, max_expansions: usize) -> Budget
pub const fn with_max_tokens(self, max_tokens: usize) -> Budget
```

Return the budget with one field replaced. `const`, so a budget can be a
constant.

```rust
use macro_lang::Budget;

const EDITOR: Budget = Budget::DEFAULT.with_max_expansions(10_000).with_max_tokens(100_000);
assert_eq!(EDITOR.max_depth, 128);
assert_eq!(EDITOR.max_expansions, 10_000);
```

---

## `Limit`

```rust,ignore
#[non_exhaustive]
pub enum Limit {
    Expansions { max: usize },
    Tokens { max: usize },
}
```

*Added in 1.1.0.* Which part of a [`Budget`](#budget) an expansion ran out of,
carried by [`ExpandError::Budget`](#expanderror). `max` is the limit that was
reached. Depth is not listed: exceeding `max_depth` keeps reporting
`ExpandError::RecursionLimit`, as it did before budgets existed.
`#[non_exhaustive]`: further limits may be added in a minor release.

**Trait implementations:** `Clone`, `Copy`, `Debug`, `PartialEq`, `Eq`, `Hash`,
`Display` (`"<max> expansions"`, `"<max> output tokens"`).

```rust
use intern_lang::Interner;
use macro_lang::{Budget, Context, ExpandError, Expander, Limit, Macro, Rule, Template};
use token_lang::Span;

let mut names = Interner::new();
let s = Span::new(0, 1);
// three!() => a b c
let three = Macro::new(names.intern("three"), vec![Rule {
    pattern: vec![],
    template: "abc".chars().map(|c| Template::token(c, s)).collect(),
}])?;

let mut expander = Expander::with_budget(Budget::DEFAULT.with_max_tokens(5));
assert!(expander.expand(&three, &[], s, Context::ROOT).is_ok());
// Three more tokens would make six.
assert_eq!(expander.expand(&three, &[], s, Context::ROOT),
           Err(ExpandError::Budget(Limit::Tokens { max: 5 })));
# Ok::<(), Box<dyn std::error::Error>>(())
```

---

## `Usage`

```rust,ignore
#[non_exhaustive]
pub struct Usage {
    pub expansions: usize,
    pub tokens: usize,
}
```

*Added in 1.1.0.* The work an [`Expander`](#expander) has charged against its
[`Budget`](#budget) since it was created or
[`reset_usage`](#expanderreset_usage) was last called: successful expansions
(compared against `max_expansions`) and the tokens they wrote (compared against
`max_tokens`, counted the same way). Returned by
[`Expander::usage`](#expanderusage). `Usage::NONE` is both counters zero.
`#[non_exhaustive]`: further counters may be added in a minor release.

**Trait implementations:** `Clone`, `Copy`, `Debug`, `Default` (=
`Usage::NONE`), `PartialEq`, `Eq`, `Hash`.

```rust
use macro_lang::{Expander, Usage};

let expander: Expander<char> = Expander::new();
assert_eq!(expander.usage(), Usage::NONE);
assert_eq!(Usage::default(), Usage::NONE);
```

---

## `Context`

```rust,ignore
pub struct Context(/* private */);

impl Context {
    pub const ROOT: Context;
    pub const fn is_root(self) -> bool;
    pub const fn as_u32(self) -> u32;
}
```

The hygiene context of a token: which expansion, if any, introduced it. A small
copyable handle. Two identifiers with the same spelling denote the same binding
only if their contexts are equal too.

**Items**

- `ROOT` — the context of tokens written directly in source. Also the `Default`.
- `is_root()` — `true` for `ROOT`.
- `as_u32()` — the raw index. Contexts are numbered densely from `0` in the
  order an expander mints them, so the index works as a key into a side table.

Contexts are only meaningful to the expander that minted them.

**Trait implementations:** `Clone`, `Copy`, `Debug`, `Default`, `PartialEq`,
`Eq`, `PartialOrd`, `Ord`, `Hash`, `Display` (formats as `#n`, the notation
rustc uses).

```rust
use macro_lang::Context;

assert!(Context::ROOT.is_root());
assert_eq!(Context::default(), Context::ROOT);
assert_eq!(Context::ROOT.as_u32(), 0);
assert_eq!(Context::ROOT.to_string(), "#0");
```

Every expansion mints its own context, so two expansions of the same macro never
share one:

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Macro, Rule, Template, Tree};
use token_lang::Span;

let mut names = Interner::new();
let s = Span::new(0, 1);
let mac = Macro::new(names.intern("t"), vec![Rule { pattern: vec![], template: vec![Template::token('t', s)] }])?;

let mut expander = Expander::new();
let ctx_of = |out: &[Tree<char>]| match out.first() {
    Some(Tree::Token { ctx, .. }) => *ctx,
    _ => Context::ROOT,
};
let a = ctx_of(&expander.expand(&mac, &[], s, Context::ROOT)?);
let b = ctx_of(&expander.expand(&mac, &[], s, Context::ROOT)?);
assert_ne!(a, b);
assert!(!a.is_root() && !b.is_root());
# Ok::<(), Box<dyn std::error::Error>>(())
```

---

## `Origin`

```rust,ignore
#[non_exhaustive]
pub struct Origin {
    pub parent: Context,
    pub macro_name: Symbol,
    pub call_site: Span,
    pub call_context: Context,
}
```

Where a minted [`Context`](#context) came from, returned by
[`Expander::origin`](#expanderorigin).

**Fields**

- `parent` — the context the token had in the macro definition, before this
  expansion marked it. `ROOT` for a macro defined in source. A resolver falls
  back to it for a macro-introduced name the expansion does not bind (a call to a
  global function from inside a template), so the name resolves where the macro
  was defined.
- `macro_name` — the macro whose expansion introduced the token.
- `call_site` — the span of the invocation that triggered the expansion.
- `call_context` — the context of the invocation itself. Following it outward
  walks the chain of nested expansions.

`#[non_exhaustive]`: only the expander builds an `Origin`, and fields may be
added in a minor release. Read the fields directly, or destructure with `..`.

**Trait implementations:** `Clone`, `Copy`, `Debug`, `PartialEq`, `Eq`, `Hash`.

A macro defined by another macro's expansion carries the definition context in
its template tokens, and `parent` reports it:

```rust
use intern_lang::Interner;
use macro_lang::{Context, Expander, Macro, Rule, Template, Tree};
use token_lang::Span;

let mut names = Interner::new();
let s = Span::new(0, 1);
let mut expander = Expander::new();

// An outer expansion produces a token `d` with a minted context...
let outer = Macro::new(names.intern("outer"), vec![Rule { pattern: vec![], template: vec![Template::token('d', s)] }])?;
let produced = expander.expand(&outer, &[], s, Context::ROOT)?;
let Some(Tree::Token { token, ctx: def_ctx }) = produced.first() else {
    return Err("expected one token".into());
};

// ...and a macro whose template is built from that output keeps the context.
let inner = Macro::new(names.intern("inner"), vec![Rule {
    pattern: vec![],
    template: vec![Template::Token { token: *token, ctx: *def_ctx }],
}])?;
let out = expander.expand(&inner, &[], s, Context::ROOT)?;
let Some(Tree::Token { ctx, .. }) = out.first() else {
    return Err("expected one token".into());
};
assert_eq!(expander.origin(*ctx).ok_or("unknown context")?.parent, *def_ctx);
# Ok::<(), Box<dyn std::error::Error>>(())
```

---

## `MacroError`

```rust,ignore
#[non_exhaustive]
pub enum MacroError {
    NoRules,
    DuplicateBinding { rule: usize, name: Symbol },
    UnboundVariable { rule: usize, name: Symbol },
    StillRepeating { rule: usize, name: Symbol },
    NoRepeatingVariable { rule: usize },
    EmptyRepetition { rule: usize },
    OptionalSeparator { rule: usize },
}
```

Why [`Macro::new`](#macronew) rejected a definition. `rule` is the index of the
offending rule in the list passed to `Macro::new`; `name` is the metavariable's
`Symbol`, which the caller resolves through its interner. The enum is
`#[non_exhaustive]`: later releases may add checks.

| Variant | Meaning | Fix |
|---|---|---|
| `NoRules` | The macro has no rules. | Give it at least one rule. |
| `DuplicateBinding` | A pattern binds `name` twice. | Rename one binding. |
| `UnboundVariable` | A template uses `name`, which the pattern never binds. | Bind it, or write the token literally. |
| `StillRepeating` | `name` repeats in the pattern but is used outside enough template repetitions. | Wrap the use in a `Template::Repeat`. |
| `NoRepeatingVariable` | A template repetition contains no metavariable repeating at its depth. | Use a repeating metavariable inside, or remove the repetition. |
| `EmptyRepetition` | A pattern repetition can match without consuming input. | Give the body a token, group, or binding that must be present. |
| `OptionalSeparator` | A `?` repetition has a separator. | Remove the separator. |

**Trait implementations:** `Clone`, `Copy`, `Debug`, `PartialEq`, `Eq`, `Hash`,
`Display`, `core::error::Error`.

```rust
use intern_lang::Interner;
use macro_lang::{Fragment, Kleene, Macro, MacroError, Pattern, Rule, Template};

let mut names = Interner::new();
let (x, m) = (names.intern("x"), names.intern("m"));
let bind = |name| Pattern::Bind { name, fragment: Fragment::Tree };

let unbound = Macro::<char>::new(m, vec![Rule { pattern: vec![], template: vec![Template::Var(x)] }]);
assert_eq!(unbound.unwrap_err(), MacroError::UnboundVariable { rule: 0, name: x });

let duplicate = Macro::<char>::new(m, vec![Rule { pattern: vec![bind(x), bind(x)], template: vec![] }]);
assert_eq!(duplicate.unwrap_err(), MacroError::DuplicateBinding { rule: 0, name: x });

let empty = Macro::<char>::new(m, vec![Rule {
    pattern: vec![Pattern::Repeat { body: vec![], separator: None, kleene: Kleene::ZeroOrMore }],
    template: vec![],
}]);
assert_eq!(empty.unwrap_err(), MacroError::EmptyRepetition { rule: 0 });
```

Rendering a definition error with the metavariable's name:

```rust
use intern_lang::Interner;
use macro_lang::{Macro, MacroError, Rule, Template};

let mut names = Interner::new();
let x = names.intern("value");
let err = Macro::<char>::new(names.intern("m"), vec![Rule { pattern: vec![], template: vec![Template::Var(x)] }])
    .unwrap_err();

let message = match err {
    MacroError::UnboundVariable { rule, name } => format!(
        "rule {rule}: `${}` is not bound by the pattern",
        names.resolve(name).unwrap_or("?")
    ),
    other => other.to_string(),
};
assert_eq!(message, "rule 0: `$value` is not bound by the pattern");
```

---

## `ExpandError`

```rust,ignore
#[non_exhaustive]
pub enum ExpandError {
    NoMatch { span: Span },
    Ambiguous { rule: usize },
    RepetitionMismatch { rule: usize },
    RecursionLimit { limit: u32 },
    Budget(Limit), // 1.1.0
    ContextOverflow,
}
```

Why [`Expander::expand`](#expanderexpand) could not expand an invocation. A failed
expansion leaves the expander unchanged. The enum is `#[non_exhaustive]`.

| Variant | Meaning | What to do |
|---|---|---|
| `NoMatch { span }` | No rule matched. `span` is the token the furthest-reaching rule could not get past, or an empty span just past the input if every rule wanted more (the `call_site` for an empty invocation). | Report "no rules expected this token" at `span`. |
| `Ambiguous { rule }` | Rule `rule` matched in more than one way. Rules after it are not tried. | Make the rule unambiguous, typically with a separator or literal between repetitions. |
| `RepetitionMismatch { rule }` | Lockstep metavariables captured different numbers of trees. | Match them in one pattern repetition, or expand them separately. |
| `RecursionLimit { limit }` | Nesting exceeded the limit (`Budget::max_depth`). | Fix a macro with no base case, or raise the limit. |
| `Budget(limit)` | *Added in 1.1.0.* The expander ran out of expansions or tokens; [`limit`]#limit says which, and its value. | Fix a macro whose output grows without bound (one that doubles its input, or expands into several calls of itself), or raise the budget with [`set_budget`]#expanderset_budget. A long-lived host that never calls [`reset_usage`]#expanderreset_usage eventually hits this on legitimate input: reset per unit of work. |
| `ContextOverflow` | All `u32::MAX` contexts are in use. | Start a fresh expander. |

**Trait implementations:** `Clone`, `Copy`, `Debug`, `PartialEq`, `Eq`, `Hash`,
`Display`, `core::error::Error`.

`NoMatch` pointing at the offending token:

```rust
use intern_lang::Interner;
use macro_lang::{Context, ExpandError, Expander, Macro, Pattern, Rule};
use token_lang::Span;
# use macro_lang::Tree;
# fn trees(src: &str) -> Vec<Tree<char>> {
#     src.char_indices().map(|(i, c)| Tree::token(c, Span::new(i as u32, i as u32 + 1))).collect()
# }

let mut names = Interner::new();
let abc = Macro::new(names.intern("abc"), vec![Rule {
    pattern: vec![Pattern::Token('a'), Pattern::Token('b'), Pattern::Token('c')],
    template: vec![],
}])?;

let mut expander = Expander::new();
let call = Span::new(0, 20);
// Stuck at `x`, the third token.
assert_eq!(expander.expand(&abc, &trees("abx"), call, Context::ROOT),
           Err(ExpandError::NoMatch { span: Span::new(2, 3) }));
// The input ended early: an empty span just past `b`.
assert_eq!(expander.expand(&abc, &trees("ab"), call, Context::ROOT),
           Err(ExpandError::NoMatch { span: Span::empty(2) }));
# Ok::<(), Box<dyn std::error::Error>>(())
```

`Ambiguous` and `RepetitionMismatch`:

```rust
use intern_lang::Interner;
use macro_lang::{Context, ExpandError, Expander, Fragment, Kleene, Macro, Pattern, Rule, Template};
use token_lang::Span;
# use macro_lang::Tree;
# fn trees(src: &str) -> Vec<Tree<char>> {
#     src.char_indices().map(|(i, c)| Tree::token(c, Span::new(i as u32, i as u32 + 1))).collect()
# }

let mut names = Interner::new();
let (a, b) = (names.intern("a"), names.intern("b"));
let star = |name| Pattern::Repeat {
    body: vec![Pattern::Bind { name, fragment: Fragment::Tree }],
    separator: None,
    kleene: Kleene::ZeroOrMore,
};
let s = Span::new(0, 1);
let mut expander = Expander::new();

// $($a:tt)* $($b:tt)* — where does `a` end?
let split = Macro::new(names.intern("split"), vec![Rule { pattern: vec![star(a), star(b)], template: vec![] }])?;
assert_eq!(expander.expand(&split, &trees("xy"), s, Context::ROOT),
           Err(ExpandError::Ambiguous { rule: 0 }));

// $($a:tt)* ; $($b:tt)*  =>  $( $a $b )* — needs equal counts.
let zip = Macro::new(names.intern("zip"), vec![Rule {
    pattern: vec![star(a), Pattern::Token(';'), star(b)],
    template: vec![Template::Repeat { body: vec![Template::Var(a), Template::Var(b)], separator: None }],
}])?;
assert!(expander.expand(&zip, &trees("ab;cd"), s, Context::ROOT).is_ok());
assert_eq!(expander.expand(&zip, &trees("ab;c"), s, Context::ROOT),
           Err(ExpandError::RepetitionMismatch { rule: 0 }));
# Ok::<(), Box<dyn std::error::Error>>(())
```

---

## Feature flags

| Feature | Default | Effect |
|---|---|---|
| `std` | yes | Links the standard library. Without it the crate is `#![no_std]` and needs only `alloc`; behaviour is identical. |

```toml
[dependencies]
macro-lang = { version = "1", default-features = false }   # no_std + alloc
```

---

## Guide: writing a front end

macro-lang does the matching, substitution, and hygiene; a language supplies the
four pieces around it. The [`examples/common`](../examples/common/mod.rs) module
implements all four for a small toy language and is the recommended starting
point.

1. **Lex** source into `token_lang::Token<Kind>`. Make `Kind` small and `Copy`;
   carry identifiers and literals as interned `Symbol`s.
2. **Group** the token stream into [`Tree`]#trees by matching delimiters,
   giving every token [`Context::ROOT`]#context (see the
   [grouping example]#tree).
3. **Lower macro definitions.** Parse your language's macro syntax — for example
   `($a:ident, $b:ident) => { ... }` — into [`Pattern`]#patterns and
   [`Template`]#templates, mapping fragment specifiers to
   [`Fragment`]#fragments (`tt` → `Fragment::Tree`, `ident` →
   `Fragment::Kind(Kind::is_ident)`), and build the [`Macro`]#macro. When a
   definition itself came out of an expansion, copy each token's context into
   `Template::Token { ctx, .. }` — and each repetition separator's into
   `Template::RepeatSeparated { ctx, .. }` — so nested hygiene is preserved.
4. **Drive expansion.** Walk the trees; at each invocation (say, an identifier
   naming a macro, `!`, and a group), resolve the macro, call
   [`expand`]#expanderexpand with the group's contents, the invocation span,
   and the **name token's context**, splice the output in place of the
   invocation, and rescan from the same position so invocations in the output
   are expanded too. Stop when none remain. A driver that instead expands each
   output before splicing it knows how deep every invocation sits; it should
   call [`expand_at`]#expanderexpand_at with that depth (see
   [bounding untrusted macros]#guide-bounding-untrusted-macros).

A compact driver for a single macro, invoked as `!(...)`:

```rust
use intern_lang::Interner;
use macro_lang::{Context, ExpandError, Expander, Fragment, Kleene, Macro, Pattern, Rule, Template, Tree};
use token_lang::{Span, Token};

/// Expands every `!( ... )` invocation of `mac` in `stream` to a fixed point.
fn drive(expander: &mut Expander<char>, mac: &Macro<char>, stream: &mut Vec<Tree<char>>) -> Result<(), ExpandError> {
    let mut i = 0;
    while i + 1 < stream.len() {
        let call = match (&stream[i], &stream[i + 1]) {
            (Tree::Token { token, ctx }, Tree::Group { trees, close, .. }) if token.kind == '!' => {
                Some((trees.clone(), token.span.merge(close.span), *ctx))
            }
            _ => None,
        };
        match call {
            Some((args, span, ctx)) => {
                let out = expander.expand(mac, &args, span, ctx)?;
                let _replaced: Vec<_> = stream.splice(i..i + 2, out).collect();
            }
            None => i += 1,
        }
    }
    Ok(())
}

// count!()               => 0
// count!($x $($rest)*)   => 1 + count!($($rest)*)
let mut names = Interner::new();
let (x, rest) = (names.intern("x"), names.intern("rest"));
let s = Span::new(0, 1);
let count = Macro::new(names.intern("count"), vec![
    Rule { pattern: vec![], template: vec![Template::token('0', s)] },
    Rule {
        pattern: vec![
            Pattern::Bind { name: x, fragment: Fragment::Tree },
            Pattern::Repeat {
                body: vec![Pattern::Bind { name: rest, fragment: Fragment::Tree }],
                separator: None,
                kleene: Kleene::ZeroOrMore,
            },
        ],
        template: vec![
            Template::token('1', s),
            Template::token('+', s),
            Template::token('!', s),
            Template::Group {
                open: Token::new('(', s),
                close: Token::new(')', s),
                body: vec![Template::Repeat { body: vec![Template::Var(rest)], separator: None }],
            },
        ],
    },
])?;

let mut stream = vec![
    Tree::token('!', s),
    Tree::Group {
        open: Token::new('(', s),
        close: Token::new(')', s),
        trees: vec![Tree::token('a', s), Tree::token('b', s)],
    },
];
drive(&mut Expander::new(), &count, &mut stream)?;
let text: String = stream
    .iter()
    .filter_map(|t| match t { Tree::Token { token, .. } => Some(token.kind), _ => None })
    .collect();
assert_eq!(text, "1+1+0");
# Ok::<(), Box<dyn std::error::Error>>(())
```

---

## Guide: resolving names with contexts

Hygiene is enforced by the name resolver, using the contexts macro-lang records.
The rule is short:

1. **Bindings are keyed by `(Symbol, Context)`.** A `let tmp` written by a
   template binds `(tmp, #3)`; the caller's `let tmp` binds `(tmp, #0)`. They
   never collide.
2. **References look up their own `(Symbol, Context)` first.** A `tmp` written by
   the same expansion finds the template's binding; the caller's `tmp` finds the
   caller's.
3. **Free macro-introduced names fall back to the definition site.** If a
   reference `(name, ctx)` finds no binding and `ctx` is not root, retry with
   `expander.origin(ctx)?.parent` — the context the token had where the macro was
   defined. That is how a template can call a global function by name.

```rust
use std::collections::HashMap;

use intern_lang::{Interner, Symbol};
use macro_lang::{Context, Expander, Fragment, Macro, Pattern, Rule, Template, Tree};
use token_lang::Span;

/// Resolves `(name, ctx)` against `scope`, falling back to definition sites.
fn resolve(
    scope: &HashMap<(Symbol, Context), &'static str>,
    expander: &Expander<char>,
    name: Symbol,
    mut ctx: Context,
) -> Option<&'static str> {
    loop {
        if let Some(found) = scope.get(&(name, ctx)) {
            return Some(found);
        }
        ctx = expander.origin(ctx)?.parent;
    }
}

let mut names = Interner::new();
let tmp = names.intern("tmp");
let x = names.intern("x");

// introduce!($x:tt) => t $x — the template writes its own `t`.
let introduce = Macro::new(names.intern("introduce"), vec![Rule {
    pattern: vec![Pattern::Bind { name: x, fragment: Fragment::Tree }],
    template: vec![Template::token('t', Span::new(0, 1)), Template::Var(x)],
}])?;
let mut expander = Expander::new();
let out = expander.expand(&introduce, &[Tree::token('t', Span::new(5, 6))], Span::new(0, 7), Context::ROOT)?;
let ctx = |i: usize| match &out[i] { Tree::Token { ctx, .. } => *ctx, _ => Context::ROOT };
let (macro_t, caller_t) = (ctx(0), ctx(1));

// Suppose the template's `t` declared `tmp` and the caller's `t` is a use of
// the caller's own `tmp`. Each resolves to its own binding.
let mut scope = HashMap::new();
scope.insert((tmp, macro_t), "the macro's temporary");
scope.insert((tmp, Context::ROOT), "the caller's variable");
assert_eq!(resolve(&scope, &expander, tmp, macro_t), Some("the macro's temporary"));
assert_eq!(resolve(&scope, &expander, tmp, caller_t), Some("the caller's variable"));

// A name the template uses but never binds falls back to the definition site.
let global = names.intern("print");
scope.insert((global, Context::ROOT), "the global function");
assert_eq!(resolve(&scope, &expander, global, macro_t), Some("the global function"));
# Ok::<(), Box<dyn std::error::Error>>(())
```

---

## Guide: bounding untrusted macros

Every expander is bounded by default — no configuration is needed for a macro
to be unable to hang the compiler or exhaust memory. What a driver can add is
*precision*: which error a runaway macro gets, and how soon.

- **Track depth if you can.** A driver that recurses into each expansion's
  output before splicing it (the shape of rustc's expansion collector) knows
  the depth of the code it is scanning. Passing it to
  [`expand_at`]#expanderexpand_at makes the recursion limit hold for every
  chain of nested expansions, including a macro that rebuilds its own
  invocation from captured tokens (`m!($a:tt $b:tt) => $a $b ($a $b)` invoked
  as `m!(m !)`). [`examples/common`]../examples/common/mod.rs has such a
  driver, `Driver::expand_tracked`.
- **Rescanning drivers rely on hygiene and the budget.** A driver that splices
  and rescans the whole stream only has the tokens to go on. Depth is then
  inferred from the contexts of the name token and the input, which catches
  every macro that writes any literal token into the call it builds. A call
  rebuilt only from captured source tokens is indistinguishable from the
  original, and the expansion and token budgets stop it instead, in bounded
  time — about 0.15 s with the defaults on the benchmark machine.
- **Reset per unit of work in a long-lived host.** Usage accumulates until
  [`reset_usage`]#expanderreset_usage. A batch compiler never needs it; a
  language server, REPL, or watch-mode compiler that keeps one expander calls
  `reset_usage()` before each document parse, REPL entry, or rebuild, so the
  budget bounds each unit of work, and one hostile document cannot use up the
  budget of the documents after it. Contexts and origins survive the reset.
  `examples/budget.rs` shows the loop.
- **Size the budget to the environment.** The defaults suit a batch compiler.
  An editor that expands on every keystroke wants tighter limits:

```rust
use macro_lang::{Budget, Expander};

const INTERACTIVE: Budget = Budget::DEFAULT
    .with_max_depth(64)
    .with_max_expansions(20_000)
    .with_max_tokens(500_000);
let expander: Expander<char> = Expander::with_budget(INTERACTIVE);
assert_eq!(expander.limit(), 64);
```

A doubling macro under the default budget — it reaches the token limit in about
twenty rounds:

```rust
use intern_lang::Interner;
use macro_lang::{Budget, Context, ExpandError, Expander, Fragment, Kleene, Limit, Macro, Pattern, Rule, Template, Tree};
use token_lang::{Span, Token};

let mut names = Interner::new();
let t = names.intern("t");
let s = Span::new(0, 1);
let each = || Template::Repeat { body: vec![Template::Var(t)], separator: None };
// dup!($($t:tt)*) => dup!($($t)* $($t)*)
let dup = Macro::new(names.intern("dup"), vec![Rule {
    pattern: vec![Pattern::Repeat {
        body: vec![Pattern::Bind { name: t, fragment: Fragment::Tree }],
        separator: None,
        kleene: Kleene::ZeroOrMore,
    }],
    template: vec![
        Template::token('d', s),
        Template::token('!', s),
        Template::Group { open: Token::new('(', s), close: Token::new(')', s), body: vec![each(), each()] },
    ],
}])?;

let mut expander = Expander::with_budget(Budget::DEFAULT.with_max_tokens(1 << 16));
let mut args = vec![Tree::token('x', s)];
let mut call_context = Context::ROOT;
let mut rounds = 0;
let err = loop {
    match expander.expand(&dup, &args, s, call_context) {
        Ok(mut out) => {
            if let Some(Tree::Token { ctx, .. }) = out.first() { call_context = *ctx; }
            match out.pop() {
                Some(Tree::Group { trees, .. }) => { args = trees; rounds += 1; }
                _ => break None,
            }
        }
        Err(err) => break Some(err),
    }
};
assert_eq!(err, Some(ExpandError::Budget(Limit::Tokens { max: 1 << 16 })));
assert!(rounds < 20);
# Ok::<(), Box<dyn std::error::Error>>(())
```

---

## Stability

As of `1.0.0` the public API is frozen. macro-lang follows
[Semantic Versioning](https://semver.org/); within the `1.x` series:

- **Added in 1.1.0** (additive, no breaking change): [`Budget`]#budget
  (`DEFAULT`, `with_max_depth`, `with_max_expansions`, `with_max_tokens`),
  [`Limit`]#limit, `ExpandError::Budget`, `Template::RepeatSeparated`, and
  [`Usage`]#usage (`NONE`), and `Expander::with_budget`, `budget`,
  `set_budget`, `usage`, `reset_usage`, and `expand_at`. These are
  part of the frozen surface from 1.1.0 on.
- **Added in 1.2.0**: [`Tree::dismantle`]#treedismantle. `Tree`'s `Clone`,
  `PartialEq` and `Hash` became hand-written, heap-stack versions with the same
  behavior as the derived ones.

- The **surface** will not change in a breaking way: [`Tree`]#tree
  (`token`, `span`, `dismantle`), [`Pattern`]#pattern, [`Fragment`]#fragment,
  [`Kleene`]#kleene (`allows`), [`Template`]#template (`token`),
  [`Rule`]#rule, [`Macro`]#macro (`new`, `name`),
  [`Expander`]#expander (`DEFAULT_LIMIT`, `new`, `with_limit`, `limit`,
  `with_budget`, `budget`, `set_budget`, `usage`, `reset_usage`, `expand`,
  `expand_at`, `origin`), [`Usage`]#usage,
  [`Budget`]#budget, [`Limit`]#limit, [`Context`]#context (`ROOT`, `is_root`, `as_u32`),
  [`Origin`]#origin, [`MacroError`]#macroerror, and
  [`ExpandError`]#expanderror. A breaking change means a new major version.
- The **matching semantics** are part of the contract: rules are tried in
  order and the first whose pattern matches the entire invocation wins; literal
  tokens compare by kind only; a rule that matches in more than one way is
  reported as `Ambiguous` and stops the search; matching never backtracks; and
  `NoMatch` locates the furthest point any rule reached.
- The **hygiene semantics** are part of the contract: each expansion mints
  fresh contexts for the literal tokens and separators its template writes —
  one context per distinct definition context — and substituted captures keep
  their contexts unchanged; `Origin` reports the definition context, macro,
  call site, and call context; a failed expansion leaves the expander
  unchanged. Contexts are numbered densely from `0` in the order they are
  minted. A `Repeat` separator is defined in the root context; a
  `RepeatSeparated` separator in its `ctx`.
- The **depth semantics**: an invocation runs one level deeper than the deepest
  of the driver's `depth` (`0` for `expand`), the expansion that produced
  `call_context`, and the expansions that produced its input tokens. *Changed
  in 1.1.0:* 1.0 read depth from `call_context` alone, which let a macro that
  rebuilt its invocation around a captured name restart at depth 1 every
  round. Depth can only be higher than 1.0 computed, never lower.
- The **budget semantics**: the expansion and token limits apply to the
  totals of successful expansions since creation or the last `reset_usage`,
  which zeroes only those counters (hygiene state, the budget, and depth are
  untouched); tokens are counted one per token, two per group,
  and every token of a substituted capture, and are charged before they are
  written; failed expansions are not charged.
- The **definition checks** in [`Macro::new`]#macronew are fixed: a macro that
  builds under `1.0` keeps building under every `1.x`.
- [`Expander::DEFAULT_LIMIT`]#expanderdefault_limit stays `128`. The
  [`Budget::DEFAULT`]#budgetdefault expansion and token limits will not be
  lowered within `1.x` (raising them is a minor change).
- `Pattern`, `Fragment`, `Template`, `Origin`, `MacroError`, `ExpandError`,
  `Budget`, `Limit`, and `Usage` are `#[non_exhaustive]`, so new elements, fields, and failure modes are
  additive minor changes. Match them with a wildcard arm.
- MSRV (Rust 1.85) is a compatibility surface: raising it is a documented minor
  change, never a patch.

What is **not** promised: the exact `Display` wording of the error types, the
`Debug` output of any type, the compiled representation of a `Macro`, and
performance figures — the benchmarks are tracked, but they are measurements,
not guarantees.

See [`../dev/ROADMAP.md`](../dev/ROADMAP.md) and
[`../CHANGELOG.md`](../CHANGELOG.md).