macroforge_ts_quote 0.1.56

Quote macro for generating TypeScript code at compile time
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
//! Rust-style templating for TypeScript code generation
//!
//! Provides a template syntax with interpolation and control flow:
//! - `@{expr}` - Interpolate expressions (calls `.to_string()`)
//! - `{| content |}` - Ident block: concatenates content without spaces (e.g., `{|get@{name}|}` → `getUser`)
//! - `{> "comment" <}` - Block comment: outputs `/* comment */` (string preserves whitespace)
//! - `{>> "doc" <<}` - Doc comment: outputs `/** doc */` (string preserves whitespace)
//! - `@@{` - Escape for literal `@{` (e.g., `"@@{foo}"` → `@{foo}`)
//! - `"string @{expr}"` - String interpolation (auto-detected)
//! - `"'^template ${expr}^'"` - JS backtick template literal (outputs `` `template ${expr}` ``)
//! - `{#if cond}...{/if}` - Conditional blocks
//! - `{#if let pattern = expr}...{/if}` - Pattern matching if-let blocks
//! - `{:else}` - Else clause
//! - `{:else if cond}` - Else-if clause
//! - `{#match expr}{:case pattern}...{/match}` - Match blocks with case arms
//! - `{#for item in list}...{/for}` - Iteration
//! - `{#while cond}...{/while}` - While loop
//! - `{#while let pattern = expr}...{/while}` - While-let pattern matching loop
//! - `{$let name = expr}` - Local constants
//! - `{$let mut name = expr}` - Mutable local binding
//! - `{$do expr}` - Execute side-effectful expression (discard result)
//! - `{$typescript stream}` - Inject a TsStream, preserving its source and runtime_patches (imports)
//!
//! Note: A single `@` not followed by `{` passes through unchanged (e.g., `email@domain.com`).

use proc_macro2::{Delimiter, Group, Span, TokenStream as TokenStream2, TokenTree};
use quote::{ToTokens, quote};
use std::iter::Peekable;

/// Creates a syntax error with contextual information about the template location.
///
/// This function constructs a [`syn::Error`] with an optional context string that
/// helps users identify where in their template the error occurred.
///
/// # Arguments
///
/// * `span` - The source span for error reporting
/// * `message` - The primary error message
/// * `context` - Optional context showing the problematic template code
///
/// # Returns
///
/// A [`syn::Error`] that can be converted to a compile error via `.to_compile_error()`.
///
/// # Example Output
///
/// ```text
/// error: Unclosed {#if} block: Missing {/if}
///   --> in: {#if condition}...
/// ```
fn template_error(span: Span, message: &str, context: Option<&str>) -> syn::Error {
    let full_message = if let Some(ctx) = context {
        format!("{}\n  --> in: {}", message, ctx)
    } else {
        message.to_string()
    };
    syn::Error::new(span, full_message)
}

/// Entry point for parsing a template token stream.
///
/// This function transforms template tokens into Rust code that builds a TypeScript
/// string at runtime. The generated code handles all interpolation, control flow,
/// and patch collection.
///
/// # Arguments
///
/// * `input` - The raw token stream from the template macro invocation
///
/// # Returns
///
/// On success, returns a [`TokenStream2`] containing Rust code that evaluates to
/// `(String, Vec<Patch>)` - the generated TypeScript source and any runtime patches
/// (imports, etc.) collected from `{$typescript}` injections.
///
/// # Errors
///
/// Returns a [`syn::Error`] if:
/// - A control flow block is unclosed (`{#if}` without `{/if}`)
/// - A terminator appears in an unexpected context
/// - String interpolation syntax is malformed
/// - An expression inside `@{}` cannot be parsed
///
/// # Generated Code Structure
///
/// ```ignore
/// {
///     let mut __out = String::new();
///     let mut __patches: Vec<Patch> = Vec::new();
///     // ... generated string-building code ...
///     (__out, __patches)
/// }
/// ```
pub fn parse_template(input: TokenStream2) -> syn::Result<TokenStream2> {
    // Parse the tokens into a Rust block that returns a String or a templating error
    let (body, _) = parse_fragment(&mut input.into_iter().peekable(), None)?;

    Ok(quote! {
        {
            let mut __out = String::new();
            let mut __patches: Vec<macroforge_ts::ts_syn::abi::Patch> = Vec::new();
            #body
            (__out, __patches)
        }
    })
}

/// Signals that cause the recursive parser to stop and return control.
///
/// When [`parse_fragment`] encounters one of these terminators while parsing,
/// it returns the accumulated output along with the terminator, allowing the
/// caller to handle the control flow appropriately.
///
/// # Variants
///
/// - `Else` - Encountered `{:else}`, signals end of if-true block
/// - `ElseIf(cond)` - Encountered `{:else if cond}`, signals chained conditional
/// - `EndIf` - Encountered `{/if}`, signals end of if block
/// - `EndFor` - Encountered `{/for}`, signals end of for loop
/// - `EndWhile` - Encountered `{/while}`, signals end of while loop
/// - `Case(pattern)` - Encountered `{:case pattern}`, signals new match arm
/// - `EndMatch` - Encountered `{/match}`, signals end of match block
#[derive(Debug, Clone)]
enum Terminator {
    /// `{:else}` - End of if-true block, start of else block
    Else,
    /// `{:else if condition}` - Chained conditional with new condition
    ElseIf(TokenStream2),
    /// `{/if}` - End of entire if/else-if/else chain
    EndIf,
    /// `{/for}` - End of for loop body
    EndFor,
    /// `{/while}` - End of while loop body
    EndWhile,
    /// `{:case pattern}` - Start of new match arm with pattern
    Case(TokenStream2),
    /// `{/match}` - End of entire match block
    EndMatch,
}

/// Classification of brace-delimited groups in the template.
///
/// When the parser encounters a `{ ... }` group, [`analyze_tag`] examines its
/// contents to determine whether it's a template control tag or regular
/// TypeScript code. This enum represents all possible classifications.
///
/// # Control Flow Tags
///
/// Tags starting with `#` introduce control structures:
/// - `{#if}`, `{#if let}` - Conditionals
/// - `{#for}` - Iteration
/// - `{#while}`, `{#while let}` - While loops
/// - `{#match}` - Pattern matching
///
/// Tags starting with `:` are continuations:
/// - `{:else}`, `{:else if}` - Conditional branches
/// - `{:case}` - Match arms
///
/// Tags starting with `/` close blocks:
/// - `{/if}`, `{/for}`, `{/while}`, `{/match}`
///
/// # Action Tags
///
/// Tags starting with `$` perform actions:
/// - `{$let}`, `{$let mut}` - Local bindings
/// - `{$do}` - Side effects
/// - `{$typescript}` - TsStream injection
///
/// # Special Syntax
///
/// - `{| ... |}` - Ident blocks (no-space concatenation)
/// - `{> "..." <}` - Block comments
/// - `{>> "..." <<}` - Doc comments
/// - `{ ... }` (no prefix) - Regular TypeScript blocks
enum TagType {
    /// `{#if condition}` - Start of conditional block
    If(TokenStream2),
    /// `{#if let pattern = expr}` - Pattern-matching conditional (pattern, expression)
    IfLet(TokenStream2, TokenStream2),
    /// `{#while condition}` - Start of while loop
    While(TokenStream2),
    /// `{#while let pattern = expr}` - Pattern-matching while loop (pattern, expression)
    WhileLet(TokenStream2, TokenStream2),
    /// `{#for item in collection}` - Start of for loop (item binding, collection expression)
    For(TokenStream2, TokenStream2),
    /// `{#match expr}` - Start of match block with expression to match
    Match(TokenStream2),
    /// `{:else}` - Else branch of conditional
    Else,
    /// `{:else if condition}` - Else-if branch with condition
    ElseIf(TokenStream2),
    /// `{:case pattern}` - Match arm with pattern
    Case(TokenStream2),
    /// `{/if}` - End of if/else-if/else block
    EndIf,
    /// `{/for}` - End of for loop
    EndFor,
    /// `{/while}` - End of while loop
    EndWhile,
    /// `{/match}` - End of match block
    EndMatch,
    /// `{$let name = expr}` - Immutable local binding
    Let(TokenStream2),
    /// `{$let mut name = expr}` - Mutable local binding
    LetMut(TokenStream2),
    /// `{$do expr}` - Execute expression for side effects (discard result)
    Do(TokenStream2),
    /// `{$typescript stream}` - Inject TsStream, preserving source and patches
    Typescript(TokenStream2),
    /// `{| content |}` - Identifier block with no internal spacing
    IdentBlock,
    /// `{> "comment" <}` - Block comment: outputs `/* comment */`
    BlockComment(String),
    /// `{>> "doc" <<}` - Doc comment: outputs `/** doc */`
    DocComment(String),
    /// Standard TypeScript/JavaScript block (no template syntax detected)
    Block,
}

/// Analyzes a brace-delimited group to determine its tag type.
///
/// This function examines the tokens inside a `{ ... }` group to classify
/// whether it's a template control tag (like `{#if}`, `{#for}`) or just
/// regular TypeScript code that should be passed through.
///
/// # Arguments
///
/// * `g` - The [`Group`] token to analyze (must have brace delimiter)
///
/// # Returns
///
/// A [`TagType`] classification indicating what kind of template construct
/// (if any) the group represents.
///
/// # Recognition Patterns
///
/// The function checks tokens in order of specificity:
///
/// 1. **Ident blocks**: `{| ... |}` - First and last tokens are `|`
/// 2. **Doc comments**: `{>> "..." <<}` - Starts with `>>`, ends with `<<`
/// 3. **Block comments**: `{> "..." <}` - Starts with `>`, ends with `<`
/// 4. **Control tags**: First token is `#`, `:`, `/`, or `$`
///    - `#if`, `#for`, `#while`, `#match` - Block openers
///    - `:else`, `:else if`, `:case` - Continuations
///    - `/if`, `/for`, `/while`, `/match` - Block closers
///    - `$let`, `$do`, `$typescript` - Actions
/// 5. **Plain blocks**: Anything else is treated as TypeScript code
///
/// # Example Classifications
///
/// ```text
/// {#if x > 0}     -> TagType::If(tokens for "x > 0")
/// {#for i in 0..5} -> TagType::For(tokens for "i", tokens for "0..5")
/// {:else}          -> TagType::Else
/// {/if}            -> TagType::EndIf
/// {$let x = 1}     -> TagType::Let(tokens for "x = 1")
/// {| foo @{bar} |} -> TagType::IdentBlock
/// { x: 1 }         -> TagType::Block
/// ```
fn analyze_tag(g: &Group) -> TagType {
    let tokens: Vec<TokenTree> = g.stream().into_iter().collect();

    // Check for {| ... |} ident block - must have at least | and |
    if tokens.len() >= 2
        && let (Some(TokenTree::Punct(first)), Some(TokenTree::Punct(last))) =
            (tokens.first(), tokens.last())
        && first.as_char() == '|'
        && last.as_char() == '|'
    {
        return TagType::IdentBlock;
    }

    // Check for {>> "string" <<} doc comment - must have >> string <<
    if tokens.len() >= 5
        && let (Some(TokenTree::Punct(p1)), Some(TokenTree::Punct(p2))) =
            (tokens.first(), tokens.get(1))
        && p1.as_char() == '>'
        && p2.as_char() == '>'
        && let (Some(TokenTree::Punct(p3)), Some(TokenTree::Punct(p4))) =
            (tokens.get(tokens.len() - 2), tokens.last())
        && p3.as_char() == '<'
        && p4.as_char() == '<'
    {
        // Extract the string literal in the middle
        if let Some(TokenTree::Literal(lit)) = tokens.get(2) {
            let content = extract_string_literal(lit);
            return TagType::DocComment(content);
        }
        // Fallback: join remaining tokens as string (for backwards compat)
        let content = tokens_to_spaced_string(&tokens[2..tokens.len() - 2]);
        return TagType::DocComment(content);
    }

    // Check for {> "string" <} block comment - must have > string <
    if tokens.len() >= 3
        && let (Some(TokenTree::Punct(first)), Some(TokenTree::Punct(last))) =
            (tokens.first(), tokens.last())
        && first.as_char() == '>'
        && last.as_char() == '<'
    {
        // Extract the string literal in the middle
        if let Some(TokenTree::Literal(lit)) = tokens.get(1) {
            let content = extract_string_literal(lit);
            return TagType::BlockComment(content);
        }
        // Fallback: join remaining tokens as string (for backwards compat)
        let content = tokens_to_spaced_string(&tokens[1..tokens.len() - 1]);
        return TagType::BlockComment(content);
    }

    if tokens.len() < 2 {
        return TagType::Block;
    }

    // Check for {# ...} tags
    if let (TokenTree::Punct(p), TokenTree::Ident(i)) = (&tokens[0], &tokens[1])
        && p.as_char() == '#'
    {
        if i == "if" {
            // Check for {#if let pattern = expr}
            if let Some(TokenTree::Ident(let_kw)) = tokens.get(2)
                && let_kw == "let"
            {
                // Format: {#if let pattern = expr}
                // Split on "=" to separate pattern from expression
                let mut pattern = TokenStream2::new();
                let mut expr = TokenStream2::new();
                let mut seen_eq = false;

                for t in tokens.iter().skip(3) {
                    if let TokenTree::Punct(eq) = t
                        && eq.as_char() == '='
                        && !seen_eq
                    {
                        seen_eq = true;
                        continue;
                    }
                    if !seen_eq {
                        t.to_tokens(&mut pattern);
                    } else {
                        t.to_tokens(&mut expr);
                    }
                }
                return TagType::IfLet(pattern, expr);
            }

            // Format: {#if condition}
            let cond: TokenStream2 = tokens.iter().skip(2).map(|t| t.to_token_stream()).collect();
            return TagType::If(cond);
        }

        if i == "match" {
            // Format: {#match expr}
            let expr: TokenStream2 = tokens.iter().skip(2).map(|t| t.to_token_stream()).collect();
            return TagType::Match(expr);
        }

        if i == "while" {
            // Check for {#while let pattern = expr}
            if let Some(TokenTree::Ident(let_kw)) = tokens.get(2)
                && let_kw == "let"
            {
                let mut pattern = TokenStream2::new();
                let mut expr = TokenStream2::new();
                let mut seen_eq = false;

                for t in tokens.iter().skip(3) {
                    if let TokenTree::Punct(eq) = t
                        && eq.as_char() == '='
                        && !seen_eq
                    {
                        seen_eq = true;
                        continue;
                    }
                    if !seen_eq {
                        t.to_tokens(&mut pattern);
                    } else {
                        t.to_tokens(&mut expr);
                    }
                }
                return TagType::WhileLet(pattern, expr);
            }

            // Simple {#while condition}
            let cond: TokenStream2 = tokens.iter().skip(2).map(|t| t.to_token_stream()).collect();
            return TagType::While(cond);
        }

        if i == "for" {
            // Format: {#for item in collection}
            let mut item = TokenStream2::new();
            let mut list = TokenStream2::new();
            let mut seen_in = false;

            // Split on "in" keyword
            for t in tokens.iter().skip(2) {
                if let TokenTree::Ident(id) = t
                    && id == "in"
                    && !seen_in
                {
                    seen_in = true;
                    continue;
                }
                if !seen_in {
                    t.to_tokens(&mut item);
                } else {
                    t.to_tokens(&mut list);
                }
            }
            return TagType::For(item, list);
        }
    }

    // Check for {$ ...} tags (let, let mut, do, typescript)
    if let (TokenTree::Punct(p), TokenTree::Ident(i)) = (&tokens[0], &tokens[1])
        && p.as_char() == '$'
    {
        if i == "let" {
            // Check for {$let mut name = expr}
            if let Some(TokenTree::Ident(mut_kw)) = tokens.get(2)
                && mut_kw == "mut"
            {
                let body: TokenStream2 =
                    tokens.iter().skip(3).map(|t| t.to_token_stream()).collect();
                return TagType::LetMut(body);
            }
            // Format: {$let name = expr}
            let body: TokenStream2 = tokens.iter().skip(2).map(|t| t.to_token_stream()).collect();
            return TagType::Let(body);
        }
        if i == "do" {
            // Format: {$do expr} - execute side-effectful expression
            let expr: TokenStream2 = tokens.iter().skip(2).map(|t| t.to_token_stream()).collect();
            return TagType::Do(expr);
        }
        if i == "typescript" {
            // Format: {$typescript stream_expr}
            let expr: TokenStream2 = tokens.iter().skip(2).map(|t| t.to_token_stream()).collect();
            return TagType::Typescript(expr);
        }
    }

    // Check for {: ...} tags (else, else if, case)
    if let (TokenTree::Punct(p), TokenTree::Ident(i)) = (&tokens[0], &tokens[1])
        && p.as_char() == ':'
    {
        if i == "else" {
            // Check for {:else if condition}
            if let Some(TokenTree::Ident(next)) = tokens.get(2)
                && next == "if"
            {
                let cond: TokenStream2 =
                    tokens.iter().skip(3).map(|t| t.to_token_stream()).collect();
                return TagType::ElseIf(cond);
            }
            return TagType::Else;
        }

        if i == "case" {
            // Format: {:case pattern}
            let pattern: TokenStream2 =
                tokens.iter().skip(2).map(|t| t.to_token_stream()).collect();
            return TagType::Case(pattern);
        }
    }

    // Check for {/ ...} (End tags)
    if let (TokenTree::Punct(p), TokenTree::Ident(i)) = (&tokens[0], &tokens[1])
        && p.as_char() == '/'
    {
        if i == "if" {
            return TagType::EndIf;
        }
        if i == "for" {
            return TagType::EndFor;
        }
        if i == "while" {
            return TagType::EndWhile;
        }
        if i == "match" {
            return TagType::EndMatch;
        }
    }

    TagType::Block
}

/// Parses an if/else-if/else chain and generates the corresponding Rust code.
///
/// This function handles the complete parsing of a conditional block, including
/// any `{:else}` or `{:else if}` continuations. It recursively processes
/// else-if chains to support arbitrarily deep conditional nesting.
///
/// # Arguments
///
/// * `iter` - Mutable iterator over remaining template tokens
/// * `initial_cond` - The condition expression from `{#if condition}`
/// * `open_span` - Span of the opening `{#if}` tag for error reporting
///
/// # Returns
///
/// On success, returns Rust code that implements the conditional:
///
/// ```ignore
/// if condition {
///     // true block
/// } else if other_condition {
///     // else-if block
/// } else {
///     // else block
/// }
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - The `{#if}` block is never closed with `{/if}`
/// - A `{:else}` block is opened but never closed
///
/// # State Machine
///
/// ```text
/// {#if cond} -> parse true block -> {:else if} -> recurse
///                                -> {:else}    -> parse else block -> {/if}
///                                -> {/if}      -> done (no else)
/// ```
fn parse_if_chain(
    iter: &mut Peekable<proc_macro2::token_stream::IntoIter>,
    initial_cond: TokenStream2,
    open_span: Span,
) -> syn::Result<TokenStream2> {
    // Parse the true block, stopping at {:else}, {:else if}, or {/if}
    let (true_block, terminator) = parse_fragment(
        iter,
        Some(&[
            Terminator::Else,
            Terminator::ElseIf(TokenStream2::new()),
            Terminator::EndIf,
        ]),
    )?;

    match terminator {
        Some(Terminator::EndIf) => {
            // Simple if without else
            Ok(quote! {
                if #initial_cond {
                    #true_block
                }
            })
        }
        Some(Terminator::Else) => {
            // if with else - parse else block until {/if}
            let (else_block, terminator) = parse_fragment(iter, Some(&[Terminator::EndIf]))?;
            if !matches!(terminator, Some(Terminator::EndIf)) {
                return Err(template_error(
                    open_span,
                    "Unclosed {:else} block: Missing {/if}",
                    Some("{:else}..."),
                ));
            }
            Ok(quote! {
                if #initial_cond {
                    #true_block
                } else {
                    #else_block
                }
            })
        }
        Some(Terminator::ElseIf(else_if_cond)) => {
            // if with else if - recursively parse the else-if chain
            // For the recursive call, we should ideally find the span of the else-if tag.
            // But keeping open_span is acceptable for now as it points to the start of the whole chain.
            let else_if_chain = parse_if_chain(iter, else_if_cond, open_span)?;
            Ok(quote! {
                if #initial_cond {
                    #true_block
                } else {
                    #else_if_chain
                }
            })
        }
        None => Err(template_error(
            open_span,
            "Unclosed {#if} block: Missing {/if}",
            Some("{#if condition}..."),
        )),
        _ => unreachable!(),
    }
}

/// Parses an if-let/else chain and generates the corresponding Rust code.
///
/// This function handles pattern-matching conditionals that use the
/// `{#if let pattern = expr}` syntax. Unlike regular if chains, if-let
/// does not support `{:else if}` continuations (only `{:else}`).
///
/// # Arguments
///
/// * `iter` - Mutable iterator over remaining template tokens
/// * `pattern` - The destructuring pattern (e.g., `Some(value)`)
/// * `expr` - The expression to match against
/// * `open_span` - Span of the opening tag for error reporting
///
/// # Returns
///
/// On success, returns Rust code that implements the pattern match:
///
/// ```ignore
/// if let pattern = expr {
///     // body when pattern matches
/// } else {
///     // optional else body
/// }
/// ```
///
/// # Errors
///
/// Returns an error if:
/// - The block is never closed with `{/if}`
/// - A `{:else}` block is opened but never closed
///
/// # Example
///
/// ```text
/// {#if let Some(name) = user.name}
///     Hello, @{name}!
/// {:else}
///     Hello, anonymous!
/// {/if}
/// ```
fn parse_if_let_chain(
    iter: &mut Peekable<proc_macro2::token_stream::IntoIter>,
    pattern: TokenStream2,
    expr: TokenStream2,
    open_span: Span,
) -> syn::Result<TokenStream2> {
    // Parse the true block, stopping at {:else} or {/if}
    let (true_block, terminator) =
        parse_fragment(iter, Some(&[Terminator::Else, Terminator::EndIf]))?;

    match terminator {
        Some(Terminator::EndIf) => {
            // Simple if let without else
            Ok(quote! {
                if let #pattern = #expr {
                    #true_block
                }
            })
        }
        Some(Terminator::Else) => {
            // if let with else - parse else block until {/if}
            let (else_block, terminator) = parse_fragment(iter, Some(&[Terminator::EndIf]))?;
            if !matches!(terminator, Some(Terminator::EndIf)) {
                return Err(template_error(
                    open_span,
                    "Unclosed {:else} block in {#if let}: Missing {/if}",
                    Some("{#if let pattern = expr}{:else}..."),
                ));
            }
            Ok(quote! {
                if let #pattern = #expr {
                    #true_block
                } else {
                    #else_block
                }
            })
        }
        None => Err(template_error(
            open_span,
            "Unclosed {#if let} block: Missing {/if}",
            Some("{#if let pattern = expr}..."),
        )),
        _ => unreachable!(),
    }
}

/// Parses a while loop and generates the corresponding Rust code.
///
/// This function handles `{#while condition}...{/while}` blocks, generating
/// a Rust while loop that conditionally produces template output.
///
/// # Arguments
///
/// * `iter` - Mutable iterator over remaining template tokens
/// * `cond` - The loop condition expression
/// * `open_span` - Span of the opening tag for error reporting
///
/// # Returns
///
/// On success, returns Rust code:
///
/// ```ignore
/// while condition {
///     // loop body (string building code)
/// }
/// ```
///
/// # Errors
///
/// Returns an error if the block is never closed with `{/while}`.
///
/// # Example
///
/// ```text
/// {$let mut i = 0}
/// {#while i < 3}
///     Item @{i}
///     {$do i += 1}
/// {/while}
/// ```
fn parse_while_loop(
    iter: &mut Peekable<proc_macro2::token_stream::IntoIter>,
    cond: TokenStream2,
    open_span: Span,
) -> syn::Result<TokenStream2> {
    let (body, terminator) = parse_fragment(iter, Some(&[Terminator::EndWhile]))?;

    if !matches!(terminator, Some(Terminator::EndWhile)) {
        return Err(template_error(
            open_span,
            "Unclosed {#while} block: Missing {/while}",
            Some("{#while condition}..."),
        ));
    }

    Ok(quote! {
        while #cond {
            #body
        }
    })
}

/// Parses a while-let loop and generates the corresponding Rust code.
///
/// This function handles `{#while let pattern = expr}...{/while}` blocks,
/// generating a Rust while-let loop that iterates while a pattern matches.
///
/// # Arguments
///
/// * `iter` - Mutable iterator over remaining template tokens
/// * `pattern` - The destructuring pattern to match
/// * `expr` - The expression to match against each iteration
/// * `open_span` - Span of the opening tag for error reporting
///
/// # Returns
///
/// On success, returns Rust code:
///
/// ```ignore
/// while let pattern = expr {
///     // loop body (string building code)
/// }
/// ```
///
/// # Errors
///
/// Returns an error if the block is never closed with `{/while}`.
///
/// # Example
///
/// ```text
/// {#while let Some(item) = iter.next()}
///     Processing @{item}
/// {/while}
/// ```
fn parse_while_let_loop(
    iter: &mut Peekable<proc_macro2::token_stream::IntoIter>,
    pattern: TokenStream2,
    expr: TokenStream2,
    open_span: Span,
) -> syn::Result<TokenStream2> {
    let (body, terminator) = parse_fragment(iter, Some(&[Terminator::EndWhile]))?;

    if !matches!(terminator, Some(Terminator::EndWhile)) {
        return Err(template_error(
            open_span,
            "Unclosed {#while let} block: Missing {/while}",
            Some("{#while let pattern = expr}..."),
        ));
    }

    Ok(quote! {
        while let #pattern = #expr {
            #body
        }
    })
}

/// Parses a match expression with case arms and generates the corresponding Rust code.
///
/// This function handles `{#match expr}{:case pattern}...{/match}` blocks,
/// generating a Rust match expression with multiple arms. Each `{:case pattern}`
/// starts a new arm.
///
/// # Arguments
///
/// * `iter` - Mutable iterator over remaining template tokens
/// * `match_expr` - The expression being matched against
/// * `open_span` - Span of the opening tag for error reporting
///
/// # Returns
///
/// On success, returns Rust code:
///
/// ```ignore
/// match expr {
///     pattern1 => {
///         // body1 (string building code)
///     }
///     pattern2 => {
///         // body2 (string building code)
///     }
///     // ...
/// }
/// ```
///
/// # Errors
///
/// Returns an error if the block is never closed with `{/match}`.
///
/// # Example
///
/// ```text
/// {#match status}
///     {:case "active"}
///         Status: Active
///     {:case "pending"}
///         Status: Pending
///     {:case _}
///         Status: Unknown
/// {/match}
/// ```
///
/// # Note
///
/// Content before the first `{:case}` is ignored. Each arm's body consists
/// of all content between its `{:case}` tag and the next `{:case}` or `{/match}`.
fn parse_match_arms(
    iter: &mut Peekable<proc_macro2::token_stream::IntoIter>,
    match_expr: TokenStream2,
    open_span: Span,
) -> syn::Result<TokenStream2> {
    let mut arms = TokenStream2::new();
    let mut current_pattern: Option<TokenStream2> = None;

    loop {
        // Parse until we hit {:case} or {/match}
        let (body, terminator) = parse_fragment(
            iter,
            Some(&[Terminator::Case(TokenStream2::new()), Terminator::EndMatch]),
        )?;

        match terminator {
            Some(Terminator::Case(pattern)) => {
                // If we have a previous pattern, emit its arm with the body we just parsed
                if let Some(prev_pattern) = current_pattern.take() {
                    arms.extend(quote! {
                        #prev_pattern => {
                            #body
                        }
                    });
                }
                // Store this pattern for the next iteration
                current_pattern = Some(pattern);
            }
            Some(Terminator::EndMatch) => {
                // Emit the final arm if we have one
                if let Some(prev_pattern) = current_pattern.take() {
                    arms.extend(quote! {
                        #prev_pattern => {
                            #body
                        }
                    });
                }
                break;
            }
            None => {
                return Err(template_error(
                    open_span,
                    "Unclosed {#match} block: Missing {/match}",
                    Some("{#match expr}{:case pattern}...{/match}"),
                ));
            }
            _ => unreachable!(),
        }
    }

    Ok(quote! {
        match #match_expr {
            #arms
        }
    })
}

/// Parses tokens without adding spaces between them.
///
/// This is a specialized parser for `{| ... |}` ident blocks where tokens
/// should be concatenated without any whitespace. This is essential for
/// building compound identifiers or strings from multiple parts.
///
/// # Arguments
///
/// * `iter` - Mutable iterator over the tokens inside the ident block
///
/// # Returns
///
/// Rust code that builds a string by concatenating all tokens without spaces.
///
/// # Differences from `parse_fragment`
///
/// - No automatic spacing between tokens
/// - No handling of control flow tags (they would break ident generation)
/// - Simplified interpolation handling (just `@{expr}`)
///
/// # Example
///
/// ```text
/// {| get @{field_name} |}
/// // Produces: "getfieldName" (if field_name = "fieldName")
/// // NOT: "get fieldName " (with spaces)
/// ```
fn parse_fragment_no_spacing(
    iter: &mut Peekable<proc_macro2::token_stream::IntoIter>,
) -> syn::Result<TokenStream2> {
    let mut output = TokenStream2::new();

    while let Some(token) = iter.peek().cloned() {
        match &token {
            // Handle @{ expr } interpolation
            TokenTree::Punct(p) if p.as_char() == '@' => {
                iter.next(); // Consume '@'

                let is_group = matches!(iter.peek(), Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace);

                if is_group {
                    if let Some(TokenTree::Group(g)) = iter.next() {
                        let content = g.stream();
                        output.extend(quote! {
                            __out.push_str(&#content.to_string());
                        });
                    }
                } else {
                    output.extend(quote! { __out.push_str("@"); });
                }
            }

            // Handle nested groups (but not ident blocks - those are already consumed)
            TokenTree::Group(g) => {
                iter.next();
                let (open, close) = match g.delimiter() {
                    Delimiter::Parenthesis => ("(", ")"),
                    Delimiter::Bracket => ("[", "]"),
                    Delimiter::Brace => ("{", "}"),
                    Delimiter::None => ("", ""),
                };
                output.extend(quote! { __out.push_str(#open); });
                let inner = parse_fragment_no_spacing(&mut g.stream().into_iter().peekable())?;
                output.extend(inner);
                output.extend(quote! { __out.push_str(#close); });
            }

            // All other tokens - just emit, no spacing
            _ => {
                let t = iter.next().unwrap();
                let s = t.to_string();
                output.extend(quote! { __out.push_str(#s); });
                // NO space added - that's the point of ident blocks
            }
        }
    }

    Ok(output)
}

/// Main recursive parser that processes template tokens and generates string-building code.
///
/// This is the core parsing function that walks through template tokens, generating
/// Rust code that builds the output string at runtime. It handles all template
/// syntax including interpolation, control flow, and special blocks.
///
/// # Arguments
///
/// * `iter` - Mutable iterator over template tokens to process
/// * `stop_at` - Optional list of terminators that should cause this function to return.
///   Used by control flow parsers to stop at `{:else}`, `{/if}`, etc.
///
/// # Returns
///
/// A tuple of:
/// 1. Rust code that builds the string (appends to `__out`)
/// 2. The terminator that caused parsing to stop, or `None` if EOF was reached
///
/// # Processing Cases
///
/// The function handles tokens in this order of priority:
///
/// 1. **Interpolation** (`@{expr}`) - Calls `expr.to_string()` and appends
/// 2. **Brace groups** (`{...}`) - Analyzed via [`analyze_tag`]:
///    - Control flow tags spawn sub-parsers
///    - Terminators cause early return
///    - Plain blocks are recursively processed
/// 3. **Other groups** (`(...)`, `[...]`) - Recursively processed
/// 4. **Backtick templates** (`"'^...^'"`) - Processed for `@{}` interpolation
/// 5. **String literals** - Checked for `@{}` interpolation
/// 6. **Plain tokens** - Appended with intelligent spacing
///
/// # Spacing Logic
///
/// The parser adds spaces between tokens following these rules:
/// - Space after identifiers (unless followed by punctuation or groups)
/// - No space after `.`, `!`, `(`, `[`, `{`, `<`, `>`, `@`, `$`
/// - No space before `)`, `]`, `}`, `>`, `.`, `,`, `;`
/// - Joint punctuation (like `::`) stays together
///
/// For explicit no-space concatenation, use `{| ... |}` ident blocks.
fn parse_fragment(
    iter: &mut Peekable<proc_macro2::token_stream::IntoIter>,
    stop_at: Option<&[Terminator]>,
) -> syn::Result<(TokenStream2, Option<Terminator>)> {
    let mut output = TokenStream2::new();

    while let Some(token) = iter.peek().cloned() {
        match &token {
            // Case 1: Interpolation @{ expr }
            TokenTree::Punct(p) if p.as_char() == '@' => {
                // Check if the NEXT token is a Group { ... }
                let p_clone = p.clone();
                iter.next(); // Consume '@'

                // Look ahead
                let is_group = matches!(iter.peek(), Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace);

                if is_group {
                    // It IS interpolation: @{ expr }
                    if let Some(TokenTree::Group(g)) = iter.next() {
                        let content = g.stream();
                        output.extend(quote! {
                            __out.push_str(&#content.to_string());
                        });
                    }
                } else {
                    // It is just a literal '@'
                    let s = p_clone.to_string();
                    output.extend(quote! { __out.push_str(#s); });
                }

                // Spacing logic after interpolation: always add space unless followed by punctuation
                // Use {| |} for explicit no-space concatenation
                let next = iter.peek();
                let next_char = match next {
                    Some(TokenTree::Punct(p)) => Some(p.as_char()),
                    _ => None,
                };

                let mut add_space = true;

                // No space at end of stream (group delimiter like ) will follow)
                if next.is_none() {
                    add_space = false;
                }

                // No space before punctuation
                if matches!(next_char, Some(c) if ".,;:?()[]{}<>!".contains(c)) {
                    add_space = false;
                }

                // No space before ( or [ groups (function calls, indexing)
                if let Some(TokenTree::Group(g)) = next {
                    match g.delimiter() {
                        Delimiter::Parenthesis | Delimiter::Bracket => add_space = false,
                        _ => {}
                    }
                }

                // No space if followed by @{ interpolation (for @{a}@{b} patterns)
                if let Some(TokenTree::Punct(p)) = next
                    && p.as_char() == '@'
                {
                    // Peek ahead to check if it's @{ (interpolation)
                    let mut peek_iter = iter.clone();
                    peek_iter.next(); // skip the @
                    if matches!(peek_iter.peek(), Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace)
                    {
                        add_space = false;
                    }
                }

                if add_space {
                    output.extend(quote! { __out.push_str(" "); });
                }
            }

            // Case 2: Groups { ... } - Could be Tag or Block
            TokenTree::Group(g) if g.delimiter() == Delimiter::Brace => {
                let tag = analyze_tag(g);
                let span = g.span();

                match tag {
                    TagType::If(cond) => {
                        iter.next(); // Consume {#if}
                        output.extend(parse_if_chain(iter, cond, span)?);
                    }
                    TagType::IfLet(pattern, expr) => {
                        iter.next(); // Consume {#if let}
                        output.extend(parse_if_let_chain(iter, pattern, expr, span)?);
                    }
                    TagType::For(item, list) => {
                        iter.next(); // Consume {#for}

                        let (body, terminator) = parse_fragment(iter, Some(&[Terminator::EndFor]))?;
                        if !matches!(terminator, Some(Terminator::EndFor)) {
                            return Err(template_error(
                                span,
                                "Unclosed {#for} block: Missing {/for}",
                                Some("{#for item in collection}..."),
                            ));
                        }

                        output.extend(quote! {
                            for #item in #list {
                                #body
                            }
                        });
                    }
                    TagType::Match(expr) => {
                        iter.next(); // Consume {#match}
                        output.extend(parse_match_arms(iter, expr, span)?);
                    }
                    TagType::While(cond) => {
                        iter.next(); // Consume {#while}
                        output.extend(parse_while_loop(iter, cond, span)?);
                    }
                    TagType::WhileLet(pattern, expr) => {
                        iter.next(); // Consume {#while let}
                        output.extend(parse_while_let_loop(iter, pattern, expr, span)?);
                    }
                    TagType::Else => {
                        if let Some(stops) = stop_at
                            && stops.iter().any(|s| matches!(s, Terminator::Else))
                        {
                            iter.next(); // Consume
                            return Ok((output, Some(Terminator::Else)));
                        }
                        return Err(template_error(
                            span,
                            "Unexpected {:else} - not inside an {#if} block",
                            None,
                        ));
                    }
                    TagType::ElseIf(cond) => {
                        if let Some(stops) = stop_at
                            && stops.iter().any(|s| matches!(s, Terminator::ElseIf(_)))
                        {
                            iter.next(); // Consume
                            return Ok((output, Some(Terminator::ElseIf(cond))));
                        }
                        return Err(template_error(
                            span,
                            "Unexpected {:else if} - not inside an {#if} block",
                            None,
                        ));
                    }
                    TagType::EndIf => {
                        if let Some(stops) = stop_at
                            && stops.iter().any(|s| matches!(s, Terminator::EndIf))
                        {
                            iter.next(); // Consume
                            return Ok((output, Some(Terminator::EndIf)));
                        }
                        return Err(template_error(
                            span,
                            "Unexpected {/if} - no matching {#if} block",
                            None,
                        ));
                    }
                    TagType::EndFor => {
                        if let Some(stops) = stop_at
                            && stops.iter().any(|s| matches!(s, Terminator::EndFor))
                        {
                            iter.next(); // Consume
                            return Ok((output, Some(Terminator::EndFor)));
                        }
                        return Err(template_error(
                            span,
                            "Unexpected {/for} - no matching {#for} block",
                            None,
                        ));
                    }
                    TagType::EndWhile => {
                        if let Some(stops) = stop_at
                            && stops.iter().any(|s| matches!(s, Terminator::EndWhile))
                        {
                            iter.next(); // Consume
                            return Ok((output, Some(Terminator::EndWhile)));
                        }
                        return Err(template_error(
                            span,
                            "Unexpected {/while} - no matching {#while} block",
                            None,
                        ));
                    }
                    TagType::Case(pattern) => {
                        if let Some(stops) = stop_at
                            && stops.iter().any(|s| matches!(s, Terminator::Case(_)))
                        {
                            iter.next(); // Consume
                            return Ok((output, Some(Terminator::Case(pattern))));
                        }
                        return Err(template_error(
                            span,
                            "Unexpected {:case} - not inside a {#match} block",
                            None,
                        ));
                    }
                    TagType::EndMatch => {
                        if let Some(stops) = stop_at
                            && stops.iter().any(|s| matches!(s, Terminator::EndMatch))
                        {
                            iter.next(); // Consume
                            return Ok((output, Some(Terminator::EndMatch)));
                        }
                        return Err(template_error(
                            span,
                            "Unexpected {/match} - no matching {#match} block",
                            None,
                        ));
                    }
                    TagType::Let(body) => {
                        iter.next(); // Consume {$let ...}
                        output.extend(quote! {
                            let #body;
                        });
                    }
                    TagType::LetMut(body) => {
                        iter.next(); // Consume {$let mut ...}
                        output.extend(quote! {
                            let mut #body;
                        });
                    }
                    TagType::Do(expr) => {
                        iter.next(); // Consume {$do ...}
                        output.extend(quote! {
                            #expr;
                        });
                    }
                    TagType::Typescript(expr) => {
                        iter.next(); // Consume {$typescript ...}
                        output.extend(quote! {
                            {
                                let __ts_stream = #expr;
                                __out.push_str(__ts_stream.source());
                                __patches.extend(__ts_stream.runtime_patches);
                            }
                        });
                    }
                    TagType::IdentBlock => {
                        iter.next(); // Consume {| ... |}

                        // Get the content between the | markers
                        let inner_tokens: Vec<TokenTree> = g.stream().into_iter().collect();
                        // Skip first | and last |, extract content in between
                        if inner_tokens.len() >= 2 {
                            let content: TokenStream2 = inner_tokens[1..inner_tokens.len() - 1]
                                .iter()
                                .map(|t| t.to_token_stream())
                                .collect();

                            // Parse with no-spacing mode
                            let inner_output =
                                parse_fragment_no_spacing(&mut content.into_iter().peekable())?;
                            output.extend(inner_output);
                        }

                        // Spacing logic after ident block - same as @{} interpolation
                        // Add space unless followed by punctuation or function call
                        let next = iter.peek();
                        let next_char = match next {
                            Some(TokenTree::Punct(p)) => Some(p.as_char()),
                            _ => None,
                        };

                        let mut add_space = true;

                        // No space at end of stream
                        if next.is_none() {
                            add_space = false;
                        }

                        // No space before punctuation
                        if matches!(next_char, Some(c) if ".,;:?()[]{}<>!".contains(c)) {
                            add_space = false;
                        }

                        // No space before ( or [ groups (function calls, indexing)
                        if let Some(TokenTree::Group(g)) = next {
                            match g.delimiter() {
                                Delimiter::Parenthesis | Delimiter::Bracket => add_space = false,
                                _ => {}
                            }
                        }

                        // No space if followed by @{ interpolation (for {|a|}@{b} concatenation)
                        if let Some(TokenTree::Punct(p)) = next
                            && p.as_char() == '@'
                        {
                            // Peek ahead to check if it's @{ (interpolation)
                            let mut peek_iter = iter.clone();
                            peek_iter.next(); // skip the @
                            if matches!(peek_iter.peek(), Some(TokenTree::Group(g)) if g.delimiter() == Delimiter::Brace)
                            {
                                add_space = false;
                            }
                        }

                        if add_space {
                            output.extend(quote! { __out.push_str(" "); });
                        }
                    }
                    TagType::BlockComment(content) => {
                        iter.next(); // Consume {> "..." <}
                        output.extend(quote! { __out.push_str("/* "); });
                        output.extend(quote! { __out.push_str(#content); });
                        output.extend(quote! { __out.push_str(" */"); });
                    }
                    TagType::DocComment(content) => {
                        iter.next(); // Consume {>> "..." <<}
                        output.extend(quote! { __out.push_str("/** "); });
                        output.extend(quote! { __out.push_str(#content); });
                        output.extend(quote! { __out.push_str(" */"); });
                    }
                    TagType::Block => {
                        // Regular TS Block { ... }
                        // Recurse to allow macros inside standard TS objects
                        iter.next(); // Consume
                        let inner_stream = g.stream();

                        output.extend(quote! { __out.push_str("{"); });
                        let (inner_parsed, _) =
                            parse_fragment(&mut inner_stream.into_iter().peekable(), None)?;
                        output.extend(inner_parsed);
                        output.extend(quote! { __out.push_str("}"); });
                    }
                }
            }

            // Case 3: Other groups (parentheses, brackets)
            TokenTree::Group(g) => {
                iter.next();
                let (open, close) = match g.delimiter() {
                    Delimiter::Parenthesis => ("(", ")"),
                    Delimiter::Bracket => ("[", "]"),
                    Delimiter::Brace => ("{", "}"), // Shouldn't reach here
                    Delimiter::None => ("", ""),
                };

                output.extend(quote! { __out.push_str(#open); });
                let (inner_parsed, _) =
                    parse_fragment(&mut g.stream().into_iter().peekable(), None)?;
                output.extend(inner_parsed);
                output.extend(quote! { __out.push_str(#close); });
            }

            // Case 4a: Backtick template literals "'^...^'" -> `...`
            TokenTree::Literal(lit) if is_backtick_template(lit) => {
                iter.next(); // Consume
                let processed = process_backtick_template(lit)?;
                output.extend(processed);
                output.extend(quote! { __out.push_str(" "); });
            }

            // Case 4b: String literals with interpolation
            TokenTree::Literal(lit) if is_string_literal(lit) => {
                iter.next(); // Consume
                let interpolated = interpolate_string_literal(lit)?;
                output.extend(interpolated);
                output.extend(quote! { __out.push_str(" "); });
            }

            // Case 5: Plain Text
            _ => {
                let t = iter.next().unwrap();
                let s = t.to_string();

                // Analyze current token
                let is_ident = matches!(&t, TokenTree::Ident(_));
                let punct_char = if let TokenTree::Punct(p) = &t {
                    Some(p.as_char())
                } else {
                    None
                };
                let is_joint = if let TokenTree::Punct(p) = &t {
                    p.spacing() == proc_macro2::Spacing::Joint
                } else {
                    false
                };

                // Analyze next token
                let next = iter.peek();
                let next_char = match next {
                    Some(TokenTree::Punct(p)) => Some(p.as_char()),
                    _ => None,
                };

                // Emit token string
                output.extend(quote! {
                    __out.push_str(#s);
                });

                // Decide whether to append a space
                // Simplified: always add space unless followed by punctuation
                // Use {| |} for explicit no-space concatenation
                let mut add_space = true;

                // No space at end of stream (group delimiter like ) will follow)
                if next.is_none() || is_joint {
                    add_space = false;
                } else if is_ident {
                    // Identifiers need space, EXCEPT when followed by punctuation or groups
                    if matches!(next_char, Some(c) if ".,;:?()[]{}<>!".contains(c)) {
                        add_space = false;
                    } else if let Some(TokenTree::Group(g)) = next {
                        match g.delimiter() {
                            Delimiter::Parenthesis | Delimiter::Bracket => add_space = false,
                            _ => {}
                        }
                    }
                    // Always add space before @ interpolation (use {| |} for concatenation)
                } else if let Some(c) = punct_char {
                    // Punctuation specific rules
                    match c {
                        '.' => add_space = false,             // obj.prop
                        '!' => add_space = false,             // !unary or non-null!
                        '(' | '[' | '{' => add_space = false, // Openers: (expr)
                        '<' | '>' => add_space = false, // Generics: Type<T> or T>(...) (compact)
                        '@' => add_space = false,       // Decorator: @Dec
                        '$' => add_space = false,       // Svelte runes: $state, $derived
                        _ => {}
                    }

                    // Never add space if next is a closing delimiter or separator
                    if matches!(next_char, Some(nc) if ".,;)]}>".contains(nc)) {
                        add_space = false;
                    }
                } else {
                    // Groups/Literals
                    // Prevent space if next is punctuation like . , ; ) ] >
                    if matches!(next_char, Some(nc) if ".,;)]}>".contains(nc)) {
                        add_space = false;
                    }
                }

                if add_space {
                    output.extend(quote! { __out.push_str(" "); });
                }
            }
        }
    }

    Ok((output, None))
}

/// Converts a slice of tokens to a space-separated string.
///
/// This is a simple utility for converting token sequences to strings
/// when the exact formatting doesn't matter (e.g., for fallback content
/// in comment blocks).
///
/// # Arguments
///
/// * `tokens` - Slice of tokens to convert
///
/// # Returns
///
/// A string with each token separated by a single space.
///
/// # Example
///
/// ```ignore
/// let tokens = vec![ident("hello"), punct(','), ident("world")];
/// assert_eq!(tokens_to_spaced_string(&tokens), "hello , world");
/// ```
fn tokens_to_spaced_string(tokens: &[TokenTree]) -> String {
    let mut result = String::new();
    for (i, token) in tokens.iter().enumerate() {
        if i > 0 {
            result.push(' ');
        }
        result.push_str(&token.to_string());
    }
    result
}

/// Extracts the inner content from a string literal, removing quotes and unescaping.
///
/// This function handles multiple string literal formats:
/// - Regular strings: `"hello"` → `hello`
/// - Raw strings: `r"hello"` → `hello`
/// - Raw strings with hashes: `r#"hello"#` → `hello`
///
/// # Arguments
///
/// * `lit` - The string literal token to extract from
///
/// # Returns
///
/// The unquoted and unescaped string content.
///
/// # Processing
///
/// For regular strings, escape sequences are processed via [`unescape_string`]:
/// - `\n` → newline
/// - `\t` → tab
/// - `\"` → quote
/// - etc.
///
/// Raw strings are returned verbatim (no escape processing needed).
fn extract_string_literal(lit: &proc_macro2::Literal) -> String {
    let s = lit.to_string();
    // Handle regular strings "..."
    if s.starts_with('"') && s.ends_with('"') && s.len() >= 2 {
        // Unescape the string content
        let inner = &s[1..s.len() - 1];
        return unescape_string(inner);
    }
    // Handle raw strings r"..." or r#"..."#
    if s.starts_with("r\"") && s.ends_with('"') {
        return s[2..s.len() - 1].to_string();
    }
    if s.starts_with("r#\"") && s.ends_with("\"#") {
        return s[3..s.len() - 2].to_string();
    }
    // Fallback: return as-is
    s
}

/// Unescapes common escape sequences in a string.
///
/// This function processes backslash escape sequences commonly found in
/// string literals, converting them to their actual character representations.
///
/// # Arguments
///
/// * `s` - The string with potential escape sequences
///
/// # Returns
///
/// The string with escape sequences replaced by their actual characters.
///
/// # Supported Escapes
///
/// | Escape | Result |
/// |--------|--------|
/// | `\n` | Newline |
/// | `\r` | Carriage return |
/// | `\t` | Tab |
/// | `\\` | Backslash |
/// | `\"` | Double quote |
/// | `\'` | Single quote |
///
/// Unknown escape sequences are preserved as-is (e.g., `\x` becomes `\x`).
fn unescape_string(s: &str) -> String {
    let mut result = String::new();
    let mut chars = s.chars().peekable();
    while let Some(c) = chars.next() {
        if c == '\\' {
            match chars.next() {
                Some('n') => result.push('\n'),
                Some('r') => result.push('\r'),
                Some('t') => result.push('\t'),
                Some('\\') => result.push('\\'),
                Some('"') => result.push('"'),
                Some('\'') => result.push('\''),
                Some(other) => {
                    result.push('\\');
                    result.push(other);
                }
                None => result.push('\\'),
            }
        } else {
            result.push(c);
        }
    }
    result
}

/// Checks if a literal token represents a string literal.
///
/// This function identifies various string literal formats in Rust:
/// - Double-quoted strings: `"hello"`
/// - Single-quoted strings: `'c'` (char literals, treated as strings here)
/// - Raw strings: `r"hello"` or `r#"hello"#`
///
/// # Arguments
///
/// * `lit` - The literal token to check
///
/// # Returns
///
/// `true` if the literal is a string-like literal that may contain
/// interpolation patterns (`@{expr}`), `false` otherwise.
fn is_string_literal(lit: &proc_macro2::Literal) -> bool {
    let s = lit.to_string();
    s.starts_with('"') || s.starts_with('\'') || s.starts_with("r\"") || s.starts_with("r#")
}

/// Checks if a literal is a backtick template literal marker.
///
/// The syntax `"'^...^'"` is used to generate JavaScript template literals
/// with backticks. This allows embedding JS `${...}` interpolation while
/// also supporting Rust `@{...}` interpolation.
///
/// # Arguments
///
/// * `lit` - The literal token to check
///
/// # Returns
///
/// `true` if the literal matches the backtick template pattern:
/// - `"'^content^'"` - Regular string with markers
/// - `r"'^content^'"` - Raw string with markers
/// - `r#"'^content^'"#` - Raw string with hashes
///
/// # Example
///
/// ```text
/// "'^Hello ${name}^'"   -> `Hello ${name}`
/// "'^<@{tag}>^'"        -> `<div>` (if tag = "div")
/// ```
fn is_backtick_template(lit: &proc_macro2::Literal) -> bool {
    let s = lit.to_string();
    // Check for "'^...^'" pattern (the outer quotes are part of the Rust string)
    if s.starts_with("\"'^") && s.ends_with("^'\"") && s.len() >= 6 {
        return true;
    }
    // Also support raw strings: r"'^...^'" or r#"'^...^'"#
    if s.starts_with("r\"'^") && s.ends_with("^'\"") {
        return true;
    }
    if s.starts_with("r#\"'^") && s.ends_with("^'\"#") {
        return true;
    }
    false
}

/// Processes a backtick template literal and generates string-building code.
///
/// This function transforms the `"'^...^'"` syntax into JavaScript template
/// literals (backticks). It handles both JavaScript `${...}` interpolation
/// (passed through) and Rust `@{expr}` interpolation (evaluated at runtime).
///
/// # Arguments
///
/// * `lit` - The literal token containing the backtick template
///
/// # Returns
///
/// Rust code that builds the template literal string, wrapped in backticks.
///
/// # Processing
///
/// 1. Extracts content between `'^` and `^'` markers
/// 2. Outputs opening backtick
/// 3. Scans for `@{...}` patterns and generates interpolation code
/// 4. Handles `@@` escape for literal `@`
/// 5. Passes through `${...}` as-is for JS runtime interpolation
/// 6. Outputs closing backtick
///
/// # Errors
///
/// Returns an error if:
/// - Control flow tags (`{#...}`, `{/...}`, `{:...}`) are found inside
/// - `@{...}` interpolation is unclosed
/// - Expression inside `@{...}` cannot be parsed
///
/// # Example
///
/// ```text
/// Input:  "'^<@{tag}>${content}</@{tag}>^'"
/// Output: `<div>${content}</div>` (if tag = "div")
/// ```
fn process_backtick_template(lit: &proc_macro2::Literal) -> syn::Result<TokenStream2> {
    let raw = lit.to_string();
    let span = lit.span();

    // Extract content between '^...^' markers
    let content = if raw.starts_with("\"'^") && raw.ends_with("^'\"") {
        &raw[3..raw.len() - 3]
    } else if raw.starts_with("r\"'^") && raw.ends_with("^'\"") {
        &raw[4..raw.len() - 3]
    } else if raw.starts_with("r#\"'^") && raw.ends_with("^'\"#") {
        &raw[5..raw.len() - 4]
    } else {
        return Ok(quote! { __out.push_str(#raw); });
    };

    // Check for common mistakes: control flow tags inside template strings
    if content.contains("{#") || content.contains("{/") || content.contains("{:") {
        return Err(template_error(
            span,
            "Template control flow tags cannot be used inside backtick template literals",
            Some(&format!(
                "\"'^{}...^'\"",
                content.chars().take(40).collect::<String>()
            )),
        ));
    }

    // Check if there are any @{} interpolations or @@ escapes
    if !content.contains('@') {
        // No @ at all, output the backtick string as-is
        // The content may contain ${} for JS interpolation, which passes through
        let mut output = TokenStream2::new();
        output.extend(quote! { __out.push_str("`"); });
        output.extend(quote! { __out.push_str(#content); });
        output.extend(quote! { __out.push_str("`"); });
        return Ok(output);
    }

    // Handle @{} Rust interpolations and @@ escapes within the backtick template
    let mut output = TokenStream2::new();
    output.extend(quote! { __out.push_str("`"); });

    let mut chars = content.chars().peekable();
    let mut current_literal = String::new();
    let mut char_pos = 0usize;

    while let Some(c) = chars.next() {
        char_pos += 1;
        if c == '@' {
            match chars.peek() {
                Some(&'@') => {
                    // @@ -> literal @
                    chars.next(); // Consume second @
                    char_pos += 1;
                    current_literal.push('@');
                }
                Some(&'{') => {
                    // @{ -> interpolation
                    // Flush current literal
                    if !current_literal.is_empty() {
                        output.extend(quote! { __out.push_str(#current_literal); });
                        current_literal.clear();
                    }

                    chars.next(); // Consume '{'
                    char_pos += 1;
                    let expr_start_pos = char_pos;

                    // Collect expression until matching '}'
                    let mut expr_str = String::new();
                    let mut brace_depth = 1;

                    for ec in chars.by_ref() {
                        char_pos += 1;
                        if ec == '{' {
                            brace_depth += 1;
                            expr_str.push(ec);
                        } else if ec == '}' {
                            brace_depth -= 1;
                            if brace_depth == 0 {
                                break;
                            }
                            expr_str.push(ec);
                        } else {
                            expr_str.push(ec);
                        }
                    }

                    // Check for unclosed brace
                    if brace_depth != 0 {
                        return Err(template_error(
                            span,
                            &format!(
                                "Unclosed @{{}} interpolation at position {}",
                                expr_start_pos
                            ),
                            Some(&format!("@{{{}", expr_str)),
                        ));
                    }

                    // Parse the expression and generate interpolation code
                    match syn::parse_str::<syn::Expr>(&expr_str) {
                        Ok(expr) => {
                            output.extend(quote! {
                                __out.push_str(&#expr.to_string());
                            });
                        }
                        Err(parse_err) => {
                            return Err(template_error(
                                span,
                                &format!(
                                    "Invalid Rust expression in backtick template interpolation: {}",
                                    parse_err
                                ),
                                Some(&format!("@{{{}}}", expr_str)),
                            ));
                        }
                    }
                }
                _ => {
                    // Just a literal @
                    current_literal.push('@');
                }
            }
        } else {
            current_literal.push(c);
        }
    }

    // Flush remaining literal
    if !current_literal.is_empty() {
        output.extend(quote! { __out.push_str(#current_literal); });
    }

    output.extend(quote! { __out.push_str("`"); });
    Ok(output)
}

/// Processes a string literal and handles `@{expr}` interpolations inside it.
///
/// This function allows Rust expressions to be interpolated within string
/// literals in the template. It generates code that builds the string at
/// runtime, inserting evaluated expressions where `@{...}` patterns appear.
///
/// # Arguments
///
/// * `lit` - The string literal token to process
///
/// # Returns
///
/// Rust code that builds the string with interpolated values.
///
/// # Processing
///
/// 1. Determines quote character (`"` or `'`) and extracts content
/// 2. If no `@` is present, outputs the literal unchanged
/// 3. Otherwise, scans for `@{...}` patterns:
///    - `@{expr}` → evaluates `expr.to_string()` and inserts result
///    - `@@` → outputs literal `@` (escape sequence)
///    - Lone `@` → outputs literal `@`
/// 4. Preserves escape sequences (`\n`, `\t`, etc.) by passing through
///
/// # Errors
///
/// Returns an error if:
/// - Control flow tags are found inside the string (not supported)
/// - `@{...}` interpolation is unclosed
/// - Expression inside `@{...}` cannot be parsed
///
/// # Example
///
/// ```text
/// Input:  "Hello @{name}, you have @{count} messages"
/// Output: "Hello Alice, you have 5 messages" (at runtime)
/// ```
fn interpolate_string_literal(lit: &proc_macro2::Literal) -> syn::Result<TokenStream2> {
    let raw = lit.to_string();
    let span = lit.span();

    // Determine quote character and extract content
    let (quote_char, content) = if raw.starts_with('"') {
        ('"', &raw[1..raw.len() - 1])
    } else if raw.starts_with('\'') {
        ('\'', &raw[1..raw.len() - 1])
    } else if raw.starts_with("r\"") {
        // Raw string r"..."
        ('"', &raw[2..raw.len() - 1])
    } else if raw.starts_with("r#") {
        // Raw string r#"..."# - find the actual content
        let hash_count = raw[1..].chars().take_while(|&c| c == '#').count();
        let start = 2 + hash_count; // r + # + "
        let end = raw.len() - 1 - hash_count; // " + #
        ('"', &raw[start..end])
    } else {
        // Not a string we recognize, just output as-is
        return Ok(quote! { __out.push_str(#raw); });
    };

    // Check if there are any interpolations or escapes
    if !content.contains('@') {
        // No @ at all, output the string as-is
        return Ok(quote! { __out.push_str(#raw); });
    }

    // Check for common mistakes: control flow tags inside strings
    if content.contains("{#") || content.contains("{/") || content.contains("{:") {
        return Err(template_error(
            span,
            "Template control flow tags cannot be used inside string literals",
            Some(&format!(
                "\"{}...\"",
                content.chars().take(40).collect::<String>()
            )),
        ));
    }

    // Parse and interpolate
    let mut output = TokenStream2::new();
    let quote_str = quote_char.to_string();
    output.extend(quote! { __out.push_str(#quote_str); });

    let mut chars = content.chars().peekable();
    let mut current_literal = String::new();
    let mut char_pos = 0usize;

    while let Some(c) = chars.next() {
        char_pos += 1;
        if c == '@' {
            match chars.peek() {
                Some(&'@') => {
                    // @@ -> literal @
                    chars.next(); // Consume second @
                    char_pos += 1;
                    current_literal.push('@');
                }
                Some(&'{') => {
                    // @{ -> interpolation
                    // Flush current literal
                    if !current_literal.is_empty() {
                        output.extend(quote! { __out.push_str(#current_literal); });
                        current_literal.clear();
                    }

                    chars.next(); // Consume '{'
                    char_pos += 1;
                    let expr_start_pos = char_pos;

                    // Collect expression until matching '}'
                    let mut expr_str = String::new();
                    let mut brace_depth = 1;

                    for ec in chars.by_ref() {
                        char_pos += 1;
                        if ec == '{' {
                            brace_depth += 1;
                            expr_str.push(ec);
                        } else if ec == '}' {
                            brace_depth -= 1;
                            if brace_depth == 0 {
                                break;
                            }
                            expr_str.push(ec);
                        } else {
                            expr_str.push(ec);
                        }
                    }

                    // Check for unclosed brace
                    if brace_depth != 0 {
                        return Err(template_error(
                            span,
                            &format!(
                                "Unclosed @{{}} interpolation at position {}",
                                expr_start_pos
                            ),
                            Some(&format!("@{{{}", expr_str)),
                        ));
                    }

                    // Parse the expression and generate interpolation code
                    match syn::parse_str::<syn::Expr>(&expr_str) {
                        Ok(expr) => {
                            output.extend(quote! {
                                __out.push_str(&#expr.to_string());
                            });
                        }
                        Err(parse_err) => {
                            return Err(template_error(
                                span,
                                &format!(
                                    "Invalid Rust expression in string interpolation: {}",
                                    parse_err
                                ),
                                Some(&format!("@{{{}}}", expr_str)),
                            ));
                        }
                    }
                }
                _ => {
                    // Just a literal @
                    current_literal.push('@');
                }
            }
        } else if c == '\\' {
            // Handle escape sequences - pass through as-is
            current_literal.push(c);
            if chars.peek().is_some() {
                current_literal.push(chars.next().unwrap());
                char_pos += 1;
            }
        } else {
            current_literal.push(c);
        }
    }

    // Flush remaining literal
    if !current_literal.is_empty() {
        output.extend(quote! { __out.push_str(#current_literal); });
    }

    output.extend(quote! { __out.push_str(#quote_str); });

    Ok(output)
}