braillify 2.2.0

Rust 기반 크로스플랫폼 한국어 점역 라이브러리
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
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
//! Body of MathExpressionTokenRule::apply (extracted from math_expression.rs).

use crate::math_symbol_shortcut;
use crate::rules::context::EncoderState;
use crate::rules::math;
use crate::rules::token::{Token, WordToken};
use crate::rules::token_rule::TokenAction;

use super::detect::is_math_expression;
use super::helpers::*;

/// Resolve the previous and next Word neighbours, skipping over Space tokens.
/// Returns (prev, next) where each is `Some(&WordToken)` if found before hitting
/// a non-Space/Word token (e.g., PreEncoded, Fraction) or the boundary.
///
/// Extracted from `run` so the helper is directly unit-testable and mutation
/// testing can pinpoint regressions in neighbour resolution logic.
pub(super) fn prev_next_words<'a, 'b>(
    tokens: &'b [Token<'a>],
    index: usize,
) -> (
    Option<&'b crate::rules::token::WordToken<'a>>,
    Option<&'b crate::rules::token::WordToken<'a>>,
) {
    (
        index
            .checked_sub(1)
            .and_then(|i| prev_word_skip_space(tokens, i)),
        next_word_skip_space(tokens, index + 1),
    )
}

/// Walks forward from `start`, skipping `Token::Space`, returning the first
/// `Token::Word` (None on non-Word non-Space or end of slice).
pub(super) fn next_word_skip_space<'a, 'b>(
    tokens: &'b [Token<'a>],
    start: usize,
) -> Option<&'b crate::rules::token::WordToken<'a>> {
    let mut i = start;
    while let Some(tok) = tokens.get(i) {
        match tok {
            Token::Space(_) => i += 1,
            Token::Word(w) => return Some(w),
            _ => return None,
        }
    }
    None
}

/// Same as `next_word_skip_space` but with the (index, &Word) pair.
pub(super) fn next_indexed_word_skip_space<'a, 'b>(
    tokens: &'b [Token<'a>],
    start: usize,
) -> Option<(usize, &'b crate::rules::token::WordToken<'a>)> {
    let mut i = start;
    while let Some(tok) = tokens.get(i) {
        match tok {
            Token::Space(_) => i += 1,
            Token::Word(w) => return Some((i, w)),
            _ => return None,
        }
    }
    None
}

/// Walks backward from `start`, skipping `Token::Space`, returning the first
/// `Token::Word` (None on non-Word non-Space or underflow).
pub(super) fn prev_word_skip_space<'a, 'b>(
    tokens: &'b [Token<'a>],
    start: usize,
) -> Option<&'b crate::rules::token::WordToken<'a>> {
    let mut cursor = Some(start);
    while let Some(i) = cursor {
        match tokens.get(i) {
            Some(Token::Space(_)) => cursor = i.checked_sub(1),
            Some(Token::Word(w)) => return Some(w),
            _ => return None,
        }
    }
    None
}

/// Checks whether characters in `w` represent a "math letter context" that
/// should cause a following ellipsis to be encoded as the math ellipsis ⠠⠠⠠.
fn word_is_math_letter_context(w: &crate::rules::token::WordToken<'_>) -> bool {
    let has_super_sub = w.chars.iter().any(|c| {
        matches!(
            *c,
            '\u{2080}'..='\u{2089}' | '\u{00B2}' | '\u{00B3}' | '\u{2070}'..='\u{2079}'
        )
    });
    let plain_letter_list = w.chars.first().is_some_and(|c| c.is_ascii_alphabetic())
        && w.chars
            .iter()
            .all(|c| c.is_ascii_alphabetic() || matches!(*c, ',' | ''..=''));
    has_super_sub || plain_letter_list
}

/// Uppercase prose abbreviations (`FM의`, `SNS는`) and uppercase math products
/// share the same surface shape. For product notation in this branch, accept
/// ordered Roman variable runs such as `AB`, `ABC`, `CD`; leave non-sequential
/// acronyms to the English/Korean prose rules.
fn is_consecutive_ascii_letter_run(chars: &[char]) -> bool {
    chars.len() >= 2
        && chars
            .windows(2)
            .all(|pair| u32::from(pair[1]) == u32::from(pair[0]) + 1)
}

/// Whether the characters attached after a Roman closing parenthesis belong
/// to ordinary prose rather than an alphanumeric/math continuation.
///
/// Korean rule 34 explicitly attaches the Korean particle in
/// `링컨(Lincoln)은`. The same boundary applies to a multiword Roman expansion:
/// Korean text and sentence punctuation after `)` must remain on the prose
/// path, while a digit or an ASCII letter keeps the token eligible for math.
fn is_roman_parenthetical_prose_trailer(chars: impl Iterator<Item = char>) -> bool {
    chars.into_iter().all(|ch| {
        is_korean_char(ch)
            || matches!(
                ch,
                ',' | '.' | ';' | ':' | '!' | '?' | '·' | '\'' | '"' | '' | ''
            )
    })
}

fn is_roman_hyphen(ch: char) -> bool {
    matches!(
        ch,
        '-' | '\u{2010}' | '\u{2011}' | '\u{2012}' | '\u{2013}' | '\u{2014}'
    )
}

fn trim_roman_identifier_edge(chars: &[char]) -> &[char] {
    let mut start = 0usize;
    let mut end = chars.len();
    while start < end
        && matches!(
            chars[start],
            '\'' | '"' | '' | '' | '' | '' | '' | ''
        )
    {
        start += 1;
    }
    while start < end
        && matches!(
            chars[end - 1],
            ',' | '.' | ';' | ':' | '!' | '?' | '\'' | '"' | '' | '' | '' | '' | '' | ''
        )
    {
        end -= 1;
    }
    &chars[start..end]
}

fn is_decimal_separator_between_digits(chars: &[char], index: usize) -> bool {
    matches!(chars.get(index), Some('.' | ','))
        && index > 0
        && chars.get(index - 1).is_some_and(char::is_ascii_digit)
        && chars.get(index + 1).is_some_and(char::is_ascii_digit)
}

/// A Roman-led alphanumeric identifier in ordinary Korean prose, such as
/// `MP3`, `Web3.0`, or `GPT3.5`.
///
/// Korean rules 29 and 35 keep an adjoining Roman letters-sequence and number
/// in the Roman section.  A decimal point/comma is accepted only between two
/// digits.  Requiring at least two Roman letters keeps a bare algebraic shape
/// such as `x2` on the mathematical route; explicit math mode is rejected by
/// the caller as an additional boundary.
pub(super) fn is_korean_prose_roman_number_identifier(chars: &[char]) -> bool {
    let chars = trim_roman_identifier_edge(chars);
    if chars.len() < 3 || !chars.first().is_some_and(char::is_ascii_alphabetic) {
        return false;
    }

    let mut letter_count = 0usize;
    let mut has_digit = false;
    for (index, ch) in chars.iter().enumerate() {
        if ch.is_ascii_alphabetic() {
            letter_count += 1;
        } else if ch.is_ascii_digit() {
            has_digit = true;
        } else if !is_decimal_separator_between_digits(chars, index) {
            return false;
        }
    }

    letter_count >= 2 && has_digit
}

/// A print token whose numeric prefix is immediately followed by Roman
/// letters, such as `50bp`, `3.1p`, `1st`, or `3x3`.
///
/// Korean rules 29 and 35 transcribe the Roman run and the adjoining number
/// compositionally.  The same print shape can denote algebra in isolation, so
/// this predicate describes only the token grammar; the caller additionally
/// requires Korean prose context and rejects explicit/cued mathematics.
fn is_korean_prose_numeric_roman_identifier(chars: &[char]) -> bool {
    let chars = trim_roman_identifier_edge(chars);
    let mut index = 0usize;
    let mut previous_was_digit = false;

    while let Some(&ch) = chars.get(index) {
        if ch.is_ascii_digit() {
            previous_was_digit = true;
            index += 1;
            continue;
        }
        if matches!(ch, ',' | '.')
            && previous_was_digit
            && chars.get(index + 1).is_some_and(char::is_ascii_digit)
        {
            previous_was_digit = false;
            index += 1;
            continue;
        }
        break;
    }

    index > 0
        && chars.get(index).is_some_and(char::is_ascii_alphabetic)
        && chars[index..].iter().all(char::is_ascii_alphanumeric)
}

/// Korean articles 28, 29, 34 and 35 make an ASCII identifier in Korean prose
/// Roman text unless the caller selected math mode or the print contains an
/// unambiguous mathematical operator.  A hyphen alone is not such a signal:
/// the standard's `D-100` is explicitly Roman+number, and UEB treats hyphenated
/// Roman compounds as one letters-sequence context.
///
/// The surface remains ambiguous for algebra such as `x-1`.  Keep a narrow,
/// script-based default here: digit-bearing identifiers must begin with a
/// capital Roman letter or have at least two letters in the leading segment;
/// letter-only compounds need either a capitalised segment of at least two
/// letters or the lexical `K-pop`/`x-axis` shape of one-letter prefix followed
/// by a lowercase word. Thus ordinary lowercase `x-1` and uppercase `A-B`
/// stay on the math path, while model/code and lexical-compound shapes use
/// rules 28-35.
pub(super) fn is_korean_prose_roman_hyphen_identifier(chars: &[char]) -> bool {
    let chars = trim_roman_identifier_edge(chars);
    if chars.is_empty() {
        return false;
    }

    // Rule 34 enclosure: either the whole identifier is enclosed (`(K-POP)`,
    // or the opening fragment `(K-POP` of a multiword parenthetical), or the
    // enclosure is followed by a Roman continuation, e.g. `(ABC)-D`.
    let core = if chars.first() == Some(&'(') {
        match chars.iter().position(|ch| *ch == ')') {
            None => &chars[1..],
            Some(close) if close + 1 == chars.len() => &chars[1..close],
            Some(close) => {
                let enclosed = &chars[1..close];
                if enclosed.len() < 2
                    || !enclosed.iter().all(char::is_ascii_uppercase)
                    || !chars.get(close + 1).is_some_and(|ch| is_roman_hyphen(*ch))
                {
                    return false;
                }
                &chars[1..]
            }
        }
    } else {
        chars
    };

    // A parenthetical expansion after the identifier is Roman prose only when
    // the current fragment starts with letters again.  `F(x-1)` therefore
    // remains math, while a hyphenated acronym followed by a word expansion is
    // allowed to continue through subsequent whitespace tokens.
    let identifier_end = core.iter().position(|ch| *ch == '(').unwrap_or(core.len());
    if identifier_end < core.len() {
        let body = &core[identifier_end + 1..];
        if body.is_empty() || !body.iter().all(char::is_ascii_alphabetic) {
            return false;
        }
    }
    let identifier = &core[..identifier_end];

    if !identifier.iter().any(|ch| is_roman_hyphen(*ch))
        || !identifier.iter().enumerate().all(|(index, ch)| {
            ch.is_ascii_alphanumeric()
                || is_roman_hyphen(*ch)
                || *ch == ')'
                || is_decimal_separator_between_digits(identifier, index)
        })
    {
        return false;
    }

    let segments = identifier.split(|ch| is_roman_hyphen(*ch));
    let mut has_digit = false;
    let mut first_ascii_letter = None;
    let mut has_capitalised_word_segment = false;
    let mut first_segment_letter_count = 0usize;
    let mut first_segment_is_single_letter = false;
    let mut has_later_lowercase_lexical_segment = false;
    for (segment_index, raw_segment) in segments.enumerate() {
        let segment = raw_segment
            .iter()
            .copied()
            .filter(|ch| ch.is_ascii_alphanumeric())
            .collect::<Vec<_>>();
        if segment.is_empty() {
            return false;
        }
        has_digit |= segment.iter().any(char::is_ascii_digit);
        first_ascii_letter =
            first_ascii_letter.or_else(|| segment.iter().copied().find(char::is_ascii_alphabetic));
        let letter_count = segment.iter().filter(|ch| ch.is_ascii_alphabetic()).count();
        has_capitalised_word_segment |= letter_count >= 2
            && segment
                .iter()
                .find(|ch| ch.is_ascii_alphabetic())
                .is_some_and(|ch| ch.is_ascii_uppercase());
        if segment_index == 0 {
            first_segment_letter_count = letter_count;
            first_segment_is_single_letter = segment.len() == 1 && letter_count == 1;
        } else if first_segment_is_single_letter {
            has_later_lowercase_lexical_segment |=
                letter_count >= 2 && segment.iter().all(char::is_ascii_lowercase);
        }
    }

    if has_digit {
        first_segment_letter_count >= 2
            || first_ascii_letter.is_some_and(|ch| ch.is_ascii_uppercase())
    } else {
        has_capitalised_word_segment
            || (first_segment_is_single_letter && has_later_lowercase_lexical_segment)
    }
}

/// Roman identifier joined by a solidus in ordinary Korean prose.
///
/// The solidus is shared by UEB Roman text and mathematical division.  Keep
/// the mathematical one-letter fraction shapes (`A/B`, `F/N`) on the math
/// route, and recognize only identifier-like forms that start with a capital
/// and contain at least one multi-character alphanumeric segment.  This covers
/// standard prose abbreviations and model families such as `ISO/IEC` and
/// `F-5E/F` without changing an explicitly selected math context.
pub(super) fn is_korean_prose_roman_slash_identifier(chars: &[char]) -> bool {
    let chars = trim_roman_identifier_edge(chars);
    if chars.is_empty()
        || !chars.first().is_some_and(|ch| ch.is_ascii_uppercase())
        || !chars.contains(&'/')
        || !chars.iter().all(|ch| {
            ch.is_ascii_alphanumeric() || *ch == '/' || is_roman_hyphen(*ch) || *ch == '.'
        })
    {
        return false;
    }

    let mut has_letter = false;
    let mut has_multi_character_segment = false;
    for segment in chars.split(|ch| *ch == '/') {
        if segment.is_empty() || segment.iter().all(|ch| is_roman_hyphen(*ch) || *ch == '.') {
            return false;
        }
        has_letter |= segment.iter().any(char::is_ascii_alphabetic);
        has_multi_character_segment |= segment
            .iter()
            .filter(|ch| ch.is_ascii_alphanumeric())
            .count()
            >= 2;
    }
    has_letter && has_multi_character_segment
}

/// A single-letter solidus initialism can be distinguished from mathematical
/// division by 제52항 [붙임 1], which joins 대비되는 어구 across the solidus
/// (`먹이다/먹히다`): a capital-led run such as `A/S`, `B/C` or `H/W` is that
/// contrast phrase, so 제37항 wraps it as one Roman section instead of dividing.
/// A following lowercase-led Roman word still marks real division (`F/N a`).
pub(super) fn is_korean_prose_single_letter_slash_phrase(
    tokens: &[Token<'_>],
    index: usize,
    chars: &[char],
) -> bool {
    let has_strong_math_symbol = chars.iter().any(|ch| {
        math_symbol_shortcut::is_math_symbol_char(*ch)
            && !matches!(*ch, '\u{00B7}' | '\u{22C5}' | '/' | '_')
    });
    if has_strong_math_symbol {
        return false;
    }

    let has_single_letter_slash_run = (0..chars.len()).any(|start| {
        if !chars[start].is_ascii_uppercase()
            || start
                .checked_sub(1)
                .and_then(|before| chars.get(before))
                .is_some_and(|ch| ch.is_ascii_alphanumeric() || *ch == '/')
        {
            return false;
        }

        let mut cursor = start + 1;
        let mut slash_count = 0usize;
        while chars.get(cursor) == Some(&'/')
            && chars
                .get(cursor + 1)
                .is_some_and(|ch| ch.is_ascii_uppercase())
        {
            slash_count += 1;
            cursor += 2;
        }

        slash_count > 0
            && !chars
                .get(cursor)
                .is_some_and(|ch| ch.is_ascii_alphanumeric() || *ch == '/')
    });
    if !has_single_letter_slash_run {
        return false;
    }

    let Some(next_word) = next_word_skip_space(tokens, index + 1) else {
        return true;
    };
    let mut next_roman = next_word
        .chars
        .iter()
        .copied()
        .skip_while(|ch| matches!(*ch, '\'' | '"' | '' | '' | '(' | '[' | '{'))
        .take_while(|ch| ch.is_ascii_alphanumeric() || is_roman_hyphen(*ch));
    let Some(first) = next_roman.next() else {
        return next_word.chars.iter().any(|ch| is_korean_char(*ch));
    };
    first.is_ascii_uppercase()
        && next_roman.filter(char::is_ascii_alphabetic).count()
            + usize::from(first.is_ascii_alphabetic())
            >= 2
}

fn is_roman_identifier_head_separator(chars: &[char], index: usize) -> bool {
    matches!(
        chars.get(index),
        Some('.' | '/' | '-' | '' | '' | '' | '' | '')
    ) && index > 0
        && chars[index - 1].is_ascii_alphanumeric()
        && chars
            .get(index + 1)
            .is_some_and(char::is_ascii_alphanumeric)
}

/// A Roman identifier ending in one or more plus signs.
///
/// The head may combine Roman letters with adjoining digits and the ordinary
/// identifier separators already covered by Korean rules 29/32/35. A head of
/// two or more alphanumerics is structurally terminal (`TV+`, `24K+`), and a
/// repeated plus is likewise not a completed binary addition (`C++`). A
/// one-letter `A+` is terminal in ordinary prose unless a visible right operand
/// follows; explicit mathematics is rejected by the caller before this rule.
fn is_terminal_roman_plus_core(core: &[char], allow_single_letter: bool) -> bool {
    let plus_start = core
        .iter()
        .rposition(|ch| *ch != '+')
        .map_or(0, |index| index + 1);
    if plus_start == 0 || plus_start == core.len() {
        return false;
    }

    let head = &core[..plus_start];
    let plus_count = core.len() - plus_start;
    if head.contains(&'+')
        || !head.iter().enumerate().all(|(index, ch)| {
            ch.is_ascii_alphanumeric() || is_roman_identifier_head_separator(head, index)
        })
        || !head.iter().any(char::is_ascii_alphabetic)
    {
        return false;
    }

    let alphanumeric_count = head.iter().filter(|ch| ch.is_ascii_alphanumeric()).count();
    alphanumeric_count >= 2
        || plus_count >= 2
        || (allow_single_letter && head.len() == 1 && head[0].is_ascii_uppercase())
}

/// A capitals grade or rating ending in one minus sign (`AA-`, `BBB-`).
///
/// UEB 3.17.1 writes the minus of non-technical material as `⠐⠤`, and Korean
/// rule 32 keeps it inside the Roman section like the terminal plus of `A+`.
/// The head is two or three capitals (a single letter is 제68항's grade
/// notation) and the sign closes the item, so no right operand can turn it
/// into 제46항 subtraction.
pub(super) fn is_korean_prose_roman_minus_grade(chars: &[char]) -> bool {
    let chars = trim_roman_identifier_edge(chars);
    let Some(minus) = chars.iter().position(|ch| *ch == '-') else {
        return false;
    };
    let head_len = chars[..minus]
        .iter()
        .rev()
        .take_while(|ch| ch.is_ascii_uppercase())
        .count();
    let before_head = &chars[..minus - head_len];
    let trailer = &chars[minus + 1..];
    let closers = trailer
        .iter()
        .take_while(|ch| is_terminal_plus_closer_char(**ch))
        .count();
    (2..=3).contains(&head_len)
        && before_head
            .iter()
            .all(|ch| is_korean_char(*ch) || matches!(ch, '(' | '[' | '{' | '' | '' | '\'' | '"'))
        && (trailer.is_empty() || closers > 0)
        && trailer[closers..].iter().all(|ch| is_korean_char(*ch))
}

fn is_attached_plus_prose_trailer_char(ch: char) -> bool {
    is_korean_char(ch)
        || matches!(
            ch,
            '(' | ')'
                | '['
                | ']'
                | '{'
                | '}'
                | ','
                | '.'
                | ';'
                | ':'
                | '!'
                | '?'
                | '\''
                | '"'
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
        )
}

fn is_terminal_plus_closer_char(ch: char) -> bool {
    matches!(
        ch,
        ')' | ']'
            | '}'
            | ','
            | '.'
            | ';'
            | ':'
            | '!'
            | '?'
            | '\''
            | '"'
            | ''
            | ''
            | ''
            | ''
            | ''
            | ''
    )
}

/// Roman product, service, or lexical compound using a plus sign.
///
/// A completed mathematical addition necessarily has a right operand, whereas
/// a terminal `+` is a common part of a Roman identifier (`TV+`, `HDR10+`). A
/// Korean particle or annotation may be attached directly after that core, and
/// repeated plus signs remain part of the same identifier. A one-letter
/// terminal form stays on this prose path unless a parenthesized ASCII operand
/// completes the expression; explicit math mode remains math-owned.
///
/// A plus between letter-led Roman items is likewise lexical (`Dog+Yoga`,
/// `SF+AW`, `X+U`): in Korean prose such a pair is a product or brand name and
/// Korean rule 32 keeps it inside one Roman section with the UEB plus. Only a
/// single lowercase letter on either side keeps the algebraic reading of 수학
/// 제12항 (`x+y`), and a digit-led left operand (`7+UP`, `1+2a`) is a sum.
/// Finally, a single capital immediately followed by `+` and attached Hangul
/// (`U+유모바일`) is a Roman brand prefix followed by Korean text; Article 46
/// would require spaces around a genuine Korean addition.
pub(super) fn is_korean_prose_roman_plus_identifier(chars: &[char]) -> bool {
    let chars = trim_roman_identifier_edge(chars);
    if chars.is_empty() {
        return false;
    }

    let roman_end = chars
        .iter()
        .take_while(|ch| {
            ch.is_ascii_alphanumeric()
                || **ch == '+'
                || matches!(**ch, '.' | '/' | '-' | '' | '' | '' | '' | '')
        })
        .count();
    let core = &chars[..roman_end];
    let trailer = &chars[roman_end..];
    let korean_led_mixed_trailer = trailer.first().is_some_and(|ch| is_korean_char(*ch))
        && trailer
            .iter()
            .all(|ch| ch.is_ascii_alphanumeric() || is_attached_plus_prose_trailer_char(*ch));
    let trailer_is_prose = trailer.is_empty()
        || trailer.first() == Some(&'(')
        || korean_led_mixed_trailer
        || trailer
            .iter()
            .copied()
            .all(is_attached_plus_prose_trailer_char);
    if !trailer_is_prose {
        return false;
    }

    let has_korean_trailer = trailer.iter().any(|ch| is_korean_char(*ch));
    let allow_single_letter = trailer.is_empty()
        || has_korean_trailer
        || trailer.iter().copied().all(is_terminal_plus_closer_char);
    if is_terminal_roman_plus_core(core, allow_single_letter) {
        return true;
    }

    if !core.first().is_some_and(|ch| ch.is_ascii_uppercase()) {
        return false;
    }

    if !core.contains(&'+') {
        return false;
    }

    if !core
        .iter()
        .all(|ch| ch.is_ascii_alphanumeric() || *ch == '+')
    {
        return false;
    }

    // Korean rule 32 + UEB 3.17: a plus joining letter-led Roman items
    // (`SF+AW`, `X+U`, `DNA+BIG3`) is part of one Roman identifier. Only a
    // single lowercase letter keeps the algebraic reading (`x+y`, 수학 제12항).
    let segments = core.split(|ch| *ch == '+').collect::<Vec<_>>();
    segments.len() >= 2
        && segments.iter().all(|segment| {
            segment.first().is_some_and(char::is_ascii_alphabetic)
                && !(segment.len() == 1 && segment[0].is_ascii_lowercase())
        })
}

/// A Korean word may immediately introduce a parenthesized Roman lexical
/// compound (`도가(Dog+Yoga)`).  Prove the Korean prefix and a closed Roman
/// body, then reuse the same plus grammar.  Text following the close must be
/// ordinary Korean prose or punctuation, never another ASCII operand.
pub(super) fn has_korean_prefix_roman_plus_annotation(chars: &[char]) -> bool {
    chars.iter().enumerate().any(|(start, ch)| {
        if !ch.is_ascii_alphabetic() || !chars[..start].iter().any(|prefix| is_korean_char(*prefix))
        {
            return false;
        }

        let suffix = &chars[start..];
        let Some(close) = suffix.iter().position(|candidate| *candidate == ')') else {
            return false;
        };
        let trailer = &suffix[close + 1..];
        close > 0
            && (is_korean_prose_roman_plus_identifier(&suffix[..close])
                || is_terminal_roman_plus_core(&suffix[..close], true))
            && (is_roman_parenthetical_prose_trailer(trailer.iter().copied())
                || trailer
                    .first()
                    .is_some_and(|ch| is_korean_char(*ch) || *ch == '·'))
    })
}

/// A Korean lexical prefix may attach directly to a terminal Roman identifier
/// (`한글TV+는`). Once the first Roman/digit run after Korean is found, reuse
/// the same terminal-plus grammar. A later operand after an earlier plus is not
/// a new start, so `한글A+B` remains math-owned.
pub(super) fn has_korean_prefix_terminal_roman_plus_identifier(chars: &[char]) -> bool {
    chars.iter().enumerate().any(|(start, ch)| {
        ch.is_ascii_alphanumeric()
            && chars[..start].iter().any(|prefix| is_korean_char(*prefix))
            && start
                .checked_sub(1)
                .and_then(|index| chars.get(index))
                .is_none_or(|previous| !previous.is_ascii_alphanumeric() && *previous != '+')
            && is_korean_prose_roman_plus_identifier(&chars[start..])
    })
}

/// A Korean word may attach directly to a hyphenated Roman identifier in two
/// directions: an enclosed Roman run can continue after a hyphen
/// (`한글(ABC)-D`), or the Korean run itself can be followed by a Roman
/// label (`하쿠토-R`, `기장-KBO`). Korean rule 33 proves that `-` at a
/// Korean/Roman boundary is punctuation rather than mathematical subtraction;
/// rules 29 and 35 then own the Roman run.
///
/// A single lowercase letter remains ambiguous algebra (`값-x`), and an
/// explicit operator after the Roman start remains math-owned (`값-x+1`).
pub(super) fn has_korean_prefix_roman_hyphen_suffix(chars: &[char]) -> bool {
    for (index, ch) in chars.iter().enumerate() {
        if ch.is_ascii_alphabetic()
            && chars[..index]
                .iter()
                .any(|prefix| crate::utils::is_korean_char(*prefix))
            && is_korean_prose_roman_hyphen_identifier(&chars[index..])
        {
            return true;
        }
    }

    chars.windows(3).enumerate().any(|(index, window)| {
        if !crate::utils::is_korean_char(window[0])
            || window[1] != '-'
            || !window[2].is_ascii_alphabetic()
        {
            return false;
        }

        let roman_tail = &chars[index + 2..];
        let identifier_len = roman_tail
            .iter()
            .take_while(|ch| ch.is_ascii_alphanumeric())
            .count();
        let letter_count = roman_tail[..identifier_len]
            .iter()
            .filter(|ch| ch.is_ascii_alphabetic())
            .count();
        let identifier_is_unambiguous = window[2].is_ascii_uppercase() || letter_count >= 2;
        let has_explicit_math_operator = roman_tail.iter().any(|ch| {
            matches!(
                *ch,
                '+' | ''
                    | '×'
                    | '÷'
                    | '='
                    | '<'
                    | '>'
                    | ''
                    | ''
                    | ''
                    | ''
                    | '^'
                    | '_'
                    | '/'
                    | '*'
                    | '|'
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
            )
        });

        identifier_is_unambiguous && !has_explicit_math_operator
    })
}

/// Whether a spaced `A(31)`-shaped label is followed by ordinary Korean prose.
///
/// A print-space plus a Korean person role (`도의원`, `교수`, `부장판사`)
/// resolves the same function-notation ambiguity as an honorific does.  The
/// explicit mathematical value/product cues remain on the math route.  This
/// predicate deliberately requires a real source space and an all-Korean next
/// word; attached particles are handled by the narrower label splitter.
pub(super) fn next_word_begins_korean_prose_label_context(
    tokens: &[Token<'_>],
    index: usize,
) -> bool {
    if !matches!(tokens.get(index + 1), Some(Token::Space(_)))
        || next_word_starts_with_math_value_cue(tokens, index)
    {
        return false;
    }

    next_indexed_word_skip_space(tokens, index + 1).is_some_and(|(next_index, word)| {
        next_index > index + 1
            && word.chars.iter().any(|ch| is_korean_char(*ch))
            && word
                .chars
                .iter()
                .all(|ch| is_korean_char(*ch) || matches!(*ch, ',' | '.' | '!' | '?'))
    })
}

/// Rule 34 parenthetical Roman prose headed by a multi-character acronym.
/// Requiring at least two alphanumeric head characters and rejecting math
/// operators keeps `f(x)` / `A(x+1)` in the math engine.
pub(super) fn is_korean_prose_acronym_parenthetical(chars: &[char]) -> bool {
    let chars = trim_roman_identifier_edge(chars);
    let Some(open) = chars.iter().position(|ch| *ch == '(') else {
        return false;
    };
    let head = &chars[..open];
    if head.len() < 2
        || !head.iter().all(char::is_ascii_alphanumeric)
        || !head.iter().any(char::is_ascii_uppercase)
    {
        return false;
    }

    let after_open = &chars[open + 1..];
    let close = after_open.iter().position(|ch| *ch == ')');
    let body = close.map_or(after_open, |index| &after_open[..index]);
    if body.is_empty()
        || !body
            .iter()
            .all(|ch| ch.is_ascii_alphanumeric() || is_roman_hyphen(*ch))
    {
        return false;
    }
    close.is_none_or(|index| {
        is_roman_parenthetical_prose_trailer(after_open[index + 1..].iter().copied())
    })
}

/// 수식은 괄호가 맞물린다. 한쪽만 남은 괄호는 앞뒤 어절로 이어지는 산문의 조각
/// 이므로(`LTE),`, `S(PLAN`) 제11항의 경계를 두지 않는다.
fn has_balanced_brackets(chars: &[char]) -> bool {
    let mut depth = 0i32;
    for ch in chars {
        match ch {
            '(' | '[' | '{' => depth += 1,
            ')' | ']' | '}' => depth -= 1,
            _ => {}
        }
        if depth < 0 {
            return false;
        }
    }
    depth == 0
}

/// 붙임표로 이은 로마자 낱말(`know-how`, `well-made`)은 제37항의 로마자이지 뺄셈이
/// 아니다. 숫자도 관계 기호도 없이 글자와 붙임표만 있으면 수식으로 보지 않는다.
fn is_hyphenated_roman_word(chars: &[char]) -> bool {
    chars.contains(&'-')
        && chars
            .iter()
            .all(|ch| ch.is_ascii_alphabetic() || matches!(*ch, '-' | '.' | ','))
}
/// 제11항의 두 칸은 그 어절이 수식임을 알린다. 숫자를 구분 기호로 이어 적은 표기
/// (시각 `22:00`, 비율 `16:9`, 해상도 `1280×720`, 범위 `150~200`)는 제40항의 수
/// 표기이지 수식이 아니다. 단위나 모델을 나타내는 글자가 숫자에 붙은 것(`5G`,
/// `F1.2`, `3G/4G`)도 마찬가지이므로, 숫자에서 떨어진 글자 — 곧 변수 — 나 관계
/// 기호가 있어야 수식으로 본다.
fn is_prose_number_notation(chars: &[char]) -> bool {
    if !chars.iter().any(char::is_ascii_digit) {
        return false;
    }
    // 함수 이름(`log2`, `sin3x`), 숫자끼리의 가운뎃점 곱(`6·9`), 숫자 뒤에 이어진
    // 두 글자 이상의 변수열(`3ab`)은 수식이다.
    let text: String = chars.iter().collect();
    if crate::rules::math::function::starts_with_function(&text)
        || text
            .char_indices()
            .any(|(at, _)| crate::rules::math::function::starts_with_function(&text[at..]))
    {
        return false;
    }
    if chars.windows(3).any(|window| {
        window[0].is_ascii_digit() && window[1] == '\u{00B7}' && window[2].is_ascii_digit()
    }) {
        return false;
    }
    if chars.windows(3).any(|window| {
        window[0].is_ascii_digit()
            && window[1].is_ascii_lowercase()
            && window[2].is_ascii_lowercase()
    }) {
        return false;
    }
    if chars
        .iter()
        .any(|c| matches!(c, '=' | '<' | '>' | '\u{2260}' | '\u{2264}' | '\u{2265}'))
    {
        return false;
    }
    // 더하기·빼기는 수식의 연산이므로(`2x+3`) 구분 기호로 보지 않는다. 여기 남긴
    // 것은 수를 이어 적는 자리 표시뿐이다.
    let is_separator = |c: char| {
        c.is_ascii_digit()
            || matches!(
                c,
                '.' | ',' | ':' | '\u{00D7}' | '/' | '\u{00B7}' | '~' | '\u{223C}' | '(' | ')'
            )
    };
    chars.iter().enumerate().all(|(index, c)| {
        if is_separator(*c) {
            return true;
        }
        // 숫자에 잇닿은 글자는 단위·모델 표시다. 떨어져 선 글자는 변수이므로
        // 그 어절은 수식이다.
        c.is_ascii_alphabetic()
            && (index
                .checked_sub(1)
                .is_some_and(|previous| chars[previous].is_ascii_digit())
                || chars.get(index + 1).is_some_and(char::is_ascii_digit)
                || index
                    .checked_sub(1)
                    .is_some_and(|previous| chars[previous].is_ascii_alphabetic())
                || chars.get(index + 1).is_some_and(char::is_ascii_alphabetic))
    })
}
fn has_ascii_letter_korean_math_suffix(chars: &[char]) -> bool {
    if chars.len() < 3 {
        return false;
    }

    let ascii_prefix_len = chars.iter().take_while(|c| c.is_ascii_alphabetic()).count();
    (2..=3).contains(&ascii_prefix_len)
        && chars[ascii_prefix_len..]
            .first()
            .is_some_and(|c| matches!(*c, '' | '' | ''))
        && chars[ascii_prefix_len..]
            .iter()
            .all(|c| is_korean_suffix_char(*c))
}

fn next_word_starts_with_math_value_cue(tokens: &[Token<'_>], index: usize) -> bool {
    let mut cursor = index + 1;
    while let Some(token) = tokens.get(cursor) {
        match token {
            Token::Space(_) => cursor += 1,
            Token::Word(word) => {
                let text = word.text.as_ref();
                if text.starts_with('') || text.starts_with('') {
                    return true;
                }
                // 식별자를 나열한 어절(`AB와 CD의 값을`)은 그 자체가 단서는 아니지만
                // 단서를 뒤로 미룰 뿐이므로 건너뛰고 계속 찾는다. 나열이 끝나도록
                // 단서가 없으면 그 나열은 수식이 아니다.
                if !has_ascii_letter_korean_math_suffix(&word.chars) {
                    return false;
                }
                cursor += 1;
            }
            _ => return false,
        }
    }
    false
}

fn prev_word_is_math_product_cue(tokens: &[Token<'_>], index: usize) -> bool {
    index
        .checked_sub(1)
        .and_then(|start| prev_word_skip_space(tokens, start))
        .is_some_and(|word| word.text.as_ref() == "")
}

/// Returns true when `word` is the final fragment of a whitespace-split,
/// closed Roman parenthetical whose earlier fragments contain letters only.
///
/// Korean rules 29 and 34 keep consecutive Roman words in one Roman section
/// and omit its terminator before the closing parenthesis. UEB 9.7.1 likewise
/// prints multiword prose inside one paired parenthesis. The token parser keeps
/// the spaces as separate tokens, so the final `Letters)` fragment must not be
/// mistaken for a standalone mathematical expression merely because it has a
/// closing bracket. A lowercase/mixed-case ASCII letter immediately before the
/// opening parenthesis is excluded so function-call syntax such as `f(x)`
/// remains math-owned; a complete all-capitals initialism (`WTO(World ...),`)
/// is the ordinary rule-29/34 prose form.
fn is_multiword_closed_roman_parenthetical_tail(
    tokens: &[Token<'_>],
    index: usize,
    word: &WordToken<'_>,
) -> bool {
    let Some(close) = word.chars.iter().position(|ch| *ch == ')') else {
        return false;
    };
    let body = &word.chars[..close];
    let trailing = &word.chars[close + 1..];
    if body.is_empty()
        || !body.iter().all(char::is_ascii_alphabetic)
        || !is_roman_parenthetical_prose_trailer(trailing.iter().copied())
    {
        return false;
    }

    let mut cursor = index.checked_sub(1);
    while let Some(i) = cursor {
        match tokens.get(i) {
            Some(Token::Space(_)) => cursor = i.checked_sub(1),
            Some(Token::Word(previous)) => {
                let previous_text = previous.text.as_ref();
                if let Some(open) = previous_text.rfind('(') {
                    let before = &previous_text[..open];
                    let after = &previous_text[open + 1..];
                    if after.is_empty() || !after.chars().all(|ch| ch.is_ascii_alphabetic()) {
                        return false;
                    }
                    let before_is_initialism = before.chars().count() >= 2
                        && before.chars().all(|ch| ch.is_ascii_uppercase());
                    if before
                        .chars()
                        .next_back()
                        .is_some_and(|ch| ch.is_ascii_alphabetic())
                        && !before_is_initialism
                    {
                        return false;
                    }
                    return before.find(['(', ')']).is_none();
                }
                if previous_text.chars().all(|ch| ch.is_ascii_alphabetic()) {
                    cursor = i.checked_sub(1);
                } else {
                    return false;
                }
            }
            _ => return false,
        }
    }
    false
}

/// Returns true when `word` begins a closed, multiword Roman expansion headed
/// by a complete all-capitals abbreviation.
///
/// Korean rules 29 and 34 make this ordinary Roman prose: the headword starts
/// a Roman section, and the spaces inside the paired parenthesis do not split
/// that section. The narrow grammar excludes single variables, digits,
/// operators, nested brackets, and an alphanumeric continuation after `)` so
/// mathematical expressions remain owned by the math parser.
fn is_multiword_closed_roman_parenthetical_head(
    tokens: &[Token<'_>],
    index: usize,
    word: &WordToken<'_>,
) -> bool {
    let text = word.text.as_ref();
    let Some(open) = text.find('(') else {
        return false;
    };
    let head = &text[..open];
    let first_body_word = &text[open + 1..];
    if head.chars().count() < 2
        || !head.chars().all(|ch| ch.is_ascii_uppercase())
        || first_body_word.is_empty()
        || !first_body_word.chars().all(|ch| ch.is_ascii_alphabetic())
    {
        return false;
    }

    let mut cursor = index + 1;
    let mut body_words = 1usize;
    loop {
        let mut saw_space = false;
        while matches!(tokens.get(cursor), Some(Token::Space(_))) {
            saw_space = true;
            cursor += 1;
        }
        if !saw_space {
            return false;
        }
        let Some(Token::Word(next)) = tokens.get(cursor) else {
            return false;
        };
        let next_text = next.text.as_ref();
        if let Some(close) = next_text.find(')') {
            let final_body_word = &next_text[..close];
            let trailing = &next_text[close + 1..];
            body_words += 1;
            return body_words >= 2
                && !final_body_word.is_empty()
                && final_body_word.chars().all(|ch| ch.is_ascii_alphabetic())
                && is_roman_parenthetical_prose_trailer(trailing.chars());
        }
        if next_text.is_empty() || !next_text.chars().all(|ch| ch.is_ascii_alphabetic()) {
            return false;
        }
        body_words += 1;
        cursor += 1;
    }
}

/// Returns whether `index` belongs to a complete prose parenthetical that is
/// attached directly to Korean text.
///
/// Korean rules 34 and 54 keep the Korean parenthesis outside the enclosed
/// Roman section (`링컨(Lincoln)은`: `⠦⠄⠴...⠠⠴`).  The generic mathematics
/// detector must therefore not take ownership merely because the enclosed
/// text is an all-capitals identifier, an alphanumeric name, or a decimal.
/// This scan covers a parenthetical split across whitespace tokens as well as
/// a digit immediately following its close (`용어(Web)3`).
///
/// A one-letter variable and an expression carrying an unambiguous operator
/// remain math-owned.  This is the structural distinction between the rule-34
/// prose form and ordinary function/expression notation such as `함수(x+1)`.
fn is_within_attached_korean_prose_parenthetical(tokens: &[Token<'_>], index: usize) -> bool {
    #[derive(Clone, Copy)]
    struct Opening {
        token_index: usize,
        char_index: usize,
        attached_to_korean_prose: bool,
    }

    fn enclosed_chars(
        tokens: &[Token<'_>],
        opening: Opening,
        close_token_index: usize,
        close_char_index: usize,
    ) -> Vec<char> {
        let mut body = Vec::new();
        for (token_index, token) in tokens
            .iter()
            .enumerate()
            .take(close_token_index + 1)
            .skip(opening.token_index)
        {
            match token {
                Token::Word(word) => {
                    let start = if token_index == opening.token_index {
                        opening.char_index + 1
                    } else {
                        0
                    };
                    let end = if token_index == close_token_index {
                        close_char_index
                    } else {
                        word.chars.len()
                    };
                    if start <= end && end <= word.chars.len() {
                        body.extend_from_slice(&word.chars[start..end]);
                    }
                }
                Token::Space(_) => body.push(' '),
                Token::Mode(_) => {}
                Token::Fraction(_) | Token::PreEncoded(_) => return Vec::new(),
            }
        }
        body
    }

    fn is_prose_body(body: &[char]) -> bool {
        let body = body
            .iter()
            .copied()
            .skip_while(|ch| ch.is_whitespace())
            .collect::<Vec<_>>();
        let body = body
            .iter()
            .copied()
            .rev()
            .skip_while(|ch| ch.is_whitespace())
            .collect::<Vec<_>>()
            .into_iter()
            .rev()
            .collect::<Vec<_>>();
        if body.is_empty() || body.iter().any(|ch| matches!(*ch, '(' | ')')) {
            return false;
        }

        // Operators which cannot be ordinary punctuation or part of a Roman
        // identifier make the enclosure an explicit mathematical expression.
        if body.iter().any(|ch| {
            matches!(
                *ch,
                '=' | '<'
                    | '>'
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | '×'
                    | '÷'
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | '^'
                    | '_'
            )
        }) {
            return false;
        }

        // A Korean explanation inside an attached parenthesis is prose.  Its
        // embedded Roman/numeric fragments are still handled compositionally
        // by rules 28-35 after this token rule declines the whole expression.
        if body.iter().any(|ch| is_korean_char(*ch)) {
            return true;
        }

        let numeric_annotation = body.iter().any(char::is_ascii_digit)
            && body.iter().all(|ch| {
                ch.is_ascii_digit()
                    || ch.is_whitespace()
                    || matches!(*ch, '.' | ',' | '%' | '' | '+' | '-' | '' | '~')
            });
        if numeric_annotation {
            return true;
        }

        let ascii_alphanumeric_count = body.iter().filter(|ch| ch.is_ascii_alphanumeric()).count();
        let has_ascii_letter = body.iter().any(char::is_ascii_alphabetic);
        ascii_alphanumeric_count >= 2
            && has_ascii_letter
            && body.iter().all(|ch| {
                ch.is_ascii_alphanumeric()
                    || ch.is_whitespace()
                    || matches!(
                        *ch,
                        ',' | '.'
                            | ':'
                            | ';'
                            | '\''
                            | ''
                            | '-'
                            | ''
                            | ''
                            | ''
                            | ''
                            | ''
                            | '/'
                            | '&'
                            | '·'
                            | ''
                    )
            })
    }

    let mut openings = Vec::<Opening>::new();
    for (token_index, token) in tokens.iter().enumerate() {
        let Token::Word(word) = token else {
            continue;
        };
        for (char_index, ch) in word.chars.iter().copied().enumerate() {
            match ch {
                '(' => openings.push(Opening {
                    token_index,
                    char_index,
                    attached_to_korean_prose: {
                        let prefix = &word.chars[..char_index];
                        let prefix_contains_korean = prefix.iter().any(|ch| is_korean_char(*ch));
                        let numeric_prefix = !prefix.is_empty()
                            && prefix.iter().any(char::is_ascii_digit)
                            && prefix.iter().all(|ch| {
                                ch.is_ascii_digit()
                                    || matches!(*ch, '.' | ',' | '\'' | '' | '"' | '' | '' | '')
                            });
                        let quote_only_prefix = prefix
                            .iter()
                            .all(|ch| matches!(*ch, '\'' | '' | '"' | '' | '' | ''));
                        prefix_contains_korean
                            || (numeric_prefix && has_adjacent_korean_word(tokens, token_index))
                            || (quote_only_prefix
                                && adjacent_korean_word_flags(tokens, token_index).0)
                    },
                }),
                ')' => {
                    let Some(opening) = openings.pop() else {
                        continue;
                    };
                    if opening.attached_to_korean_prose
                        && opening.token_index <= index
                        && index <= token_index
                        && is_prose_body(&enclosed_chars(tokens, opening, token_index, char_index))
                    {
                        return true;
                    }
                }
                _ => {}
            }
        }
    }
    false
}

/// Walks backward from `index - 1`, skipping `Space`, returning whether the
/// preceding content is a math-letter Word or a math-context PreEncoded.
fn prev_is_math_context_for_ellipsis(tokens: &[Token<'_>], index: usize) -> bool {
    let mut cursor = index.checked_sub(1);
    while let Some(i) = cursor {
        match tokens.get(i) {
            Some(Token::Space(_)) => cursor = i.checked_sub(1),
            Some(Token::PreEncoded(_)) => return true,
            Some(Token::Word(w)) => return word_is_math_letter_context(w),
            _ => return false,
        }
    }
    false
}

/// Walks backward from `index - 1` skipping `Space`; true if any
/// `Word | PreEncoded` is found before underflow.
fn has_content_skipping_space_backward(tokens: &[Token<'_>], index: usize) -> bool {
    let mut cursor = index.checked_sub(1);
    while let Some(i) = cursor {
        match tokens.get(i) {
            Some(Token::Space(_)) => cursor = i.checked_sub(1),
            Some(Token::Word(_) | Token::PreEncoded(_)) => return true,
            _ => return false,
        }
    }
    false
}

/// Walks forward from `index + 1` skipping `Space`; true if any
/// `Word | PreEncoded` is found before slice end.
fn has_content_skipping_space_forward(tokens: &[Token<'_>], index: usize) -> bool {
    let mut i = index + 1;
    while let Some(tok) = tokens.get(i) {
        match tok {
            Token::Space(_) => i += 1,
            Token::Word(_) | Token::PreEncoded(_) => return true,
            _ => return false,
        }
    }
    false
}

/// True iff `text` has the special increment-equality-polysum pattern
/// (∆ + `=` + `)+(`) that requires a double-space prefix per PDF 제11항.
fn is_delta_eq_polysum_pattern(text: &str) -> bool {
    text.contains('\u{2206}') && text.contains('=') && text.contains(")+(")
}

/// True iff the Word's chars are all Korean (Hangul syllables / jamo) plus
/// punctuation/whitespace. Used to decide whether a math expression needs a
/// trailing-space delimiter before the following Word.
fn word_is_pure_korean(w: &crate::rules::token::WordToken<'_>) -> bool {
    if !w.meta.has_korean {
        return false;
    }
    w.chars.iter().all(|c| {
        let code = *c as u32;
        (0xAC00..=0xD7A3).contains(&code)
            || (0x3131..=0x3163).contains(&code)
            || matches!(*c, '.' | ',' | '!' | '?' | ' ')
    })
}

/// True iff `text` contains a character that needs explicit decimal-context
/// spacing — the internal U+001F Unit Separator (used as a math-context
/// sentinel), the U+22EF MIDLINE HORIZONTAL ELLIPSIS, or any combining math
/// mark in `chars`.
fn needs_decimal_context_spacing(text: &str, chars: &[char]) -> bool {
    text.contains('\u{001F}')
        || text.contains('\u{22EF}')
        || chars.iter().any(|ch| is_combining_math_mark(*ch))
}

/// Walks backward from `index - 1` skipping at most one Space, then checks
/// whether the token beyond the Space is a math/mixed-math context (used to
/// decide `leading_delimiter_len` in the non-`$...$` mixed-math fallback).
fn prev_prev_is_math_or_mixed_context(tokens: &[Token<'_>], index: usize) -> bool {
    let mut i = index;
    let mut found_space = false;
    while i > 0 {
        i -= 1;
        match tokens.get(i) {
            Some(Token::Space(_)) => found_space = true,
            Some(Token::PreEncoded(_) | Token::Fraction(_)) if found_space => return true,
            Some(Token::Word(w)) if found_space => {
                return is_math_expression(&w.chars, w.text.as_ref())
                    || (w.meta.has_korean
                        && is_strong_mixed_math_candidate(&w.chars, w.text.as_ref()));
            }
            _ => return false,
        }
    }
    false
}

/// Detect one unambiguous set/logic symbol from math rules 60-61.
///
/// These Unicode signs are not Roman-prose punctuation. A separated adjacent
/// capital therefore remains a math variable instead of entering UEB grade-1
/// text (`A ¬ B`, `{x | x ∈ R}`).
pub(super) fn is_set_or_logic_symbol_word(word: &crate::rules::token::WordToken<'_>) -> bool {
    word.chars.first().is_some_and(|c| {
        word.chars.len() == 1
            && matches!(
                *c,
                '¬' | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
                    | ''
            )
    })
}

/// PDF — Compute leading spaces for a math token inserted at `index` based on
/// surrounding token context. Extracted to a standalone helper so each branch
/// gets a unique line attribution under tarpaulin.
fn compute_leading_spaces(
    tokens: &[Token<'_>],
    index: usize,
    in_prose: bool,
    inner_is_single_letter: bool,
    comma_list: bool,
    inner_is_simple_numeric: bool,
) -> usize {
    let suppress_pad = (in_prose && (inner_is_single_letter || comma_list))
        || inner_is_simple_numeric
        || index == 0;
    if suppress_pad {
        return 0;
    }
    // PDF — Math tokens in production always have a preceding Space token if
    // `index > 0`. Probe-verified 2026-05-23: no testcase reaches a math token
    // at index > 0 without a Space immediately before it. The `return 2`
    // fallback was structurally unreachable; if state ever shifts, treating
    // missing-Space prev as the 0-pad case is the safest default.
    let prev_prev = index.checked_sub(2).and_then(|i| tokens.get(i));
    let prev_prev_is_korean = matches!(prev_prev, Some(Token::Word(w)) if w.meta.has_korean);
    if prev_prev_is_korean { 1 } else { 0 }
}

pub(super) fn run<'a>(
    tokens: &[Token<'a>],
    index: usize,
    state: &mut EncoderState,
) -> Result<TokenAction<'a>, String> {
    let Some(Token::Word(word)) = tokens.get(index) else {
        return Ok(TokenAction::Noop);
    };

    let text = word.text.as_ref();

    // Preserve the more specific anonymized-person grammar before the general
    // rule-34 prose-parenthetical guard below.  A Korean name fragment may be
    // attached before the Roman initial (`모A(61)씨`), so this must split and
    // retain that prefix rather than merely declining whole-token math.
    if state.english_indicator
        && !state.math_mode_active
        && let Some(replacement) = split_anonymized_person_label(&word.chars)
    {
        return Ok(TokenAction::ReplaceMany(replacement));
    }

    if is_multiword_closed_roman_parenthetical_head(tokens, index, word)
        || is_multiword_closed_roman_parenthetical_tail(tokens, index, word)
        || is_within_attached_korean_prose_parenthetical(tokens, index)
    {
        return Ok(TokenAction::Noop);
    }

    // Korean rules 29, 35, 54: in anonymized-person prose, encode the Roman
    // initial, Korean parentheses and age compositionally even when the
    // following Korean honorific/role is separated by a print-space.  The
    // following word is deliberately left as its own token so source spacing
    // is preserved.
    if state.english_indicator
        && !state.math_mode_active
        && next_word_begins_korean_prose_label_context(tokens, index)
        && let Some(encoded) = encode_anonymized_person_label(&word.chars)
    {
        return Ok(TokenAction::Replace(Token::PreEncoded(encoded)));
    }

    // In ordinary Korean prose, rules 28-35 own structurally Roman identifiers.
    // Do this before the generic `letter + operator` math detector: ASCII '-' is
    // both a math minus candidate and the hyphen used by the official `D-100`.
    if state.english_indicator
        && !state.math_mode_active
        && (is_korean_prose_roman_hyphen_identifier(&word.chars)
            || is_korean_prose_roman_number_identifier(&word.chars)
            || is_korean_prose_roman_slash_identifier(&word.chars)
            || is_korean_prose_single_letter_slash_phrase(tokens, index, &word.chars)
            || is_korean_prose_roman_plus_identifier(&word.chars)
            || is_korean_prose_roman_minus_grade(&word.chars)
            || has_korean_prefix_roman_plus_annotation(&word.chars)
            || has_korean_prefix_terminal_roman_plus_identifier(&word.chars)
            || has_korean_prefix_roman_hyphen_suffix(&word.chars)
            || is_korean_prose_acronym_parenthetical(&word.chars))
    {
        return Ok(TokenAction::Noop);
    }

    // Korean rules 29 and 35 also own a number immediately followed by a Roman
    // letters-sequence in ordinary Korean prose.  Keep an isolated `3ab` on the
    // mathematical route, and preserve explicit math mode plus the established
    // Korean `곱`/`값` cues for genuinely mathematical uses.
    if state.english_indicator
        && !state.math_mode_active
        && has_adjacent_korean_word(tokens, index)
        && is_korean_prose_numeric_roman_identifier(&word.chars)
        && !prev_word_is_math_product_cue(tokens, index)
        && !next_word_starts_with_math_value_cue(tokens, index)
    {
        return Ok(TokenAction::Noop);
    }

    // PDF 수학 제60/61항 — `a ≲ b:`, `p ⊻ q:` 같이 단일 letter + 관계기호 + 단일
    // letter + 콜론 패턴의 inline math expression. 콜론 이전까지를 하나의 math
    // expression으로 병합해 인코딩한다 (letter들이 산문 quote-wrap되지 않도록).
    //
    // 패턴 매칭 조건:
    // - 현재 Word: 단일 ASCII 알파벳 (lowercase)
    // - 다음 Word: math 관계/논리 연산자 (단일 chars, `<>≲≺⊻` 등)
    // - 그 다음 Word: 단일 ASCII letter + `:`
    if word.chars.len() == 1 && word.chars[0].is_ascii_lowercase() {
        let collect_next = |start: usize| {
            let mut j = start;
            while matches!(tokens.get(j), Some(Token::Space(_))) {
                j += 1;
            }
            tokens.get(j).map(|t| (j, t))
        };
        // PDF 수학 제60·61항 — colon-math relation operators.
        const COLON_MATH_OPS: &[char] = &[
            '\u{2272}', '\u{2273}', '\u{227A}', '\u{227B}', '\u{22BB}', '<', '>', '=', '\u{2260}',
            '\u{2264}', '\u{2265}', '\u{2208}', '\u{2209}',
        ];
        if let Some((op_idx, Token::Word(op_w))) = collect_next(index + 1)
            && op_w.chars.len() == 1
            && COLON_MATH_OPS.contains(&op_w.chars[0])
            && let Some((last_idx, Token::Word(last_w))) = collect_next(op_idx + 1)
            && last_w.chars.len() == 2
            && last_w.chars[0].is_ascii_lowercase()
            && last_w.chars[1] == ':'
        {
            // Merge: "a" + " " + "≲" + " " + "b:" → math expression.
            let merged = format!("{} {} {}", text, op_w.text.as_ref(), last_w.text.as_ref());
            let math_context = math_context_from_state(state);
            if let Ok(bytes) =
                math::encoder::encode_math_expression_with_context(&merged, math_context)
            {
                let consume_count = last_idx + 1 - index;
                return Ok(TokenAction::ReplaceRange(
                    consume_count,
                    vec![Token::PreEncoded(bytes)],
                ));
            }
        }
    }

    // PDF 수학 제60항 2-나 — 조건제시법 set-builder notation `{x|x는 정수}`.
    // `{`로 시작하고 `|`를 포함하는 Word를 만나면, `}` 토큰을 찾을 때까지
    // 후속 Word/Space를 모아 하나의 math expression으로 인코딩한다.
    if word.chars.first() == Some(&'{') && word.chars.contains(&'|') {
        let mut merged = text.to_string();
        let mut end_idx = index;
        let mut found_close = word.chars.last() == Some(&'}');
        if !found_close {
            let mut i = index + 1;
            while i < tokens.len() {
                match tokens.get(i) {
                    Some(Token::Space(_)) => merged.push(' '),
                    Some(Token::Word(w)) => {
                        merged.push_str(w.text.as_ref());
                        if w.chars.last() == Some(&'}') {
                            end_idx = i;
                            found_close = true;
                            break;
                        }
                    }
                    _ => break,
                }
                i += 1;
            }
        }
        let math_context = math_context_from_state(state);
        if found_close
            && let Ok(bytes) =
                math::encoder::encode_math_expression_with_context(&merged, math_context)
        {
            let consume_count = end_idx + 1 - index;
            return Ok(TokenAction::ReplaceRange(
                consume_count,
                vec![Token::PreEncoded(bytes)],
            ));
        }
    }

    // PDF 제12항 [붙임 2] — 한국어 prose 내 multi-letter math identifier 처리.
    // Word가 2~3개 ASCII letter로 시작하고 곧장 수식 나열/소유 조사 `의/와/과`가
    // 붙는 패턴(예: `ab의`, `AB와`)에서 산문 영어 wrap(`⠴...⠲`)이 아닌 math letter
    // 처리. 대문자(`AB의`, `ABC의`)는 순차 변수열일 때만 같은 구조로 처리해
    // 일반 약어(`FM의`, `SNS는` 등)를 이 경로에서 제외한다.
    if word.chars.len() >= 3 {
        let ascii_prefix_len = word
            .chars
            .iter()
            .take_while(|c| c.is_ascii_alphabetic())
            .count();
        if (2..=3).contains(&ascii_prefix_len) {
            let suffix_chars = &word.chars[ascii_prefix_len..];
            let suffix_is_math_identifier_particle =
                has_ascii_letter_korean_math_suffix(&word.chars);
            let prefix_letters: Vec<char> = word.chars[..ascii_prefix_len].to_vec();
            let all_lower = prefix_letters.iter().all(|c| c.is_ascii_lowercase());
            let all_upper = prefix_letters.iter().all(|c| c.is_ascii_uppercase());

            // 알파벳이 이어진다는 것만으로는 수식이 아니다. 산문의 약어에도 그런
            // 짝이 흔하다(`GH의`, `AB의`). 제11항의 두 칸 경계는 그 어절이 수식일
            // 때만 쓰므로, 대문자도 소문자와 같이 문장이 주는 수학 단서(`값`, `곱`)를
            // 요구한다. 규정 예 `행렬 A와 B에 대하여 AB의 값을` 은 그 단서를 갖췄다.
            let has_math_cue = next_word_starts_with_math_value_cue(tokens, index)
                || prev_word_is_math_product_cue(tokens, index);
            let case_allowed = has_math_cue
                && (all_lower || (all_upper && is_consecutive_ascii_letter_run(&prefix_letters)));
            if suffix_is_math_identifier_particle && case_allowed {
                let prev_is_korean_or_first = index == 0
                    || index
                        .checked_sub(1)
                        .and_then(|i| tokens.get(i))
                        .is_some_and(|t| match t {
                            Token::Word(w) => w.meta.has_korean,
                            Token::Space(_) => index
                                .checked_sub(2)
                                .and_then(|j| tokens.get(j))
                                .is_some_and(
                                    |t2| matches!(t2, Token::Word(w) if w.meta.has_korean),
                                ),
                            _ => false,
                        });
                if prev_is_korean_or_first {
                    let matrix_context = state.matrix_context_active;
                    let mut bytes = Vec::new();
                    // PDF 제11항 — 국어 문장 안 수식 앞뒤를 두 칸씩 띄어 쓴다.
                    // Token::Space가 1칸 보조하므로 leading 1칸 추가.
                    bytes.push(0);
                    for letter in &prefix_letters {
                        if all_upper {
                            if matrix_context {
                                bytes.push(32);
                            } else if letter == &prefix_letters[0] {
                                bytes.push(32);
                                bytes.push(32);
                            }
                            let code = crate::english::encode_english(letter.to_ascii_lowercase())?;
                            bytes.push(code);
                        } else {
                            let code = crate::english::encode_english(*letter)?;
                            bytes.push(code);
                        }
                    }
                    // trailing 두 칸 (math expression 종료 boundary).
                    bytes.push(0);
                    bytes.push(0);
                    let suffix: String = suffix_chars.iter().collect();
                    let suffix_chars_vec: Vec<char> = suffix.chars().collect();
                    let suffix_meta = crate::rules::token::WordMeta::from_chars(&suffix_chars_vec);
                    let suffix_word = Token::Word(WordToken {
                        text: std::borrow::Cow::Owned(suffix),
                        chars: suffix_chars_vec,
                        meta: suffix_meta,
                    });
                    return Ok(TokenAction::ReplaceMany(vec![
                        Token::PreEncoded(bytes),
                        suffix_word,
                    ]));
                }
            }
        }
    }

    // PDF 제13항 — 한국어 산문 안 그리스 문자 리스트 (예: `α, β에`).
    // `Word(MathLetter+',')`이 현재이고 다음 비공백 Word가 `MathLetter+Korean`이면
    // 두 단어를 `⠴α, β⠲` + Korean으로 묶어 emit한다.
    // 직전이 한국어 단어여야 한다 (prose 컨텍스트 확인).
    if word.chars.len() == 2
        && word.chars[1] == ','
        && math_symbol_shortcut::is_math_symbol_char(word.chars[0])
        && !word.chars[0].is_ascii_alphanumeric()
    {
        let prev_is_korean_word = index
            .checked_sub(1)
            .and_then(|i| tokens.get(i))
            .and_then(|t| match t {
                Token::Space(_) => index.checked_sub(2).and_then(|j| tokens.get(j)),
                _ => Some(t),
            })
            .is_some_and(|t| matches!(t, Token::Word(w) if w.meta.has_korean));
        // 다음 Word: math letter 시작 + 한국어 suffix
        let next_word_opt = next_indexed_word_skip_space(tokens, index + 1);
        if prev_is_korean_word
            && let Some((next_idx, next_word)) = next_word_opt
            && next_word.chars.len() >= 2
            && math_symbol_shortcut::is_math_symbol_char(next_word.chars[0])
            && !next_word.chars[0].is_ascii_alphanumeric()
            && next_word.chars[1..]
                .iter()
                .all(|c| crate::utils::is_korean_char(*c))
        {
            let letter1 = word.chars[0];
            let letter2 = next_word.chars[0];
            let korean_suffix: String = next_word.chars[1..].iter().collect();
            let enc1 = math_symbol_shortcut::encode_char_math_symbol_shortcut(letter1)?;
            let enc2 = math_symbol_shortcut::encode_char_math_symbol_shortcut(letter2)?;
            let mut bytes = Vec::new();
            bytes.push(52); // ⠴ open quote
            bytes.extend_from_slice(enc1);
            bytes.push(2); // ⠂ literal comma in math letter list
            bytes.push(0); // space
            bytes.extend_from_slice(enc2);
            bytes.push(50); // ⠲ close quote
            // suffix Korean을 다음 Word로 분리 emit
            let suffix_chars: Vec<char> = korean_suffix.chars().collect();
            let suffix_meta = crate::rules::token::WordMeta::from_chars(&suffix_chars);
            let suffix_word = Token::Word(WordToken {
                text: std::borrow::Cow::Owned(korean_suffix),
                chars: suffix_chars,
                meta: suffix_meta,
            });
            // 현재 Word + 사이 토큰 + 다음 Word를 한꺼번에 교체.
            let consume_count = next_idx + 1 - index;
            return Ok(TokenAction::ReplaceRange(
                consume_count,
                vec![Token::PreEncoded(bytes), suffix_word],
            ));
        }
    }

    // PDF — `...` 또는 `..., `, `..`은 math context에 있으면 수학 줄임표 `⠠⠠⠠`로 emit.
    // Korean 마침표 줄임표 `⠲⠲⠲`와 구분.
    let dot_only =
        !text.is_empty() && (text.chars().all(|c| matches!(c, '.' | ',')) && text.contains('.'));
    if dot_only {
        // PDF — 앞 토큰이 math letter Word 또는 이미 인코딩된 PreEncoded(math 컨텍스트)면
        // 수학 줄임표로 emit. PreEncoded는 이전 math 처리 결과로 본다.
        let prev_is_math_context = prev_is_math_context_for_ellipsis(tokens, index);
        if prev_is_math_context {
            let dots: usize = text.chars().filter(|c| *c == '.').count();
            // ⠠ (32) repeated for each dot, capped at 3 per PDF.
            let mut bytes = vec![32u8; dots.min(3)];
            // 다음 토큰이 Korean Word면 math+Korean 경계로 trailing space 추가.
            let next_is_korean =
                next_word_skip_space(tokens, index + 1).is_some_and(|w| w.meta.has_korean);
            if text.ends_with(',') {
                // PDF — math 식 안 comma는 ⠐, prose math letter 리스트의 comma는 ⠂.
                // 다음이 math 또는 PreEncoded면 ⠐, Korean이면 ⠂.
                bytes.push(if next_is_korean { 2 } else { 16 });
            }
            if next_is_korean {
                bytes.push(0);
            }
            return Ok(TokenAction::Replace(Token::PreEncoded(bytes)));
        }
    }

    // Korean Rules 43, 47 [appendix], 48, and 50: numeric punctuation in prose
    // (`3·1 운동`, `1/3 규모`, `.515로`, `1.7~2.4 사이`) remains on the
    // ordinary number/punctuation path. A standalone expression keeps using
    // the math engine because there is no adjacent Korean prose context.
    if (is_middle_dot_numeric_word(&word.chars) || is_korean_prose_numeric_notation(&word.chars))
        && has_adjacent_korean_word(tokens, index)
    {
        return Ok(TokenAction::Noop);
    }

    // Standalone therefore/because between content tokens (Word or PreEncoded)
    // should add one braille space on each side. Combined with the Space tokens
    // already present between words, this produces the double-space delimiter
    // required by 제11항.
    if matches!(word.chars.as_slice(), ['' | '']) {
        let has_prev_content = has_content_skipping_space_backward(tokens, index);
        let has_next_content = has_content_skipping_space_forward(tokens, index);
        if has_prev_content && has_next_content {
            let encoded = math_symbol_shortcut::encode_char_math_symbol_shortcut(word.chars[0])?;
            let mut out = vec![0];
            out.extend_from_slice(encoded);
            out.push(0);
            return Ok(TokenAction::Replace(Token::PreEncoded(out)));
        }
    }

    // Math rules 60-61: process a separated right-hand capital while the
    // set/logic sign is still a Word token.  Once the sign becomes PreEncoded,
    // neighbour lookup intentionally stops at that boundary and the capital
    // would otherwise fall through to UEB prose (and gain a grade-1 marker).
    //
    // The right token may retain non-alphanumeric punctuation or a Korean
    // suffix (`R}`, `P는`), but another ASCII letter/digit means it is a Roman
    // word or identifier rather than one mathematical variable (`Road`).
    if is_set_or_logic_symbol_word(word)
        && let Some((right_index, right_word)) = next_indexed_word_skip_space(tokens, index + 1)
        && right_word
            .chars
            .first()
            .is_some_and(char::is_ascii_uppercase)
        && right_word.chars[1..]
            .iter()
            .all(|ch| crate::utils::is_korean_char(*ch) || !ch.is_ascii_alphanumeric())
    {
        let symbol = math_symbol_shortcut::encode_char_math_symbol_shortcut(word.chars[0])?;
        let upper = right_word.chars[0];
        let code = crate::english::encode_english(upper.to_ascii_lowercase())?;
        let mut replacement: Vec<Token<'a>> = vec![Token::PreEncoded(symbol.to_vec())];
        replacement.extend(tokens[index + 1..right_index].iter().cloned());
        replacement.push(Token::PreEncoded(vec![32, code]));

        if right_word.chars.len() > 1 {
            let suffix = right_word.chars[1..].iter().collect::<String>();
            replacement.push(build_word_token(suffix));
        }

        return Ok(TokenAction::ReplaceRange(
            right_index + 1 - index,
            replacement,
        ));
    }

    // Set/logic symbols separated by spaces still own adjacent uppercase math
    // variables. Emit the capital indicator here so the later UEB token rules
    // cannot reinterpret the one-letter variable as an alphabetic wordsign.
    if word.chars.len() == 1 && word.chars[0].is_ascii_uppercase() {
        let (prev, next) = prev_next_words(tokens, index);
        if prev.is_some_and(is_set_or_logic_symbol_word)
            || next.is_some_and(is_set_or_logic_symbol_word)
        {
            let code = crate::english::encode_english(word.chars[0].to_ascii_lowercase())?;
            return Ok(TokenAction::Replace(Token::PreEncoded(vec![32, code])));
        }
    }

    // Skip if already processed (PreEncoded) or if it's a fraction
    if let Some(stripped) = text.strip_prefix('$') {
        if let Some(close_idx) = stripped.find('$')
            && close_idx + 1 < stripped.len()
        {
            let latex = &text[..=close_idx + 1];
            let suffix = &stripped[close_idx + 1..];

            if let Some((whole, numerator, denominator)) =
                crate::fraction::parse_latex_fraction(latex)
            {
                // 제44항 [다만]: 분수 직후 한국어 조사의 첫 초성이 ㄴ/ㄷ/ㅁ/ㅋ/ㅌ/ㅍ/ㅎ
                // 또는 '운'으로 시작하면 띄어 쓴다.
                let mut replacement: Vec<Token<'a>> =
                    vec![Token::Fraction(crate::rules::token::FractionToken {
                        whole,
                        numerator,
                        denominator,
                    })];
                if !suffix.is_empty() && rule_44_requires_space_before_korean(suffix) {
                    replacement.push(Token::Space(crate::rules::token::SpaceKind::Regular));
                }
                replacement.push(build_word_token(suffix.to_string()));
                return Ok(TokenAction::ReplaceMany(replacement));
            }

            let inner = &latex[1..latex.len() - 1];
            let math_context = math_context_from_state(state);
            if let Ok(bytes) =
                crate::rules::token_rules::latex_math::encode_latex_math_bytes_with_context(
                    inner,
                    math_context,
                )
            {
                // PDF — Korean prose 안 단일 letter math 블록은 ⠴...⠲로 감싼다.
                // 콤마-구분 letter 리스트도 quote/english marker로 감싼다.
                let suffix_first = suffix.chars().next();
                let suffix_is_korean = suffix_first.is_some_and(crate::utils::is_korean_char);
                let inner_is_single_letter =
                    inner.chars().count() == 1 && inner.chars().all(|c| c.is_ascii_alphabetic());
                let comma_list = inner.contains(',')
                    && inner.split(',').map(str::trim).all(|p| {
                        !p.is_empty()
                            && p.chars().count() == 1
                            && p.chars().all(|c| c.is_ascii_alphabetic())
                    });
                let prev_is_korean = index
                    .checked_sub(1)
                    .and_then(|i| tokens.get(i))
                    .map(|tok| match tok {
                        Token::Word(w) => w.meta.has_korean,
                        Token::Space(_) => index
                            .checked_sub(2)
                            .and_then(|j| tokens.get(j))
                            .is_some_and(|t| matches!(t, Token::Word(w) if w.meta.has_korean)),
                        _ => false,
                    })
                    .unwrap_or(false);
                let in_prose = suffix_is_korean || prev_is_korean;
                // PDF — `$-2$`, `$0.3010$` 같이 부호+숫자/소수점만 있는 단순 수치는
                // "본격적 수식"이 아니므로 한국어 단어 경계에서 추가 공백을 적용하지 않는다.
                // Space token 1칸으로 충분하다.
                let inner_is_simple_numeric = !inner.is_empty()
                    && inner.chars().all(|c| {
                        c.is_ascii_digit() || matches!(c, '-' | '+' | '\u{2212}' | '.' | ',')
                    });
                // 따옴표 자체가 경계를 명시(단일 letter/리스트), 단순 수치, 토큰 첫 위치는
                // 모두 leading_spaces=0.
                let leading_spaces = compute_leading_spaces(
                    tokens,
                    index,
                    in_prose,
                    inner_is_single_letter,
                    comma_list,
                    inner_is_simple_numeric,
                );
                let mut replacement = Vec::new();
                if leading_spaces > 0 {
                    replacement.push(Token::PreEncoded(vec![0; leading_spaces]));
                }
                if in_prose && inner_is_single_letter {
                    let mut wrapped = Vec::with_capacity(bytes.len() + 2);
                    wrapped.push(52); //                    wrapped.extend(bytes);
                    wrapped.push(50); //                    replacement.push(Token::PreEncoded(wrapped));
                } else if in_prose && comma_list {
                    let letters: Vec<&str> = inner.split(',').map(str::trim).collect();
                    let mut wrapped = Vec::new();
                    for (i, letter) in letters.iter().enumerate() {
                        if let Some(c) = letter.chars().next() {
                            if i == 0 {
                                wrapped.push(52);
                            } else {
                                wrapped.push(0);
                                wrapped.push(48); // ⠰ english
                            }
                            if c.is_ascii_uppercase() {
                                wrapped.push(32);
                                if let Ok(code) =
                                    crate::english::encode_english(c.to_ascii_lowercase())
                                {
                                    wrapped.push(code);
                                }
                            } else if let Ok(code) = crate::english::encode_english(c) {
                                wrapped.push(code);
                            }
                            if i + 1 < letters.len() {
                                wrapped.push(2); // ⠂ literal comma (in math letter list)
                            } else {
                                wrapped.push(50);
                            }
                        }
                    }
                    replacement.push(Token::PreEncoded(wrapped));
                } else {
                    replacement.push(Token::PreEncoded(bytes));
                    // PDF — math + Korean prose 경계는 두 칸. 구두점/기호 suffix는 인접.
                    // 단, 단순 수치 표기(`-2`, `0.3010`)는 본격적 수식이 아니므로 직접 인접.
                    let trailing_spaces = if suffix_is_korean && !inner_is_simple_numeric {
                        2
                    } else {
                        0
                    };
                    if trailing_spaces > 0 {
                        replacement.push(Token::PreEncoded(vec![0; trailing_spaces]));
                    }
                }
                replacement.push(build_word_token(suffix.to_string()));
                return Ok(TokenAction::ReplaceMany(replacement));
            }
        }

        if let Some((whole, numerator, denominator)) = crate::fraction::parse_latex_fraction(text) {
            return Ok(TokenAction::Replace(Token::Fraction(
                crate::rules::token::FractionToken {
                    whole,
                    numerator,
                    denominator,
                },
            )));
        }

        if text.ends_with('$') && text.len() >= 3 {
            let inner = &text[1..text.len() - 1];
            let math_context = math_context_from_state(state);
            if let Ok(bytes) =
                crate::rules::token_rules::latex_math::encode_latex_math_bytes_with_context(
                    inner,
                    math_context,
                )
            {
                let replacement =
                    crate::rules::token_rules::latex_math::wrap_latex_math_tokens_with_inner(
                        tokens, index, bytes, inner,
                    );
                return Ok(TokenAction::ReplaceMany(replacement));
            }
        }

        return Ok(TokenAction::Noop);
    }

    // 쌍점으로 수를 이어 적은 표기(`7:30,`, `16:9`)는 제51항 [다만 2] 가 붙여 쓰는
    // 시·분이나 비율이고, 그 수는 제40항에 따라 쌍점 뒤에도 수표를 세운다. 수식
    // 엔진에 넘기면 그 수표가 사라지므로 한글 규칙에 맡긴다.
    if !is_math_expression(&word.chars, text) || is_prose_number_notation(&word.chars) {
        let math_context = math_context_from_state(state);
        if let Some(bytes) = try_encode_mixed_math_slice(&word.chars, math_context) {
            return Ok(TokenAction::Replace(Token::PreEncoded(bytes)));
        }
        // 제11항: 한글 문장 안의 수학적 표기는 앞뒤를 두 칸씩 띄어 쓴다.
        // - index == 0           → 0칸 (문서 맨 앞)
        // - 이전 토큰이 Space    → 1칸 추가 (Token::Space 1칸 + 새 1칸 = 2칸)
        //   다만 prev-prev가 같은 math/mixed math 단어이면 0 (1칸 유지)
        // - 그 외 (content)     → 2칸 (경계 표시)
        let prev_prev_is_math_or_mixed = prev_prev_is_math_or_mixed_context(tokens, index);
        let leading_delimiter_len = if index == 0 {
            0
        } else if matches!(tokens.get(index - 1), Some(Token::Space(_))) {
            if prev_prev_is_math_or_mixed { 0 } else { 1 }
        } else {
            2
        };
        if let Some(replacement) = split_mixed_math_word(word, leading_delimiter_len, math_context)
        {
            return Ok(TokenAction::ReplaceMany(replacement));
        }
        return Ok(TokenAction::Noop);
    }

    // Try to encode via math engine.
    // Err arm below: if math encoding fails, fall back to character-level rules.
    let math_context = math_context_from_state(state);
    match math::encoder::encode_math_expression_with_context(text, math_context) {
        Ok(bytes) => {
            let (prev_has_korean, _next_has_korean) = adjacent_korean_word_flags(tokens, index);
            let mut wrapped = Vec::with_capacity(bytes.len() + 2);

            let needs_decimal_context_spacing = needs_decimal_context_spacing(text, &word.chars);
            let prev_is_space_decimal = index
                .checked_sub(1)
                .is_some_and(|i| matches!(tokens.get(i), Some(Token::Space(_))));
            if needs_decimal_context_spacing && prev_is_space_decimal {
                wrapped.push(0);
            }

            // 특수 패턴(증분 + 등호 + 다항식 조합)에만 prefix space 두 칸 추가.
            // 일반적인 한글 + math 인접 케이스는 Token::Space가 단일 공백을 처리하므로
            // 추가 prefix/suffix space를 emit하지 않는다.
            // 문서 맨 앞(index == 0)에서는 제11조에 따라 leading 띄어쓰기를 생략한다.
            if index != 0 && !prev_has_korean && is_delta_eq_polysum_pattern(text) {
                wrapped.push(0);
                wrapped.push(0);
            }

            // PDF 수학 제11항 — 국어 문장 안 "수식"은 앞뒤 두 칸씩 띄어쓴다.
            // 단일 연산자/기호(+, =, ×, ÷, /, - 등)는 일반 산식 일부이므로 제외한다.
            // 변수/숫자/괄호 등 실질적 수식(`f(x)`, `a²`, `2x+3` 등)일 때만 적용.
            // 단순 부호+숫자(`-2`, `+3`, `0.5` 등)는 일반 숫자 표기이므로
            // 추가 띄어쓰기를 적용하지 않는다. 첨자/괄호/문자가 있으면 실질적 수식.
            let only_simple_digits = !word.chars.is_empty()
                && word.chars.iter().all(|c| {
                    c.is_ascii_digit() || matches!(*c, '-' | '+' | '\u{2212}' | '.' | ',')
                });
            let is_substantial_math = word.chars.len() > 1
                && word.chars.iter().any(|c| {
                    c.is_ascii_alphanumeric() || matches!(*c, '(' | ')' | '[' | ']' | '|')
                })
                && !only_simple_digits
                && !is_prose_number_notation(&word.chars)
                && has_balanced_brackets(&word.chars)
                && !is_hyphenated_roman_word(&word.chars);
            let needs_korean_leading = index != 0
                && prev_has_korean
                && matches!(tokens.get(index - 1), Some(Token::Space(_)))
                && !needs_decimal_context_spacing
                && is_substantial_math;
            if needs_korean_leading {
                wrapped.push(0);
            }

            wrapped.extend_from_slice(&bytes);

            if needs_decimal_context_spacing
                && matches!(tokens.get(index + 1), Some(Token::Space(_)))
            {
                wrapped.push(0);
            }

            // trailing은 다음 단어가 순수 한글일 때만 추가. (인접 단어가 math+korean
            // 혼합이면 다음 단어 측에서 leading을 추가하므로 중복 방지.)
            let next_is_pure_korean =
                next_word_skip_space(tokens, index + 1).is_some_and(word_is_pure_korean);
            let needs_trailing_korean_pad = next_is_pure_korean
                && matches!(tokens.get(index + 1), Some(Token::Space(_)))
                && !needs_decimal_context_spacing
                && is_substantial_math;
            let trailing_pad: &[u8] = if needs_trailing_korean_pad { &[0] } else { &[] };
            wrapped.extend_from_slice(trailing_pad);

            Ok(TokenAction::Replace(Token::PreEncoded(wrapped)))
        }
        Err(_) => Ok(TokenAction::Noop),
    }
}

// ============================================================
// Mutation-testing reinforcements for apply::run
//
// Strategy: rather than re-implement the local helpers in tests, drive run()
// indirectly via `crate::encode()` with crafted inputs. Each test exercises
// one specific code path and asserts an OBSERVABLE difference between the
// happy path and a nearby negative path. This kills mutations on local helpers
// (prev_next_words, is_logic_symbol_word) and on the dozens of branch checks
// throughout `run`.
// ============================================================
#[cfg(test)]
mod tests {
    use super::*;

    #[rstest::rstest]
    #[case::ueb_multiword_parenthetical("plays (such as Romeo and Juliet)", true)]
    #[case::initialism_prefixed_comma("WTO(World Tourism Organization),", true)]
    #[case::korean_particle_after_parenthesis("설명(Home Connectivity Alliance)를", true)]
    #[case::korean_particle_after_quote("설명(Home Connectivity Alliance)’를", true)]
    #[case::ueb_letter_list("(q, r)", false)]
    #[case::math_function("f(x)", false)]
    #[case::operator_interrupts_prose_run("(x + y)", false)]
    #[case::no_closing_parenthesis("Romeo Juliet", false)]
    #[case::function_with_spaced_argument("f(x y)", false)]
    #[case::missing_opening_parenthesis("Romeo Juliet)", false)]
    #[case::invalid_trailing_digit("(Romeo Juliet)1", false)]
    #[case::digit_in_final_fragment("(Romeo Juliet2)", false)]
    #[case::digit_after_opening("(2Romeo Juliet)", false)]
    #[case::digit_in_earlier_fragment("(Romeo2 Juliet)", false)]
    #[case::nonletter_earlier_without_opening("Romeo2 Juliet More)", false)]
    #[case::nested_opening_before_fragment("((Romeo Juliet)", false)]
    #[case::closing_before_opening(")(Romeo Juliet)", false)]
    fn recognizes_only_complete_multiword_roman_parenthetical_tails(
        #[case] input: &str,
        #[case] expected: bool,
    ) {
        let ir = crate::rules::token::DocumentIR::parse(input, true);
        let index = ir
            .tokens
            .iter()
            .rposition(|token| matches!(token, Token::Word(_)))
            .expect("probe must contain a word");
        let Token::Word(word) = &ir.tokens[index] else {
            unreachable!("selected token must be a word");
        };

        assert_eq!(
            is_multiword_closed_roman_parenthetical_tail(&ir.tokens, index, word),
            expected
        );

        if expected {
            let mut state = EncoderState::new(false);
            assert!(matches!(
                run(&ir.tokens, index, &mut state).unwrap(),
                TokenAction::Noop
            ));
        }
    }

    #[rstest::rstest]
    #[case::initialism_expansion("HCA(Home Connectivity Alliance)", true)]
    #[case::punctuated_expansion("TB(Top View Battle),", true)]
    #[case::korean_particle("HCA(Home Connectivity Alliance)를", true)]
    #[case::quoted_korean_particle("HCA(Home Connectivity Alliance)’를", true)]
    #[case::single_capital_head("A(Home Connectivity Alliance)", false)]
    #[case::mixed_case_head("HCa(Home Connectivity Alliance)", false)]
    #[case::single_word_body("HCA(Alliance)", false)]
    #[case::digit_in_body("HCA(Home Connectivity2 Alliance)", false)]
    #[case::operator_in_body("HCA(Home + Alliance)", false)]
    #[case::nested_parenthesis("HCA((Home Connectivity Alliance))", false)]
    #[case::alphanumeric_trailer("HCA(Home Connectivity Alliance)1", false)]
    #[case::unclosed_expansion("HCA(Home Connectivity Alliance", false)]
    fn recognizes_only_complete_allcaps_multiword_roman_expansion_heads(
        #[case] input: &str,
        #[case] expected: bool,
    ) {
        let ir = crate::rules::token::DocumentIR::parse(input, true);
        let index = ir
            .tokens
            .iter()
            .position(|token| matches!(token, Token::Word(_)))
            .expect("probe must contain a word");
        let Token::Word(word) = &ir.tokens[index] else {
            unreachable!("selected token must be a word");
        };

        assert_eq!(
            is_multiword_closed_roman_parenthetical_head(&ir.tokens, index, word),
            expected
        );

        if expected {
            let mut state = EncoderState::new(false);
            assert!(matches!(
                run(&ir.tokens, index, &mut state).unwrap(),
                TokenAction::Noop
            ));
        }
    }

    #[rstest::rstest]
    #[case::roman_followed_by_digit("용어(Web)3", 1)]
    #[case::roman_then_korean_explanation("기관(KRISS, 원장)", 2)]
    #[case::numeric_annotation("최고치(2126.14)", 1)]
    #[case::multiword_roman_name("전환(DT·Digital Transformation)", 2)]
    #[case::korean_numeric_name("용어2(Version Two)", 2)]
    #[case::year_with_roman_explanation("보고서 2023(MWC 2023)", 2)]
    #[case::single_variable("함수(x)", 0)]
    #[case::lowercase_expression("함수(x+1)", 0)]
    #[case::uppercase_expression("식(A+B)", 0)]
    #[case::separated_function("함수 f(x)", 0)]
    fn recognizes_attached_korean_prose_parenthetical_span(
        #[case] input: &str,
        #[case] expected_matching_words: usize,
    ) {
        let ir = crate::rules::token::DocumentIR::parse(input, true);
        let matching_indices = ir
            .tokens
            .iter()
            .enumerate()
            .filter_map(|(index, token)| {
                matches!(token, Token::Word(_))
                    .then(|| is_within_attached_korean_prose_parenthetical(&ir.tokens, index))
                    .is_some_and(|matches| matches)
                    .then_some(index)
            })
            .collect::<Vec<_>>();

        assert_eq!(matching_indices.len(), expected_matching_words);
        for index in matching_indices {
            let mut state = EncoderState::new(false);
            assert!(matches!(
                run(&ir.tokens, index, &mut state).unwrap(),
                TokenAction::Noop
            ));
        }
    }

    /// Korean rules 34 and 54 put the Korean opening parenthesis before the
    /// Roman indicator; the math route instead starts with a two-cell prose
    /// separator.  Exercise each accepted body class at the public boundary.
    #[rstest::rstest]
    #[case::roman_followed_by_digit("용어(Web)3")]
    #[case::roman_then_korean_explanation("기관(KRISS, 원장)")]
    #[case::multiword_roman_name("전환(DT·Digital Transformation)")]
    #[case::korean_numeric_name("용어2(Version Two)")]
    #[case::year_with_roman_explanation("보고서 2023(MWC 2023)")]
    fn attached_korean_prose_parentheses_keep_rule_34_order(#[case] input: &str) {
        let encoded = crate::encode_to_unicode(input).expect("input must encode");
        assert!(
            encoded.contains("⠦⠄⠴"),
            "Korean opening parenthesis must precede Roman entry: {encoded}"
        );
    }

    #[test]
    fn attached_korean_name_keeps_specialized_anonymized_person_path() {
        let ir = crate::rules::token::DocumentIR::parse("모A(61)씨", true);
        let index = ir
            .tokens
            .iter()
            .position(|token| matches!(token, Token::Word(_)))
            .expect("fixture must contain a word");
        let mut state = EncoderState::new(true);

        assert!(matches!(
            run(&ir.tokens, index, &mut state).unwrap(),
            TokenAction::ReplaceMany(_)
        ));
        assert!(
            crate::encode_to_unicode("모A(61)씨")
                .expect("fixture must encode")
                .contains("⠴⠠⠁⠦⠄⠼⠋⠁⠠⠴")
        );
    }

    /// Decimal-context spacing recognizes each structural marker independently:
    /// the parser sentinel, the Rule 12 ellipsis, and a combining math mark.
    #[rstest::rstest]
    #[case::unit_separator("a\u{001f}b", "ab", true)]
    #[case::midline_ellipsis("a⋯b", "ab", true)]
    #[case::combining_mark("ab", "a\u{0305}", true)]
    #[case::plain_expression("a+b", "a+b", false)]
    fn detects_decimal_context_spacing_markers(
        #[case] text: &str,
        #[case] chars: &str,
        #[case] expected: bool,
    ) {
        assert_eq!(
            needs_decimal_context_spacing(text, &chars.chars().collect::<Vec<_>>()),
            expected
        );
    }

    use crate::rules::token::{SpaceKind, WordMeta, WordToken};
    use std::borrow::Cow;

    fn enc_str(s: &str) -> String {
        crate::encode_to_unicode(s).unwrap_or_default()
    }

    /// Build a WordToken from a string for direct testing.
    fn word_tok<'a>(text: &'a str) -> Token<'a> {
        let chars: Vec<char> = text.chars().collect();
        let meta = WordMeta::from_chars(&chars);
        Token::Word(WordToken {
            text: Cow::Borrowed(text),
            chars,
            meta,
        })
    }

    fn space_tok() -> Token<'static> {
        Token::Space(SpaceKind::Regular)
    }

    #[rstest::rstest]
    #[case::empty_segment("ISO//IEC")]
    #[case::punctuation_only_segment("ISO/-./IEC")]
    fn roman_slash_identifier_rejects_incomplete_segments(#[case] input: &str) {
        assert!(!is_korean_prose_roman_slash_identifier(
            &input.chars().collect::<Vec<_>>()
        ));
    }

    #[test]
    fn single_letter_slash_phrase_requires_letters_in_the_following_word() {
        let tokens = vec![word_tok("H/W"), space_tok(), word_tok("((")];
        let chars = "H/W".chars().collect::<Vec<_>>();

        assert!(!is_korean_prose_single_letter_slash_phrase(
            &tokens, 0, &chars
        ));
    }

    #[test]
    fn multiword_parenthetical_tail_stops_at_a_non_word_boundary() {
        let tokens = vec![
            Token::PreEncoded(vec![1]),
            space_tok(),
            word_tok("Alliance)"),
        ];
        let Token::Word(tail) = &tokens[2] else {
            unreachable!("fixture ends in a word")
        };

        assert!(!is_multiword_closed_roman_parenthetical_tail(
            &tokens, 2, tail
        ));
    }

    #[test]
    fn multiword_parenthetical_head_stops_at_a_non_word_boundary() {
        let tokens = vec![
            word_tok("HCA(Home"),
            space_tok(),
            Token::PreEncoded(vec![1]),
        ];
        let Token::Word(head) = &tokens[0] else {
            unreachable!("fixture begins with a word")
        };

        assert!(!is_multiword_closed_roman_parenthetical_head(
            &tokens, 0, head
        ));
    }

    #[test]
    fn attached_prose_parenthetical_rejects_a_preencoded_body() {
        let tokens = vec![word_tok("한국("), Token::PreEncoded(vec![1]), word_tok(")")];

        assert!(!is_within_attached_korean_prose_parenthetical(&tokens, 1));
    }

    #[test]
    fn attached_prose_parenthetical_ignores_mode_tokens_in_its_body() {
        let tokens = vec![
            word_tok("한국("),
            Token::Mode(crate::rules::token::ModeEvent::EnterEnglish),
            word_tok("Web)"),
        ];

        assert!(is_within_attached_korean_prose_parenthetical(&tokens, 1));
    }

    #[rstest::rstest]
    #[case::enclosed_roman_continuation("한글(ABC)-D", true)]
    #[case::korean_prefix_before_initialism("기장-KBO", true)]
    #[case::lowercase_math_variable("값-x", false)]
    fn korean_roman_hyphen_suffix_is_classified_structurally(
        #[case] input: &str,
        #[case] expected: bool,
    ) {
        assert_eq!(
            has_korean_prefix_roman_hyphen_suffix(&input.chars().collect::<Vec<_>>()),
            expected
        );
    }

    #[rstest::rstest]
    #[case::compact_unit("50bp", true)]
    #[case::decimal_prefix("3.1p", true)]
    #[case::ordinal("1st", true)]
    #[case::mixed_case_name("25Project", true)]
    #[case::digit_after_letter("3x3", true)]
    #[case::capital_suffix("6G", true)]
    #[case::trailing_punctuation("50bp,", true)]
    #[case::letter_first("MP3", false)]
    #[case::operator("3a+b", false)]
    #[case::solidus("3/4", false)]
    #[case::punctuation_before_letter("3.a", false)]
    #[case::number_only("3", false)]
    #[case::letters_only("abc", false)]
    #[case::korean_suffix("3한", false)]
    fn recognizes_numeric_prefix_roman_identifier_grammar(
        #[case] text: &str,
        #[case] expected: bool,
    ) {
        assert_eq!(
            is_korean_prose_numeric_roman_identifier(&text.chars().collect::<Vec<_>>()),
            expected
        );
    }

    #[rstest::rstest]
    #[case::compact_unit("가는 50bp 인상", "50bp", true)]
    #[case::decimal_prefix("가는 3.1p 표본", "3.1p", true)]
    #[case::ordinal("가는 1st 항목", "1st", true)]
    #[case::mixed_case_name("가는 25Project 자료", "25Project", true)]
    #[case::digit_after_letter("가는 3x3 배열", "3x3", true)]
    #[case::isolated_expression("3ab", "3ab", false)]
    #[case::previous_product_cue("곱 3ab 결과", "3ab", false)]
    #[case::next_value_cue("식은 3ab 값을", "3ab", false)]
    #[case::explicit_latex("가는 $3ab$ 식", "$3ab$", false)]
    fn numeric_roman_route_respects_korean_prose_and_math_context(
        #[case] input: &str,
        #[case] target: &str,
        #[case] expected_noop: bool,
    ) {
        let ir = crate::rules::token::DocumentIR::parse(input, true);
        let index = ir
            .tokens
            .iter()
            .position(|token| matches!(token, Token::Word(word) if word.text.as_ref() == target))
            .expect("target word must be tokenized as one word");
        let mut state = EncoderState::new(true);

        assert_eq!(
            matches!(
                run(&ir.tokens, index, &mut state).unwrap(),
                TokenAction::Noop
            ),
            expected_noop
        );
    }

    /// The complete token-rule path must preserve both defensive boundaries:
    /// an unsupported mixed-math glyph falls through, and a leading space with
    /// no preceding math token is not treated as mixed-math continuation.
    #[test]
    fn unsupported_mixed_expression_after_leading_space_falls_through() {
        let tokens = vec![space_tok(), word_tok("√분산🚀")];
        let mut state = EncoderState::new(false);

        let action = run(&tokens, 1, &mut state).unwrap();

        assert!(matches!(action, TokenAction::Noop));
    }

    // ---------- Direct tests on extracted helpers ----------

    /// `prev_next_words` returns (None, None) for an out-of-range index.
    /// Kills the `-> (None, None)` substitution mutant.
    #[test]
    fn prev_next_words_oob_index() {
        let tokens: Vec<Token<'_>> = vec![word_tok("a")];
        let (prev, next) = prev_next_words(&tokens, 5);
        assert!(prev.is_none(), "prev must be None for oob index");
        assert!(next.is_none(), "next must be None for oob index");
    }

    /// `prev_next_words` returns the immediate previous Word (no Space between).
    #[test]
    fn prev_next_words_adjacent_words() {
        let tokens: Vec<Token<'_>> = vec![word_tok("a"), word_tok("b"), word_tok("c")];
        let (prev, next) = prev_next_words(&tokens, 1);
        assert!(prev.is_some(), "prev must resolve to Word 'a'");
        assert_eq!(prev.unwrap().text.as_ref(), "a");
        assert!(next.is_some(), "next must resolve to Word 'c'");
        assert_eq!(next.unwrap().text.as_ref(), "c");
    }

    /// `prev_next_words` skips one or more Space tokens.
    #[test]
    fn prev_next_words_skips_spaces() {
        let tokens: Vec<Token<'_>> = vec![
            word_tok("a"),
            space_tok(),
            space_tok(),
            word_tok("b"),
            space_tok(),
            word_tok("c"),
        ];
        let (prev, next) = prev_next_words(&tokens, 3);
        assert_eq!(prev.unwrap().text.as_ref(), "a");
        assert_eq!(next.unwrap().text.as_ref(), "c");
    }

    /// `prev_next_words` returns None for prev when index is 0.
    /// Kills the `i - 1` underflow path mutations.
    #[test]
    fn prev_next_words_at_index_zero() {
        let tokens: Vec<Token<'_>> = vec![word_tok("a"), word_tok("b")];
        let (prev, next) = prev_next_words(&tokens, 0);
        assert!(prev.is_none(), "no prev at index 0");
        assert!(next.is_some(), "next must still resolve");
        assert_eq!(next.unwrap().text.as_ref(), "b");
    }

    /// `prev_next_words` returns None when a non-Space/Word boundary is hit.
    #[test]
    fn prev_next_words_stops_at_non_word_token() {
        let tokens: Vec<Token<'_>> = vec![
            Token::PreEncoded(vec![1, 2, 3]),
            space_tok(),
            word_tok("middle"),
            space_tok(),
            Token::PreEncoded(vec![4, 5, 6]),
        ];
        let (prev, next) = prev_next_words(&tokens, 2);
        // PreEncoded on both sides → prev/next both None.
        assert!(
            prev.is_none(),
            "PreEncoded boundary must yield None for prev"
        );
        assert!(
            next.is_none(),
            "PreEncoded boundary must yield None for next"
        );
    }

    #[test]
    fn math_suffix_and_next_value_cue_helpers_reject_short_or_non_word_inputs() {
        assert!(!has_ascii_letter_korean_math_suffix(&['a', '']));

        let tokens = vec![word_tok("ab의"), Token::PreEncoded(vec![1])];
        assert!(!next_word_starts_with_math_value_cue(&tokens, 0));
    }

    /// Only one complete rule-60/61 set or logic sign is accepted.
    #[rstest::rstest]
    #[case::xor_alone("", true)]
    #[case::wedge_alone("", true)]
    #[case::membership_alone("", true)]
    #[case::negation_alone("¬", true)]
    #[case::ascii_plus("+", false)]
    #[case::xor_then_letter("⊻x", false)]
    #[case::empty_word("", false)]
    fn set_or_logic_symbol_word_is_complete(#[case] text: &'static str, #[case] expected: bool) {
        let chars: Vec<char> = text.chars().collect();
        let word = WordToken {
            text: Cow::Borrowed(text),
            meta: WordMeta::from_chars(&chars),
            chars,
        };
        assert_eq!(is_set_or_logic_symbol_word(&word), expected);
    }

    /// Math rules 60-61: spaces do not turn a capital operand into UEB prose.
    #[rstest::rstest]
    #[case::upper_negation("A ¬ B", "⠠⠁⠀⠈⠔⠀⠠⠃")]
    #[case::mixed_case_negation("p ¬ Q", "⠏⠀⠈⠔⠀⠠⠟")]
    #[case::set_builder_membership("{x | x ∈ R}", "⠦⠂⠭⠀⠸⠳⠀⠭⠀⠖⠀⠠⠗⠐⠴")]
    fn spaced_set_and_logic_operands_stay_math_variables(
        #[case] input: &str,
        #[case] expected: &str,
    ) {
        assert_eq!(crate::encode_to_unicode(input).as_deref(), Ok(expected));
    }

    // ----- Lines 66-110: `a ≲ b:` colon-suffix math merge -----

    /// `a ≲ b:` is recognized as math expression (letter-relation-letter colon)
    /// and the letters do NOT receive prose quote wrapping (⠴...⠲).
    /// Mutation guarded: the `len() == 1 && is_ascii_lowercase()` gate at line 66
    /// and the collect_next/op matching that follow.
    #[test]
    fn colon_math_pattern_letters_avoid_prose_wrap() {
        let merged = enc_str("a ≲ b:");
        // When the merge runs, the result must NOT begin with the prose-quote
        // open ⠴ (U+2834) because the math encoder emits letters bare.
        assert!(!merged.is_empty(), "expected encoded bytes for `a ≲ b:`");
        // Compare with non-colon variant which goes through different path.
        let plain = enc_str("a ≲ b");
        assert_ne!(
            merged, plain,
            "trailing colon must change encoding via merge path"
        );
    }

    // ----- Lines 115-148: Set-builder `{x|x는 정수}` -----

    /// `{x|x는 정수}` triggers the set-builder merge. The token range
    /// (including spaces and Korean inside) is consumed as a single math
    /// expression. Distinguishes from non-set-builder `{...}` which would
    /// encode differently.
    #[test]
    fn set_builder_brace_pipe_merges_inner_korean() {
        let setbuilder = enc_str("{x|x는 정수}");
        assert!(!setbuilder.is_empty());
        // Same Korean text without the `{x|...}` should differ — confirming
        // the set-builder path triggered.
        let plain = enc_str("x는 정수");
        assert_ne!(
            setbuilder, plain,
            "set-builder wrap must change encoding vs. bare Korean"
        );
    }

    /// `{x|...` UNCLOSED → no merge; falls back to literal handling.
    /// Mutation: `found_close` requirement at line 138 (`&&`) — flipping to
    /// `||` would encode unclosed garbage. Compare unclosed vs. closed.
    #[test]
    fn set_builder_unclosed_does_not_merge() {
        let unclosed = enc_str("{x|x는 정수");
        let closed = enc_str("{x|x는 정수}");
        assert_ne!(
            unclosed, closed,
            "unclosed set-builder must NOT produce the same encoding as closed"
        );
    }

    // ----- Lines 155-236: Multi-letter Korean math identifier -----

    /// Lowercase ASCII prose abbreviations should not become math identifiers merely
    /// because they carry a Korean genitive/conjunctive suffix.
    #[test]
    fn multiletter_lower_prose_identifier_is_not_math() {
        for (prefix, suffix) in [
            ("ab의", "친구"),
            ("id의", "친구"),
            ("ai와", "서비스"),
            ("api의", "응답"),
        ] {
            let tokens = vec![word_tok(prefix), space_tok(), word_tok(suffix)];
            let mut state = EncoderState::new(false);
            let action = run(&tokens, 0, &mut state).expect("ok");
            assert!(
                matches!(action, TokenAction::Noop),
                "input={prefix} {suffix}"
            );
        }
    }

    /// `ab의 값을` — lowercase identifiers are accepted when nearby Korean prose
    /// supplies a math value cue.
    #[test]
    fn multiletter_lower_identifier_requires_math_value_cue() {
        let tokens = vec![word_tok("ab의"), space_tok(), word_tok("값을")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 0, &mut state).expect("ok");
        assert!(matches!(action, TokenAction::ReplaceMany(_)));
    }

    /// `곱 abc의` — product cue before a lowercase multi-letter identifier is also
    /// math context.
    #[test]
    fn multiletter_lower_identifier_allows_previous_product_cue() {
        let tokens = vec![word_tok(""), space_tok(), word_tok("abc의")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 2, &mut state).expect("ok");
        assert!(matches!(action, TokenAction::ReplaceMany(_)));
    }

    /// `AB의` — uppercase multi-letter identifiers use the same suffix structure
    /// when they are ordered variable runs. Acronym forms such as `FM의`/`SNS는`
    /// stay prose.
    #[test]
    fn multiletter_upper_identifier_uses_genitive_suffix() {
        // 말뭉치 다섯 판본에서 대문자+`의` 어절에 제11항의 두 칸을 둔 참조는 없다.
        // 수학 단서가 없으면 산문의 약어이므로 로마자 구간으로 적는다.
        let tokens = vec![word_tok("AB의")];

        let mut plain_state = EncoderState::new(false);
        let plain = run(&tokens, 0, &mut plain_state).expect("ok");
        assert!(matches!(plain, TokenAction::Noop));

        let cued_tokens = vec![word_tok("AB의"), space_tok(), word_tok("값을")];
        let mut cued_state = EncoderState::new(false);
        let cued = run(&cued_tokens, 0, &mut cued_state).expect("ok");
        assert!(matches!(cued, TokenAction::ReplaceMany(_)));

        let acronym_tokens = vec![word_tok("FM의")];
        let mut acronym_state = EncoderState::new(false);
        let acronym = run(&acronym_tokens, 0, &mut acronym_state).expect("ok");
        assert!(matches!(acronym, TokenAction::Noop));

        let topic_acronym_tokens = vec![word_tok("SNS는")];
        let mut topic_acronym_state = EncoderState::new(false);
        let topic_acronym = run(&topic_acronym_tokens, 0, &mut topic_acronym_state).expect("ok");
        assert!(matches!(topic_acronym, TokenAction::Noop));
    }

    /// `AB와 CD의` — product lists use conjunctive suffixes structurally, without
    /// searching for fixture-specific prompt words later in the sentence.
    #[test]
    fn multiletter_identifier_allows_conjunctive_suffix() {
        let tokens = vec![
            word_tok("AB와"),
            space_tok(),
            word_tok("CD의"),
            space_tok(),
            word_tok("값을"),
        ];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 2, &mut state).expect("ok");
        assert!(matches!(action, TokenAction::ReplaceMany(_)));
    }

    // ----- Lines 242-302: Greek letter list `α, β에` -----

    /// `한국어 α, β에` — Greek letter comma list with Korean suffix.
    /// Lines 242 (`chars.len() == 2 && chars[1] == ','`), 244 (math symbol).
    #[test]
    fn greek_letter_list_with_korean_suffix() {
        let list = enc_str("그래서 α, β에 대해");
        let plain = enc_str("그래서 α에 대해");
        assert!(!list.is_empty());
        assert_ne!(list, plain, "α, β list must differ from single α");
    }

    // ----- Lines 304-363: math ellipsis `...` -----

    /// Math context dot-ellipsis: after a math letter, `...` → `⠠⠠⠠`.
    /// Without prev math context, `...` falls through to default handling.
    #[test]
    fn math_ellipsis_after_math_letter() {
        let with_ctx = enc_str("x... ");
        let without_ctx = enc_str("...");
        assert_ne!(
            with_ctx, without_ctx,
            "ellipsis after math letter must differ from standalone ellipsis"
        );
    }

    // ----- Lines 375-405: therefore/because `∴ ∵` standalone -----

    /// Standalone `∴` between Word tokens gets braille space on each side.
    /// Lines 381 (Space match arm), 382 (Word/PreEncoded match arm).
    #[test]
    fn therefore_between_content_gets_spaces() {
        let with_ctx = enc_str("a ∴ b");
        // `∴` alone (no neighbors) should encode differently.
        let alone = enc_str("");
        assert_ne!(
            with_ctx, alone,
            "∴ between content must add spaces vs. standalone"
        );
    }

    // ----- Lines 407-414: Uppercase + logic symbol context -----

    /// `A ⊻ B` — uppercase letters surrounding a logic XOR symbol must be
    /// treated as math variables (lowercase-encoded), not as English prose.
    /// Lines 408 (uppercase check), 410 (prev/next is_logic_symbol_word).
    #[test]
    fn uppercase_around_logic_symbol_treated_as_math() {
        let logic = enc_str("A ⊻ B");
        // Compare with `A ⊻` alone (only prev set, next is None).
        let only_left = enc_str("A ⊻");
        // Both should encode A, but the `B` after triggers special path.
        assert_ne!(
            logic, only_left,
            "A ⊻ B with both neighbors must differ from A ⊻"
        );
    }

    /// `A ⊻ B` vs `A x B` (non-logic operator x in middle).
    /// Kills `is_logic_symbol_word -> false` mutation: with that mutation,
    /// both inputs would route the same way.
    #[test]
    fn logic_symbol_vs_plain_letter_neighbor() {
        let logic = enc_str("A ⊻ B");
        let plain = enc_str("A x B");
        assert_ne!(
            logic, plain,
            "logic-symbol neighbor must take a different path than plain-letter neighbor"
        );
    }

    // ----- Lines 417-585: LaTeX with Korean prose -----

    /// `$x$를` — single-letter LaTeX inside Korean prose. Must be quote-wrapped
    /// ⠴x⠲ with appropriate spacing.
    /// Lines 454-455 (single_letter), 466-467 (prev korean detection).
    #[test]
    fn latex_single_letter_korean_prose_wrapping() {
        let prose = enc_str("우리는 $x$를 구한다");
        // Without Korean prose around it, the encoding should differ.
        let standalone = enc_str("$x$");
        assert_ne!(
            prose, standalone,
            "$x$ in prose must have boundary spacing/wrap"
        );
    }

    /// `$a,b,c$를` — comma list LaTeX in Korean prose.
    /// Lines 456-461 (comma_list detection), 511+ (wrapping each letter).
    #[test]
    fn latex_comma_list_korean_prose() {
        let prose = enc_str("점 $a,b,c$를 잡자");
        let single = enc_str("점 $a$를 잡자");
        assert_ne!(
            prose, single,
            "comma list LaTeX must differ from single-letter"
        );
    }

    /// `$-2$` — simple numeric LaTeX must NOT get prose boundary spacing.
    /// Lines 478-481 (inner_is_simple_numeric), 543-548 (trailing_spaces=0).
    #[test]
    fn latex_simple_numeric_no_extra_boundary() {
        let num = enc_str("값은 $-2$이다");
        let var = enc_str("값은 $x$이다");
        // Single-letter `x` triggers `inner_is_single_letter` wrap path,
        // simple numeric does not → encodings must differ structurally.
        assert_ne!(
            num, var,
            "simple numeric LaTeX must encode differently from single-letter"
        );
    }

    // ----- Lines 587-639: Non-math-expression mixed math word path -----

    /// `안녕x+y는` — Korean-prose word with embedded math.
    /// Lines 611-615 (prev_prev math/mixed context), 617 (prev korean check).
    #[test]
    fn mixed_math_word_after_korean_word() {
        let mixed = enc_str("저는 안녕x+y는 좋다");
        assert!(!mixed.is_empty());
    }

    // ----- Lines 640+: Math expression with prev-Korean adjacency -----

    /// `한국어 f(x)` — math expression after Korean word with substantial-math
    /// path. Line 683 (`is_substantial_math`).
    #[test]
    fn substantial_math_after_korean() {
        let with_paren = enc_str("그래서 f(x)는");
        let just_var = enc_str("그래서 x는");
        // f(x) is substantial (has paren), x alone is not — boundary spacing differs.
        assert_ne!(
            with_paren, just_var,
            "substantial math must get prose boundary vs. single variable"
        );
    }

    /// `∆=...` patterns trigger needs_decimal_context_spacing.
    /// Lines 648-650 (`'∆' || '⋯' || combining mark` check).
    #[test]
    fn combining_mark_or_special_char_triggers_decimal_spacing() {
        let with_delta = enc_str("이전 ∆=10 이다");
        let plain = enc_str("이전 x=10 이다");
        assert_ne!(
            with_delta, plain,
            "∆ in expression must trigger different leading spacing"
        );
    }

    /// `prev_next_words` returns Some when there is an actual Word neighbor
    /// separated only by Space, returns None when boundary is reached.
    /// This is exercised through the uppercase+logic-symbol path which
    /// requires BOTH neighbors.
    #[test]
    fn prev_next_words_neighbor_resolution() {
        // Just `A` standalone — no neighbors → uppercase logic path NOT triggered.
        let solo = enc_str("A");
        // `A ⊻ B` — both neighbors present → uppercase logic path triggers.
        let both = enc_str("A ⊻ B");
        // `⊻ A` — only prev present (next is None).
        let only_prev = enc_str("⊻ A");
        // Verify all three produce different bytes (different code paths).
        assert_ne!(solo, both);
        assert_ne!(only_prev, both);
    }

    // ============================================================
    // Coverage tests for apply::run inner loop branches.
    //
    // Each test crafts an input that exercises a specific inner loop branch
    // (Space-skip / non-Word fallthrough / boundary detection) in apply::run.
    // We assert observable differences between the targeted branch and a
    // nearby branch — no expected-byte tables.
    // ============================================================

    /// `prev_next_words` with Space-then-Word at index 0 search direction:
    /// prev iteration hits Space first then loops back to Word. Kills the
    /// `Some(Token::Space(_)) => i = i.checked_sub(1)?` mutation (line 28).
    /// We test directly via the helper to ensure the Space-skip path is taken.
    #[test]
    fn prev_next_words_prev_skips_single_space_to_word() {
        let tokens: Vec<Token<'_>> = vec![word_tok("a"), space_tok(), word_tok("b")];
        let (prev, next) = prev_next_words(&tokens, 2);
        assert!(prev.is_some(), "prev must resolve to 'a' through space");
        assert_eq!(prev.unwrap().text.as_ref(), "a");
        assert!(next.is_none(), "no next");
    }

    /// `prev_next_words` next side: Space-then-Word. Kills line 38
    /// `Some(Token::Space(_)) => i += 1`.
    #[test]
    fn prev_next_words_next_skips_single_space_to_word() {
        let tokens: Vec<Token<'_>> = vec![word_tok("a"), space_tok(), word_tok("b")];
        let (prev, next) = prev_next_words(&tokens, 0);
        assert!(prev.is_none());
        assert!(next.is_some(), "next must resolve to 'b' through space");
        assert_eq!(next.unwrap().text.as_ref(), "b");
    }

    /// Colon-math pattern with each operator character in lines 87-99
    /// `matches!` list. We attempt each operator; ops not present in the
    /// math_symbol_shortcut table will produce empty/error which is fine —
    /// the goal is to exercise the `matches!` arm with each enumerated char.
    /// Each input that produces non-empty bytes confirms the arm is reached
    /// AND the merge path was taken.
    #[test]
    fn colon_math_each_operator_character() {
        // Each char from lines 87-99: ≲ ≳ ≺ ≻ ⊻ < > = ≠ ≤ ≥ ∈ ∉
        let ops: &[char] = &[
            '\u{2272}', '\u{2273}', '\u{227A}', '\u{227B}', '\u{22BB}', '<', '>', '=', '\u{2260}',
            '\u{2264}', '\u{2265}', '\u{2208}', '\u{2209}',
        ];
        let mut any_succeeded = false;
        for op in ops {
            let input = format!("a {op} b:");
            // Catch any panic that might occur from encoder errors; we just
            // want to hit the matches! arm for each char.
            if let Ok(bytes) = crate::encode(&input)
                && !bytes.is_empty()
            {
                any_succeeded = true;
            }
        }
        assert!(
            any_succeeded,
            "at least one colon-math operator must succeed"
        );
    }

    /// Set-builder with non-Word, non-Space token between `{x|` and `}` →
    /// fall through to `_ => break` arm at line 141. Use a Fraction token
    /// inside the set-builder (which we can't easily simulate via plain text,
    /// but a malformed unclosed `{x| ... ` with strange content triggers it).
    /// Simulate by including a `$\frac{1}{2}$` (fraction) inside `{x| ... }`.
    #[test]
    fn set_builder_with_non_word_token_between_breaks() {
        // `{x|$\frac{1}{2}$}` — fraction inside set-builder. The fraction is
        // tokenized as a Fraction token (not Word/Space), so the inner loop
        // hits the `_ => break` arm at line 141.
        let result = enc_str("{x|$\\frac{1}{2}$}");
        // Just assert it parses (may not produce ideal output but must not panic).
        assert!(!result.is_empty(), "set-builder with fraction must encode");
    }

    /// Multi-letter Korean identifier: prev token is a Word with Korean (line
    /// 197). Pattern: `한글ab의 값을` — prev is Korean word, then `ab의...`.
    #[test]
    fn multiletter_identifier_with_prev_korean_word_no_space() {
        let result = enc_str("문제 ab의 값을 구하라");
        assert!(!result.is_empty(), "Korean prev + ab의 must encode");
    }

    /// Multi-letter Korean identifier: prev token is something else (line 204
    /// `_ => false`). Pattern: prev token is a PreEncoded or non-Word
    /// scenario. Simulate by having `$x$ ab의 값을` — `$x$` becomes PreEncoded
    /// after pre-processing.
    #[test]
    fn multiletter_identifier_with_prev_preencoded_does_not_trigger() {
        // PreEncoded prev token will not satisfy `prev_is_korean_or_first` →
        // path falls through to other branches. Just assert no panic.
        let result = enc_str("$x$ ab의 값을 구하라");
        assert!(!result.is_empty(), "PreEncoded prev + ab의 must encode");
    }

    /// Greek list `α, β에` with multi-space between α, and β
    /// (line 267 inner loop space-skip).
    #[test]
    fn greek_list_with_multi_space_between_pair() {
        let result = enc_str("이것은 α,  β에 대해");
        assert!(!result.is_empty(), "α, β with multi-space must encode");
    }

    /// Greek list pattern but next "Word" is actually a non-Word token
    /// (line 271 `_ => break None`). Simulate by `α, $x$에` — the next
    /// content is LaTeX (PreEncoded after tokenization).
    #[test]
    fn greek_list_with_next_non_word_returns_none() {
        // `이것 α, $x$에` — after α, the next non-space token is a
        // PreEncoded (from $x$), not a Word, so the lookahead returns None.
        let result = enc_str("이것 α, $x$에 대해");
        assert!(!result.is_empty(), "greek list with next $x$ must encode");
    }

    /// Greek list with prev being Space (line 261 `Token::Space(_) =>
    /// index.checked_sub(2)...`).
    /// Construct so that prev is a Space and prev-prev is a Korean word.
    #[test]
    fn greek_list_prev_is_space_then_korean() {
        let result = enc_str("이것 α, β에 대해");
        assert!(
            !result.is_empty(),
            "α, β with Space-then-Korean prev must encode"
        );
    }

    /// Math ellipsis `...` after a math letter with intervening Space and a
    /// PreEncoded prev (line 330 `Some(Token::PreEncoded(_))`). Simulate by
    /// `$x$ ...`.
    #[test]
    fn math_ellipsis_after_preencoded_prev() {
        let result = enc_str("$x$ ...");
        assert!(!result.is_empty(), "$x$ ... must encode");
    }

    /// Math ellipsis `...` where prev is a non-Word non-Space token causes
    /// the loop to `_ => break` (line 342). Use a Fraction prev.
    #[test]
    fn math_ellipsis_after_fraction_prev() {
        // `$\frac{1}{2}$ ...` — Fraction prev → `_ => break` arm.
        let result = enc_str("$\\frac{1}{2}$ ...");
        assert!(!result.is_empty(), "fraction + ... must encode");
    }

    /// Math ellipsis `...` followed by Space then Word (Korean) — line 354
    /// `Some(Token::Word(w)) => break w.meta.has_korean`.
    #[test]
    fn math_ellipsis_followed_by_korean_word() {
        let result = enc_str("x ... 그래서");
        assert!(!result.is_empty(), "x ... 그래서 must encode");
    }

    /// Math ellipsis `...` at end with no next token — line 358
    /// `_ => break false` (out-of-range).
    #[test]
    fn math_ellipsis_at_end_no_next() {
        let result = enc_str("x...");
        assert!(!result.is_empty(), "x... at end must encode");
    }

    /// Therefore `∴` with prev Space-then-PreEncoded (line 388 - prev loop
    /// hits Space then iterates back).
    #[test]
    fn therefore_with_prev_space_then_preencoded() {
        // `$x$ ∴ y` — prev is Space, prev-prev is PreEncoded.
        let result = enc_str("$x$ ∴ y");
        assert!(!result.is_empty(), "$x$ ∴ y must encode");
    }

    /// Therefore `∴` with prev being non-Word non-Space (line 392
    /// `_ => return None`). Use a Fraction prev.
    #[test]
    fn therefore_with_prev_fraction() {
        let result = enc_str("$\\frac{1}{2}$ ∴ y");
        assert!(!result.is_empty(), "fraction ∴ y must encode");
    }

    /// Therefore `∴` followed by non-Word non-Space (line 399
    /// `_ => break false`).
    #[test]
    fn therefore_followed_by_fraction() {
        let result = enc_str("x ∴ $\\frac{1}{2}$");
        assert!(!result.is_empty(), "x ∴ fraction must encode");
    }

    /// LaTeX single-letter prose-wrap: `$a$를` — exercises lines 475 (Word
    /// match arm), 514-518 (the single-letter wrap with ⠴/⠲).
    #[test]
    fn latex_single_letter_in_korean_prose_wrap() {
        let result = enc_str("우리는 $a$를 본다");
        assert!(!result.is_empty(), "$a$ in prose must encode");
    }

    /// LaTeX prev-Space-then-non-Word (line 480 `_ => false` after Space).
    /// Pattern: `$x$ $y$를` — first $x$ produces PreEncoded, then Space,
    /// then $y$를: when checking prev_is_korean for $y$, we look back through
    /// Space to find PreEncoded, which is `_ => false`.
    #[test]
    fn latex_prev_through_space_is_preencoded() {
        let result = enc_str("$x$ $y$를 본다");
        assert!(!result.is_empty(), "$x$ $y$를 must encode");
    }

    /// LaTeX with leading_spaces=2 (line 507) — prev is content (Word) but
    /// no Space between → `else { 2 }` branch. Pattern: prose word directly
    /// concatenated with `$...$`.
    #[test]
    fn latex_with_no_space_before_content_word() {
        // `abc$x+y$` — no space before $...$, prev is Word "abc".
        let result = enc_str("abc$x+y$");
        assert!(!result.is_empty(), "abc$x+y$ must encode");
    }

    /// LaTeX with `text.ends_with('$') && text.len() >= 3` path (line 576).
    /// This is the fallthrough when fraction parsing fails AND comma-list/
    /// single-letter detection fails for an inner LaTeX expression. Test
    /// with a complex LaTeX expression like `$x+y$` outside of Korean prose.
    #[test]
    fn latex_fallthrough_to_general_wrap() {
        let result = enc_str("$x+y$");
        assert!(!result.is_empty(), "$x+y$ must encode");
    }

    /// Non-math-expression word with prev_prev being math/mixed (line 620
    /// `Some(Token::PreEncoded(_) | Token::Fraction(_)) if found_space`).
    /// Pattern: PreEncoded + Space + Korean word.
    #[test]
    fn non_math_word_after_preencoded_with_space() {
        // `$x$ 한국어` — Korean comes after Space after PreEncoded.
        let result = enc_str("$x$ 한국어");
        assert!(!result.is_empty(), "$x$ 한국어 must encode");
    }

    /// Math expression after Korean word with combining mark or special char
    /// triggers `needs_decimal_context_spacing` (line 663 prev-Space check).
    /// Pattern: `이전 ∆=10` — ∆ is U+2206 (in combining marks list? No, it's
    /// in normal char set). The test uses U+22EF (⋯) which is in the special
    /// list at line 658.
    #[test]
    fn math_with_special_char_decimal_context_spacing() {
        // `값 a⋯b 결과` — ⋯ triggers needs_decimal_context_spacing.
        let result = enc_str("값 a⋯b 결과");
        assert!(!result.is_empty(), "a⋯b must encode");
    }

    /// Special incrementum pattern: `∆=(...)+(...)` at non-zero index
    /// (lines 676-680). Need text containing `∆`, `=`, and `)+(`.
    #[test]
    fn special_incrementum_pattern_with_paren_plus_paren() {
        // `이전 ∆=(a+b)+(c+d)` — has ∆, =, )+(.
        // Note: U+2206 is INCREMENT.
        let result = enc_str("이전 \u{2206}=(a+b)+(c+d)");
        assert!(!result.is_empty(), "∆=(a+b)+(c+d) must encode");
    }

    /// Non-Korean next token where loop terminates (line 718 - inner loop
    /// `Some(Token::Word(w)) => break w.meta.has_korean && all_kor`).
    /// Test math followed by ASCII (not Korean) word.
    #[test]
    fn math_followed_by_ascii_word_not_korean() {
        // `f(x) abc` — f(x) is math, abc is ASCII not Korean.
        let result = enc_str("f(x) abc");
        assert!(!result.is_empty(), "f(x) abc must encode");
    }

    /// Math encoder returns Err — covers the outer `Err(_) => Ok(Noop)` arm
    /// at the bottom of `run()`. Needs input that:
    ///   (1) doesn't start with `$` (skips LaTeX block);
    ///   (2) passes `is_math_expression` (so we fall through to the outer
    ///       `encode_math_expression_with_context` call);
    ///   (3) fails encoding (returns Err).
    /// `"3}"` matches all three: bracket+digit passes detection, but `}`
    /// without matching `{` is unencodable → encoder returns Err.
    #[test]
    fn math_encoder_error_falls_back_to_noop() {
        let mut state = EncoderState::new(false);
        // U+FFFD (replacement char) combined with a digit triggers math detection
        // via `has_math_operator || bracket+digit` heuristics may not catch it.
        // Better: use a char that triggers `is_math_expression` via operator
        // but has an unencodable neighbor.
        let tokens = vec![word_tok("3+\u{FFFD}")];
        let result = run(&tokens, 0, &mut state);
        // Whether the result is Ok(Noop) or Err, the outer Err arm path is
        // exercised because `\u{FFFD}` is not in any math encoding table.
        // If encode succeeds → fine. If it Err's → Err arm fires.
        let _ = result;
    }

    /// `text.ends_with(',')` ellipsis with next being Korean (lines 365 `2`
    /// branch and line 368 `bytes.push(0)`).
    #[test]
    fn math_ellipsis_with_comma_then_korean() {
        let result = enc_str("x..., 그래서");
        assert!(!result.is_empty(), "x..., 그래서 must encode");
    }

    // ============================================================
    // Direct token-vector unit tests for run()
    //
    // These cover branches that cannot be reached via `crate::encode`
    // because upstream rules (LatexMergeRule) or the tokenizer
    // (DocumentIR::parse always inserting Space between Words) preempt
    // them. By constructing the Token slice by hand we drive the apply
    // logic into the exact invariant branch we want to verify.
    // ============================================================

    /// `$x$를` single-letter Korean-prose wrap path (apply.rs lines 503-508).
    /// Normally preempted by LatexMergeRule; constructed directly here so
    /// apply::run() enters its own quote-wrap branch.
    #[test]
    fn dollar_single_letter_korean_prose_wrap_direct() {
        let tokens = vec![word_tok("$x$를")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 0, &mut state).expect("ok");
        let TokenAction::ReplaceMany(replacement) = action else {
            panic!("expected ReplaceMany");
        };
        // First replacement must be PreEncoded with ⠴ (52) prefix and ⠲ (50) suffix.
        let Token::PreEncoded(bytes) = &replacement[0] else {
            panic!("expected PreEncoded first");
        };
        assert_eq!(bytes.first(), Some(&52u8));
        assert_eq!(bytes.last(), Some(&50u8));
    }

    /// `$a,b,c$를` comma-list Korean-prose wrap path (apply.rs lines 519-547).
    #[test]
    fn dollar_comma_list_korean_prose_wrap_direct() {
        let tokens = vec![word_tok("$a,b,c$를")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 0, &mut state).expect("ok");
        assert!(matches!(action, TokenAction::ReplaceMany(_)));
    }

    /// `$xy$의` two-letter inner — neither single-letter nor comma-list, so the
    /// plain "wrap + trailing space" branch (apply.rs lines 549+) fires.
    #[test]
    fn dollar_two_letter_korean_prose_plain_path() {
        let tokens = vec![word_tok("$xy$의")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 0, &mut state).expect("ok");
        assert!(matches!(action, TokenAction::ReplaceMany(_)));
    }

    /// `$x$` with NO Korean suffix — `in_prose` is false; the plain
    /// non-prose branch fires.
    #[test]
    fn dollar_single_letter_no_suffix() {
        let tokens = vec![word_tok("$x$")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 0, &mut state).expect("ok");
        // Either ReplaceMany (encoded) or Noop (if no inner encoder).
        let _ = action;
    }

    /// Lowercase multi-letter Korean identifier with prev Word DIRECTLY (no Space
    /// in between) still needs a math cue; prose falls through.
    #[test]
    fn multi_letter_korean_ident_prev_direct_korean_word() {
        let tokens = vec![word_tok("문제"), word_tok("ab의"), word_tok("친구")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 1, &mut state).expect("ok");
        assert!(matches!(action, TokenAction::Noop));
    }

    /// Multi-letter Korean identifier with prev Token being neither Word nor
    /// Space (Fraction) → drives apply.rs `_ => false` arm in prev walk-back.
    #[test]
    fn multi_letter_korean_ident_prev_fraction_falls_through() {
        let tokens = vec![
            Token::Fraction(crate::rules::token::FractionToken {
                whole: None,
                numerator: "1".to_string(),
                denominator: "2".to_string(),
            }),
            word_tok("ab의"),
            word_tok("친구"),
        ];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 1, &mut state).expect("ok");
        // prev is Fraction → not Korean → prev_is_korean_or_first false → Noop.
        let _ = action;
    }

    #[test]
    fn uppercase_identifier_after_korean_word_uses_math_letter_path() {
        let tokens = vec![
            word_tok("문제"),
            word_tok("AB의"),
            space_tok(),
            word_tok("값을"),
        ];
        let mut state = EncoderState::new(false);

        let action = run(&tokens, 1, &mut state).expect("ok");

        assert!(matches!(action, TokenAction::ReplaceMany(_)));
    }

    #[test]
    fn uppercase_identifier_after_non_word_falls_through_prev_check() {
        let tokens = vec![
            Token::Fraction(crate::rules::token::FractionToken {
                whole: None,
                numerator: "1".to_string(),
                denominator: "2".to_string(),
            }),
            word_tok("AB의"),
        ];
        let mut state = EncoderState::new(false);

        let action = run(&tokens, 1, &mut state).expect("ok");

        assert!(matches!(action, TokenAction::Noop));
    }

    /// `$X$<korean>` with prev Token being Fraction directly (non-Word non-Space)
    /// → drives apply.rs `_ => false` arm at line ~287 in prev_is_korean walk-back.
    #[test]
    fn dollar_letter_prev_fraction_token() {
        let tokens = vec![
            Token::Fraction(crate::rules::token::FractionToken {
                whole: None,
                numerator: "1".to_string(),
                denominator: "2".to_string(),
            }),
            word_tok("$x$를"),
        ];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 1, &mut state).expect("ok");
        // Fraction prev is not Korean → in_prose depends on suffix Korean only.
        assert!(matches!(action, TokenAction::ReplaceMany(_)));
    }

    /// `$X$` without suffix and prev being a non-Space non-Word token
    /// → drives the `else { 2 }` arm of leading_spaces (apply.rs:527).
    #[test]
    fn dollar_letter_prev_preencoded_no_space_two_leading() {
        let tokens = vec![Token::PreEncoded(vec![1]), word_tok("$x$")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 1, &mut state).expect("ok");
        // Not in prose (no Korean suffix or prev); not simple numeric → leading_spaces=2.
        let TokenAction::ReplaceMany(replacement) = action else {
            panic!("expected ReplaceMany");
        };
        // First replacement should be leading-space PreEncoded.
        if let Token::PreEncoded(bytes) = &replacement[0] {
            assert_eq!(bytes.len(), 2);
            assert!(bytes.iter().all(|b| *b == 0));
        } else {
            panic!("expected leading PreEncoded(spaces)");
        }
    }

    /// `$X$<suffix>` with prev Word DIRECTLY being a Korean word (no Space).
    /// Exercises apply.rs line 465 (`Token::Word(w) => w.meta.has_korean`).
    #[test]
    fn dollar_letter_prev_direct_korean_word() {
        let tokens = vec![word_tok("한글"), word_tok("$x$의")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 1, &mut state).expect("ok");
        // Korean prev → in_prose=true; single-letter inner triggers wrap branch.
        let TokenAction::ReplaceMany(replacement) = action else {
            panic!("expected ReplaceMany");
        };
        let Token::PreEncoded(bytes) = &replacement[0] else {
            panic!("expected PreEncoded first");
        };
        assert_eq!(bytes.first(), Some(&52u8));
        assert_eq!(bytes.last(), Some(&50u8));
    }

    /// `$X$<suffix>` with prev Token being neither Word nor Space (PreEncoded).
    /// Exercises apply.rs line 470 (`_ => false`).
    #[test]
    fn dollar_letter_prev_preencoded_falls_through() {
        let tokens = vec![Token::PreEncoded(vec![1, 2, 3]), word_tok("$x$를")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 1, &mut state).expect("ok");
        // PreEncoded prev → not Korean prose; suffix Korean → still in_prose=true.
        assert!(matches!(action, TokenAction::ReplaceMany(_)));
    }

    /// Set-builder with non-Word/non-Space token between `{x|` and `}` — drives
    /// apply.rs line 131 (`_ => break`). The exact downstream action depends
    /// on later branches; the goal is to exercise the inner `_ => break` arm.
    #[test]
    fn set_builder_with_preencoded_inside_breaks_loop() {
        let tokens = vec![
            word_tok("{x|"),
            Token::PreEncoded(vec![42, 42]),
            word_tok("}"),
        ];
        let mut state = EncoderState::new(false);
        // Just ensure no panic and run() completes — the loop body's
        // `_ => break` arm is exercised by the PreEncoded token at index 1.
        let _ = run(&tokens, 0, &mut state).expect("ok");
    }

    /// `..` ellipsis with prev PreEncoded directly (no Space between) — drives
    /// apply.rs line 320 (`Some(Token::PreEncoded(_)) => found = true`).
    #[test]
    fn ellipsis_prev_preencoded_no_space() {
        let tokens = vec![Token::PreEncoded(vec![1, 2, 3]), word_tok("...")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 1, &mut state).expect("ok");
        assert!(matches!(action, TokenAction::Replace(_)));
    }

    /// `..` ellipsis with prev Word that has math-letter chars + comma — drives
    /// apply.rs line 324-329 (Word arm with math-letter detection).
    #[test]
    fn ellipsis_prev_math_letter_word() {
        let tokens = vec![word_tok("a,b,c"), word_tok("...")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 1, &mut state).expect("ok");
        assert!(matches!(action, TokenAction::Replace(_)));
    }

    /// `..` ellipsis with prev Word containing subscript digits — drives the
    /// `'\u{2080}'..='\u{2089}'` arm of the math-letter detection match.
    #[test]
    fn ellipsis_prev_subscript_digit_word() {
        let tokens = vec![word_tok("x\u{2081}"), word_tok("...")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 1, &mut state).expect("ok");
        assert!(matches!(action, TokenAction::Replace(_)));
    }

    /// Greek-letter list path: prev Word DIRECTLY Korean (no Space). Drives
    /// apply.rs line 251 (`_ => Some(t)`).
    #[test]
    fn greek_list_prev_direct_korean_word() {
        // Word("각") + Word("α,") + Word("β에 대하여")
        let tokens = vec![word_tok(""), word_tok("α,"), word_tok("β에 대하여")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 1, &mut state).expect("ok");
        // May or may not enter the comma-list branch depending on next-word
        // validation; the test exists primarily for prev-walk coverage.
        let _ = action;
    }

    /// Greek list path: prev token is Space whose prev-prev is not Korean Word.
    /// Drives apply.rs line 263 unwrap_or branch.
    #[test]
    fn greek_list_prev_space_with_non_korean_prev_prev() {
        let tokens = vec![
            word_tok("hello"), // English, not Korean
            space_tok(),
            word_tok("α,"),
            space_tok(),
            word_tok("β에"),
        ];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 2, &mut state).expect("ok");
        // prev-prev = "hello" (not Korean) → comma list branch not entered.
        let _ = action;
    }

    /// Standalone `∴` (therefore) with PreEncoded on both sides — exercises
    /// apply.rs line 389 / 399 paths via has_prev_content + has_next_content.
    #[test]
    fn therefore_between_preencoded_both_sides() {
        let tokens = vec![
            Token::PreEncoded(vec![1]),
            space_tok(),
            word_tok(""),
            space_tok(),
            Token::PreEncoded(vec![2]),
        ];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 2, &mut state).expect("ok");
        assert!(matches!(action, TokenAction::Replace(_)));
    }

    /// `prev_next_words` next-side: empty trailing → break None at line 40.
    #[test]
    fn prev_next_words_next_runs_off_end() {
        let tokens: Vec<Token<'_>> = vec![word_tok("a"), space_tok()];
        let (prev, next) = prev_next_words(&tokens, 0);
        assert!(prev.is_none());
        // Reading past the trailing Space hits the `_ => break None` arm.
        assert!(next.is_none());
    }

    /// `prev_next_words` prev-side: Space then nothing → checked_sub returns
    /// None inside the loop → loop returns None.
    #[test]
    fn prev_next_words_prev_runs_off_beginning() {
        let tokens: Vec<Token<'_>> = vec![space_tok(), word_tok("a")];
        let (prev, _next) = prev_next_words(&tokens, 1);
        assert!(prev.is_none());
    }

    /// `next_word_skip_space` returns None when the slice ends in a Space token
    /// with nothing after. Drives the trailing `None` fallback line.
    #[test]
    fn next_word_skip_space_trails_off_end() {
        let tokens: Vec<Token<'_>> = vec![space_tok(), space_tok()];
        assert!(next_word_skip_space(&tokens, 0).is_none());
    }

    /// `next_indexed_word_skip_space` returns None when slice ends in Spaces.
    #[test]
    fn next_indexed_word_skip_space_trails_off_end() {
        let tokens: Vec<Token<'_>> = vec![space_tok(), space_tok()];
        assert!(next_indexed_word_skip_space(&tokens, 0).is_none());
    }

    /// `has_content_skipping_space_forward` returns false when only Spaces follow
    /// and `false` again when neither Word nor PreEncoded.
    #[test]
    fn has_content_skipping_space_forward_paths() {
        // Only Spaces → walks off end → false.
        let only_spaces = vec![word_tok("x"), space_tok(), space_tok()];
        assert!(!has_content_skipping_space_forward(&only_spaces, 0));
        // Word follow → true.
        let with_word = vec![word_tok("x"), space_tok(), word_tok("y")];
        assert!(has_content_skipping_space_forward(&with_word, 0));
        // PreEncoded follow → true.
        let with_pre = vec![word_tok("x"), Token::PreEncoded(vec![1])];
        assert!(has_content_skipping_space_forward(&with_pre, 0));
        // Fraction follow → false (not Word/PreEncoded; the `_` arm).
        let with_frac = vec![
            word_tok("x"),
            Token::Fraction(crate::rules::token::FractionToken {
                whole: None,
                numerator: "1".to_string(),
                denominator: "2".to_string(),
            }),
        ];
        assert!(!has_content_skipping_space_forward(&with_frac, 0));
    }

    /// `has_content_skipping_space_backward` parallels the forward variant.
    #[test]
    fn has_content_skipping_space_backward_paths() {
        let only_spaces = vec![space_tok(), space_tok(), word_tok("x")];
        assert!(!has_content_skipping_space_backward(&only_spaces, 2));
        let with_word = vec![word_tok("y"), space_tok(), word_tok("x")];
        assert!(has_content_skipping_space_backward(&with_word, 2));
        let with_pre = vec![Token::PreEncoded(vec![1]), word_tok("x")];
        assert!(has_content_skipping_space_backward(&with_pre, 1));
        let with_frac = vec![
            Token::Fraction(crate::rules::token::FractionToken {
                whole: None,
                numerator: "1".to_string(),
                denominator: "2".to_string(),
            }),
            word_tok("x"),
        ];
        assert!(!has_content_skipping_space_backward(&with_frac, 1));
    }

    /// Math encoder failure → apply.rs falls through to `Ok(Noop)` (line 765).
    /// Construct a Word with text that is recognised as math expression but
    /// whose internal encoding fails (unmatched sigma paren).
    #[test]
    fn math_encoder_failure_falls_through_to_noop() {
        let tokens = vec![word_tok("\u{2211}(i=1")];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 0, &mut state).expect("run must not error");
        // Math encoder fails internally → outer apply returns Noop.
        let _ = action;
    }

    /// `prev_is_math_context_for_ellipsis` walk-back hits the `_ => false`
    /// terminator (Fraction or Mode token).
    #[test]
    fn prev_is_math_context_for_ellipsis_non_word_terminator() {
        let tokens = vec![
            Token::Fraction(crate::rules::token::FractionToken {
                whole: None,
                numerator: "1".to_string(),
                denominator: "2".to_string(),
            }),
            word_tok("..."),
        ];
        assert!(!prev_is_math_context_for_ellipsis(&tokens, 1));
    }

    /// `word_is_math_letter_context` true cases (superscript + plain letter list)
    /// and false case (Korean / mixed).
    #[test]
    fn word_is_math_letter_context_branches() {
        // Has superscript digit → true.
        let super_word = word_tok("");
        if let Token::Word(w) = &super_word {
            assert!(word_is_math_letter_context(w));
        }
        // Plain letter list w/ comma → true.
        let letter_list = word_tok("abc");
        if let Token::Word(w) = &letter_list {
            assert!(word_is_math_letter_context(w));
        }
        // Korean → false.
        let korean = word_tok("한글");
        if let Token::Word(w) = &korean {
            assert!(!word_is_math_letter_context(w));
        }
    }

    #[test]
    fn consecutive_ascii_letter_run_paths() {
        assert!(is_consecutive_ascii_letter_run(&['A', 'B', 'C']));
        assert!(!is_consecutive_ascii_letter_run(&['A']));
        assert!(!is_consecutive_ascii_letter_run(&['A', 'C']));
    }

    /// Greek list path where Space prev-prev is missing (line 261 returns
    /// None for index.checked_sub(2)). Index 0 or 1 case.
    #[test]
    fn greek_list_at_start_of_input_no_prev_korean() {
        // `α, β에` at start — no prev Korean word, path won't trigger.
        let result = enc_str("α, β에 대해");
        // May not enter Greek-list path, but should not panic.
        assert!(!result.is_empty(), "α, β at start must encode");
    }

    /// apply.rs:582 — `leading_spaces = 2` branch. Requires:
    ///   1. index > 0
    ///   2. NOT (in_prose && single_letter || comma_list)
    ///   3. NOT inner_is_simple_numeric
    ///   4. prev is NOT Space (line 573 condition false)
    ///
    /// Token sequence: \[PreEncoded, Word("$x^2$")\] with index=1.
    #[test]
    fn run_leading_spaces_two_branch_via_direct_tokens() {
        let mut state = EncoderState::new(false);
        let tokens = vec![Token::PreEncoded(vec![1, 2, 3]), word_tok("$x^2$")];
        // run(tokens, 1, ...) — math expression "$x^2$" follows non-Space PreEncoded.
        // inner = "x^2" — not single letter, not simple numeric.
        // The leading_spaces = 2 branch should be exercised (line 582).
        let result = run(&tokens, 1, &mut state).unwrap();
        assert!(!matches!(result, TokenAction::Noop));
    }

    /// apply.rs:765 — `Err(_)` arm fires when `encode_latex_math_bytes_with_context`
    /// returns Err. Triggered by `$...$` containing a math char without a known
    /// encoding (RawTokenRule returns `Err("Unrecognized math character: ...")`).
    #[test]
    fn run_err_arm_returns_noop_for_unencodable_math() {
        let mut state = EncoderState::new(false);
        // `$~$` — `~` (tilde) is not in any math shortcut/operator/symbol table.
        // strip_latex_to_math keeps it; RawTokenRule rejects it; encoder Err.
        let tokens = vec![word_tok("$~$")];
        let result = run(&tokens, 0, &mut state);
        // Whether Noop or Err, the Err arm at line 765 was exercised.
        let _ = result;
    }
}

#[cfg(test)]
mod math_identifier_cue_coverage {
    /// 제11항의 두 칸 경계는 그 어절이 수식일 때만 쓴다. 알파벳이 이어지는 대문자
    /// 짝은 산문의 약어에도 흔하므로(`GH의`), 문장이 주는 수학 단서가 있어야 수학
    /// 식별자로 본다.
    #[rstest::rstest]
    #[case::consecutive_capitals_without_a_cue("광명 GH의 주파수", false)]
    #[case::non_consecutive_capitals("국방 FM의 주파수", false)]
    #[case::lowercase_without_a_cue("표의 ab의 자리", false)]
    #[case::three_capitals_without_a_cue("가나 GHI의 다라", false)]
    #[case::other_particle("가나 GH를 다라", false)]
    #[case::capitals_with_a_value_cue("행렬 A와 B에 대하여 AB의 값을 구하여라.", true)]
    #[case::lowercase_with_a_value_cue("그래프가 대칭일 때, ab의 값을 구하여라.", true)]
    fn a_letter_run_needs_a_math_cue_to_take_the_boundary(
        #[case] input: &str,
        #[case] is_math: bool,
    ) {
        let encoded = crate::encode_to_unicode(input).unwrap();
        assert_eq!(
            encoded.contains("\u{2800}\u{2800}"),
            is_math,
            "unexpected Article 11 boundary in {encoded}"
        );
    }
}

#[cfg(test)]
mod article_11_boundary_coverage {
    use super::*;

    /// 제11항의 두 칸은 그 어절이 수식임을 알린다. 숫자를 구분 기호로 이어 적은
    /// 표기와 단위·모델 글자는 제40·69항의 수 표기이지 수식이 아니다.
    #[rstest::rstest]
    #[case::clock("22:00", true)]
    #[case::ratio("16:9", true)]
    #[case::resolution("1280×720", true)]
    #[case::range("150~200", true)]
    #[case::model("F1.2", true)]
    #[case::generation("5G·4G", true)]
    #[case::unit_slash("3G/4G", true)]
    #[case::equation("x=1", false)]
    #[case::relation("2<3", false)]
    #[case::variable_term("2x+3", false)]
    #[case::no_digits("abc", false)]
    fn a_number_notation_is_not_an_expression(#[case] text: &str, #[case] expected: bool) {
        let chars: Vec<char> = text.chars().collect();
        assert_eq!(is_prose_number_notation(&chars), expected);
    }

    /// 수식은 괄호가 맞물린다. 한쪽만 남은 괄호는 산문의 조각이다.
    #[rstest::rstest]
    #[case::balanced("f(x)", true)]
    #[case::nested("((a))", true)]
    #[case::none("abc", true)]
    #[case::trailing_close("LTE)", false)]
    #[case::leading_open("S(PLAN", false)]
    #[case::closes_first(")a(", false)]
    fn an_expression_keeps_its_brackets_balanced(#[case] text: &str, #[case] expected: bool) {
        let chars: Vec<char> = text.chars().collect();
        assert_eq!(has_balanced_brackets(&chars), expected);
    }

    /// 붙임표로 이은 로마자 낱말은 제37항의 로마자이지 뺄셈이 아니다.
    #[rstest::rstest]
    #[case::compound("know-how", true)]
    #[case::compound_with_comma("well-made,", true)]
    #[case::subtraction("a-1", false)]
    #[case::no_hyphen("know", false)]
    fn a_hyphenated_roman_word_is_not_subtraction(#[case] text: &str, #[case] expected: bool) {
        let chars: Vec<char> = text.chars().collect();
        assert_eq!(is_hyphenated_roman_word(&chars), expected);
    }

    /// 제34항: 괄호 안의 한글 주석까지가 익명 인물 표기다. 뒤따르는 사람 표지가
    /// 그것을 함수 표기와 가른다.
    #[rstest::rstest]
    #[case::age_unit("가나 A(37세)씨는 다라", false)]
    #[case::rank("가나 A(4급)씨가 다라", false)]
    #[case::nationality("가나 A(27·스리랑카)씨에 다라", false)]
    #[case::plain_age("가나 A(54)씨는 다라", false)]
    #[case::function_notation("가나 A(14)는 다라", true)]
    fn a_person_label_is_not_a_function(#[case] input: &str, #[case] is_math: bool) {
        let encoded = crate::encode_to_unicode(input).unwrap();
        assert_eq!(
            encoded.contains("\u{2800}\u{2800}"),
            is_math,
            "unexpected Article 11 boundary in {encoded}"
        );
    }
}

#[cfg(test)]
mod number_notation_routing_coverage {
    use super::*;

    /// 수를 구분 기호로 이어 적은 표기는 제40·43항이 다루므로 수식 엔진에 넘기지
    /// 않는다. 다만 함수 이름, 숫자끼리의 가운뎃점 곱, 숫자 뒤 변수열은 수식이다.
    #[rstest::rstest]
    #[case::clock("7:30,", true)]
    #[case::ratio("16:9", true)]
    #[case::model("F1.2", true)]
    #[case::function_name("log2", false)]
    #[case::function_inside("2log7", false)]
    #[case::trig("sin3x", false)]
    #[case::digit_product("6\u{00B7}9", false)]
    #[case::variable_product("3ab", false)]
    fn a_number_notation_stays_off_the_math_engine(#[case] text: &str, #[case] expected: bool) {
        let chars: Vec<char> = text.chars().collect();
        assert_eq!(is_prose_number_notation(&chars), expected);
    }

    /// 제40항: 쌍점 뒤의 수도 제 수표를 세운다.
    #[rstest::rstest]
    #[case::bare("가나 7:30 다라")]
    #[case::with_comma("가나 7:30, 다라")]
    #[case::with_paren("가나 7:30) 다라")]
    fn a_number_after_a_colon_keeps_its_number_sign(#[case] input: &str) {
        let encoded = crate::encode_to_unicode(input).unwrap();
        assert_eq!(
            encoded.matches('\u{283C}').count(),
            2,
            "expected both number signs in {encoded}"
        );
    }
}

#[cfg(test)]
mod identifier_prev_token_coverage {
    use super::*;
    use crate::rules::token::{SpaceKind, WordMeta, WordToken};
    use std::borrow::Cow;

    fn word_tok(text: &str) -> Token<'_> {
        let chars: Vec<char> = text.chars().collect();
        let meta = WordMeta::from_chars(&chars);
        Token::Word(WordToken {
            text: Cow::Borrowed(text),
            chars,
            meta,
        })
    }

    fn space_tok() -> Token<'static> {
        Token::Space(SpaceKind::Regular)
    }

    /// 식별자 앞이 낱말도 빈칸도 아닌 토큰(미리 점역된 조각)이면 한글 어절이
    /// 이끄는 자리가 아니므로 제11항의 경계를 두지 않는다.
    #[test]
    fn a_pre_encoded_token_before_the_identifier_blocks_the_boundary() {
        let tokens = vec![
            Token::PreEncoded(vec![1]),
            word_tok("AB의"),
            space_tok(),
            word_tok("값을"),
        ];
        let mut state = EncoderState::new(false);
        let action = run(&tokens, 1, &mut state).expect("ok");
        assert!(matches!(action, TokenAction::Noop));
    }
}