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
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Moderately AI Inc.
//! Hand-written, zero-copy SQL tokenizer (byte-offset cursor + token buffer).
//!
//! The tokenizer turns source text into [`Token`]s, each a lexical [`TokenKind`]
//! plus the byte [`Span`] it occupies. Tokens are `Copy` and borrow nothing:
//! text is recovered as `&source[span]` on demand, so token buffers are
//! cache-dense and outlive the scan borrow.
//!
//! Why hand-written and not `logos`: the non-regular cases — nested `/* … */`
//! block comments and PostgreSQL `$tag$ … $tag$` dollar-quoting — cannot be
//! expressed by a DFA. The hot loop is driven by the shared `[u8; 256]`
//! lexer-class table from `squonk-ast`.
//!
//! ```
//! use squonk::ast::Keyword;
//! use squonk::tokenizer::{tokenize, TokenKind};
//!
//! let src = "SELECT 1";
//! let tokens = tokenize(src).expect("valid SQL lexes");
//! assert_eq!(tokens[0].kind, TokenKind::Keyword(Keyword::Select));
//! assert_eq!(&src[tokens[0].span.start() as usize..tokens[0].span.end() as usize], "SELECT");
//! ```
//!
//! ## Errors
//!
//! [`tokenize`] is fail-fast: the first lexical problem (unterminated string,
//! quoted identifier, block comment, or dollar-quote; a stray byte) stops the
//! scan and returns a [`LexError`]. That error type is intentionally local to
//! this module — `squonk::error` is built concurrently by `m1-parse-error`,
//! and the two are reconciled later in `m1-engine`.
//!
//! ## Trivia
//!
//! Whitespace and comments are skipped, never emitted, but stay offset-recoverable:
//! gaps between adjacent token spans are exactly the trivia. A tool that needs the
//! comments/whitespace themselves can *opt in* to capturing each trivia run's
//! [`Span`] into a [`TriviaIndex`] — [`tokenize_with_trivia`] returns one alongside
//! the tokens, and `ParseConfig::capture_trivia` homes one on the parse root. Capture is off
//! by default so the hot lexer path pays nothing for it.
mod cursor;
mod error;
mod scan;
mod token;
mod trivia;
pub use cursor::Cursor;
pub use error::{LexError, LexErrorKind};
pub(crate) use scan::escape_string_segments_are_valid;
pub use token::{Operator, Punctuation, Token, TokenKind};
pub use trivia::{TriviaIndex, TriviaKind, TriviaRange};
pub(crate) use trivia::{NoTrivia, TriviaSink};
use crate::ast::Span;
use crate::ast::dialect::FeatureSet;
const STREAM_BUFFER_MIN_INITIAL_CAPACITY: usize = 8;
const STREAM_BUFFER_MAX_INITIAL_CAPACITY: usize = 64;
const STREAM_BUFFER_RETAINED_CAPACITY: usize = 4096;
/// Tokenize `src` eagerly into a flat token buffer.
///
/// Trivia (whitespace and comments) is skipped. On the first lexical error the
/// scan stops and returns a [`LexError`]. An eager `Vec` is the M1 contract; a
/// lazy/streaming driver over the same cursor is a later optimization.
///
/// # Errors
///
/// Returns [`LexError`] for an unterminated string/quoted-identifier/block-
/// comment/dollar-quote, a stray byte, or a source longer than `u32::MAX` bytes
/// (whose offsets would not fit the `u32` spans this tokenizer uses).
pub fn tokenize(src: &str) -> Result<Vec<Token>, LexError> {
tokenize_with(src, &FeatureSet::ANSI)
}
/// Tokenize `src` eagerly using `features` for dialect-owned lexical data.
///
/// This is the parser-facing entry point: keyword recognition is shared, while
/// byte-class dispatch comes from the dialect feature set. The parser itself
/// uses an internal buffered cursor to scan lazily; [`tokenize_with`] remains
/// the public eager tokenizer for callers that want a full token vector.
///
/// # Errors
///
/// Returns the same lexical errors as [`tokenize`].
pub fn tokenize_with(src: &str, features: &FeatureSet) -> Result<Vec<Token>, LexError> {
// Spans are `u32`; a longer source could not be addressed. Huge inputs are
// served by statement streaming (ADR-0005), not by widening spans (ADR-0002).
if u32::try_from(src.len()).is_err() {
return Err(LexError::new(LexErrorKind::SourceTooLong, Span::new(0, 0)));
}
let mut cursor = Cursor::new(src);
// Most tokens are a few bytes wide; this hint avoids the early reallocations
// without meaningfully over-allocating, and is cheap to revise later.
let mut tokens = Vec::with_capacity(src.len() / 8 + 8);
// The default sink discards trivia at compile time (zero overhead, ADR-0005);
// callers wanting the spans use `tokenize_with_trivia`.
let mut state = scan::LexState::default();
while let Some(token) = scan::next_token(&mut cursor, features, &mut NoTrivia, &mut state)? {
tokens.push(token);
}
Ok(tokens)
}
/// Tokenize `src` eagerly, also recovering an offset-sorted [`TriviaIndex`] of the
/// skipped comments and whitespace.
///
/// The tokenizer-output recovery path (the parse-root path is
/// [`ParseConfig::capture_trivia`](crate::ParseConfig::capture_trivia)): the returned tokens are
/// *identical* to [`tokenize_with`]'s — trivia stays out of the token stream — while
/// the [`TriviaIndex`] carries each `--`/`#`/`/* */` comment and whitespace run as a
/// queryable span. This pays the capture cost [`tokenize_with`] avoids, so reach for
/// it only when a formatter/linter/diagnostic actually needs the trivia.
///
/// # Errors
///
/// Returns the same lexical errors as [`tokenize`].
pub fn tokenize_with_trivia(
src: &str,
features: &FeatureSet,
) -> Result<(Vec<Token>, TriviaIndex), LexError> {
if u32::try_from(src.len()).is_err() {
return Err(LexError::new(LexErrorKind::SourceTooLong, Span::new(0, 0)));
}
let mut cursor = Cursor::new(src);
let mut tokens = Vec::with_capacity(src.len() / 8 + 8);
let mut trivia: Vec<TriviaRange> = Vec::new();
let mut state = scan::LexState::default();
while let Some(token) = scan::next_token(&mut cursor, features, &mut trivia, &mut state)? {
tokens.push(token);
}
Ok((tokens, TriviaIndex::new(trivia)))
}
/// A parser-facing token cursor that grows on demand.
///
/// The public [`TokenCursor`] below remains the zero-allocation cursor over an
/// already-tokenized slice. `BufferedTokenCursor` is the streaming parser seam:
/// it drives the byte cursor only as grammar lookahead asks for tokens, while
/// retaining random access within the current statement so checkpoints can
/// rewind without rescanning.
#[derive(Debug)]
pub(crate) struct BufferedTokenCursor<'s> {
source: BufferedTokenSource<'s>,
features: Option<FeatureSet>,
buffer: Vec<Token>,
buffer_start: usize,
pos: usize,
/// Out-of-band trivia capture, opt-in: `Some` records each skipped
/// comment/whitespace run, `None` (the default) discards them through the
/// compile-time-dead [`NoTrivia`] sink. Unlike `buffer`, this is *not* drained
/// between statements ([`discard_consumed`](Self::discard_consumed)) — a parse
/// root keeps the whole source's trivia — which is why trivia capture is the
/// collecting trivia-capture path, not the bounded streaming iterator.
trivia: Option<Vec<TriviaRange>>,
}
#[derive(Debug)]
enum BufferedTokenSource<'s> {
Slice(&'s [Token]),
Stream {
cursor: Cursor<'s>,
reached_eof: bool,
/// First lexical fault encountered while filling the lazy buffer. Scanning an
/// unterminated construct advances the byte cursor to EOF before returning the
/// error, so the fault must be retained: speculative lookahead may rewind the
/// token position and encounter this boundary again.
lex_error: Option<LexError>,
/// Cross-token lexical state (an open versioned-comment region) — owned
/// here so it survives across `scan_loop` refills exactly as the cursor
/// position does.
lex: scan::LexState,
},
}
impl<'s> BufferedTokenCursor<'s> {
/// Create a cursor over an already-tokenized slice.
pub(crate) fn from_tokens(tokens: &'s [Token]) -> Self {
Self {
source: BufferedTokenSource::Slice(tokens),
features: None,
buffer: Vec::new(),
buffer_start: 0,
pos: 0,
trivia: None,
}
}
/// Create a streaming cursor over `src` using `features` for lexical data.
///
/// No token is scanned by construction; the first call to [`peek`](Self::peek),
/// [`peek_nth`](Self::peek_nth), or [`advance`](Self::advance) grows the
/// buffer just far enough to satisfy that operation. Trivia is discarded; use
/// [`streaming_with_trivia`](Self::streaming_with_trivia) to capture it.
pub(crate) fn streaming(src: &'s str, features: &FeatureSet) -> Result<Self, LexError> {
Self::streaming_inner(src, features, None)
}
/// Like [`streaming`](Self::streaming), but records skipped comment/whitespace
/// runs into an offset-sorted side-table, drained with [`take_trivia`](Self::take_trivia).
pub(crate) fn streaming_with_trivia(
src: &'s str,
features: &FeatureSet,
) -> Result<Self, LexError> {
Self::streaming_inner(src, features, Some(Vec::new()))
}
fn streaming_inner(
src: &'s str,
features: &FeatureSet,
trivia: Option<Vec<TriviaRange>>,
) -> Result<Self, LexError> {
if u32::try_from(src.len()).is_err() {
return Err(LexError::new(LexErrorKind::SourceTooLong, Span::new(0, 0)));
}
Ok(Self {
source: BufferedTokenSource::Stream {
cursor: Cursor::new(src),
reached_eof: false,
lex_error: None,
lex: scan::LexState::default(),
},
features: Some(features.clone()),
buffer: Vec::new(),
buffer_start: 0,
pos: 0,
trivia,
})
}
/// Take the captured trivia, leaving capture disabled.
///
/// Yields an empty index when capture was off (the default). The scanner records
/// in source order, so the ranges are already sorted by offset.
pub(crate) fn take_trivia(&mut self) -> TriviaIndex {
TriviaIndex::new(self.trivia.take().unwrap_or_default())
}
/// The current absolute token index.
pub(crate) fn pos(&self) -> usize {
self.pos
}
/// The token at the cursor without advancing, or `None` at end of input.
pub(crate) fn peek(&mut self) -> Result<Option<Token>, LexError> {
self.peek_nth(0)
}
/// The token `n` positions ahead without advancing.
pub(crate) fn peek_nth(&mut self, n: usize) -> Result<Option<Token>, LexError> {
let target = self.pos.saturating_add(n);
match &self.source {
BufferedTokenSource::Slice(tokens) => Ok(tokens.get(target).copied()),
BufferedTokenSource::Stream { .. } => {
// Fast path: a token already buffered needs no lexing, so skip the
// `fill_through` feature borrow + scan-loop condition entirely. This
// is the common case — grammar lookahead re-peeks the current token
// several times per position (predicate/postfix/infix checks), and
// each of those hits an already-buffered token.
//
// The cursor never rewinds below the retained buffer (`pos >=
// buffer_start`, asserted in `seek`) and `n >= 0`, so `target >=
// buffer_start`: `wrapping_sub` is exact and the single `get` bound
// check both confirms the token is buffered and indexes it. This
// collapses the old explicit-bound + `checked_sub` + `get` (three
// branches) into one — the hot path is now a subtract and a bounds
// check. A target before `buffer_start` (a broken invariant) wraps to
// a huge offset, `get` misses, and the slow path returns `None`,
// matching the previous `checked_sub` underflow behaviour.
let offset = target.wrapping_sub(self.buffer_start);
if let Some(&token) = self.buffer.get(offset) {
return Ok(Some(token));
}
self.fill_through(target)?;
Ok(self.buffered_token(target))
}
}
}
/// Return the token at the cursor and advance past it, or `None` at end.
pub(crate) fn advance(&mut self) -> Result<Option<Token>, LexError> {
let token = self.peek()?;
if token.is_some() {
self.pos += 1;
}
Ok(token)
}
/// True once every token has been consumed.
pub(crate) fn is_eof(&mut self) -> Result<bool, LexError> {
Ok(self.peek()?.is_none())
}
/// Jump to an absolute token index for restoring a speculation checkpoint.
pub(crate) fn seek(&mut self, pos: usize) {
debug_assert!(
pos >= self.buffer_start,
"cannot rewind before the retained token buffer",
);
self.pos = pos;
}
/// The token immediately before the cursor, if it is still retained.
pub(crate) fn preceding(&self) -> Option<Token> {
let previous = self.pos.checked_sub(1)?;
match &self.source {
BufferedTokenSource::Slice(tokens) => tokens.get(previous).copied(),
BufferedTokenSource::Stream { .. } => self.buffered_token(previous),
}
}
/// Drop consumed streaming tokens once the parser is between statements.
///
/// Checkpoints never survive across statement boundaries, so consumed tokens
/// can be discarded there without breaking rewind. Keeping a small retained
/// capacity avoids carrying a giant statement's token allocation through the
/// rest of a long script.
pub(crate) fn discard_consumed(&mut self) {
if !matches!(&self.source, BufferedTokenSource::Stream { .. }) {
return;
}
let consumed = self
.pos
.saturating_sub(self.buffer_start)
.min(self.buffer.len());
if consumed == 0 {
return;
}
self.buffer.drain(..consumed);
self.buffer_start += consumed;
if self.buffer.is_empty() && self.buffer.capacity() > STREAM_BUFFER_RETAINED_CAPACITY {
self.buffer.shrink_to(STREAM_BUFFER_MAX_INITIAL_CAPACITY);
}
}
#[cfg(test)]
pub(crate) fn buffered_len(&self) -> usize {
self.buffer.len()
}
fn fill_through(&mut self, target: usize) -> Result<(), LexError> {
// Move the trivia buffer out so the scan loop can borrow `self` for the
// cursor/buffer while still recording into the (now-local) sink — `&mut
// self.trivia` and `&mut self` could not coexist otherwise. Picking the sink
// here, once, also keeps the per-token scan loop at one statically-known
// monomorphization: the `None` arm hands `scan_loop` the compile-time-dead
// `NoTrivia` sink, so the off path is the pre-trivia loop exactly (no branch,
// no `Vec`, no push); the `Some` arm records into the side-table (ADR-0005).
// Moving an `Option<Vec>` is three words and never touches the elements.
let mut trivia = self.trivia.take();
let result = match &mut trivia {
Some(sink) => self.scan_loop(target, sink),
None => self.scan_loop(target, &mut NoTrivia),
};
self.trivia = trivia;
result
}
/// The per-token scan loop, generic over the trivia sink. Fills the buffer up to
/// `target` (or end of input), recording any skipped trivia into `trivia`.
fn scan_loop<S: TriviaSink>(&mut self, target: usize, trivia: &mut S) -> Result<(), LexError> {
let BufferedTokenSource::Stream {
cursor,
reached_eof,
lex_error,
lex,
} = &mut self.source
else {
return Ok(());
};
let features = self
.features
.as_ref()
.expect("streaming token cursors carry dialect features");
if let Some(error) = *lex_error {
return Err(error);
}
while !*reached_eof && self.buffer_start + self.buffer.len() <= target {
match scan::next_token(cursor, features, trivia, lex) {
Ok(Some(token)) => {
if self.buffer.capacity() == 0 {
self.buffer
.reserve_exact(stream_buffer_initial_capacity(cursor.src()));
}
self.buffer.push(token);
}
Ok(None) => *reached_eof = true,
Err(error) => {
*lex_error = Some(error);
return Err(error);
}
}
}
Ok(())
}
fn buffered_token(&self, index: usize) -> Option<Token> {
let offset = index.checked_sub(self.buffer_start)?;
self.buffer.get(offset).copied()
}
}
fn stream_buffer_initial_capacity(src: &str) -> usize {
(src.len() / 4 + 8).clamp(
STREAM_BUFFER_MIN_INITIAL_CAPACITY,
STREAM_BUFFER_MAX_INITIAL_CAPACITY,
)
}
/// A random-access cursor over a tokenized buffer.
///
/// This is the eager slice cursor: it can [`peek`](Self::peek) and
/// [`advance`](Self::advance) like a stream, but also look ahead with
/// [`peek_nth`](Self::peek_nth) and rewind with [`seek`](Self::seek). It borrows
/// the buffer and copies the `Copy` tokens out, so it allocates nothing. The
/// parser's production path uses the internal lazy cursor above.
#[derive(Clone, Copy, Debug)]
pub struct TokenCursor<'t> {
tokens: &'t [Token],
pos: usize,
}
impl<'t> TokenCursor<'t> {
/// Create a cursor at the first token of `tokens`.
pub fn new(tokens: &'t [Token]) -> Self {
Self { tokens, pos: 0 }
}
/// The current token index.
pub fn pos(&self) -> usize {
self.pos
}
/// Number of tokens not yet consumed.
pub fn remaining(&self) -> usize {
self.tokens.len() - self.pos.min(self.tokens.len())
}
/// True once every token has been consumed.
pub fn is_eof(&self) -> bool {
self.pos >= self.tokens.len()
}
/// The token at the cursor without advancing, or `None` at end of buffer.
pub fn peek(&self) -> Option<Token> {
self.tokens.get(self.pos).copied()
}
/// The token `n` positions ahead without advancing.
pub fn peek_nth(&self, n: usize) -> Option<Token> {
self.pos
.checked_add(n)
.and_then(|index| self.tokens.get(index))
.copied()
}
/// Return the token at the cursor and advance past it, or `None` at end.
pub fn advance(&mut self) -> Option<Token> {
let token = self.peek();
if token.is_some() {
self.pos += 1;
}
token
}
/// Jump to an absolute token index (for restoring a speculation checkpoint).
///
/// Indices past the end are allowed and simply leave the cursor at EOF.
pub fn seek(&mut self, pos: usize) {
self.pos = pos;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ast::Keyword;
use crate::ast::dialect::lex_class::{CLASS_IDENTIFIER_CONTINUE, CLASS_IDENTIFIER_START};
use crate::ast::dialect::{
ByteClasses, CommentSyntax, ExpressionSyntax, FeatureDelta, FeatureSet, IdentifierQuote,
IdentifierSyntax, NumericLiteralSyntax, OperatorSyntax, ParameterSyntax, QueryTailSyntax,
SessionVariableSyntax, StringLiteralSyntax,
};
/// Vertical tab (`0x0b`) is per-dialect whitespace data with three distinct,
/// engine-measured shapes (`whitespace-vertical-tab-sqlite-duckdb`), so one
/// parametric matrix pins the class across every preset. Each cell is whether
/// `tokenize_with` accepts (`Ok`) — the level at which the `0x0b` accept/reject is
/// decided — under that preset. Oracle-verified: PostgreSQL/MySQL fold it as
/// ordinary whitespace; SQLite folds it only as a run *continuation* (rides an open
/// whitespace run, cannot start one); DuckDB folds it only as statement *trim*
/// (leading/trailing of a `;`-segment, a hard error interior to a statement);
/// ANSI keeps it strict everywhere.
#[test]
fn vertical_tab_whitespace_class_is_per_dialect() {
struct Case {
input: &'static str,
ansi: bool,
postgres: bool,
mysql: bool,
sqlite: bool,
duckdb: bool,
}
// Columns: ANSI, PostgreSQL, MySQL, SQLite, DuckDB (accept = tokenizes clean).
const CASES: &[Case] = &[
// Lone `\v`: PG/MySQL empty; SQLite can't start a run (reject); DuckDB trims
// it as an empty statement (accept).
Case {
input: "\x0b",
ansi: false,
postgres: true,
mysql: true,
sqlite: false,
duckdb: true,
},
// Leading `\v` before a statement: SQLite still can't start a run; DuckDB
// trims the leading edge.
Case {
input: "\x0bSELECT 1",
ansi: false,
postgres: true,
mysql: true,
sqlite: false,
duckdb: true,
},
// Trailing `\v` after a statement: DuckDB trims the trailing edge; SQLite
// rejects (the `\v` follows `1`, a non-whitespace byte).
Case {
input: "SELECT 1\x0b",
ansi: false,
postgres: true,
mysql: true,
sqlite: false,
duckdb: true,
},
// DuckDB treats comments as content for boundary trim: the middle `\v`
// lies between two comments and is therefore not at a segment edge.
Case {
input: ";\x0b/**/\x0b/***/\n;\x0b",
ansi: false,
postgres: true,
mysql: true,
sqlite: false,
duckdb: false,
},
// Trailing `\v` after a `;`: a leading edge of the (empty) next statement.
Case {
input: "SELECT 1;\x0b",
ansi: false,
postgres: true,
mysql: true,
sqlite: false,
duckdb: true,
},
// `\v` between two content tokens: interior — DuckDB rejects; SQLite rejects
// (the `\v` would start a run after `SELECT`); PG/MySQL fold it.
Case {
input: "SELECT\x0b1",
ansi: false,
postgres: true,
mysql: true,
sqlite: false,
duckdb: false,
},
// `\v` riding an open whitespace run (space then `\v`): the SQLite-distinctive
// accept — a run continuation. DuckDB still rejects (interior, adjacency to a
// real space does not rescue it).
Case {
input: "SELECT \x0b1",
ansi: false,
postgres: true,
mysql: true,
sqlite: true,
duckdb: false,
},
// `\v` then a space, interior: SQLite rejects (the `\v` starts the run), and so
// does DuckDB (interior boundary byte).
Case {
input: "SELECT\x0b 1",
ansi: false,
postgres: true,
mysql: true,
sqlite: false,
duckdb: false,
},
// Whole-input blank run reducing to empty: SQLite rides the run from the space;
// DuckDB trims. Both accept.
Case {
input: " \x0b ",
ansi: false,
postgres: true,
mysql: true,
sqlite: true,
duckdb: true,
},
// Interior `\v` flanked by real spaces: DuckDB rejects (boundary byte with
// content on both sides); SQLite accepts (rides the run).
Case {
input: "SELECT 1 \x0b FROM t",
ansi: false,
postgres: true,
mysql: true,
sqlite: true,
duckdb: false,
},
];
for case in CASES {
let expect = [
(FeatureSet::ANSI, case.ansi, "ANSI"),
(FeatureSet::POSTGRES, case.postgres, "PostgreSQL"),
(FeatureSet::MYSQL, case.mysql, "MySQL"),
(FeatureSet::SQLITE, case.sqlite, "SQLite"),
(FeatureSet::DUCKDB, case.duckdb, "DuckDB"),
];
for (features, accepts, name) in expect {
assert_eq!(
tokenize_with(case.input, &features).is_ok(),
accepts,
"{name} tokenization of {:?} should {}",
case.input,
if accepts { "accept" } else { "reject" },
);
}
}
}
#[test]
fn token_is_a_twelve_byte_copy_value() {
// ADR-0005: a `Token` is a borrow-free {kind, span} pair; pinning it at 12
// bytes keeps the token stream cache-dense. `size_of::<Token>()` was
// unpinned anywhere in the nextest suite, so this is its governed pin.
assert_eq!(std::mem::size_of::<Token>(), 12);
}
/// Slice a token's span back out of the source.
fn text<'s>(src: &'s str, token: &Token) -> &'s str {
&src[token.span.start() as usize..token.span.end() as usize]
}
/// `(kind, sliced text)` for every token, the spine of most assertions.
fn lexed(src: &str) -> Vec<(TokenKind, &str)> {
tokenize(src)
.expect("expected a clean tokenization")
.iter()
.map(|token| (token.kind, text(src, token)))
.collect()
}
#[test]
fn tokenizes_a_representative_select() {
use Operator::{NotEq, Plus};
use Punctuation::Comma;
// `d`, `b`, `u`, `x` are not keywords in the full inventory, so they lex as
// `Word`; `a`/`t` would now lex as the SQL:2016 single-letter keywords.
let src = "SELECT d, b + 1 FROM u WHERE x <> 'it''s'";
let tokens = tokenize(src).expect("clean tokenization");
let expected = [
(TokenKind::Keyword(Keyword::Select), "SELECT"),
(TokenKind::Word, "d"),
(TokenKind::Punctuation(Comma), ","),
(TokenKind::Word, "b"),
(TokenKind::Operator(Plus), "+"),
(TokenKind::Number, "1"),
(TokenKind::Keyword(Keyword::From), "FROM"),
(TokenKind::Word, "u"),
(TokenKind::Keyword(Keyword::Where), "WHERE"),
(TokenKind::Word, "x"),
(TokenKind::Operator(NotEq), "<>"),
(TokenKind::String, "'it''s'"),
];
assert_eq!(tokens.len(), expected.len());
for (token, (kind, slice)) in tokens.iter().zip(expected) {
assert_eq!(token.kind, kind);
assert_eq!(text(src, token), slice);
}
// Spell out a couple of spans numerically to pin down offsets directly.
assert_eq!(tokens[0].span, Span::new(0, 6)); // SELECT
assert_eq!(tokens.last().expect("non-empty").span, Span::new(34, 41)); // 'it''s'
}
#[test]
fn every_span_slices_back_to_exact_source() {
// Mixes words, numbers, operators, punctuation, both quote forms, a
// dollar-quote, and trivia between every pair of tokens.
let src = "select c1 ,\t3.14 -- note\n+ \"Odd\"\nfrom /* x */ $$body$$ ;";
let tokens =
tokenize_with(src, &FeatureSet::POSTGRES).expect("clean PostgreSQL tokenization");
assert!(!tokens.is_empty());
for token in &tokens {
let slice = text(src, token);
assert_eq!(slice.len() as u32, token.span.len());
assert!(!slice.is_empty(), "real tokens are never empty");
}
// Trivia is out-of-band: the gap before `from` is the comment + newline.
let from = tokens
.iter()
.find(|t| text(src, t) == "from")
.expect("from present");
assert_eq!(from.kind, TokenKind::Keyword(Keyword::From));
}
#[test]
fn keyword_lookup_is_case_insensitive_but_ascii_only() {
assert_eq!(
lexed("SeLeCt asc café FROM"),
[
(TokenKind::Keyword(Keyword::Select), "SeLeCt"),
(TokenKind::Keyword(Keyword::Asc), "asc"),
(TokenKind::Word, "café"),
(TokenKind::Keyword(Keyword::From), "FROM"),
],
);
}
#[test]
fn nested_block_comment_is_one_skipped_unit() {
let tokens = tokenize("/* a /* b */ c */").expect("balanced nesting");
assert!(tokens.is_empty(), "a fully nested comment yields no tokens");
// And it does not swallow what follows it.
assert_eq!(
lexed("/* a /* b */ c */x"),
[(TokenKind::Word, "x")],
"the token after the comment survives"
);
}
#[test]
fn line_comment_runs_to_end_of_line_only() {
assert_eq!(
lexed("d -- comment to EOL\nb"),
[(TokenKind::Word, "d"), (TokenKind::Word, "b")],
);
// A line comment at EOF with no trailing newline is fine.
assert!(tokenize("d --tail").expect("ok").len() == 1);
}
/// A MySQL-like preset that recognises `#` line comments.
const HASH_COMMENT_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.comment_syntax(CommentSyntax {
line_comment_hash: true,
..CommentSyntax::ANSI
}));
#[test]
fn hash_line_comment_is_skipped_only_when_enabled() {
// Enabled: `#` runs to end of line, exactly like `--`.
let tokens =
tokenize_with("d # comment to EOL\nb", &HASH_COMMENT_FEATURES).expect("hash comment");
assert_eq!(
tokens.iter().map(|t| t.kind).collect::<Vec<_>>(),
[TokenKind::Word, TokenKind::Word],
);
// A `#` comment at EOF with no trailing newline is fine.
assert_eq!(
tokenize_with("d #tail", &HASH_COMMENT_FEATURES)
.expect("ok")
.len(),
1,
);
// Disabled (ANSI): `#` is in no lexical class, so it is a stray byte.
let err = tokenize("d # b").expect_err("# is stray under ANSI");
assert_eq!(err.kind, LexErrorKind::StrayByte);
assert_eq!(err.span, Span::new(2, 3));
}
#[test]
fn hash_comment_and_hash_identifier_are_mutually_exclusive() {
// The documented either/or: a dialect uses `#` for comments OR for
// identifiers, never both. The comment branch is checked before byte-class
// identifier dispatch, so with `line_comment_hash` on, `#temp` to EOL is a
// comment — only `x` on the next line survives.
let src = "#temp\nx";
let comment = tokenize_with(src, &HASH_COMMENT_FEATURES).expect("hash comment wins");
assert_eq!(comment.len(), 1);
assert_eq!(comment[0].kind, TokenKind::Word);
assert_eq!(text(src, &comment[0]), "x");
// The other side of the either/or: a T-SQL-like preset marks `#` an
// identifier byte (and leaves hash-comments off), so `#temp` is one word.
const TEMP_IDENT_FEATURES: FeatureSet = FeatureSet::ANSI.with(
FeatureDelta::EMPTY.byte_classes(
ByteClasses::STANDARD
.with_class(b'#', CLASS_IDENTIFIER_START | CLASS_IDENTIFIER_CONTINUE),
),
);
let ident = tokenize_with("#temp", &TEMP_IDENT_FEATURES).expect("hash identifier");
assert_eq!(ident.len(), 1);
assert_eq!(ident[0].kind, TokenKind::Word);
assert_eq!(text("#temp", &ident[0]), "#temp");
}
/// DuckDB's `#n` positional column reference, gated by
/// `ExpressionSyntax::positional_column`.
const POSITIONAL_COLUMN_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.expression_syntax(ExpressionSyntax {
positional_column: true,
..ExpressionSyntax::ANSI
}));
#[test]
fn hash_positional_column_lexes_only_when_enabled() {
// Enabled (DuckDB): `#` + digits is one token spanning the sigil and the digits.
let tokens = tokenize_with("#12", &POSITIONAL_COLUMN_FEATURES).expect("positional column");
assert_eq!(tokens.len(), 1);
assert_eq!(tokens[0].kind, TokenKind::PositionalColumn);
assert_eq!(text("#12", &tokens[0]), "#12");
// In an `ORDER BY` tail each `#n` lexes as its own token beside the comma.
let src = "ORDER BY #2 , #1";
let kinds: Vec<_> = tokenize_with(src, &POSITIONAL_COLUMN_FEATURES)
.expect("clean tokenization")
.iter()
.map(|t| t.kind)
.collect();
assert_eq!(
kinds
.iter()
.filter(|k| **k == TokenKind::PositionalColumn)
.count(),
2,
);
// A `#` with no following digit stays a stray byte even with the feature on,
// matching DuckDB's own "syntax error at or near #".
let bare = tokenize_with("#a", &POSITIONAL_COLUMN_FEATURES).expect_err("bare # is stray");
assert_eq!(bare.kind, LexErrorKind::StrayByte);
assert_eq!(bare.span, Span::new(0, 1));
// Disabled (ANSI): `#` is in no lexical class, so `#1` is a stray byte.
let err = tokenize("#1").expect_err("# is stray under ANSI");
assert_eq!(err.kind, LexErrorKind::StrayByte);
assert_eq!(err.span, Span::new(0, 1));
}
/// The MySQL comment surface — `/*!…*/` conditional inclusion gated at
/// `CommentSyntax::MYSQL_8_VERSION_BOUND` (80499) and non-nesting block
/// comments — applied to the ANSI base so these tests pin the comment shape
/// in isolation. Every expectation below was verified against a live mysql:8
/// (8.4.10) server.
const VERSIONED_COMMENT_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.comment_syntax(CommentSyntax::MYSQL));
fn lexed_versioned(src: &str) -> Vec<(TokenKind, &str)> {
tokenize_with(src, &VERSIONED_COMMENT_FEATURES)
.expect("clean tokenization")
.iter()
.map(|t| (t.kind, text(src, t)))
.collect()
}
#[test]
fn versioned_comment_body_lexes_as_live_tokens_only_when_enabled() {
use crate::ast::Keyword;
// Enabled: the wrapper is trivia, the body is live input — the engine
// *executes* `/*!…*/` content, so `SELECT /*!40101 1 */` is `SELECT 1`.
assert_eq!(
lexed_versioned("SELECT /*!40101 1 */"),
[
(TokenKind::Keyword(Keyword::Select), "SELECT"),
(TokenKind::Number, "1")
],
);
// Disabled (every non-MySQL dialect): the whole construct stays an
// ordinary skipped block comment — the pre-existing behaviour.
assert_eq!(
lexed("SELECT /*!40101 1 */"),
[(TokenKind::Keyword(Keyword::Select), "SELECT")],
);
}
#[test]
fn versioned_comment_version_gate_and_digit_rule_follow_the_engine() {
// (input, live tokens after SELECT) — the engine's digit rule: exactly
// five or six abutting digits form the version (include iff <= 80499);
// 0-4 digits are not a version (digits stay body tokens, unconditional
// include); from >=7 digits the first five are the version.
let cases: &[(&str, &[&str])] = &[
("SELECT /*! 1 */", &["1"]), // no version: include
("SELECT /*!80499 1 */", &["1"]), // at the bound: include
("SELECT /*!80500 1 */", &[]), // above the bound: skip
("SELECT /*!99999 1 */", &[]), // future 5-digit: skip
("SELECT /*!1234 9 */", &["1234", "9"]), // 4 digits: not a version
("SELECT /*!123456 9 */", &[]), // 6-digit 123456 > bound: skip
("SELECT /*!012345 9 */", &["9"]), // 6-digit 012345 = 12345: include
("SELECT /*!1234567 9 */", &["67", "9"]), // first five 12345: include, 67 leaks
("SELECT /*! 40101 */", &["40101"]), // space breaks abutment: no version
];
for (src, expected) in cases {
let body: Vec<&str> = lexed_versioned(src)[1..]
.iter()
.map(|(_, text)| *text)
.collect();
assert_eq!(&body, expected, "digit-rule divergence for {src:?}");
}
}
#[test]
fn versioned_region_flag_semantics_match_the_engine() {
use crate::ast::Keyword;
// An inner plain comment consumes its own terminator (non-nesting), so
// the region continues past it.
assert_eq!(
lexed_versioned("SELECT /*!40101 1 /* c */ + 2 */"),
[
(TokenKind::Keyword(Keyword::Select), "SELECT"),
(TokenKind::Number, "1"),
(TokenKind::Operator(Operator::Plus), "+"),
(TokenKind::Number, "2"),
],
);
// A passing inner `/*!NNNNN` is a no-op marker — the state is a flag,
// not a depth — so the first region-level `*/` closes the (single)
// region and the second leaks as the `*` `/` operator tokens.
assert_eq!(
lexed_versioned("SELECT /*!40101 1 /*!40101 + 10 */ */ x"),
[
(TokenKind::Keyword(Keyword::Select), "SELECT"),
(TokenKind::Number, "1"),
(TokenKind::Operator(Operator::Plus), "+"),
(TokenKind::Number, "10"),
(TokenKind::Operator(Operator::Star), "*"),
(TokenKind::Operator(Operator::Slash), "/"),
(TokenKind::Word, "x"),
],
);
// A failing inner marker discards only up to the next `*/`; the outer
// region stays open (engine: `SELECT /*!40101 1 /*!99999 + 10 */ + 2 */`
// evaluates to 3 on mysql:8).
assert_eq!(
lexed_versioned("SELECT /*!40101 1 /*!99999 + 10 */ + 2 */"),
[
(TokenKind::Keyword(Keyword::Select), "SELECT"),
(TokenKind::Number, "1"),
(TokenKind::Operator(Operator::Plus), "+"),
(TokenKind::Number, "2"),
],
);
}
#[test]
fn versioned_region_string_protection_and_raw_discard() {
use crate::ast::Keyword;
// Included body: a `*/` inside a string literal is string content — the
// close is recognised only where a token would start, which is exactly
// the engine's protection.
assert_eq!(
lexed_versioned("SELECT /*!40101 '*/' */"),
[
(TokenKind::Keyword(Keyword::Select), "SELECT"),
(TokenKind::String, "'*/'"),
],
);
// Discarded body: raw bytes, NOT string-aware — an unbalanced quote is
// harmless and the first `*/` closes (engine-verified).
assert_eq!(
lexed_versioned("SELECT /*!99999 ' */ 1"),
[
(TokenKind::Keyword(Keyword::Select), "SELECT"),
(TokenKind::Number, "1"),
],
);
}
#[test]
fn versioned_region_unterminated_is_a_lex_error() {
// Both the included and the discarded form reject at EOF, like an
// unterminated block comment, with the span anchored at the opener.
for src in ["SELECT /*!40101 1", "SELECT /*!99999 x"] {
let err = tokenize_with(src, &VERSIONED_COMMENT_FEATURES)
.expect_err("an unterminated region must not lex");
assert_eq!(err.kind, LexErrorKind::UnterminatedBlockComment, "{src:?}");
assert_eq!(err.span, Span::new(7, src.len() as u32), "{src:?}");
}
}
#[test]
fn block_comment_nesting_is_dialect_data() {
use crate::ast::Keyword;
// MySQL: the first `*/` closes a block comment — `/* a /* b */` is a
// complete comment and the `1` is live (the engine parses this).
assert_eq!(
lexed_versioned("SELECT /* a /* b */ 1"),
[
(TokenKind::Keyword(Keyword::Select), "SELECT"),
(TokenKind::Number, "1")
],
);
// The balanced-nesting spelling leaks its tail under MySQL instead.
assert_eq!(
lexed_versioned("SELECT /* a /* b */ zz */ 1")
.iter()
.map(|(kind, _)| *kind)
.collect::<Vec<_>>(),
[
TokenKind::Keyword(Keyword::Select),
TokenKind::Word,
TokenKind::Operator(Operator::Star),
TokenKind::Operator(Operator::Slash),
TokenKind::Number,
],
);
// ANSI keeps the permissive nesting baseline: the same prefix is an
// unterminated (still-nested) comment.
let err = tokenize("SELECT /* a /* b */ 1").expect_err("ANSI nests");
assert_eq!(err.kind, LexErrorKind::UnterminatedBlockComment);
}
#[test]
fn versioned_comment_markers_are_recorded_as_trivia() {
// An included region records its opener (with the version digits) and its
// closer as separate BlockComment runs — the version number is
// offset-recoverable for tooling even though it is not a token.
let src = "SELECT /*!40101 1 */ x";
let (tokens, trivia) =
tokenize_with_trivia(src, &VERSIONED_COMMENT_FEATURES).expect("clean tokenization");
assert_eq!(tokens.len(), 3);
let comments: Vec<&str> = trivia
.all()
.iter()
.filter(|run| run.kind() == TriviaKind::BlockComment)
.map(|run| &src[run.span().start() as usize..run.span().end() as usize])
.collect();
assert_eq!(comments, ["/*!40101", "*/"]);
// A discarded region is one whole BlockComment run, like the comment the
// engine treats it as.
let src = "SELECT /*!99999 1 */ x";
let (tokens, trivia) =
tokenize_with_trivia(src, &VERSIONED_COMMENT_FEATURES).expect("clean tokenization");
assert_eq!(tokens.len(), 2);
let comments: Vec<&str> = trivia
.all()
.iter()
.filter(|run| run.kind() == TriviaKind::BlockComment)
.map(|run| &src[run.span().start() as usize..run.span().end() as usize])
.collect();
assert_eq!(comments, ["/*!99999 1 */"]);
}
#[test]
fn double_ampersand_lexes_as_one_operator_token() {
// The `&&` lexeme always tokenizes (its *meaning* is dialect data resolved
// in the parser); a lone `&` stays `Amp`.
assert_eq!(
lexed("d && b"),
[
(TokenKind::Word, "d"),
(TokenKind::Operator(Operator::AmpAmp), "&&"),
(TokenKind::Word, "b"),
],
);
assert_eq!(
lexed("d & b"),
[
(TokenKind::Word, "d"),
(TokenKind::Operator(Operator::Amp), "&"),
(TokenKind::Word, "b"),
],
);
}
#[test]
fn postgres_at_family_operators_lex_as_single_tokens() {
use Operator::{AtGt, LtAt, MinusGt, MinusGtGt};
// Under PostgreSQL each containment / JSON lexeme is one operator token.
let cases: &[(&str, Operator)] = &[
("@>", AtGt),
("<@", LtAt),
("->", MinusGt),
("->>", MinusGtGt),
];
for (src, op) in cases {
let tokens = tokenize_with(src, &FeatureSet::POSTGRES)
.unwrap_or_else(|err| panic!("{src:?} lexes: {err:?}"));
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::Operator(*op));
assert_eq!(text(src, &tokens[0]), *src);
}
// A bare `@` (not opening `@>`) is the prefix absolute-value operator under the
// general operator surface (`custom_operators`): `@ x` lexes as one `Custom` operator
// token spanning just the `@`, then the word `x`.
let tokens = tokenize_with("@ x", &FeatureSet::POSTGRES).expect("bare `@` is the operator");
assert_eq!(tokens[0].kind, TokenKind::Operator(Operator::Custom));
assert_eq!(text("@ x", &tokens[0]), "@");
// Contiguous munch even without surrounding spaces: `d@>b` and `d->>b` (`d`/`b`
// are non-keyword words; a single letter like `a` is a SQL:2016 keyword here).
assert_eq!(
tokenize_with("d@>b", &FeatureSet::POSTGRES)
.expect("contiguous lexes")
.iter()
.map(|t| t.kind)
.collect::<Vec<_>>(),
[TokenKind::Word, TokenKind::Operator(AtGt), TokenKind::Word],
);
assert_eq!(
tokenize_with("d->>b", &FeatureSet::POSTGRES)
.expect("contiguous lexes")
.iter()
.map(|t| t.kind)
.collect::<Vec<_>>(),
[
TokenKind::Word,
TokenKind::Operator(MinusGtGt),
TokenKind::Word
],
);
}
#[test]
fn pipe_arrow_lexes_only_under_pipe_syntax() {
// The BigQuery/ZetaSQL `|>` separator is a feature-gated maximal munch. Build an
// ANSI dialect with only `pipe_syntax` flipped on so the test isolates this gate.
let pipe = FeatureSet::ANSI.with(FeatureDelta::EMPTY.query_tail_syntax(QueryTailSyntax {
pipe_syntax: true,
..QueryTailSyntax::ANSI
}));
// Gate on: `|>` is one PipeArrow token, contiguous-munch even without spaces.
// `d`/`b` are non-keyword words (a lone `a` is a SQL:2016 keyword here).
let tokens = tokenize_with("d |> b", &pipe).expect("`|>` lexes under pipe syntax");
let kinds: Vec<_> = tokens.iter().map(|t| t.kind).collect();
assert_eq!(
kinds,
[
TokenKind::Word,
TokenKind::Operator(Operator::PipeArrow),
TokenKind::Word
],
);
assert_eq!(
tokenize_with("d|>b", &pipe)
.expect("contiguous `|>` lexes")
.iter()
.map(|t| t.kind)
.collect::<Vec<_>>(),
[
TokenKind::Word,
TokenKind::Operator(Operator::PipeArrow),
TokenKind::Word
],
);
// `||` is untouched by the new arm — still one Concat token under the same dialect.
assert_eq!(
tokenize_with("d || b", &pipe)
.expect("`||` still lexes")
.iter()
.map(|t| t.kind)
.collect::<Vec<_>>(),
[
TokenKind::Word,
TokenKind::Operator(Operator::Concat),
TokenKind::Word
],
);
// Gate off (ANSI): the bytes stay `|` then `>`, so no dialect's `|`/`||` lexing
// shifts. (Under POSTGRES the `|>` bytes are one `Custom` operator instead — the
// general operator surface's maximal munch — which is a different not-PipeArrow
// reading; ANSI, with neither `pipe_syntax` nor `custom_operators`, isolates the
// plain `|` `>` split this gate is about.)
assert_eq!(
tokenize_with("d |> b", &FeatureSet::ANSI)
.expect("`|` `>` lexes")
.iter()
.map(|t| t.kind)
.collect::<Vec<_>>(),
[
TokenKind::Word,
TokenKind::Operator(Operator::Pipe),
TokenKind::Operator(Operator::Gt),
TokenKind::Word
],
);
// Maximal munch is contiguous only: a space breaks `| >` even with the gate on.
assert_eq!(
tokenize_with("d | > b", &pipe)
.expect("spaced `| >` lexes")
.iter()
.map(|t| t.kind)
.collect::<Vec<_>>(),
[
TokenKind::Word,
TokenKind::Operator(Operator::Pipe),
TokenKind::Operator(Operator::Gt),
TokenKind::Word
],
);
}
#[test]
fn postgres_operator_munch_is_contiguous_only() {
// Maximal munch is over abutting bytes only: a space breaks `->`, so `d - > b`
// stays a binary minus then a greater-than rather than a JSON arrow.
assert_eq!(
tokenize_with("d - > b", &FeatureSet::POSTGRES)
.expect("spaced lexes")
.iter()
.map(|t| t.kind)
.collect::<Vec<_>>(),
[
TokenKind::Word,
TokenKind::Operator(Operator::Minus),
TokenKind::Operator(Operator::Gt),
TokenKind::Word,
],
);
}
#[test]
fn mysql_at_forms_are_unaffected_by_the_pg_operators() {
// The MySQL session-variable preset leaves the containment operators off, so
// `@name`/`@@name` keep lexing as single Variable tokens — the PostgreSQL `@`
// operator scanner does not regress the MySQL `@`/`@@` boundary the token-prefix
// work established.
for src in ["@x", "@@version", "@@global.time_zone"] {
let tokens = tokenize_with(src, &SESSION_VARIABLE_FEATURES)
.unwrap_or_else(|err| panic!("{src:?} lexes: {err:?}"));
assert_eq!(tokens.len(), 1, "{src:?} is one Variable token");
assert_eq!(tokens[0].kind, TokenKind::Variable);
assert_eq!(text(src, &tokens[0]), src);
}
}
#[test]
fn dollar_quoting_is_a_single_string_token() {
for src in ["$$x$$", "$tag$y$tag$", "$$$$", "$tag$$tag$"] {
let tokens = tokenize_with(src, &FeatureSet::POSTGRES).expect("balanced dollar-quote");
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::String);
assert_eq!(text(src, &tokens[0]), src, "the whole literal is the span");
}
// A differently-tagged inner delimiter is body text, not a close.
let src = "$a$ inner $b$ still body $a$";
let tokens = tokenize_with(src, &FeatureSet::POSTGRES).expect("balanced");
assert_eq!(tokens.len(), 1);
assert_eq!(text(src, &tokens[0]), src);
}
#[test]
fn dollar_quote_near_miss_candidates_do_not_skip_the_true_terminator() {
// Regression for the position-scan-to-`$` rewrite of `scan_dollar_quote`:
// each body below plants a `$` that opens a candidate close but fails to
// match the full delimiter. A resume that skips the whole failed-match
// width (`delim.len()` bytes) instead of just the one candidate byte
// could step past a true terminator hiding inside the near miss.
let cases: &[&str] = &[
// An embedded '$' with no tag following at all.
"$tag$a$b$tag$",
// Two sequential near-misses (`$ta$` shares a 3-byte prefix with
// `$tag$`, then its own close is also not `tag$`) ahead of the close.
"$tag$x$ta$y$tag$",
// The tightest overlap: `$ta` immediately precedes the real `$tag$`,
// so the byte that breaks the near-miss comparison is itself the
// opening '$' of the true terminator.
"$tag$x$ta$tag$",
// Empty tag (`$$`) with a lone embedded '$' that never doubles up.
"$$a$b$$",
];
for src in cases {
let tokens = tokenize_with(src, &FeatureSet::POSTGRES)
.unwrap_or_else(|err| panic!("{src:?} should lex cleanly: {err:?}"));
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::String);
assert_eq!(
text(src, &tokens[0]),
*src,
"{src:?} span covers the whole literal"
);
}
// Unterminated: a trailing lone '$' is one last failing candidate, and
// the scan must still run to end of input and report the whole span —
// identical to the byte-at-a-time scan this replaces.
let src = "$tag$abc$";
let err = tokenize_with(src, &FeatureSet::POSTGRES).expect_err("never closes");
assert_eq!(err.kind, LexErrorKind::UnterminatedDollarQuote);
assert_eq!(err.span, Span::new(0, src.len() as u32));
}
/// A preset enabling both positional placeholder forms (`$n` and `?`) plus
/// dollar-quoting, so the `$`-disambiguation is exercised with both `$` paths live.
const PARAMETER_FEATURES: FeatureSet = FeatureSet::POSTGRES
.with(FeatureDelta::EMPTY.parameters(ParameterSyntax {
positional_dollar: true,
anonymous_question: true,
..ParameterSyntax::POSTGRES
}))
// The anonymous `?` placeholder and the `jsonb` `?` operator claim the same
// trigger; enabling the placeholder here vacates the operators so the preset
// stays lexically consistent (the `?` still lexes as the placeholder either way).
.with(FeatureDelta::EMPTY.operator_syntax(OperatorSyntax {
jsonb_operators: false,
..OperatorSyntax::POSTGRES
}));
/// A preset enabling the named placeholder forms (`:name` and `@name`) on top of
/// PostgreSQL, so the `:`/`::` and `@`/`@@` disambiguations are exercised with the
/// typecast and dollar-quoting paths also live.
const NAMED_PARAMETER_FEATURES: FeatureSet =
FeatureSet::POSTGRES.with(FeatureDelta::EMPTY.parameters(ParameterSyntax {
named_colon: true,
named_at: true,
..ParameterSyntax::POSTGRES
}));
const SESSION_VARIABLE_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.session_variables(SessionVariableSyntax::MYSQL));
#[test]
fn parameter_placeholders_lex_as_one_token_only_when_enabled() {
// Positional `$1` and anonymous `?` each lex as a single Parameter token,
// with the span covering the whole placeholder.
for src in ["$1", "$42", "?"] {
let tokens = tokenize_with(src, &PARAMETER_FEATURES).expect("placeholder lexes");
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::Parameter);
assert_eq!(text(src, &tokens[0]), src);
}
// Disabled (ANSI): `$` and `?` are both stray bytes.
let err = tokenize("$1").expect_err("`$1` is stray under ANSI");
assert_eq!(err.kind, LexErrorKind::StrayByte);
assert_eq!(err.span, Span::new(0, 1));
let err = tokenize("?").expect_err("`?` is stray under ANSI");
assert_eq!(err.kind, LexErrorKind::StrayByte);
}
#[test]
fn positional_parameter_does_not_collide_with_dollar_quoting() {
// The `$`-disambiguation: with both positional parameters and dollar-quoting
// enabled, `$` + digit is a parameter while `$tag$`/`$$` open a string.
let tokens = tokenize_with("$1 $$x$$", &PARAMETER_FEATURES).expect("both `$` forms lex");
assert_eq!(
tokens.iter().map(|t| t.kind).collect::<Vec<_>>(),
[TokenKind::Parameter, TokenKind::String],
);
assert_eq!(text("$1 $$x$$", &tokens[0]), "$1");
assert_eq!(text("$1 $$x$$", &tokens[1]), "$$x$$");
// With dollar-quoting on but positional off, `$1` is not a valid dollar-quote
// opener (a tag cannot start with a digit), so it stays a stray byte.
const DOLLAR_QUOTE_ONLY: FeatureSet =
FeatureSet::POSTGRES.with(FeatureDelta::EMPTY.parameters(ParameterSyntax::ANSI));
let err = tokenize_with("$1", &DOLLAR_QUOTE_ONLY)
.expect_err("`$1` rejects when positional parameters are off");
assert_eq!(err.kind, LexErrorKind::StrayByte);
assert_eq!(err.span, Span::new(0, 1));
// `$$x$$` is still one dollar-quoted String when positional is on.
let tokens = tokenize_with("$$x$$", &PARAMETER_FEATURES).expect("dollar quote still lexes");
assert_eq!(tokens.len(), 1);
assert_eq!(tokens[0].kind, TokenKind::String);
}
/// A T-SQL-like preset recognising `$1234.56` money literals on top of ANSI.
/// ANSI-based (no positional `$n`, no dollar-quoting), so the `$` byte belongs to
/// money alone — the dialect-disjoint arrangement a real money dialect has.
const MONEY_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.numeric_literals(NumericLiteralSyntax {
money_literals: true,
..NumericLiteralSyntax::ANSI
}));
#[test]
fn money_literals_lex_as_one_number_only_when_enabled() {
// `$1234.56`, `$100`, leading-dot `$.5`, and trailing-dot `$1.` each lex as a
// single Number token spanning the `$` (the money type is recovered from the
// prefix at parse time, ADR-0006).
for src in ["$1234.56", "$100", "$.5", "$1."] {
let tokens = tokenize_with(src, &MONEY_FEATURES).expect("money literal lexes");
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::Number);
assert_eq!(text(src, &tokens[0]), src, "the whole literal is the span");
}
// A signed money value is a separate `-` operator then the money Number; signs
// are never folded into the literal token (ADR-0006).
let src = "-$1000";
let tokens = tokenize_with(src, &MONEY_FEATURES).expect("signed money lexes");
assert_eq!(
tokens.iter().map(|t| t.kind).collect::<Vec<_>>(),
[TokenKind::Operator(Operator::Minus), TokenKind::Number],
);
assert_eq!(text(src, &tokens[1]), "$1000");
// Disabled (ANSI): `$` is in no lexical class, so it is a stray byte.
let err = tokenize("$100").expect_err("`$100` is stray under ANSI");
assert_eq!(err.kind, LexErrorKind::StrayByte);
assert_eq!(err.span, Span::new(0, 1));
}
#[test]
fn money_resolves_the_dollar_dispatch_deterministically() {
// `$.` with no following digit is not money: it falls through the `$` arms. Under
// money-only ANSI there is no further `$` form, so it is a stray byte.
let err =
tokenize_with("$.x", &MONEY_FEATURES).expect_err("`$.` without a digit is not money");
assert_eq!(err.kind, LexErrorKind::StrayByte);
assert_eq!(err.span, Span::new(0, 1));
// All three `$` forms live at once (a synthetic config — no shipped dialect
// enables money *and* the PostgreSQL `$` forms): money claims `$.5`, dollar
// quoting claims `$$x$$`, and the documented money-before-parameter priority
// makes `$1234.56` one money Number rather than the `$1234` parameter + `.56`.
const ALL_DOLLAR_FORMS: FeatureSet =
FeatureSet::POSTGRES.with(FeatureDelta::EMPTY.numeric_literals(NumericLiteralSyntax {
money_literals: true,
..NumericLiteralSyntax::POSTGRES
}));
let src = "$1234.56 $.5 $$x$$";
let tokens = tokenize_with(src, &ALL_DOLLAR_FORMS).expect("all `$` forms lex");
assert_eq!(
tokens.iter().map(|t| t.kind).collect::<Vec<_>>(),
[TokenKind::Number, TokenKind::Number, TokenKind::String],
);
assert_eq!(text(src, &tokens[0]), "$1234.56");
assert_eq!(text(src, &tokens[1]), "$.5");
assert_eq!(text(src, &tokens[2]), "$$x$$");
}
#[test]
fn named_parameters_lex_as_one_token_only_when_enabled() {
// `:name` / `@name` each lex as a single Parameter token spanning sigil + name.
for src in [":name", "@name", ":x1", "@_v"] {
let tokens =
tokenize_with(src, &NAMED_PARAMETER_FEATURES).expect("named placeholder lexes");
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::Parameter);
assert_eq!(text(src, &tokens[0]), src);
}
// Disabled (PostgreSQL has neither named form): the `:` stays a lone-colon
// punctuation token rather than absorbing the following name, so `:name` is `:` then a
// separate name — never one Parameter token.
let tokens = tokenize_with(":name", &FeatureSet::POSTGRES).expect("`:` is punctuation");
assert_eq!(tokens.len(), 2, "`:name` is `:` then a separate name");
assert_eq!(tokens[0].kind, TokenKind::Punctuation(Punctuation::Colon));
assert_eq!(text(":name", &tokens[0]), ":");
// `@name` under PostgreSQL is NOT a named parameter either: with `named_at` off, the
// `@` lexes as the prefix absolute-value operator (`Custom`, the general operator
// surface) spanning just the `@`, then the name as a separate token — two tokens, not
// one Parameter. (The name itself may lex as a keyword, which is irrelevant here.)
let tokens = tokenize_with("@name", &FeatureSet::POSTGRES).expect("`@name` lexes");
assert_eq!(
tokens.len(),
2,
"`@name` is the `@` operator then a separate name"
);
assert_eq!(tokens[0].kind, TokenKind::Operator(Operator::Custom));
assert_eq!(text("@name", &tokens[0]), "@");
assert_ne!(tokens[0].kind, TokenKind::Parameter);
}
#[test]
fn session_variables_lex_as_one_token_only_when_enabled() {
// `@name`, `@@name`, and `@@scope.name` each lex as a single Variable token
// spanning the sigil, any scope prefix, and the name (the scope `.` never
// escapes as separate punctuation).
for src in [
"@user_count",
"@@max_connections",
"@@global.time_zone",
"@@session.sql_mode",
] {
let tokens =
tokenize_with(src, &SESSION_VARIABLE_FEATURES).expect("session variable lexes");
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::Variable);
assert_eq!(text(src, &tokens[0]), src);
}
// `@@global` with no abutting `.name` is one token (a system variable literally
// named `global`); a trailing `.` with no identifier is left as punctuation.
let tokens =
tokenize_with("@@global", &SESSION_VARIABLE_FEATURES).expect("bare `@@global` lexes");
assert_eq!(tokens.len(), 1);
assert_eq!(text("@@global", &tokens[0]), "@@global");
// Disabled (ANSI): `@` is a lexical stray byte, never a variable token.
let err = tokenize_with("@x", &FeatureSet::ANSI).expect_err("`@` is stray under ANSI");
assert_eq!(err.kind, LexErrorKind::StrayByte);
assert_eq!(err.span, Span::new(0, 1));
}
#[test]
fn named_colon_parameter_does_not_collide_with_cast_or_slice() {
// The `:`-disambiguation: with `:name` on, the sigil only binds an
// identifier-start byte, so `::` stays the typecast and `:` before a digit
// stays the array-slice separator — both unchanged.
let tokens = tokenize_with("x::y", &NAMED_PARAMETER_FEATURES).expect("cast still lexes");
assert_eq!(
tokens.iter().map(|t| t.kind).collect::<Vec<_>>(),
[
TokenKind::Word,
TokenKind::Punctuation(Punctuation::DoubleColon),
TokenKind::Word,
],
"`::` is still the typecast, never a `:`-parameter",
);
let tokens = tokenize_with("x[1:2]", &NAMED_PARAMETER_FEATURES).expect("slice still lexes");
assert_eq!(
tokens.iter().map(|t| t.kind).collect::<Vec<_>>(),
[
TokenKind::Word,
TokenKind::Punctuation(Punctuation::LBracket),
TokenKind::Number,
TokenKind::Punctuation(Punctuation::Colon),
TokenKind::Number,
TokenKind::Punctuation(Punctuation::RBracket),
],
"a lone `:` before a digit is still the slice separator",
);
// `@@x`: the second `@` is not an identifier byte, so the `@name` arm never fires on
// the first `@`. This preset inherits PostgreSQL's `jsonb` operators, whose `@@` match
// operator munches the two `@` bytes, leaving `x` a word — so `@@` stays disjoint from
// the `@name` form by its second byte.
let tokens = tokenize_with("@@x", &NAMED_PARAMETER_FEATURES)
.expect("`@@` lexes as the jsonb match operator, not the `@name` form");
assert_eq!(
tokens.iter().map(|t| t.kind).collect::<Vec<_>>(),
[TokenKind::Operator(Operator::AtAt), TokenKind::Word],
"`@@` is the jsonb match operator, then the `x` word",
);
}
#[test]
fn postgres_escape_string_is_a_single_string_token() {
let src = "E'can\\'t' e'line\\n'";
let tokens = tokenize_with(src, &FeatureSet::POSTGRES).expect("escape strings lex");
assert_eq!(tokens.len(), 2);
assert_eq!(tokens[0].kind, TokenKind::String);
assert_eq!(text(src, &tokens[0]), "E'can\\'t'");
assert_eq!(tokens[1].kind, TokenKind::String);
assert_eq!(text(src, &tokens[1]), "e'line\\n'");
}
#[test]
fn postgres_escape_string_rejects_malformed_escapes_at_lex_time() {
// PostgreSQL validates escape content in its scanner; a short/invalid Unicode
// escape, an escape decoding to NUL, or a byte escape that breaks UTF-8 is
// rejected while lexing (matching libpg_query), not deferred to the accessor.
for src in [
"E'\\u12'",
"E'\\uD800'",
"E'\\U00110000'",
"E'\\0'",
"E'\\x00'",
"E'\\xff'",
"E'\\xc3'",
] {
let err = tokenize_with(src, &FeatureSet::POSTGRES)
.expect_err("malformed escape is rejected at lex time");
assert_eq!(err.kind, LexErrorKind::InvalidEscapeSequence, "for {src:?}");
// The error covers the whole escape-string token.
assert_eq!(err.span, Span::new(0, src.len() as u32), "for {src:?}");
}
}
#[test]
fn postgres_escape_string_accepts_unknown_and_valid_escapes() {
// An unknown escape (`\q`), a bare `\x`, and well-formed byte/Unicode escapes
// all lex as one string token — the boundary the real parser draws; the value
// is recovered later via `as_str` (ADR-0006).
for src in [
"E'\\q'",
"E'\\x'",
"E'\\q\\x'",
"E'\\xc3\\xa9'",
"E'line\\n\\t'",
"E'\\141\\x62c'",
] {
let tokens = tokenize_with(src, &FeatureSet::POSTGRES)
.unwrap_or_else(|err| panic!("{src:?} should lex: {err:?}"));
assert_eq!(tokens.len(), 1, "for {src:?}");
assert_eq!(tokens[0].kind, TokenKind::String, "for {src:?}");
assert_eq!(text(src, &tokens[0]), src, "for {src:?}");
}
}
#[test]
fn ansi_tokenizer_does_not_accept_dollar_quoted_strings() {
let err = tokenize("$$x$$").expect_err("dollar quotes are PostgreSQL-specific");
assert_eq!(err.kind, LexErrorKind::StrayByte);
assert_eq!(err.span, Span::new(0, 1));
}
#[test]
fn string_literal_rejects_a_raw_nul_byte_at_lex_time() {
// PG parity: a raw NUL byte (0x00) in a string literal's source is rejected
// while lexing in every form (confirmed against the libpg_query oracle), with
// the error spanning the whole literal. This is the raw-byte case, distinct
// from an escape that *decodes* to NUL (`E'\\x00'`, an InvalidEscapeSequence).
let cases: &[(&str, &FeatureSet)] = &[
("'a\0b'", &FeatureSet::ANSI), // ordinary
("'\0'", &FeatureSet::ANSI), // ordinary, lone NUL
("E'a\0b'", &FeatureSet::POSTGRES), // escape string, raw NUL byte
("$$a\0b$$", &FeatureSet::POSTGRES), // dollar-quoted
("$tag$a\0b$tag$", &FeatureSet::POSTGRES), // tagged dollar-quote
("N'a\0b'", &NATIONAL_STRING_FEATURES), // national string
("U&'a\0b'", &UNICODE_STRING_FEATURES), // unicode-escape string
("B'1\0'", &BIT_STRING_FEATURES), // bit-string constant
// Backslash-escape dialect: a NUL right after `\` is still rejected — the
// check scans the finished token, so escaping cannot smuggle a NUL past it.
("'a\\\0b'", &MYSQL_STRING_FEATURES),
];
for (src, features) in cases {
let err = tokenize_with(src, features)
.expect_err("a raw NUL in a string literal is rejected at lex time");
assert_eq!(err.kind, LexErrorKind::NulByteInString, "for {src:?}");
assert_eq!(
err.span,
Span::new(0, src.len() as u32),
"the error spans the whole literal for {src:?}",
);
}
// Sanity: a non-NUL control byte (0x01) is accepted — PostgreSQL accepts it
// too, so the check is specific to NUL, not control bytes generally — and a
// clean string still lexes as one token.
for src in ["'a\x01b'", "'ab'"] {
let tokens = tokenize_with(src, &FeatureSet::ANSI).expect("non-NUL string lexes");
assert_eq!(tokens.len(), 1, "for {src:?}");
assert_eq!(tokens[0].kind, TokenKind::String, "for {src:?}");
assert_eq!(text(src, &tokens[0]), src, "for {src:?}");
}
}
#[test]
fn quoted_identifier_rejects_a_raw_nul_byte_at_lex_time() {
// PG parity: a raw NUL byte (0x00) in a quoted identifier's source is rejected
// while lexing, in every configured quote form, with the error spanning the
// whole identifier. PostgreSQL rejects a NUL in all lexable text via its
// NUL-terminated-C-string boundary (confirmed against the libpg_query oracle for
// the `"…"` form it enables). This is its own error kind, not `NulByteInString`:
// a quoted identifier is interned, never materialised through `Literal::as_str`.
// A SQL Server-like preset whose only identifier quote is the asymmetric `[…]`.
const BRACKET_IDENT_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.identifier_quotes(&[
IdentifierQuote::Asymmetric {
open: '[',
close: ']',
},
]));
let cases: &[(&str, &FeatureSet)] = &[
("\"a\0b\"", &FeatureSet::ANSI), // standard double-quoted identifier
("\"\0\"", &FeatureSet::ANSI), // lone NUL
("[a\0b]", &BRACKET_IDENT_FEATURES), // T-SQL bracket identifier
("`a\0b`", &MYSQL_STRING_FEATURES), // MySQL backtick identifier
];
for (src, features) in cases {
let err = tokenize_with(src, features)
.expect_err("a raw NUL in a quoted identifier is rejected at lex time");
assert_eq!(err.kind, LexErrorKind::NulByteInIdentifier, "for {src:?}");
assert_eq!(
err.span,
Span::new(0, src.len() as u32),
"the error spans the whole identifier for {src:?}",
);
}
// Sanity: a non-NUL control byte (0x01) is accepted (the check is specific to
// NUL), and a clean quoted identifier still lexes as one token.
for src in ["\"a\x01b\"", "\"ab\""] {
let tokens = tokenize_with(src, &FeatureSet::ANSI).expect("non-NUL identifier lexes");
assert_eq!(tokens.len(), 1, "for {src:?}");
assert_eq!(tokens[0].kind, TokenKind::QuotedIdent, "for {src:?}");
assert_eq!(text(src, &tokens[0]), src, "for {src:?}");
}
}
#[test]
fn comment_rejects_a_raw_nul_byte_at_lex_time() {
// PG parity: a raw NUL byte (0x00) inside a comment is rejected while lexing. A
// comment is skipped as trivia — the scanner consumes it to end-of-line / `*/`
// without inspecting the bytes — so it is the one lexable context the value-bearing
// string/identifier NUL gates cannot see. PostgreSQL rejects a NUL in *all* lexable
// text via its NUL-terminated-C-string boundary (confirmed against the libpg_query
// oracle), so this closes the gap; the minimized fuzz reproducer is the bare `--\0-`
// line comment (fuzz-pg-differential-crash-2b8d66f9). A raw NUL cannot be written in
// a Rust raw string, so these use `\0` escapes.
let cases: &[(&str, &FeatureSet)] = &[
("--\0-", &FeatureSet::POSTGRES), // the 4-byte fuzz reproducer, comment to EOF
("-- a\0b", &FeatureSet::ANSI), // `--` line comment, NUL mid-body
("-- a\0b\nSELECT 1", &FeatureSet::ANSI), // NUL before the comment-ending newline
("# a\0b", &HASH_COMMENT_FEATURES), // MySQL `#` line comment
("/* a\0b */", &FeatureSet::ANSI), // `/* … */` block comment
("/* a /* \0 */ b */", &FeatureSet::POSTGRES), // NUL in a nested block comment
("SELECT/* \0 */1", &FeatureSet::ANSI), // block comment between real tokens
];
for (src, features) in cases {
let err = tokenize_with(src, features)
.expect_err("a raw NUL in a comment is rejected at lex time");
assert_eq!(err.kind, LexErrorKind::NulByteInComment, "for {src:?}");
}
// The error spans the comment run (here the whole 4-byte reproducer).
let err = tokenize_with("--\0-", &FeatureSet::POSTGRES).expect_err("rejected");
assert_eq!(err.span, Span::new(0, 4));
// Sanity: a non-NUL control byte (0x01) in a comment is accepted — PostgreSQL
// accepts it, so the check is specific to NUL, not control bytes — and a
// comment-only input still lexes to zero tokens (the comment is pure trivia).
for src in ["-- a\x01b", "/* a\x01b */"] {
assert!(
tokenize_with(src, &FeatureSet::ANSI)
.expect("a non-NUL control byte in a comment lexes")
.is_empty(),
"for {src:?}",
);
}
}
#[test]
fn line_comment_terminator_set_is_dialect_data() {
// The line-comment terminator set is dialect data (tokenizer-line-comment-terminator-set).
// A `\n` ends a `--`/`#` line comment in every dialect; a bare `\r` ends it only where
// `CommentSyntax::line_comment_ends_at_carriage_return` is set — Postgres and DuckDb,
// whose flex scanner's comment body is `[^\n\r]*` — while Ansi/Sqlite/MySql/Lenient read
// a `\r` as ordinary comment content and end only at `\n`. A `\x0b`/`\x0c` (vertical tab /
// form feed, in the flex `space` set but not its newline set) is never a terminator. All
// four axes measured against pg_query, rusqlite, libduckdb, and live mysql; the
// oracle-parity half is pinned in the conformance suite.
//
// Probe: `<marker> x<TERM>Y`. If TERM ends the comment, `Y` is a live word token; if not,
// the whole tail is trivia and there are zero tokens.
// `\r` drives the `#` comment through the same code path, but no shipped dialect pairs `#`
// comments with the CR terminator, so an ad-hoc preset exercises that combination.
const HASH_CR_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.comment_syntax(CommentSyntax {
line_comment_hash: true,
line_comment_ends_at_carriage_return: true,
..CommentSyntax::ANSI
}));
// Presets where `\r` ends a line comment (`--` for Postgres/DuckDb, `#` for the ad-hoc
// preset), and presets where it does not (`--` for Ansi, `#` for the plain hash preset).
let cr_on: &[(&str, &FeatureSet)] = &[
("--", &FeatureSet::POSTGRES),
("--", &FeatureSet::DUCKDB),
("#", &HASH_CR_FEATURES),
];
let cr_off: &[(&str, &FeatureSet)] =
&[("--", &FeatureSet::ANSI), ("#", &HASH_COMMENT_FEATURES)];
// (terminator, ends the comment where CR is on?, ends where CR is off?)
let terminators: &[(&str, bool, bool)] = &[
("\n", true, true), // newline: the universal terminator
("\r", true, false), // carriage return: only where the flag is on
("\r\n", true, true), // CRLF: the trailing `\n` ends it even without CR-termination
("\x0b", false, false), // vertical tab: never a terminator
("\x0c", false, false), // form feed: never a terminator
];
let check = |marker: &str, features: &FeatureSet, term: &str, ends: bool| {
let src = format!("{marker} x{term}Y");
let tokens =
tokenize_with(&src, features).unwrap_or_else(|e| panic!("lex {src:?}: {e:?}"));
if ends {
assert_eq!(tokens.len(), 1, "{src:?} should end the comment");
assert_eq!(text(&src, &tokens[0]), "Y", "live token after {src:?}");
} else {
assert!(tokens.is_empty(), "{src:?} is one comment run to EOF");
}
};
for (term, ends_on, ends_off) in terminators {
for (marker, features) in cr_on {
check(marker, features, term, *ends_on);
}
for (marker, features) in cr_off {
check(marker, features, term, *ends_off);
}
}
// Trivia-span decision: the terminating `\r` is left for the whitespace scan, so it joins
// the following whitespace run, never the comment run — matching PG, whose `[^\n\r]*` body
// excludes the `\r` (which is `CLASS_WHITESPACE` in every preset). For `-- x\rY` under
// Postgres: comment `[0,4)`, whitespace `\r` `[4,5)`, then `Y` `[5,6)`.
let src = "-- x\rY";
let (tokens, trivia) = tokenize_with_trivia(src, &FeatureSet::POSTGRES).expect("clean");
assert_eq!(text(src, &tokens[0]), "Y");
assert_eq!(tokens[0].span, Span::new(5, 6));
let runs: Vec<(TriviaKind, Span)> =
trivia.all().iter().map(|r| (r.kind(), r.span())).collect();
assert_eq!(
runs,
[
(TriviaKind::LineComment, Span::new(0, 4)),
(TriviaKind::Whitespace, Span::new(4, 5)),
],
"the terminating `\\r` belongs to the following whitespace, not the comment run",
);
// Flag off (Ansi): the same bytes are one comment run to EOF — the `\r` stays comment
// content, so there is no token and no separate whitespace run.
let (tokens, trivia) = tokenize_with_trivia(src, &FeatureSet::ANSI).expect("clean");
assert!(tokens.is_empty());
let runs: Vec<(TriviaKind, Span)> =
trivia.all().iter().map(|r| (r.kind(), r.span())).collect();
assert_eq!(runs, [(TriviaKind::LineComment, Span::new(0, 6))]);
}
#[test]
fn zero_length_delimited_identifier_is_rejected_at_lex_time() {
// SQL's `<delimited identifier body>` requires at least one character.
// PostgreSQL rejects an empty delimited identifier while scanning
// ("zero-length delimited identifier", `scan.l`) and MySQL rejects an empty
// backtick the same way — unconditionally (no dialect legitimately accepts
// one), so every configured identifier-quote style is covered here: symmetric
// `"..."`, symmetric backtick, and asymmetric `[...]`.
const BRACKET_IDENT_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.identifier_quotes(&[
IdentifierQuote::Asymmetric {
open: '[',
close: ']',
},
]));
let cases: &[(&str, &FeatureSet)] = &[
("\"\"", &FeatureSet::ANSI), // standard double-quoted identifier
("``", &MYSQL_STRING_FEATURES), // MySQL backtick identifier
("[]", &BRACKET_IDENT_FEATURES), // T-SQL bracket identifier
];
for (src, features) in cases {
let err = tokenize_with(src, features)
.expect_err("an empty delimited identifier body is rejected at lex time");
assert_eq!(
err.kind,
LexErrorKind::ZeroLengthDelimitedIdentifier,
"for {src:?}"
);
// The span covers both delimiters (PG parity), not a zero-width point.
assert_eq!(err.span, Span::new(0, src.len() as u32), "for {src:?}");
}
// `U&""` is scanned by the dedicated `U&"..."` Unicode-escaped *identifier* arm
// (UNICODE_STRING_FEATURES enables `unicode_strings`), so the zero-length body is
// rejected there over the whole `U&""` lexeme — matching PostgreSQL, whose `U&"..."`
// scanner arm raises "zero-length delimited identifier" spanning the full `U&""`.
let err = tokenize_with("U&\"\"", &UNICODE_STRING_FEATURES)
.expect_err("an empty U&\"\" body is a zero-length delimited identifier");
assert_eq!(err.kind, LexErrorKind::ZeroLengthDelimitedIdentifier);
assert_eq!(
err.span,
Span::new(0, 4),
"the span covers the whole `U&\"\"`, the leading `U&` included"
);
// The doubled-close escape is not a zero-length body: `""""` / ```` ```` ```` /
// `[]]]` each carry exactly one *literal* close character, so all four bytes
// stay one valid QuotedIdent — the critical case the empty-body check must not
// regress, for every style above.
for (src, features) in [
("\"\"\"\"", &FeatureSet::ANSI),
("````", &MYSQL_STRING_FEATURES),
("[]]]", &BRACKET_IDENT_FEATURES),
] {
let tokens = tokenize_with(src, features)
.expect("a doubled-close escape is one valid identifier, not an empty body");
assert_eq!(tokens.len(), 1, "for {src:?}");
assert_eq!(tokens[0].kind, TokenKind::QuotedIdent, "for {src:?}");
assert_eq!(text(src, &tokens[0]), src, "for {src:?}");
}
// Non-regression: an empty *string* is ordinary valid SQL and must never trip
// the identifier-only check, in every scan that shares this loop — standard
// `''`, and MySQL `""` (a string, not an identifier, whenever
// `double_quoted_strings` is on — the identifier-vs-string asymmetry this fix
// must respect).
for (src, features) in [("''", &FeatureSet::ANSI), ("\"\"", &MYSQL_STRING_FEATURES)] {
let tokens = tokenize_with(src, features).expect("an empty string literal is valid");
assert_eq!(tokens.len(), 1, "for {src:?}");
assert_eq!(tokens[0].kind, TokenKind::String, "for {src:?}");
assert_eq!(text(src, &tokens[0]), src, "for {src:?}");
}
}
#[test]
fn numbers_cover_int_float_leading_dot_and_exponent() {
assert_eq!(lexed("42"), [(TokenKind::Number, "42")]);
assert_eq!(lexed("3.14"), [(TokenKind::Number, "3.14")]);
assert_eq!(lexed("1."), [(TokenKind::Number, "1.")]);
assert_eq!(lexed(".5"), [(TokenKind::Number, ".5")]);
assert_eq!(lexed("1e10"), [(TokenKind::Number, "1e10")]);
assert_eq!(lexed("2.5E-3"), [(TokenKind::Number, "2.5E-3")]);
// A bare `.` is the member dot, not a number.
assert_eq!(
lexed("d.b"),
[
(TokenKind::Word, "d"),
(TokenKind::Punctuation(Punctuation::Dot), "."),
(TokenKind::Word, "b"),
],
);
// `1e` is not a valid exponent: the number ends, `e` is a word.
assert_eq!(
lexed("1e+"),
[
(TokenKind::Number, "1"),
(TokenKind::Word, "e"),
(TokenKind::Operator(Operator::Plus), "+"),
],
);
}
#[test]
fn operators_and_punctuation_pick_the_right_subkinds() {
use Operator::{Concat, Eq, GtEq, LtEq, NotEq};
assert_eq!(
lexed("d||b"),
[
(TokenKind::Word, "d"),
(TokenKind::Operator(Concat), "||"),
(TokenKind::Word, "b"),
],
);
assert_eq!(
lexed("<= >= <> != ="),
[
(TokenKind::Operator(LtEq), "<="),
(TokenKind::Operator(GtEq), ">="),
(TokenKind::Operator(NotEq), "<>"),
(TokenKind::Operator(NotEq), "!="),
(TokenKind::Operator(Eq), "="),
],
);
assert_eq!(
lexed("();,"),
[
(TokenKind::Punctuation(Punctuation::LParen), "("),
(TokenKind::Punctuation(Punctuation::RParen), ")"),
(TokenKind::Punctuation(Punctuation::Semicolon), ";"),
(TokenKind::Punctuation(Punctuation::Comma), ","),
],
);
}
#[test]
fn colon_and_double_colon_lex_as_punctuation() {
use Punctuation::{Colon, DoubleColon, LBracket, RBracket};
// `::` munches as one DoubleColon (the typecast operator). `d`/`b` are not
// keywords in the full inventory, so they lex as plain words.
assert_eq!(
lexed("d::b"),
[
(TokenKind::Word, "d"),
(TokenKind::Punctuation(DoubleColon), "::"),
(TokenKind::Word, "b"),
],
);
// A lone `:` is the array-slice separator; the surrounding numbers still lex
// as separate tokens (the `:` does not begin a numeric literal).
assert_eq!(
lexed("d[1:2]"),
[
(TokenKind::Word, "d"),
(TokenKind::Punctuation(LBracket), "["),
(TokenKind::Number, "1"),
(TokenKind::Punctuation(Colon), ":"),
(TokenKind::Number, "2"),
(TokenKind::Punctuation(RBracket), "]"),
],
);
// Maximal munch: `:::` is `::` followed by a lone `:`.
assert_eq!(
lexed(":::"),
[
(TokenKind::Punctuation(DoubleColon), "::"),
(TokenKind::Punctuation(Colon), ":"),
],
);
}
#[test]
fn quoted_identifier_handles_doubled_quote_escape() {
let src = r#""Odd""Name""#;
let tokens = tokenize(src).expect("balanced quotes");
assert_eq!(tokens.len(), 1);
assert_eq!(tokens[0].kind, TokenKind::QuotedIdent);
assert_eq!(text(src, &tokens[0]), src);
}
#[test]
fn tokenize_with_reads_dialect_byte_classes_and_identifier_quote() {
const FEATURES: FeatureSet = FeatureSet::ANSI.with(
FeatureDelta::EMPTY
.identifier_quotes(&[IdentifierQuote::Symmetric('`')])
.byte_classes(
ByteClasses::STANDARD
.with_class(b'@', CLASS_IDENTIFIER_START | CLASS_IDENTIFIER_CONTINUE),
),
);
let tokens = tokenize_with("@name `Odd``Name`", &FEATURES).expect("custom dialect lexes");
assert_eq!(tokens.len(), 2);
assert_eq!(tokens[0].kind, TokenKind::Word);
assert_eq!(text("@name `Odd``Name`", &tokens[0]), "@name");
assert_eq!(tokens[1].kind, TokenKind::QuotedIdent);
assert_eq!(text("@name `Odd``Name`", &tokens[1]), "`Odd``Name`");
}
#[test]
fn asymmetric_and_multiple_identifier_quote_styles_lex() {
// A SQL Server-like preset: asymmetric `[...]` (doubled `]]` escape) plus `"..."`.
const FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.identifier_quotes(&[
IdentifierQuote::Asymmetric {
open: '[',
close: ']',
},
IdentifierQuote::Symmetric('"'),
]));
let src = r#"[a b] "c d" [a]]b]"#;
let tokens = tokenize_with(src, &FEATURES).expect("multi-style dialect lexes");
assert_eq!(tokens.len(), 3);
assert!(tokens.iter().all(|t| t.kind == TokenKind::QuotedIdent));
assert_eq!(text(src, &tokens[0]), "[a b]");
assert_eq!(text(src, &tokens[1]), r#""c d""#);
// The doubled close `]]` escapes a literal `]`, so all of `[a]]b]` is one token.
assert_eq!(text(src, &tokens[2]), "[a]]b]");
}
#[test]
fn brackets_stay_punctuation_when_bracket_quoting_is_off() {
// ANSI does not bracket-quote, so `[` remains `LBracket` punctuation.
let tokens = tokenize("[d]").expect("ANSI lexes brackets as punctuation");
assert_eq!(
tokens.iter().map(|t| t.kind).collect::<Vec<_>>(),
vec![
TokenKind::Punctuation(Punctuation::LBracket),
TokenKind::Word,
TokenKind::Punctuation(Punctuation::RBracket),
],
);
}
/// A SQL Server-like string preset: `N'...'` national strings on; the rest off.
const NATIONAL_STRING_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.string_literals(StringLiteralSyntax {
national_strings: true,
..StringLiteralSyntax::ANSI
}));
/// A MySQL-like string preset: `"..."` is a string, `'...'` honours backslash
/// escapes, and the identifier quote moves to the backtick.
const MYSQL_STRING_FEATURES: FeatureSet = FeatureSet::ANSI.with(
FeatureDelta::EMPTY
.string_literals(StringLiteralSyntax {
double_quoted_strings: true,
backslash_escapes: true,
..StringLiteralSyntax::ANSI
})
.identifier_quotes(&[IdentifierQuote::Symmetric('`')]),
);
/// A unicode-escape string preset: `U&'...'` on.
const UNICODE_STRING_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.string_literals(StringLiteralSyntax {
unicode_strings: true,
..StringLiteralSyntax::ANSI
}));
/// A bit-string preset: `B'...'` / `X'...'` on.
const BIT_STRING_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.string_literals(StringLiteralSyntax {
bit_string_literals: true,
..StringLiteralSyntax::ANSI
}));
/// A SQLite-like blob preset: eager even-hex `x'...'` on, bit strings off.
const BLOB_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.string_literals(StringLiteralSyntax {
blob_literals: true,
..StringLiteralSyntax::ANSI
}));
/// The MySQL layout: blob and bit-string both on, so `x`/`X` resolves to the eager
/// blob arm by scan precedence and `B`/`b` keeps the deferred bit-string.
const BLOB_AND_BIT_STRING_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.string_literals(StringLiteralSyntax {
blob_literals: true,
bit_string_literals: true,
..StringLiteralSyntax::ANSI
}));
/// A MySQL-like charset-introducer preset: `_charset'...'` on (with backslash
/// escapes, as MySQL has them) — the rest of the string surface left off. `"` keeps
/// its ANSI role as a symmetric identifier quote (`double_quoted_strings` off), which
/// is exactly the `ANSI_QUOTES`-on layout where the double-quoted introducer must not
/// apply.
const CHARSET_INTRODUCER_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.string_literals(StringLiteralSyntax {
charset_introducers: true,
backslash_escapes: true,
..StringLiteralSyntax::ANSI
}));
/// A MySQL-like charset-introducer preset with double-quoted strings on: both
/// `_charset'...'` and `_charset"..."` introduce a string, because `"..."` is itself
/// a string here (`double_quoted_strings` on — the MySQL `ANSI_QUOTES`-off layout,
/// with the identifier quote moved to the backtick so `"` is unambiguously a string).
const CHARSET_INTRODUCER_DQ_FEATURES: FeatureSet = FeatureSet::ANSI.with(
FeatureDelta::EMPTY
.string_literals(StringLiteralSyntax {
charset_introducers: true,
double_quoted_strings: true,
backslash_escapes: true,
..StringLiteralSyntax::ANSI
})
.identifier_quotes(&[IdentifierQuote::Symmetric('`')]),
);
#[test]
fn national_string_lexes_as_one_string_token_only_when_enabled() {
// Enabled: `N'x'` is a single String spanning the `N` prefix.
let tokens =
tokenize_with("N'x'", &NATIONAL_STRING_FEATURES).expect("national strings lex");
assert_eq!(tokens.len(), 1);
assert_eq!(tokens[0].kind, TokenKind::String);
assert_eq!(text("N'x'", &tokens[0]), "N'x'");
// Lower-case `n` is accepted too (mirrors `E'`/`e'`).
assert_eq!(
tokenize_with("n'x'", &NATIONAL_STRING_FEATURES).expect("ok")[0].kind,
TokenKind::String,
);
// Disabled (ANSI): `N` is a word, `'x'` a separate string.
assert_eq!(
lexed("N'x'"),
[(TokenKind::Word, "N"), (TokenKind::String, "'x'")],
);
}
#[test]
fn charset_introducer_lexes_as_one_string_token_only_when_enabled() {
// Enabled: `_utf8mb4'x'` is a single String spanning the `_charset` introducer,
// exactly as `N'x'` spans the `N` prefix.
for src in ["_utf8mb4'x'", "_latin1'x'", "_utf8'caf\\'e'"] {
let tokens =
tokenize_with(src, &CHARSET_INTRODUCER_FEATURES).expect("charset introducer lexes");
assert_eq!(tokens.len(), 1, "one token for {src:?}");
assert_eq!(tokens[0].kind, TokenKind::String, "kind for {src:?}");
assert_eq!(text(src, &tokens[0]), src, "span for {src:?}");
}
// A bare `_name` with no abutting quote stays an ordinary identifier — the
// introducer never steals a leading-`_` word.
assert_eq!(
tokenize_with("_utf8 'x'", &CHARSET_INTRODUCER_FEATURES).expect("ok")[0].kind,
TokenKind::Word,
);
// A lone `_` before the quote is not a charset name (the name must be non-empty),
// so `_` lexes as a word and `'x'` as a separate string.
let tokens = tokenize_with("_'x'", &CHARSET_INTRODUCER_FEATURES).expect("ok");
assert_eq!(
(tokens[0].kind, tokens[1].kind),
(TokenKind::Word, TokenKind::String),
);
// Disabled (ANSI): `_utf8` is a word, `'x'` a separate string.
assert_eq!(
lexed("_utf8'x'"),
[(TokenKind::Word, "_utf8"), (TokenKind::String, "'x'")],
);
}
#[test]
fn charset_introducer_spans_a_double_quoted_body_only_when_double_quotes_string() {
// With `"..."` a string (MySQL `ANSI_QUOTES` off), the introducer spans a
// double-quoted body exactly as it spans a single-quoted one: the whole
// `_charset"..."` is one String token covering the introducer prefix (ADR-0006),
// the mirror of `_latin1'x'`. The doubled `""` and a backslash-escaped `\"` are
// in-body, not terminators.
for src in [r#"_latin1"x""#, r#"_utf8"a""b""#, r#"_utf8mb4"caf\"e""#] {
let tokens = tokenize_with(src, &CHARSET_INTRODUCER_DQ_FEATURES)
.expect("double-quoted charset introducer lexes");
assert_eq!(tokens.len(), 1, "one token for {src:?}");
assert_eq!(tokens[0].kind, TokenKind::String, "kind for {src:?}");
assert_eq!(text(src, &tokens[0]), src, "span for {src:?}");
}
// The single-quote form still lexes as one String under the same preset.
let tokens = tokenize_with(r#"_latin1'x'"#, &CHARSET_INTRODUCER_DQ_FEATURES).expect("ok");
assert_eq!((tokens.len(), tokens[0].kind), (1, TokenKind::String));
// `ANSI_QUOTES`-on semantics: where `"..."` is an *identifier* (double-quoted
// strings off, `"` a symmetric identifier quote), the introducer is NOT applied —
// `_latin1` stays an ordinary word and `"x"` is a separate quoted identifier, so
// the two never fuse into one charset-tagged string.
let src = r#"_latin1"x""#;
let tokens = tokenize_with(src, &CHARSET_INTRODUCER_FEATURES).expect("ok");
assert_eq!(
(tokens[0].kind, text(src, &tokens[0])),
(TokenKind::Word, "_latin1"),
);
assert_eq!(
(tokens[1].kind, text(src, &tokens[1])),
(TokenKind::QuotedIdent, r#""x""#),
);
}
#[test]
fn double_quoted_string_takes_precedence_over_identifier_quoting() {
// `"a b"` is a String, `` `c d` `` is the identifier quote, and `'x'` still
// works — the MySQL ANSI_QUOTES-off layout.
let src = r#""a b" `c d` 'x'"#;
let tokens = tokenize_with(src, &MYSQL_STRING_FEATURES).expect("mysql strings lex");
assert_eq!(tokens.len(), 3);
assert_eq!(tokens[0].kind, TokenKind::String);
assert_eq!(text(src, &tokens[0]), r#""a b""#);
assert_eq!(tokens[1].kind, TokenKind::QuotedIdent);
assert_eq!(tokens[2].kind, TokenKind::String);
// ANSI still lexes `"a b"` as a quoted identifier (behaviour unchanged).
assert_eq!(
tokenize(r#""a b""#).expect("ansi")[0].kind,
TokenKind::QuotedIdent,
);
}
#[test]
fn backslash_escapes_keep_an_escaped_quote_inside_the_string() {
// With escapes on, `\'` does not terminate, so `'a\'b'` is one token.
let tokens =
tokenize_with(r"'a\'b'", &MYSQL_STRING_FEATURES).expect("backslash-escaped quote");
assert_eq!(tokens.len(), 1);
assert_eq!(tokens[0].kind, TokenKind::String);
assert_eq!(text(r"'a\'b'", &tokens[0]), r"'a\'b'");
// ANSI (no backslash escapes): `'a\'` closes, then `b`, then an unterminated `'`.
assert_eq!(
tokenize(r"'a\'b'")
.expect_err("trailing quote never closes")
.kind,
LexErrorKind::UnterminatedString,
);
}
/// A preset enabling every radix prefix plus underscore separators (PG 14-like),
/// with `reject_trailing_junk` off so the loose split-token behaviour (`0xZ` -> `0`
/// then `xZ`, a trailing `1_` -> number then word) stays observable — the strict
/// PostgreSQL reject is exercised separately under `FeatureSet::POSTGRES`.
const RADIX_NUMBER_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.numeric_literals(NumericLiteralSyntax {
hex_integers: true,
octal_integers: true,
binary_integers: true,
underscore_separators: true,
// Loose fixture: the leading-underscore radix opener stays off, so `0x_1F`
// keeps the `0` + word split the reject-off tests below observe.
radix_leading_underscore: false,
money_literals: false,
reject_trailing_junk: false,
}));
#[test]
fn radix_prefixed_integers_lex_as_one_number_only_when_enabled() {
for src in ["0x1F", "0Xbeef", "0o17", "0b1010"] {
let tokens = tokenize_with(src, &RADIX_NUMBER_FEATURES).expect("radix integer lexes");
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::Number);
assert_eq!(text(src, &tokens[0]), src);
}
// Disabled (ANSI): `0x1F` splits into the number `0` and the word `x1F`.
assert_eq!(
lexed("0x1F"),
[(TokenKind::Number, "0"), (TokenKind::Word, "x1F")],
);
// A prefix with no valid radix digit falls through to `0` + word.
assert_eq!(
tokenize_with("0xZ", &RADIX_NUMBER_FEATURES).expect("ok")[0].kind,
TokenKind::Number,
);
assert_eq!(
text(
"0xZ",
&tokenize_with("0xZ", &RADIX_NUMBER_FEATURES).expect("ok")[0]
),
"0",
);
}
#[test]
fn underscore_separators_join_digit_groups_only_when_enabled() {
// Enabled: `_` between digits is part of the number, across decimal,
// fractional, exponent, and radix forms.
for src in ["1_500_000", "1_000.000_5", "1_0e1_0", "0xFF_FF"] {
let tokens =
tokenize_with(src, &RADIX_NUMBER_FEATURES).expect("separated number lexes");
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::Number);
assert_eq!(text(src, &tokens[0]), src);
}
// A trailing `_` is not a separator: the number stops, `_000` is a word.
let src = "1_000";
let tokens = tokenize_with(src, &RADIX_NUMBER_FEATURES).expect("ok");
assert_eq!(
tokens.len(),
1,
"separator between digits stays in the number"
);
let trailing = tokenize_with("1_", &RADIX_NUMBER_FEATURES).expect("ok");
assert_eq!(
trailing.iter().map(|t| t.kind).collect::<Vec<_>>(),
[TokenKind::Number, TokenKind::Word],
);
// Disabled (ANSI): `_` is an identifier byte, so `1_500` is `1` + `_500`.
assert_eq!(
lexed("1_500"),
[(TokenKind::Number, "1"), (TokenKind::Word, "_500")],
);
}
/// PostgreSQL over the same radix/separator surface as [`RADIX_NUMBER_FEATURES`] but
/// with `reject_trailing_junk` forced *off* — the dialect override that proves the
/// trailing-junk reject and strict-`_` placement are honoured off the shared table,
/// not baked into the scanner (test both the default and an override).
const POSTGRES_LOOSE_NUMBERS: FeatureSet =
FeatureSet::POSTGRES.with(FeatureDelta::EMPTY.numeric_literals(NumericLiteralSyntax {
reject_trailing_junk: false,
..NumericLiteralSyntax::POSTGRES
}));
/// The 18 malformed numeric forms `numerology.sql` proves PostgreSQL parse-rejects
/// (pg-numeric-literal-lexer-validation): an identifier glued to a number, a radix
/// prefix with missing/invalid digits, junk/empty exponents, and misplaced `_`
/// separators. Every one decomposes to "trailing junk after a numeric literal".
const TRAILING_JUNK_NUMBERS: &[&str] = &[
"123abc", "0x", "1x", "0x0y", "0x0o", "0b", "1b", "0b0x", "0o", "1o", "0o0x", "0.a",
"0.0a", ".0a", "0.0e1a", "0.0e", "100_", "100__000",
];
#[test]
fn postgres_rejects_trailing_junk_after_numeric_literals() {
// The reject direction (PostgreSQL, `reject_trailing_junk` on): each malformed
// form is a single `TrailingJunkAfterNumber` lex error whose span covers the
// whole bad lexeme (the number *and* the abutting identifier run), mirroring
// PG's `{decinteger}{identifier}` munch.
for &src in TRAILING_JUNK_NUMBERS {
let err = tokenize_with(src, &FeatureSet::POSTGRES)
.expect_err(&format!("{src:?} is trailing junk under PostgreSQL"));
assert_eq!(err.kind, LexErrorKind::TrailingJunkAfterNumber, "{src:?}");
assert_eq!(
&src[err.span.start() as usize..err.span.end() as usize],
src,
"{src:?} error span covers the whole malformed lexeme",
);
}
// Strict `_` placement is the same knob: a `_` not flanked by two digits stops the
// number and surfaces as junk, so these misplaced-separator forms reject too.
for &src in &["1_000_.5", "1_000._5", "1_000.5_", "1_000.5e_1"] {
let err = tokenize_with(src, &FeatureSet::POSTGRES).expect_err(&format!(
"{src:?} has a misplaced separator under PostgreSQL"
));
assert_eq!(err.kind, LexErrorKind::TrailingJunkAfterNumber, "{src:?}");
}
}
#[test]
fn trailing_junk_reject_is_dialect_data_not_baked_in() {
// The accept direction, proven by a *same-dialect override*: flip
// `reject_trailing_junk` off on PostgreSQL and every malformed form lexes cleanly
// again — the number, then its trailing text as an ordinary word (the loose
// DuckDB/MySQL behaviour) — so the reject is dialect data a future dialect cannot
// bypass, not a hard-wired scanner rule.
for &src in TRAILING_JUNK_NUMBERS {
let tokens = tokenize_with(src, &POSTGRES_LOOSE_NUMBERS)
.unwrap_or_else(|err| panic!("{src:?} must lex loosely with the flag off: {err}"));
assert!(
tokens.len() >= 2,
"{src:?} splits into a number and trailing text"
);
assert_eq!(
tokens[0].kind,
TokenKind::Number,
"{src:?} opens with a number"
);
}
// `123abc` is the canonical split: number `123`, then the word `abc`.
assert_eq!(
tokenize_with("123abc", &POSTGRES_LOOSE_NUMBERS)
.expect("loose")
.iter()
.map(|t| (t.kind, text("123abc", t)))
.collect::<Vec<_>>(),
[(TokenKind::Number, "123"), (TokenKind::Word, "abc")],
);
}
#[test]
fn postgres_lexes_valid_radix_and_separator_numbers_as_one_token() {
// Zero new over-rejection: every valid PG-17 numeric form — radix integers, `_`
// grouping (including a radix body that opens with `_`, `0x_…`), the plain
// decimal/scientific forms, and a bare `1.`/`.5` — stays a single `Number`
// spanning its whole text, under the same `reject_trailing_junk`-on dialect.
for src in [
"0x42F",
"0o273",
"0b100101",
"0x1F",
"1_000_000",
"1_2_3",
"0b_10_0101",
"0x1EEE_FFFF",
"0o2_73",
"1_000.000_005",
"1_000.",
".000_005",
"1_000.5e0_1",
"1e10",
".5",
"5.",
"1.5",
] {
let tokens = tokenize_with(src, &FeatureSet::POSTGRES)
.unwrap_or_else(|err| panic!("{src:?} is a valid PostgreSQL number: {err}"));
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::Number, "{src:?}");
assert_eq!(text(src, &tokens[0]), src, "{src:?} spans its whole text");
}
}
#[test]
fn sqlite_numeric_boundary_matches_the_engine() {
// The measured rusqlite 3.53.2 boundary (sqlite-numeric-trailing-junk-over-acceptance):
// SQLite lexes a numeric literal abutting identifier chars as one TK_ILLEGAL token,
// rejects a misplaced/leading `_`, but accepts interior `_` separators (3.46+). The
// fitted Sqlite preset now carries `reject_trailing_junk` + `underscore_separators`
// (with `radix_leading_underscore` OFF), so it reproduces the boundary exactly.
for &src in &[
// trailing junk after a numeric literal
"1SETECT",
".122ualCvT",
"2ES",
"123abc",
"1a",
"1.a",
".1a",
"1.5x",
// bad radix body / junk exponent
"0x1g",
"0xG",
"0x",
"0x1Fz",
"1e5x",
"1e",
// misplaced or leading `_`
"1_",
"1__0",
"1._5",
// leading-underscore radix body: PG accepts `0x_1F`, SQLite rejects it
"0x_1F",
] {
let err = tokenize_with(src, &FeatureSet::SQLITE)
.expect_err(&format!("{src:?} is trailing junk under SQLite"));
assert_eq!(err.kind, LexErrorKind::TrailingJunkAfterNumber, "{src:?}");
}
// The accept side stays one Number token: valid radix, interior `_` across decimal,
// fraction, exponent, and radix bodies (the `1_000_000` regression the flip could
// otherwise have caused).
for src in [
"0x1F",
"1e5",
"1.5",
".5",
"1_000",
"1_000_000",
"0x1_F",
"1.5_5",
"1e1_0",
] {
let tokens = tokenize_with(src, &FeatureSet::SQLITE)
.unwrap_or_else(|err| panic!("{src:?} is a valid SQLite number: {err}"));
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::Number, "{src:?}");
assert_eq!(text(src, &tokens[0]), src, "{src:?} spans its whole text");
}
}
#[test]
fn radix_leading_underscore_is_dialect_data_not_baked_in() {
// Prove the axis rides the shared table, not a hard-wired scanner rule,
// by toggling it on both a default (SQLite: off) and an override. `0x_1F` — a radix
// body opening with `_` — rejects as trailing junk when off, and lexes as one hex
// Number when the same dialect flips the flag on.
let src = "0x_1F";
let off = tokenize_with(src, &FeatureSet::SQLITE)
.expect_err("SQLite rejects a leading-underscore radix body");
assert_eq!(off.kind, LexErrorKind::TrailingJunkAfterNumber);
let sqlite_with_leading =
FeatureSet::SQLITE.with(FeatureDelta::EMPTY.numeric_literals(NumericLiteralSyntax {
radix_leading_underscore: true,
..FeatureSet::SQLITE.numeric_literals
}));
let tokens = tokenize_with(src, &sqlite_with_leading)
.expect("the flag flip admits the leading-underscore radix body");
assert_eq!(tokens.len(), 1, "one Number token");
assert_eq!(tokens[0].kind, TokenKind::Number);
assert_eq!(text(src, &tokens[0]), src);
}
#[test]
fn sqlite_dollar_is_an_identifier_continuation_byte() {
// The measured rusqlite boundary (sqlite-lexer-under-acceptance-bundle): SQLite's
// IdChar set includes `$` as a *continuation* byte, so `L$C3`/`a$b`/`t$x`/`a$` are one
// identifier each, while a *leading* `$name` is the dollar-named placeholder and a lone
// `$` a stray byte. The fitted Sqlite preset carries `dollar_in_identifiers`.
for src in ["L$C3", "a$b", "t$x", "a$"] {
let tokens = tokenize_with(src, &FeatureSet::SQLITE)
.unwrap_or_else(|err| panic!("{src:?} is one SQLite identifier: {err}"));
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::Word, "{src:?}");
assert_eq!(text(src, &tokens[0]), src, "{src:?} spans its whole text");
}
// Leading `$name` stays the placeholder; a lone `$` is a stray byte.
let param = tokenize_with("$abc", &FeatureSet::SQLITE).expect("$abc is a placeholder");
assert_eq!(param[0].kind, TokenKind::Parameter);
assert_eq!(text("$abc", ¶m[0]), "$abc");
assert_eq!(
tokenize_with("$", &FeatureSet::SQLITE)
.expect_err("a lone $ is a stray byte")
.kind,
LexErrorKind::StrayByte,
);
// Dialect data, not baked in: the same SQLite preset with the flag off stops the word
// at the `$`, so `a$b` is the word `a` then the `$b` dollar-named placeholder.
let off =
FeatureSet::SQLITE.with(FeatureDelta::EMPTY.identifier_syntax(IdentifierSyntax {
dollar_in_identifiers: false,
..FeatureSet::SQLITE.identifier_syntax
}));
let toks = tokenize_with("a$b", &off).expect("`a` then `$b`");
assert_eq!(text("a$b", &toks[0]), "a", "the word stops at `$` when off");
assert_eq!(
text("a$b", &toks[1]),
"$b",
"`$b` is a placeholder when off"
);
assert_eq!(toks[1].kind, TokenKind::Parameter);
}
#[test]
fn sqlite_empty_quoted_identifier_lexes_in_every_style() {
// The measured rusqlite boundary: SQLite admits a zero-length quoted identifier in
// every quote style (`empty_quoted_identifiers`), unique among the shipped engines.
for src in ["``", "\"\"", "[]"] {
let tokens = tokenize_with(src, &FeatureSet::SQLITE)
.unwrap_or_else(|err| panic!("{src:?} is an empty SQLite quoted ident: {err}"));
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::QuotedIdent, "{src:?}");
assert_eq!(text(src, &tokens[0]), src, "{src:?} spans its delimiters");
}
// Dialect data, not baked in: the same SQLite preset with the flag off restores the
// universal zero-length reject for every style.
let off =
FeatureSet::SQLITE.with(FeatureDelta::EMPTY.identifier_syntax(IdentifierSyntax {
empty_quoted_identifiers: false,
..FeatureSet::SQLITE.identifier_syntax
}));
for src in ["``", "\"\"", "[]"] {
assert_eq!(
tokenize_with(src, &off)
.expect_err(&format!("{src:?} rejects with the flag off"))
.kind,
LexErrorKind::ZeroLengthDelimitedIdentifier,
"{src:?}",
);
}
}
#[test]
fn sqlite_block_comment_eof_and_nesting_match_the_engine() {
// The measured rusqlite boundary: SQLite silently closes an unterminated `/* …` at EOF
// (as long as a byte follows the `/*`) and does NOT nest block comments. The fitted
// Sqlite preset carries `unterminated_block_comment_at_eof` and `nested_block_comments`
// off. `1/*x` -> just the `1` (comment is trailing trivia); `/*x`/`/* a /* b */` are all
// trivia (empty token stream).
for src in ["/*x", "/* a /* b */", "/* x ", "/**"] {
let tokens = tokenize_with(src, &FeatureSet::SQLITE)
.unwrap_or_else(|err| panic!("{src:?}: {err}"));
assert!(tokens.is_empty(), "{src:?} is all trivia, got {tokens:?}");
}
let one = tokenize_with("1/*x", &FeatureSet::SQLITE).expect("1 then a closed comment");
assert_eq!(one.len(), 1, "just the `1`");
assert_eq!(one[0].kind, TokenKind::Number);
// A *bare* `/*` at EOF (no byte after `*`) is the `/` slash operator, not a comment.
assert_eq!(
tokenize_with("/*", &FeatureSet::SQLITE)
.expect("bare /* at EOF lexes as operators")
.iter()
.map(|t| t.kind)
.collect::<Vec<_>>(),
[
TokenKind::Operator(Operator::Slash),
TokenKind::Operator(Operator::Star),
],
);
// Non-nesting leaves the tail after the first `*/`: `/* a /* b */ c */` -> `c` then `*/`.
let tail = tokenize_with("/* a /* b */ c */", &FeatureSet::SQLITE).expect("tail leaks");
assert_eq!(
text("/* a /* b */ c */", &tail[0]),
"c",
"first `*/` closes the comment"
);
// Dialect data, not baked in: with the EOF flag off the same input is the hard error.
let off = FeatureSet::SQLITE.with(FeatureDelta::EMPTY.comment_syntax(CommentSyntax {
unterminated_block_comment_at_eof: false,
..FeatureSet::SQLITE.comment_syntax
}));
assert_eq!(
tokenize_with("1/*x", &off)
.expect_err("unterminated at EOF errors with the flag off")
.kind,
LexErrorKind::UnterminatedBlockComment,
);
}
#[test]
fn sqlite_numbered_question_parameter_matches_the_engine() {
// The measured rusqlite boundary: SQLite numbered `?NNN` parameters (`numbered_question`).
// The number is a maximal digit munch, so `?1abc` is `?1` then the identifier `abc`.
for src in ["?1", "?123", "?32766"] {
let tokens = tokenize_with(src, &FeatureSet::SQLITE)
.unwrap_or_else(|err| panic!("{src:?} is a numbered parameter: {err}"));
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::Parameter, "{src:?}");
assert_eq!(text(src, &tokens[0]), src, "{src:?} spans the whole `?NNN`");
}
let split = tokenize_with("?1abc", &FeatureSet::SQLITE).expect("?1abc lexes");
assert_eq!(
text("?1abc", &split[0]),
"?1",
"the number is a maximal munch"
);
assert_eq!(split[0].kind, TokenKind::Parameter);
assert_eq!(text("?1abc", &split[1]), "abc");
// Dialect data, not baked in: with numbering off but the anonymous `?` still on, `?1`
// splits into a bare `?` placeholder and the number `1`.
let off = FeatureSet::SQLITE.with(FeatureDelta::EMPTY.parameters(ParameterSyntax {
numbered_question: false,
..FeatureSet::SQLITE.parameters
}));
let anon = tokenize_with("?1", &off).expect("?1 lexes as `?` then `1`");
assert_eq!(
text("?1", &anon[0]),
"?",
"the `?` is the anonymous placeholder"
);
assert_eq!(anon[0].kind, TokenKind::Parameter);
assert_eq!(text("?1", &anon[1]), "1");
}
#[test]
fn unicode_escape_string_lexes_only_when_enabled() {
// Enabled: `U&'d\0061t\+000061'` is one String including the `U&` prefix;
// the `\` is body, not a quote escape.
let src = r"U&'a\0062''c'";
let tokens = tokenize_with(src, &UNICODE_STRING_FEATURES).expect("unicode strings lex");
assert_eq!(tokens.len(), 1);
assert_eq!(tokens[0].kind, TokenKind::String);
assert_eq!(text(src, &tokens[0]), src, "doubled '' stays in the body");
// Disabled (ANSI): `U` word, `&` operator, `'...'` string — three tokens.
assert_eq!(
lexed("U&'x'"),
[
(TokenKind::Word, "U"),
(TokenKind::Operator(Operator::Amp), "&"),
(TokenKind::String, "'x'"),
],
);
}
#[test]
fn unicode_escape_identifier_lexes_only_on_the_full_trigger() {
// Enabled: `U&"d\0061ta"` is one QuotedIdent spanning the `U&` prefix; the `\`
// is body (no escape decoding at lex time), and the case-insensitive `u&"` lead
// works too. Disjoint from the `U&'...'` string arm by the third byte.
for src in [r#"U&"d\0061ta""#, r#"u&"x""#, r#"U&"""""#] {
let tokens = tokenize_with(src, &UNICODE_STRING_FEATURES).expect("unicode ident lexes");
assert_eq!(tokens.len(), 1, "{src}");
assert_eq!(tokens[0].kind, TokenKind::QuotedIdent, "{src}");
assert_eq!(text(src, &tokens[0]), src, "{src} spans the `U&` prefix");
}
// The full `U&"` trigger is required: a bare `U&` before a non-quote stays a `U`
// word, a `&` operator, and the following token — `U` is identifier-start, so this
// arm must never steal a plain identifier followed by the `&` operator.
let bare = tokenize_with("U&1", &UNICODE_STRING_FEATURES).expect("U&1 lexes");
assert_eq!(
bare.iter()
.map(|t| (t.kind, text("U&1", t)))
.collect::<Vec<_>>(),
[
(TokenKind::Word, "U"),
(TokenKind::Operator(Operator::Amp), "&"),
(TokenKind::Number, "1"),
],
);
// Disabled (ANSI): `U&"x"` decomposes into a `U` word, `&` operator, and a plain
// quoted identifier — three tokens.
assert_eq!(
lexed(r#"U&"x""#),
[
(TokenKind::Word, "U"),
(TokenKind::Operator(Operator::Amp), "&"),
(TokenKind::QuotedIdent, r#""x""#),
],
);
}
#[test]
fn bit_string_lexes_as_one_string_token_only_when_enabled() {
// Enabled: `B'1010'` / `X'1FF'` are single String tokens spanning the marker,
// in either case. A malformed body (`X'1FG'`) still lexes — the digit check is
// deferred to the accessor, mirroring PostgreSQL.
for src in ["B'1010'", "b'1010'", "X'1FF'", "x'1ff'", "X'1FG'"] {
let tokens = tokenize_with(src, &BIT_STRING_FEATURES).expect("bit strings lex");
assert_eq!(tokens.len(), 1, "{src}");
assert_eq!(tokens[0].kind, TokenKind::String, "{src}");
assert_eq!(text(src, &tokens[0]), src, "{src} spans the marker");
}
// The quote must abut the marker: `B '1010'` is a word then a string.
assert_eq!(
tokenize_with("B '1010'", &BIT_STRING_FEATURES)
.expect("ok")
.iter()
.map(|t| t.kind)
.collect::<Vec<_>>(),
[TokenKind::Word, TokenKind::String],
);
// Disabled (ANSI): `B` is a word, `'1010'` a separate string.
assert_eq!(
lexed("B'1010'"),
[(TokenKind::Word, "B"), (TokenKind::String, "'1010'")],
);
}
#[test]
fn blob_literal_lexes_eagerly_validated_even_hex_only_when_enabled() {
// Enabled (SQLite): an even count of hex digits — including the zero-byte `x''`
// — is a single String token spanning the marker, in either marker case.
for src in ["x'53514C'", "X'53514c'", "x'1A'", "x''"] {
let tokens = tokenize_with(src, &BLOB_FEATURES).expect("even-hex blobs lex");
assert_eq!(tokens.len(), 1, "{src}");
assert_eq!(tokens[0].kind, TokenKind::String, "{src}");
assert_eq!(text(src, &tokens[0]), src, "{src} spans the marker");
}
// Zero new over-acceptance: an odd digit count or a non-hex body is an eager
// lex-time reject — SQLite's "unrecognized token" / MySQL's ER_PARSE_ERROR
// (probed) — with the error span covering the whole malformed lexeme.
for src in ["x'ABC'", "X'1FF'", "x'0'", "x'XY'"] {
let err = tokenize_with(src, &BLOB_FEATURES)
.expect_err(&format!("{src:?} is a malformed blob"));
assert_eq!(err.kind, LexErrorKind::MalformedBlobLiteral, "{src:?}");
assert_eq!(
err.span,
Span::new(0, src.len() as u32),
"{src:?} spans the whole lexeme",
);
}
// An unterminated body stays the unterminated-string reject, not a malformed
// blob (there is no closing quote to validate against).
assert_eq!(
tokenize_with("x'1A", &BLOB_FEATURES)
.expect_err("unterminated")
.kind,
LexErrorKind::UnterminatedString,
);
// The quote must abut the marker (`x '1A'` is a word then a string), and `B`
// never triggers the blob arm — with bit strings off it stays a word.
for src in ["x '1A'", "B'1010'"] {
assert_eq!(
tokenize_with(src, &BLOB_FEATURES)
.expect("ok")
.iter()
.map(|t| t.kind)
.collect::<Vec<_>>(),
[TokenKind::Word, TokenKind::String],
"{src}",
);
}
// Disabled (ANSI): `x` is a word, `'1A'` a separate string.
assert_eq!(
lexed("x'1A'"),
[(TokenKind::Word, "x"), (TokenKind::String, "'1A'")],
);
// The MySQL layout (both flags on): `x`/`X` resolves to the eager blob arm — the
// odd-hex body that a deferred bit-string would tolerate rejects — while `B'…'`
// keeps the deferred bit-string scan (`B'1FG'`-style bodies still lex; the digit
// check stays at the accessor).
assert_eq!(
tokenize_with("x'ABC'", &BLOB_AND_BIT_STRING_FEATURES)
.expect_err("the eager blob arm claims x")
.kind,
LexErrorKind::MalformedBlobLiteral,
);
let tokens =
tokenize_with("B'1012'", &BLOB_AND_BIT_STRING_FEATURES).expect("B stays deferred");
assert_eq!(tokens.len(), 1);
assert_eq!(tokens[0].kind, TokenKind::String);
}
#[test]
fn custom_identifier_start_need_not_also_be_continue() {
const FEATURES: FeatureSet = FeatureSet::ANSI.with(
FeatureDelta::EMPTY
.byte_classes(ByteClasses::STANDARD.with_class(b'@', CLASS_IDENTIFIER_START)),
);
let tokens = tokenize_with("@name", &FEATURES).expect("custom start byte lexes");
assert_eq!(tokens.len(), 1);
assert_eq!(tokens[0].kind, TokenKind::Word);
assert_eq!(text("@name", &tokens[0]), "@name");
}
#[test]
fn non_ascii_identifier_round_trips_through_its_span() {
// A word may start with and contain multi-byte UTF-8; the span must land
// on char boundaries so slicing does not panic.
let src = "café = δ";
let tokens = tokenize(src).expect("clean");
assert_eq!(tokens[0].kind, TokenKind::Word);
assert_eq!(text(src, &tokens[0]), "café");
assert_eq!(tokens[2].kind, TokenKind::Word);
assert_eq!(text(src, &tokens[2]), "δ");
}
#[test]
fn unicode_letters_start_and_continue_identifiers() {
// The identifier-start/continue policy is the Unicode *letter* property
// (`char::is_alphabetic`), not ASCII-only: accented Latin, Greek, and CJK
// letters all begin and continue an unquoted identifier as one Word.
for src in ["café", "naïve", "δ", "Ünüm", "表", "数据", "_underscore"] {
let tokens = tokenize(src).expect("Unicode-letter identifier lexes");
assert_eq!(tokens.len(), 1, "{src:?} is one word");
assert_eq!(tokens[0].kind, TokenKind::Word);
assert_eq!(text(src, &tokens[0]), src, "the whole word is the span");
}
}
#[test]
fn non_ascii_identifier_policy_is_dialect_data() {
let control = "\u{009c}";
let err = tokenize_with(control, &FeatureSet::ANSI)
.expect_err("ANSI restricts unquoted identifiers to Unicode letters");
assert_eq!(err.kind, LexErrorKind::StrayByte);
let tokens = tokenize_with(control, &FeatureSet::POSTGRES)
.expect("PostgreSQL admits every high-bit code point in an identifier");
assert_eq!(tokens.len(), 1);
assert_eq!(tokens[0].kind, TokenKind::Word);
assert_eq!(text(control, &tokens[0]), control);
}
#[test]
fn non_letter_code_points_are_not_identifier_starts() {
// A non-letter code point does not begin an identifier under the Unicode
// policy — deliberately stricter than a raw "any high byte" rule, which would
// silently fold an emoji or symbol into a word. The stray-byte span covers the
// whole character, so the error offset stays on a char boundary.
let err = tokenize("🎉").expect_err("an emoji is not an identifier start");
assert_eq!(err.kind, LexErrorKind::StrayByte);
assert_eq!(err.span, Span::new(0, 4), "span is the whole 4-byte char");
// A symbol mid-stream is likewise rejected (here U+00B0 DEGREE SIGN, 2 bytes,
// after a clean word and a space).
let err = tokenize("a °").expect_err("a symbol is not an identifier char");
assert_eq!(err.kind, LexErrorKind::StrayByte);
assert_eq!(err.span, Span::new(2, 4));
}
#[test]
fn digits_continue_but_never_start_an_identifier() {
// An ASCII digit never starts an identifier: a leading digit is the number,
// and the trailing letters are a separate word.
assert_eq!(
lexed("1abc"),
[(TokenKind::Number, "1"), (TokenKind::Word, "abc")],
);
// A Unicode digit (Arabic-Indic `٣`, which has the numeric property) continues
// an identifier but does not start one — mirroring ASCII digits.
let cont = tokenize("a٣").expect("a Unicode digit continues an identifier");
assert_eq!(cont.len(), 1);
assert_eq!(cont[0].kind, TokenKind::Word);
assert_eq!(text("a٣", &cont[0]), "a٣");
// Leading, that same digit neither starts an identifier nor (being non-ASCII)
// a number, so it is a stray byte.
let err = tokenize("٣").expect_err("a Unicode digit does not start an identifier");
assert_eq!(err.kind, LexErrorKind::StrayByte);
}
/// An ANSI-based preset with only the `$`-in-identifier policy flipped on, so the
/// effect is attributable to that one dialect-data knob and nothing else.
const DOLLAR_IDENT_FEATURES: FeatureSet =
FeatureSet::ANSI.with(FeatureDelta::EMPTY.identifier_syntax(IdentifierSyntax {
dollar_in_identifiers: true,
..IdentifierSyntax::ANSI
}));
#[test]
fn dollar_in_identifiers_is_dialect_data() {
// PostgreSQL accepts `$` as an identifier-continue character, so `foo$bar` is a
// single Word; `a$1` shows `$` and a digit both continuing after a letter start.
for src in ["foo$bar", "a$1"] {
let tokens =
tokenize_with(src, &FeatureSet::POSTGRES).expect("PG accepts `$` in identifiers");
assert_eq!(tokens.len(), 1, "{src:?} is one word under PostgreSQL");
assert_eq!(tokens[0].kind, TokenKind::Word);
assert_eq!(text(src, &tokens[0]), src);
}
// Strict ANSI forbids `$` in identifiers: `foo` ends at the `$`, which is then a
// stray byte (ANSI has no `$` lexeme). Same source, different acceptance — the
// difference is dialect *data*, not tree shape.
let err = tokenize("foo$bar").expect_err("ANSI forbids `$` in identifiers");
assert_eq!(err.kind, LexErrorKind::StrayByte);
assert_eq!(
err.span,
Span::new(3, 4),
"the `$` after `foo` is the stray byte"
);
// Flipping only the one knob on an ANSI base reproduces the PostgreSQL
// acceptance, proving it is exactly that knob doing the work.
let tokens =
tokenize_with("foo$bar", &DOLLAR_IDENT_FEATURES).expect("the flag alone enables `$`");
assert_eq!(tokens.len(), 1);
assert_eq!(text("foo$bar", &tokens[0]), "foo$bar");
}
#[test]
fn quoted_identifier_bypasses_the_unicode_identifier_policy() {
// Quoting accepts characters the unquoted policy rejects — an emoji, `$`,
// punctuation, spaces — as one QuotedIdent spanning the delimiters. The policy
// governs *unquoted* identifiers only.
for src in ["\"🎉\"", "\"a b$%\"", "\"δ\""] {
let tokens = tokenize(src).expect("a quoted identifier accepts anything");
assert_eq!(tokens.len(), 1, "{src:?} is one token");
assert_eq!(tokens[0].kind, TokenKind::QuotedIdent);
assert_eq!(text(src, &tokens[0]), src);
}
}
#[test]
fn empty_and_trivia_only_inputs_yield_no_tokens() {
assert!(tokenize("").expect("ok").is_empty());
assert!(tokenize(" \t\n ").expect("ok").is_empty());
assert!(tokenize("-- just a comment").expect("ok").is_empty());
assert!(
tokenize("/* only */ /* comments */")
.expect("ok")
.is_empty()
);
}
#[test]
fn unterminated_string_reports_a_lex_error_with_the_full_span() {
let src = "SELECT 'it''s";
let err = tokenize(src).expect_err("the literal never closes");
assert_eq!(err.kind, LexErrorKind::UnterminatedString);
// Span runs from the opening quote to end of input.
assert_eq!(err.span, Span::new(7, 13));
assert_eq!(
&src[err.span.start() as usize..err.span.end() as usize],
"'it''s"
);
}
#[test]
fn other_unterminated_constructs_report_their_kinds() {
assert_eq!(
tokenize(r#""abc"#).expect_err("no close").kind,
LexErrorKind::UnterminatedQuotedIdent,
);
assert_eq!(
tokenize("/* open").expect_err("no close").kind,
LexErrorKind::UnterminatedBlockComment,
);
assert_eq!(
tokenize("/* a /* b */")
.expect_err("inner closes, outer does not")
.kind,
LexErrorKind::UnterminatedBlockComment,
);
assert_eq!(
tokenize_with("$tag$ body", &FeatureSet::POSTGRES)
.expect_err("no close")
.kind,
LexErrorKind::UnterminatedDollarQuote,
);
}
#[test]
fn stray_byte_reports_an_error_at_its_offset() {
// `@` is in no lexical class.
let err = tokenize("a @ b").expect_err("@ is stray");
assert_eq!(err.kind, LexErrorKind::StrayByte);
assert_eq!(err.span, Span::new(2, 3));
// A `$` that opens no valid dollar-quote is also stray.
let err = tokenize("$ x").expect_err("lone $ is stray");
assert_eq!(err.kind, LexErrorKind::StrayByte);
assert_eq!(err.span, Span::new(0, 1));
}
#[test]
fn token_cursor_peeks_advances_and_seeks_for_backtracking() {
let tokens = tokenize("d , b").expect("clean");
let mut cursor = TokenCursor::new(&tokens);
assert_eq!(cursor.remaining(), 3);
assert_eq!(cursor.peek().map(|t| t.kind), Some(TokenKind::Word));
assert_eq!(
cursor.peek_nth(1).map(|t| t.kind),
Some(TokenKind::Punctuation(Punctuation::Comma)),
);
let checkpoint = cursor.pos();
assert_eq!(cursor.advance().map(|t| t.kind), Some(TokenKind::Word));
assert_eq!(
cursor.advance().map(|t| t.kind),
Some(TokenKind::Punctuation(Punctuation::Comma))
);
assert_eq!(cursor.pos(), 2);
// Rewind to the checkpoint and re-read — the speculation seam.
cursor.seek(checkpoint);
assert_eq!(cursor.pos(), 0);
assert_eq!(cursor.advance().map(|t| t.kind), Some(TokenKind::Word));
// Drain to the end.
cursor.seek(tokens.len());
assert!(cursor.is_eof());
assert_eq!(cursor.remaining(), 0);
assert_eq!(cursor.advance(), None);
}
#[test]
fn buffered_token_cursor_grows_for_lookahead_and_rewinds() {
let mut cursor = BufferedTokenCursor::streaming("d.*", &FeatureSet::ANSI)
.expect("streaming cursor starts");
assert_eq!(cursor.buffered_len(), 0, "no token is scanned up front");
let checkpoint = cursor.pos();
assert_eq!(
cursor
.peek_nth(2)
.expect("lookahead scans")
.map(|token| token.kind),
Some(TokenKind::Operator(Operator::Star)),
"lookahead can cross the current buffer boundary",
);
assert_eq!(cursor.pos(), checkpoint, "lookahead does not consume");
assert_eq!(cursor.buffered_len(), 3, "lookahead grew the buffer");
assert_eq!(
cursor
.advance()
.expect("first token")
.map(|token| token.kind),
Some(TokenKind::Word),
);
assert_eq!(
cursor
.advance()
.expect("second token")
.map(|token| token.kind),
Some(TokenKind::Punctuation(Punctuation::Dot)),
);
cursor.seek(checkpoint);
assert_eq!(
cursor
.advance()
.expect("rewound token")
.map(|token| token.kind),
Some(TokenKind::Word),
"checkpoint rewind reuses the retained lazy buffer",
);
}
#[test]
fn buffered_token_cursor_replays_lex_error_after_speculation() {
let mut cursor = BufferedTokenCursor::streaming("foo\"", &FeatureSet::ANSI)
.expect("streaming cursor starts");
let checkpoint = cursor.pos();
let first = cursor
.peek_nth(1)
.expect_err("lookahead reaches the unterminated identifier");
assert_eq!(first.kind, LexErrorKind::UnterminatedQuotedIdent);
cursor.seek(checkpoint);
assert_eq!(
cursor
.advance()
.expect("buffered word")
.map(|token| token.kind),
Some(TokenKind::Word),
);
let replayed = cursor
.peek()
.expect_err("the lexical fault remains after rewind and buffered consumption");
assert_eq!(replayed, first);
}
#[test]
fn buffered_token_cursor_discards_consumed_statement_tokens() {
let mut cursor = BufferedTokenCursor::streaming("SELECT 1; SELECT 2", &FeatureSet::ANSI)
.expect("streaming cursor starts");
assert_eq!(
cursor.advance().expect("select").map(|token| token.kind),
Some(TokenKind::Keyword(Keyword::Select)),
);
assert_eq!(
cursor.advance().expect("literal").map(|token| token.kind),
Some(TokenKind::Number),
);
assert_eq!(
cursor.advance().expect("semicolon").map(|token| token.kind),
Some(TokenKind::Punctuation(Punctuation::Semicolon)),
);
assert_eq!(
cursor
.peek()
.expect("next statement")
.map(|token| token.kind),
Some(TokenKind::Keyword(Keyword::Select)),
"separator scanning may already hold one next-statement lookahead",
);
cursor.discard_consumed();
assert_eq!(
cursor.buffered_len(),
1,
"only the next statement lookahead remains buffered",
);
assert_eq!(
cursor
.peek()
.expect("retained token")
.map(|token| token.kind),
Some(TokenKind::Keyword(Keyword::Select)),
);
}
#[test]
fn lex_error_displays_its_kind_and_range() {
let err = LexError::new(LexErrorKind::StrayByte, Span::new(2, 3));
assert_eq!(err.to_string(), "stray byte at bytes 2..3");
}
#[test]
fn opt_in_trivia_capture_records_kinds_and_spans() {
use TriviaKind::{BlockComment, LineComment, Whitespace};
// Whitespace, a block comment, and a line comment between the lexemes, each a
// distinct trivia run with its exact source span. `d`/`b` are not keywords in
// the inventory, so they lex as plain words.
let src = "d /*c*/ b -- tail\n";
let (tokens, trivia) =
tokenize_with_trivia(src, &FeatureSet::ANSI).expect("clean tokenization");
// Token stream is exactly the two words — no trivia leaked in.
assert_eq!(
tokens.iter().map(|t| t.kind).collect::<Vec<_>>(),
[TokenKind::Word, TokenKind::Word],
);
// Every trivia run, in source order, with kind and the text its span slices.
let recorded: Vec<_> = trivia
.all()
.iter()
.map(|r| {
let span = r.span();
(r.kind(), &src[span.start() as usize..span.end() as usize])
})
.collect();
assert_eq!(
recorded,
[
(Whitespace, " "),
(BlockComment, "/*c*/"),
(Whitespace, " "),
(Whitespace, " "),
(LineComment, "-- tail"),
(Whitespace, "\n"),
],
);
}
#[test]
fn trivia_capture_leaves_the_token_stream_identical() {
// The opt-in capture must not perturb the token stream: the tokens are the
// same with capture on as with it off (trivia stays strictly out-of-band).
let src = "SELECT /* x */ a,\n -- note\n b FROM t";
let plain = tokenize_with(src, &FeatureSet::ANSI).expect("plain lex");
let (with_capture, trivia) =
tokenize_with_trivia(src, &FeatureSet::ANSI).expect("captured lex");
assert_eq!(
plain, with_capture,
"capture must not change the token stream"
);
assert!(!trivia.is_empty(), "the comments/whitespace were captured");
}
#[test]
fn tokens_and_trivia_tile_the_source_without_overlap() {
// Strong invariant: every source byte belongs either to exactly one token or
// to exactly one trivia run — tokens and trivia partition the input. This is
// the precise sense in which trivia is "out-of-band yet offset-recoverable".
let src = " SELECT /*c*/ a, -- z\n 'lit' ";
let (tokens, trivia) =
tokenize_with_trivia(src, &FeatureSet::ANSI).expect("clean tokenization");
// Merge token and trivia spans, sort by start, and walk: each must begin
// exactly where the previous ended, covering [0, len) with no gap or overlap.
let mut spans: Vec<Span> = tokens.iter().map(|t| t.span).collect();
spans.extend(trivia.all().iter().map(|r| r.span()));
spans.sort_by_key(|s| (s.start(), s.end()));
let mut cursor = 0u32;
for span in &spans {
assert_eq!(span.start(), cursor, "no gap or overlap before {span:?}");
cursor = span.end();
}
assert_eq!(cursor, src.len() as u32, "spans cover the whole source");
}
#[test]
fn trivia_free_source_captures_an_empty_index() {
// Capture on, but no comments/whitespace to record between the tokens: the
// index is empty (and an empty index owns an empty, unallocated `Vec`).
let (tokens, trivia) =
tokenize_with_trivia("a+b", &FeatureSet::ANSI).expect("clean tokenization");
assert_eq!(tokens.len(), 3);
assert!(trivia.is_empty(), "no trivia between adjacent lexemes");
}
}