mindfork 0.11.1

A terminal AI chat written in Rust: local models via llama.cpp or OpenAI, Anthropic, Gemini and Grok in the cloud, with persistent memory, notes, RAG and tools.
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
%YAML 1.2
---
version: 2
# http://www.sublimetext.com/docs/3/syntax.html
name: Elixir
scope: source.elixir

file_extensions: [ex, exs]
first_line_match: ^#!\s*/.*\b(?:elixirc?|iex)

authors:
  # Contributed several times and later on finished a complete rewrite with lots of new features.
  - Aziz Köksal <aziz.koeksal@gmail.com>

  # Transformed elixir-tmbundle to a sublime-syntax file with additional improvements.
  - Po Chen <chenpaul914@gmail.com>

  # Created elixir-tmbundle by basing off of the Textmate bundle for Ruby (by James Edward Gray II).
  - José Valim <jose.valim@dashbit.co>

variables:
  module_name: '[A-Z][a-zA-Z\d_]*+'
  atom_id_suffix: '[\w@]*+[?!]?+'
  atom_id: (?>[[:alpha:]_]{{atom_id_suffix}})
  identifier: (?>[[:lower:]_]\w*+[?!]?+)
  no_id_key_suffix: (?=\b(?!{{atom_id_suffix}}:(?!:)|[?!])|(?<=[?!])(?!:(?!:)))
  no_key_suffix: (?!{{atom_id_suffix}}:(?!:))
  no_colon_suffix: (?![:'"[:alpha:]])

  # NB: the keywords are ordered by number of occurrences in the compiler source code of Elixir.
  # Stats can be produced using `Makeup.Lexers.ElixirLexer.lex()`.
  # To be more accurate "scripts/count_tokens_by_scope.py" can be used.
  # Run `cat lib/**/*.ex > all.ex` to join all source files into a single file.
  closing_keyword: (?>end|else|after|rescue|catch){{no_id_key_suffix}}
  closing_token: (?>[)}\];#]|>>(?!>)|%>|$|{{closing_keyword}})

  binary_operator: |
    (?x)(?>
      (?<!\s)\[ | <?<~ | ~>>? | <~> | [*=/|\\>] | \.(?!\.\.) | <(?!<[^<~] | %=)
    | \^{3} | && | (?<!\s)[-+]\S | [-+][-+>\s] | ::(?!:) | $ | !=
    | (?>when|and|in|or|not\s+in){{no_id_key_suffix}}
    )

  operator: |
    (?x)(?>
      =~ | ={1,3} | @ | <?\|> | (?!<%=)[<-](?![~=] | < | --)[->]? | \|{1,3} | ::
    | &{1,3} | \+{1,3} | \\\\ | \^{3} | \^ | \.{1,3} | !={0,2} | [<>]= | \*\*?
    | >>> | >(?!>) | // | / | <<< | ~~~ | <~> | <?<~ | ~>>? | ---
    )

  has_arguments: |
    (?x)(?=
      \(
    | \s* (?!\?:) (?>{{operator}}: {{no_colon_suffix}} | \\(?!\\))
    | (?! \s* (?>, | {{closing_token}} | {{binary_operator}}) )
    )
  no_suffix_then_arguments: '{{no_id_key_suffix}}{{has_arguments}}'

  atom_symbol: (?>{{atom_id}}|<<>>|\.\.//|{{operator}}|%?{}|%)
  member: (?>{{identifier}}|{{operator}}|{})

  # This regex is used to recognize Elixir inside Markdown comments.
  # The rules have been commented out as the recognition wasn't reliable enough.
  # It was incomplete, maybe distracting and speed may have been affected, too.
  # It can be re-enabled by uncommenting all lines containing "{{is_markdown_elixir}}".
  is_markdown_elixir: |
    (?x)
    ```\s*elixir\b
    | (?<=\s{4})(?>
        (?>iex|\.{3}).*?>
      | (?>
          def(?>module|impl|macro|guard|struct|delegate|protocol|p)?
        | (?#if|for|)unless|case|cond|with|try|receive|quote|unquote(?:_splicing)?
        ){{no_id_key_suffix}}
        (?>\(|.+\bdo\b)
      )

contexts:
  main:
    - include: core_syntax

  prototype:
    # NB: rules in the prototype context will always match before any other.
    - include: merge_conflict # Merge conflict markers can occur anywhere.
    # Commented out: see "is_markdown_elixir" above.
    # - include: iex_dots       # iex prompt continuation: ...>

  core_syntax:
    # The rules are approximately ordered by their likeliness of occurrence
    # in Elixir's and Phoenix's source code.
    # Some rules have to come before others in any case, like atom_keyword for example.
    - include: atom_keyword            # key:
    - include: def_blocks              # def f(a, b, c)
    - include: elixir_keywords         # when, true, and, else ...
    - include: built_ins               # is_binary(x)
    - include: alias                   # alias M
    - include: fn_block                # fn a -> a end
    - include: elixir_functions        # if, case, with
    - include: special_form            # __MODULE__, __ENV__
    - include: do_block                # do end
    - include: sql_or_fragment         # sql("SELECT * FROM t"); fragment("meta->>key")
    - include: modules_or_ids_or_calls # Module, id, M.f(), x.y()
    - include: comma_and_skip_ws       # ,
    - include: operator                # a == b
    - include: atom_symbol             # :key
    - include: string                  # "x", << x >>, ~w[x]a, ~r/x/
    - include: dot_accessor            # .x
    - include: tuple                   # {a, b, c}
    - include: item_access             # x[y]
    - include: list                    # [a, b, c]
    - include: module_attribute        # @type t, @spec s, @doc "D"
    - include: numeric                 # 1, 2.0e3, 0xFF, 0b10, 0o7
    - include: map                     # %{}, %M{}
    - include: comment                 # # ...
    - include: capture                 # &Module.f/1, &(&1<>&2)
    - include: char_literal            # ?x
    - include: paren                   # (x)
    - include: line_continuation       # \

  merge_conflict:
    - match: ^(?=<{7}|={7}|>{7})
      push:
        - clear_scopes: true
        - match: (?>(<+)(.*\n)|(=+)(.*\n)|(>+)(.*\n))
          scope: text.git.conflict
          captures:
            1: punctuation.section.block.begin.git.conflict
            2: comment.line.current.git.conflict
            3: punctuation.section.block.middle.git.conflict
            4: comment.line.middle.git.conflict
            5: punctuation.section.block.end.git.conflict
            6: comment.line.incoming.git.conflict
          pop: 1

  ## Functions

  fn_block:
    - match: fn{{no_id_key_suffix}}
      scope: punctuation.section.block.begin.elixir keyword.other.fn.elixir
      push: [fn_block_end_pop, arrow_clauses_body_pop, fn_single_body_or_pop]

  fn_single_body_or_pop:
    - match: (?=->)
      set:
        - match: (?=end{{no_id_key_suffix}})
          pop: 1
        - include: core_syntax
    - include: if_non_space_or_eol_pop

  fn_block_end_pop:
    - include: block_end_pop
    - include: if_closing_token_pop
    - include: core_syntax

  arrow_clauses_body_pop:
    - match: (?=\S)
      set:
        - match: (?=->|when{{no_id_key_suffix}})
          push: inlined_core_syntax_pop
        # NB: no default parameters in arrow clauses
        - match: \\\\(?!:{{no_colon_suffix}})
          scope: keyword.operator.default.elixir invalid.illegal.default-operator.elixir
        - include: parameters_or_if_closing_pop
    - match: $
      set:
        - include: if_closing_token_pop
        - match: ^(\s*)(?=[^#\s])
          push: [indented_core_syntax_pop, params_until_arrow_pop]
        - include: core_syntax

  inlined_core_syntax_pop:
    - match: (?=;)
      pop: 1
    - include: core_syntax_or_if_closing_pop

  params_until_arrow_pop:
    - match: (?=->|when{{no_id_key_suffix}})
      pop: 1
    - include: parameters_or_if_closing_pop

  indented_core_syntax_pop:
    - match: ^(?=\1[^#\s]|(?!\1)(?!\s*(#|$)))
      pop: 1
    - include: core_syntax_or_if_closing_pop

  function_header_pop:
    - match: \(
      scope: punctuation.section.arguments.begin.elixir
      set:
        - meta_scope: meta.function-call.arguments.elixir
        - include: arguments_closing_pop
        - match: ''
          push: function_header_args_pop
    - match: \s?
      scope: punctuation.section.arguments.begin.elixir
      set:
        - meta_scope: meta.function-call.arguments.elixir
        - include: arguments_ws_closing_pop
        - match: ''
          push: function_header_args_pop

  function_header_args_pop:
    - match: (?={{atom_symbol}}:(?!:))|(?<=[^:]:)
      set: arguments_ws_rest_pop

    - match: (?=unquote\(\s*:(?!:(?!:)))
      set: [function_params_or_eol_pop, function_atom_name_pop, unquote_pop]

    - match: (?=unquote(?:_splicing)?\()
      set: [function_params_or_eol_pop, arguments_pop, unquote_pop]

    - match: not{{no_suffix_then_arguments}}
      scope: entity.name.function.elixir
      set: function_free_form_header_pop

    - include: atom_keyword
    - include: func_do_block_pop
    - include: block_or_keyword
    - include: paren_param

    - match: '{{identifier}}(?=\s){{has_arguments}}'
      scope: entity.name.function.elixir
      set: function_free_form_header_pop

    - match: '{{identifier}}(?=\s*([(),;]|do{{no_id_key_suffix}}|$))'
      comment: e.g. "def func," OR "def func(a, b)" OR "def func do" OR "def func"
      scope: entity.name.function.elixir
      set: function_params_or_eol_pop

    - match: (?=\S)
      set:
        - match: |
            (?x)
            (?>((?=_)(?<!%>){{identifier}})|({{identifier}}))?
            \s*
            ({{operator}}|(?>and|in|or){{no_id_key_suffix}})
          comment: e.g. x && y
          captures:
            1: variable.parameter.unused.elixir
            2: variable.parameter.elixir
            3: entity.name.function.elixir
          set:
            - match: (?=,|when{{no_id_key_suffix}})
              set: arguments_ws_rest_pop
            - include: func_do_block_pop
            - include: if_ws_closing_token_or_eol_pop
            - include: paren_param
            - include: parameters_or_if_closing_pop
            - include: if_non_space_pop
        - include: if_ws_closing_token_or_eol_pop
        - include: paren
        - match: \{(?!}:(?!:(?!:)))
          scope: entity.name.function.elixir
          set:
            - meta_scope: meta.function.parameters.elixir
            - match: \}
              scope: entity.name.function.elixir
              set: arguments_ws_rest_pop
            - include: parameters_or_if_closing_pop
        - include: parameters_or_if_closing_pop
        - include: function_params_or_eol_pop

  function_free_form_header_pop:
    # E.g.: "def func a, b do {a, b} end"
    - match: (?={{atom_symbol}}:(?!:))|(?<=[^:]:)|(?=when{{no_id_key_suffix}})
      comment: break at first atom key (usually "do:")
      set: arguments_ws_rest_pop
    - include: arg_comma_and_skip_ws
    - include: function_ws_params_pop

  function_ws_params_pop:
    - include: func_do_block_pop
    - include: if_ws_closing_token_or_eol_pop
    - include: paren
    - include: parameters_or_if_closing_pop
    - include: if_non_space_pop

  function_params_or_eol_pop:
    - match: \(
      scope: punctuation.definition.parameters.begin.elixir
      set:
        - meta_scope: meta.function.parameters.elixir
        - match: \)
          scope: punctuation.definition.parameters.end.elixir
          set: function_body_pop
        - include: parameters_or_if_closing_pop
    - match: (?=,)
      set: arguments_ws_rest_pop
    - include: func_do_block_pop
    - include: if_ws_closing_token_or_eol_pop
    - include: if_non_space_pop

  function_body_pop:
    - include: func_do_block_pop
    - include: arguments_ws_rest_pop

  function_atom_name_pop:
    - match: \(
      scope: punctuation.section.arguments.begin.elixir
      set:
        - meta_scope: meta.function-call.arguments.elixir
        - include: arguments_closing_pop
        - match: ':'
          scope: constant.other.symbol.elixir punctuation.definition.constant.begin.elixir
          push:
            - match: (?=<%)|{{atom_symbol}}
              scope: constant.other.symbol.elixir entity.name.function.elixir
              pop: 1
            - match: (["'])
              scope: punctuation.definition.constant.begin.elixir
              set:
                - meta_scope: constant.other.symbol.quoted.elixir
                - match: \1
                  scope: punctuation.definition.constant.end.elixir
                  pop: 1
                - include: interpolated_elixir
                - match: \\?.|\n
                  scope: entity.name.function.elixir
            - include: if_empty_pop
        - include: core_syntax

  ## Parameters

  parameters:
    - include: block_or_keyword
    - include: special_form
    - match: (?=\^\s*{{identifier}}{{no_key_suffix}}(?!\s*\.(?!\.))(?!{{has_arguments}}))
      push:
        - include: operator
        - include: identifier_pop
    - match: |
        (?x)
        (?>((?=_)(?<!%>){{identifier}})|({{identifier}}))
        (?=\s*(?>[,;)}\]=|\\]|\.\.|::|->|<-|<>|>>|(?>when|do){{no_id_key_suffix}}|$))
      captures:
        1: variable.parameter.unused.elixir
        2: variable.parameter.elixir
    - match: (?=\\\\(?!:{{no_colon_suffix}})|<-)
      push:
        - match: (?=,|(?>when|do){{no_id_key_suffix}}|do:(?!:))
          pop: 1
        - include: core_syntax_or_if_closing_pop
    - include: paren_param
    - include: list_param
    - include: tuple_param
    - include: map_param
    - include: binary_string_param
    # Use last_id_argument here to avoid matching `@attr do` as an entity symbol.
    - include: last_id_argument
    - include: core_syntax

  parameters_or_if_closing_pop:
    - include: if_closing_token_pop
    - include: parameters

  paren_param:
    - match: (?=\()
      push: paren_param_pop

  paren_param_pop:
    - match: \(
      scope: punctuation.definition.parameters.begin.elixir
      set:
        - meta_scope: meta.function.parameters.elixir
        - match: \)
          scope: punctuation.definition.parameters.end.elixir
          pop: 1
        - include: parameters_or_if_closing_pop

  tuple_param:
    - match: \{
      scope: punctuation.section.sequence.begin.elixir
      push:
        - meta_scope: meta.sequence.tuple.elixir
        - match: \}
          scope: punctuation.section.sequence.end.elixir
          pop: 1
        - include: parameters_or_if_closing_pop

  list_param:
    - match: \[
      scope: punctuation.section.brackets.begin.elixir
      push:
        - meta_scope: meta.brackets.elixir
        - match: \]
          scope: punctuation.section.brackets.end.elixir
          pop: 1
        - include: cons_operator
        - include: parameters_or_if_closing_pop

  map_param:
    - match: \%
      scope: punctuation.section.mapping.begin.elixir
      push:
        - meta_scope: meta.mapping.elixir
        - match: (?=unquote\()
          set: [map_param_body_pop, arguments_pop, unquote_pop]
        - include: alias_names
        - match: ((?=_)(?<!%>){{identifier}})|({{identifier}})
          captures:
            1: variable.parameter.unused.elixir
            2: variable.parameter.elixir
          set: map_param_body_pop
        - include: map_param_body_pop

  map_param_body_pop:
    - match: \{
      scope: punctuation.section.mapping.begin.elixir
      set:
        - include: map_closing_pop
        - include: parameters_or_if_closing_pop
    - include: if_closing_token_pop
    - include: if_non_space_or_eol_pop

  binary_string_param:
    - include: stray_binary_end
    - match: <<(?![<~])
      scope: string.other.binary.elixir punctuation.definition.string.begin.elixir
      push:
        - meta_scope: meta.string.binary.elixir
        - include: binary_string_body_pop
        - include: parameters_or_if_closing_pop

  ## Types

  spec_definition_pop:
    - match: (?=\()
      set: [type_definition_next_pop, type_arguments_or_pop]
    - include: type_definition_next_pop

  spec_op_definition_pop:
    # E.g.: @spec integer &&& integer :: integer
    - meta_scope: meta.type.elixir
    - match: (?x)((?!::|\.(?!\.)){{operator}} | (?>and|in|or|not){{no_id_key_suffix}})
      scope: variable.other.spec.elixir
      set: spec_op_definition_next_pop
    - include: if_types_end_pop
    - include: spec_op_definition_next_pop

  spec_op_definition_next_pop:
    - meta_scope: meta.type.elixir
    - include: return_types_pop
    - match: (?=[,;])
      pop: 1
    - include: types_or_if_closing_pop
    - include: if_non_space_pop

  type_definition_pop:
    - meta_scope: meta.type.elixir
    - match: (?=\()
      set: [type_definition_next_pop, type_params_pop]
    - include: type_definition_next_pop

  type_params_pop:
    - match: \(
      scope: punctuation.definition.parameters.begin.elixir
      set:
        - meta_scope: meta.type.elixir meta.function.parameters.elixir
        - match: \)
          scope: punctuation.definition.parameters.end.elixir
          pop: 1
        - include: parameters_or_if_closing_pop

  type_definition_next_pop:
    - include: comments
    - include: return_types_pop
    - include: if_non_space_pop

  types:
    - match: (?=unquote(?:_splicing)?\()
      push: [type_arguments_or_pop, arguments_pop, unquote_pop]
    - include: block_or_keyword
    - include: special_form
    - match: |
        (?x)(
          (?> # Ordered by likeliness of occurrence.
            binary|term|atom|any|integer|boolean|map|list|module
          | (?>(?:non_)?neg_|pos_)?integer|no_return|pid|iodata
          | number|float|keyword|timeout|tuple|reference|charlist
          | fun(?:ction)?|char|struct|arity|port|identifier|none\b|mfa
          | node|byte|as_boolean|bitstring|iolist|maybe_improper_list
          | (?>
              nonempty_char
            | (?>(?:nonempty_)?maybe_|nonempty_)improper_|nonempty_
            )?list
          )
        ){{no_id_key_suffix}}
        | (?>(optional)|(required)|(record))(?={{no_suffix_then_arguments}})
        | ({{identifier}})
      captures:
        1: support.type.elixir
        2: keyword.other.optional.elixir
        3: keyword.other.required.elixir
        4: keyword.other.record.elixir
        5: storage.type.custom.elixir
      push: type_arguments_or_pop
    - include: list_type
    - include: tuple_type
    - include: module_name
    - include: dot_type
    - include: map_type
    - include: binary_string_type
    - include: paren_type
    - include: comments

  types_or_if_closing_pop:
    - include: types
    - include: if_closing_token_pop
    - include: core_syntax

  dot_type:
    - match: \.(?!\.)
      scope: punctuation.accessor.dot.elixir
      push:
        - include: unquote_call_pop
        - match: '{{identifier}}{{no_id_key_suffix}}'
          scope: storage.type.remote.elixir
          set: type_arguments_or_pop
        - include: if_non_space_or_eol_pop

  type_arguments_or_pop:
    - match: \(
      scope: punctuation.section.arguments.begin.elixir
      set:
        - meta_scope: meta.type.elixir meta.function-call.arguments.elixir
        - include: arguments_closing_pop
        - include: arg_comma_and_skip_ws
        - include: named_type
        - include: types_or_if_closing_pop
    - include: if_empty_pop

  named_type:
    - include: block_or_keyword
    - match: '{{identifier}}(?=\s*::(?!:))'
      scope: variable.other.named-type.elixir

  return_types_pop:
    - match: ::(?!:)
      scope: meta.type.elixir keyword.operator.colon.elixir
      set: [first_and_next_types_pop, if_non_space_pop]

  if_types_end_pop:
    # Avoid matching e.g. "def func" while typing the return types.
    - match: (?=when{{no_id_key_suffix}}|@?{{identifier}}{{no_id_key_suffix}}\s{{has_arguments}})
      comment: no when clause directly after "::"
      pop: 1
    - include: if_closing_token_pop

  first_and_next_types_pop:
    - include: comments
    - include: if_types_end_pop
    - match: (?=\S)
      set:
        - meta_scope: meta.type.elixir
        # NB: we can't prevent matching more than one type expression.
        # Just move to next_types_pop after the first newline, `|`, `::` or `when`.
        - match: (?=$|\|(?![:|>])|::(?!:)|when{{no_id_key_suffix}})
          set: [next_types_pop, if_non_space_pop]
        - match: (?=[,;])
          pop: 1
        - include: named_type
        - include: types_or_if_closing_pop

  next_types_pop:
    - include: comments
    - match: when{{no_id_key_suffix}}
      scope: keyword.operator.when.elixir
      set: [when_types_clause_pop, invalid_comma_or_non_space_pop]
    - match: (\|(?![:|>]))|(::(?!:))
      captures:
        1: keyword.operator.union.elixir
        2: keyword.operator.colon.elixir
      push: [if_non_space_pop, union_types_eol_pop, if_non_space_pop]
    - include: if_ws_closing_token_or_eol_pop
    - include: if_non_space_or_eol_pop

  union_types_eol_pop:
    - meta_scope: meta.type.elixir
    - include: comments
    - match: |
        (?x)(?=
          [,;] | \|(?![:|>]) | ::(?!:) | when{{no_id_key_suffix}}
        | @?{{identifier}}{{no_id_key_suffix}}\s{{has_arguments}}
        )
      pop: 1
    - include: named_type
    - include: types_or_if_closing_pop
    - include: if_ws_closing_token_or_eol_pop
    - include: if_non_space_or_eol_pop

  when_types_clause_pop:
    - meta_scope: meta.type.elixir
    - include: comments
    - include: atom_keyword
    - match: (?<=[^:]:)
      comment: skips to keyword value
      push: [when_var_pop, invalid_comma_or_non_space_pop]
    - include: comma_and_skip_ws
    - include: if_ws_closing_token_or_eol_pop
    - include: if_non_space_or_eol_pop

  when_var_pop:
    - match: var{{no_id_key_suffix}}(?!{{has_arguments}})
      comment: "`var` is a particular support type. Elixir recognizes it only after a `when`."
      scope: support.type.elixir
    - include: first_and_next_types_pop

  paren_type:
    - match: \(
      scope: punctuation.section.group.begin.elixir
      push:
        - meta_scope: meta.parens.elixir
        - match: \)
          scope: punctuation.section.group.end.elixir
          pop: 1
        - include: named_type
        - include: types_or_if_closing_pop

  tuple_type:
    - match: \{
      scope: punctuation.section.sequence.begin.elixir
      push:
        - meta_scope: meta.braces.elixir
        - match: \}
          scope: punctuation.section.sequence.end.elixir
          pop: 1
        - include: named_type
        - include: types_or_if_closing_pop

  list_type:
    - match: \[
      scope: punctuation.section.brackets.begin.elixir
      push:
        - meta_scope: meta.brackets.elixir
        - match: \]
          scope: punctuation.section.brackets.end.elixir
          pop: 1
        - include: named_type
        - include: types_or_if_closing_pop

  map_type:
    - match: \%
      scope: punctuation.section.mapping.begin.elixir
      push:
        - meta_scope: meta.mapping.elixir
        - match: (?=unquote\()
          set: [map_type_body_pop, arguments_pop, unquote_pop]
        - include: alias_names
        - match: _(?>{{module_name}}|{{identifier}})?
          scope: variable.type.unused.elixir invalid.illegal.struct.elixir
          set: map_type_body_pop
        - include: map_type_body_pop

  map_type_body_pop:
    - match: \{
      scope: punctuation.section.mapping.begin.elixir
      set:
        - include: map_closing_pop
        - include: named_type
        - include: types_or_if_closing_pop
    - include: if_closing_token_pop
    - include: if_non_space_or_eol_pop

  binary_string_type:
    - include: stray_binary_end
    - match: <<(?![<~])
      scope: string.other.binary.elixir punctuation.definition.string.begin.elixir
      push:
        - meta_scope: meta.string.binary.elixir
        - include: binary_string_body_pop
        - include: named_type
        - include: types_or_if_closing_pop

  # Numeric

  numeric:
    - match: |
        (?x)(?>
          (0 (x) \h (?:_?\h)*)
        | (0 (b) [01] (?:_?[01])*)
        | (0 (o) [0-7] (?:_?[0-7])*)
        | (\d(?:_?\d)* (\.) \d(?:_?\d)* (?:[eE] [-+]? \d(?:_?\d)*)?)
        | (\d(?:_?\d)*)
        )([_\d]*)
      comment: (?:_?\d)* because double or trailing '_' are invalid
      captures:
        1: constant.numeric.hex.elixir
        2: punctuation.separator.numeric.elixir
        3: constant.numeric.binary.elixir
        4: punctuation.separator.numeric.elixir
        5: constant.numeric.octal.elixir
        6: punctuation.separator.numeric.elixir
        7: constant.numeric.float.elixir
        8: punctuation.separator.decimal.elixir
        9: constant.numeric.integer.elixir
        10: invalid.illegal.numeric.elixir

  ## Strings

  binary_string:
    - include: stray_binary_end
    - match: <<(?![<~])
      scope: string.other.binary.elixir punctuation.definition.string.begin.elixir
      push:
        - meta_scope: meta.string.binary.elixir
        - include: binary_string_body_pop
        - include: core_syntax_or_if_closing_pop

  stray_binary_end:
    - match: '>>(?!>)'
      scope: punctuation.definition.string.end.elixir invalid.illegal.stray-closing-binary.elixir

  binary_string_body_pop:
    - match: '>>(?!>)'
      scope: string.other.binary.elixir punctuation.definition.string.end.elixir
      pop: 1
    - match: ::(?!:)
      scope: keyword.operator.colon.elixir
      push:
        - meta_scope: meta.type.binary.elixir
        - include: bitstring_type
        - include: if_non_space_pop

  bitstring_type:
    - include: atom_symbol
    - include: block_or_keyword
    - include: unquote_call
    - match: -(?![->])|\*|(?>\d(?:_?\d)*)
      scope: storage.type.binary.elixir
    - match: '{{identifier}}'
      scope: storage.type.binary.elixir
      push: arguments_or_pop
    - include: paren_binary

  paren_binary:
    - match: \(
      scope: punctuation.section.group.begin.elixir
      push:
        - meta_scope: meta.parens.elixir
        - match: \)
          scope: punctuation.section.group.end.elixir
          pop: 1
        - include: bitstring_type
        - include: core_syntax_or_if_closing_pop

  escaped_char:
    # Avoid possibly matching closing string delimiter: [^'"/\\)\]}>#]
    - match: (?x) \\x (?> \h{2} | (?>\h|([^'"/\\)\]}>#])){1,2} | )
      scope: constant.character.escape.hex.elixir
      captures:
        1: invalid.illegal.escape.hex.elixir
    - match: (?x) \\u (?> (?>\h{4}|{\h{1,6}}) | ({}|{?[^'"/\\)\]}>#]{1,6}}?))
      scope: constant.character.escape.unicode.elixir
      captures:
        1: invalid.illegal.escape.unicode.elixir
    - match: \\.
      scope: constant.character.escape.char.elixir

  interpolated_elixir:
    - match: (?=#{)
      push:
        - clear_scopes: true
        - match: '#{'
          scope: punctuation.section.interpolation.begin.elixir
          set:
            - clear_scopes: true
            - meta_content_scope: source.elixir.embedded
            - meta_scope: source.elixir meta.string.elixir meta.interpolation.elixir
            - match: \}
              scope: punctuation.section.interpolation.end.elixir
              pop: 1
            - include: core_syntax

  escaped_or_interpolated:
    - include: line_continuation
    - include: escaped_char
    - include: interpolated_elixir

  pcre_erlang:
    - include: scope:source.pcree

  elixir_in_regex:
    - include: line_continuation
    - include: interpolated_elixir

  simple_string:
    - match: \'
      comment: single quoted string (allows for interpolation)
      scope: punctuation.definition.string.begin.elixir
      push:
        - meta_scope: meta.string.elixir string.quoted.single.elixir
        - match: \'
          scope: punctuation.definition.string.end.elixir
          pop: 1
        - include: escaped_or_interpolated

    - match: \"
      comment: double quoted string (allows for interpolation)
      scope: punctuation.definition.string.begin.elixir
      push:
        - meta_scope: meta.string.elixir string.quoted.double.elixir
        - match: \"
          scope: punctuation.definition.string.end.elixir
          pop: 1
        - include: escaped_or_interpolated

  heredoc_regex_interpolated:
    - match: (""")(.*)\n
      captures:
        1: punctuation.definition.string.begin.elixir
        2: invalid.illegal.opening-heredoc.elixir
      push:
        - meta_scope: meta.string.elixir string.quoted.triple.double.elixir
        - include: heredoc_string_closing_double_pop
        - match: ''
          push: pcre_erlang
          with_prototype:
            - include: elixir_in_regex
            - match: (?=(?>\\.|[^"])*?""")
              pop: 1

    - match: (''')(.*)\n
      captures:
        1: punctuation.definition.string.begin.elixir
        2: invalid.illegal.opening-heredoc.elixir
      push:
        - meta_scope: meta.string.elixir string.quoted.triple.single.elixir
        - include: heredoc_string_closing_single_pop
        - match: ''
          push: pcre_erlang
          with_prototype:
            - include: elixir_in_regex
            - match: (?=^(?>\\.|[^'])*?''')
              pop: 1

  heredoc_regex_raw:
    - match: (""")(.*)\n
      comment: Triple-quoted heredocs
      captures:
        1: punctuation.definition.string.begin.elixir
        2: invalid.illegal.opening-heredoc.elixir
      push:
        - meta_scope: meta.string.elixir string.quoted.triple.double.elixir
        - include: heredoc_string_closing_double_pop
        - match: ''
          push: pcre_erlang
          with_prototype:
            - match: (?=(?>\\.|[^"])*?""")
              pop: 1

    - match: (''')(.*)\n
      comment: Triple-quoted heredocs
      captures:
        1: punctuation.definition.string.begin.elixir
        2: invalid.illegal.opening-heredoc.elixir
      push:
        - meta_scope: meta.string.elixir string.quoted.triple.single.elixir
        - include: heredoc_string_closing_single_pop
        - match: ''
          push: pcre_erlang
          with_prototype:
            - match: (?=^(?>\\.|[^'])*?''')
              pop: 1

  heredoc_string_interpolated:
    - match: (""")(.*)\n
      comment: Triple-quoted heredocs
      captures:
        1: punctuation.definition.string.begin.elixir
        2: invalid.illegal.opening-heredoc.elixir
      push:
        - meta_scope: meta.string.elixir string.quoted.triple.double.elixir
        - include: escaped_or_interpolated
        - include: heredoc_string_closing_double_pop

    - match: (''')(.*)\n
      comment: Triple-quoted heredocs
      captures:
        1: punctuation.definition.string.begin.elixir
        2: invalid.illegal.opening-heredoc.elixir
      push:
        - meta_scope: meta.string.elixir string.quoted.triple.single.elixir
        - include: escaped_or_interpolated
        - include: heredoc_string_closing_single_pop

  heredoc_string_raw:
    - match: (""")(.*)\n
      comment: Triple-quoted heredocs
      captures:
        1: punctuation.definition.string.begin.elixir
        2: invalid.illegal.opening-heredoc.elixir
      push:
        - meta_scope: meta.string.elixir string.quoted.triple.double.elixir
        - match: \\"
          scope: constant.character.escape.char.elixir
        - include: heredoc_string_closing_double_pop

    - match: (''')(.*)\n
      comment: Triple-quoted heredocs
      captures:
        1: punctuation.definition.string.begin.elixir
        2: invalid.illegal.opening-heredoc.elixir
      push:
        - meta_scope: meta.string.elixir string.quoted.triple.single.elixir
        - match: \\'
          scope: constant.character.escape.char.elixir
        - include: heredoc_string_closing_single_pop

  heredoc_string_closing_double_pop:
    - match: \s*((?>\\.|[^"])*?)\s*(""")
      captures:
        1: invalid.illegal.closing-heredoc.elixir
        2: punctuation.definition.string.end.elixir
      pop: 1

  heredoc_string_closing_double_w_meta_pop:
    - meta_scope: meta.string.elixir
    - include: heredoc_string_closing_double_pop

  heredoc_string_closing_single_pop:
    - match: \s*((?>\\.|[^'])*?)\s*(''')
      captures:
        1: invalid.illegal.closing-heredoc.elixir
        2: punctuation.definition.string.end.elixir
      pop: 1

  string:
    - include: heredoc_string_interpolated
    - include: simple_string
    - include: binary_string

    - match: (?x) (~[a-zA-Z])\n | ~[a-zA-Z]([^{\[<(/|"'])
      comment: catch invalid sigils first
      scope: meta.string.elixir storage.type.string.elixir
      captures:
        1: invalid.illegal.sigil-string.elixir
        2: invalid.illegal.string-delimiter.elixir

    # Look for 'a' behind the closing delimiter.
    # Bracket delimiters are not matched yet: <>, {}, [] and ()
    - match: (?=~w([/|"'])(?>\\.|(?!\1).)*\1a)
      comment: highlight words as atoms
      push:
        - match: (~w)(.)
          captures:
            1: storage.type.string.elixir
            2: string.quoted.other.atom.elixir punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir
            - meta_content_scope: string.quoted.other.atom.elixir constant.other.symbol.atom.elixir
            - match: \s+
              push:
                - clear_scopes: 1
                - include: if_empty_pop
            - include: escaped_or_interpolated
            - match: (\2)(a)
              captures:
                1: string.quoted.other.atom.elixir punctuation.definition.string.end.elixir
                2: string.quoted.modifiers.elixir storage.type.string.elixir
              pop: 1

    - match: ~L(?=["'])(?!''')
      comment: LiveView
      scope: meta.string.elixir storage.type.string.elixir
      push:
        - match: (""")(.*)\n
          captures:
            1: punctuation.definition.string.begin.elixir
            2: invalid.illegal.opening-heredoc.elixir
          set:
            - meta_scope: meta.string.elixir
            - match: (?=""")
              set: [string_modifiers_and_pop, heredoc_string_closing_double_w_meta_pop]
            - match: \\"
              scope: constant.character.escape.char.elixir
            - include: scope:text.html.eex
              apply_prototype: true
        - match: \"
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir
            - match: \\"
              scope: constant.character.escape.char.elixir
            - include: string_closing_dquote
            - include: scope:text.html.eex
              apply_prototype: true
        - match: \'
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir
            - match: \\'
              scope: constant.character.escape.char.elixir
            - include: string_closing_squote
            - include: scope:text.html.eex
              apply_prototype: true

    - match: ~H(?=["'])(?!''')
      comment: LiveView HEEx template
      scope: meta.string.elixir storage.type.string.elixir
      push:
        - match: (""")(.*)\n
          captures:
            1: punctuation.definition.string.begin.elixir
            2: invalid.illegal.opening-heredoc.elixir
          set:
            - meta_scope: meta.string.elixir
            - match: (?=""")
              set: [string_modifiers_and_pop, heredoc_string_closing_double_w_meta_pop]
            - match: \\"
              scope: constant.character.escape.char.elixir
            - include: scope:text.html.heex
              apply_prototype: true
        - match: \"
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir
            - match: \\"
              scope: constant.character.escape.char.elixir
            - include: string_closing_dquote
            - include: scope:text.html.heex
              apply_prototype: true
        - match: \'
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir
            - match: \\'
              scope: constant.character.escape.char.elixir
            - include: string_closing_squote
            - include: scope:text.html.heex
              apply_prototype: true

    - match: ~F(?=["'])(?!''')
      comment: Surface template
      scope: meta.string.elixir storage.type.string.elixir
      push:
        - match: (""")(.*)\n
          captures:
            1: punctuation.definition.string.begin.elixir
            2: invalid.illegal.opening-heredoc.elixir
          set:
            - meta_scope: meta.string.elixir
            - match: (?=""")
              set: [string_modifiers_and_pop, heredoc_string_closing_double_w_meta_pop]
            - match: \\"
              scope: constant.character.escape.char.elixir
            - include: scope:text.html.surface
              apply_prototype: true
        - match: \"
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir
            - match: \\"
              scope: constant.character.escape.char.elixir
            - include: string_closing_dquote
            - include: scope:text.html.surface
              apply_prototype: true
        - match: \'
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir
            - match: \\'
              scope: constant.character.escape.char.elixir
            - include: string_closing_squote
            - include: scope:text.html.surface
              apply_prototype: true

    - match: ~y(?=""")
      comment: YAML with interpolation
      scope: meta.string.elixir storage.type.string.elixir
      push:
        - match: (""")(.*)\n
          captures:
            1: punctuation.definition.string.begin.elixir
            2: invalid.illegal.opening-heredoc.elixir
          set:
            - meta_scope: meta.string.elixir
            - match: (?=""")
              set: [string_modifiers_and_pop, heredoc_string_closing_double_w_meta_pop]
            - match: ''
              push: scope:source.yaml
              with_prototype:
                - include: escaped_or_interpolated
                - match: (?=""")
                  pop: 1

    - match: ~Y(?=""")
      comment: YAML raw
      scope: meta.string.elixir storage.type.string.elixir
      push:
        - match: (""")(.*)\n
          captures:
            1: punctuation.definition.string.begin.elixir
            2: invalid.illegal.opening-heredoc.elixir
          set:
            - meta_scope: meta.string.elixir
            - match: (?=""")
              set: [string_modifiers_and_pop, heredoc_string_closing_double_w_meta_pop]
            - match: ''
              push: scope:source.yaml
              with_prototype:
                - match: \\"
                  scope: constant.character.escape.char.elixir
                - match: (?=""")
                  pop: 1

    - match: ~j(?=")
      comment: JSON with interpolation
      scope: meta.string.elixir storage.type.string.elixir
      push:
        - match: (""")(.*)\n
          captures:
            1: punctuation.definition.string.begin.elixir
            2: invalid.illegal.opening-heredoc.elixir
          set:
            - meta_scope: meta.string.elixir
            - match: (?=""")
              set: [string_modifiers_and_pop, heredoc_string_closing_double_w_meta_pop]
            - match: ''
              push: scope:source.json
              with_prototype:
                - include: escaped_or_interpolated
                - match: (?=""")
                  pop: 1
        - match: \"
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir
            - include: string_closing_dquote
            - match: ''
              push: scope:source.json
              with_prototype:
                - include: escaped_or_interpolated
                - match: (?=")
                  pop: 1

    - match: ~J(?=")
      comment: JSON raw
      scope: meta.string.elixir storage.type.string.elixir
      push:
        - match: (""")(.*)\n
          captures:
            1: punctuation.definition.string.begin.elixir
            2: invalid.illegal.opening-heredoc.elixir
          set:
            - meta_scope: meta.string.elixir
            - match: (?=""")
              set: [string_modifiers_and_pop, heredoc_string_closing_double_w_meta_pop]
            - match: ''
              push: scope:source.json
              with_prototype:
                - match: \\"
                  scope: constant.character.escape.char.elixir
                - match: (?=""")
                  pop: 1
        - match: \"
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir
            - include: string_closing_dquote
            - match: ''
              push: scope:source.json
              with_prototype:
                - match: \\"
                  scope: constant.character.escape.char.elixir
                - match: (?=")
                  pop: 1

    - match: ~r
      comment: regex sigil string with interpolation
      scope: meta.string.elixir storage.type.string.elixir
      push:
        - match: (?="""|''')
          set:
            - match: (?<="""|''')
              set: string_modifiers_and_pop
            - include: heredoc_regex_interpolated
        - match: \"
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.interpolated.elixir
            - include: string_closing_dquote
            - match: ''
              push: pcre_erlang
              with_prototype:
                - include: elixir_in_regex
                - match: (?=")
                  pop: 1
        - match: \'
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.interpolated.elixir
            - include: string_closing_squote
            - match: ''
              push: pcre_erlang
              with_prototype:
                - include: elixir_in_regex
                - match: (?=')
                  pop: 1
        - match: /
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.interpolated.elixir
            - include: string_closing_slash
            - match: ''
              push: pcre_erlang
              with_prototype:
                - include: elixir_in_regex
                - match: (?=/)
                  pop: 1
        - match: \|
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.interpolated.elixir
            - include: string_closing_pipe
            - match: ''
              push: pcre_erlang
              with_prototype:
                - include: elixir_in_regex
                - match: (?=\|)
                  pop: 1
        - match: \{
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.interpolated.elixir
            - include: string_closing_curly
            - match: ''
              push: pcre_erlang
              with_prototype:
                - include: elixir_in_regex
                - match: (?=})
                  pop: 1
        - match: \[
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.interpolated.elixir
            - include: string_closing_square
            - match: ''
              push: pcre_erlang
              with_prototype:
                - include: elixir_in_regex
                - match: (?=])
                  pop: 1
        - match: \<
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.interpolated.elixir
            - include: string_closing_angle
            - match: ''
              push: pcre_erlang
              with_prototype:
                - include: elixir_in_regex
                - match: (?=>)
                  pop: 1
        - match: \(
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.interpolated.elixir
            - include: string_closing_round
            - match: ''
              push: pcre_erlang
              with_prototype:
                - include: elixir_in_regex
                - include: if_closing_paren_pop

    - match: ~R
      comment: regex sigil string without interpolation
      scope: meta.string.elixir storage.type.string.elixir
      push:
        - match: (?="""|''')
          set:
            - match: (?<="""|''')
              set: string_modifiers_and_pop
            - include: heredoc_regex_raw
        # NB: not used as it doesn't correctly match the closing / inside the char set: ~R/[/]/
        # - match: (?=[/|"'])
        #   set:
        #     - match: (?<=[/|"'])
        #       set: string_modifiers_and_pop
        #     - match: ([/|"'])
        #       scope: punctuation.definition.string.begin.elixir
        #       push: pcre_erlang
        #       with_prototype:
        #         - match: \1
        #           scope: punctuation.definition.string.end.elixir
        #           pop: 1
        - match: \"
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.quoted.other.literal.regex.elixir
            - include: string_closing_dquote
            - match: ''
              push: pcre_erlang
              with_prototype:
                - match: \\"
                  scope: constant.character.escape.pcree
                - match: (?=")
                  pop: 1
        - match: \'
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.quoted.other.literal.regex.elixir
            - include: string_closing_squote
            - match: ''
              push: pcre_erlang
              with_prototype:
                - match: \\'
                  scope: constant.character.escape.pcree
                - match: (?=')
                  pop: 1
        - match: /
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.quoted.other.literal.regex.elixir
            - include: string_closing_slash
            - match: ''
              push: pcre_erlang
              with_prototype:
                - match: \\/
                  scope: constant.character.escape.pcree
                - match: (?=/)
                  pop: 1
        - match: \|
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.quoted.other.literal.regex.elixir
            - include: string_closing_pipe
            - match: ''
              push: pcre_erlang
              with_prototype:
                - match: \\\|
                  scope: constant.character.escape.pcree
                - match: (?=\|)
                  pop: 1
        - match: \{
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.quoted.other.literal.regex.elixir
            - include: string_closing_curly
            - match: ''
              push: pcre_erlang
              with_prototype:
                - match: \\}
                  scope: constant.character.escape.pcree
                - match: (?=})
                  pop: 1
        - match: \[
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.quoted.other.literal.regex.elixir
            - include: string_closing_square
            - match: ''
              push: pcre_erlang
              with_prototype:
                - match: \\]
                  scope: constant.character.escape.pcree
                - match: (?=])
                  pop: 1
        - match: \<
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.quoted.other.literal.regex.elixir
            - include: string_closing_angle
            - match: ''
              push: pcre_erlang
              with_prototype:
                - match: \\>
                  scope: constant.character.escape.pcree
                - match: (?=>)
                  pop: 1
        - match: \(
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.quoted.other.literal.regex.elixir
            - include: string_closing_round
            - match: ''
              push: pcre_erlang
              with_prototype:
                - match: \\\)
                  scope: constant.character.escape.pcree
                - include: if_closing_paren_pop

    - match: ~[a-z]
      comment: with sigil and with interpolation
      scope: meta.string.elixir storage.type.string.elixir
      push:
        - match: (?="""|''')
          set:
            - match: (?<="""|''')
              set: string_modifiers_and_pop
            - include: heredoc_string_interpolated
        - match: (?=[/|"'])
          set:
            - meta_scope: meta.string.elixir
            # (?<=[a-z]) avoids matching again after the closing delimiter. E.g.: ~s||//
            - match: (?<=[a-z])([/|"'])
              captures:
                1: string.quoted.other.literal.lower.elixir punctuation.definition.string.begin.elixir
              push:
                - meta_content_scope: string.quoted.other.literal.lower.elixir
                - match: \1
                  scope: string.quoted.other.literal.lower.elixir punctuation.definition.string.end.elixir
                  pop: 1
                - include: escaped_or_interpolated
            - match: ''
              set: string_modifiers_and_pop
        - match: \{
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.interpolated.elixir
            - include: string_closing_curly
            - include: escaped_or_interpolated
        - match: \[
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.interpolated.elixir
            - include: string_closing_square
            - include: escaped_or_interpolated
        - match: \<
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.interpolated.elixir
            - include: string_closing_angle
            - include: escaped_or_interpolated
        - match: \(
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.interpolated.elixir
            - include: string_closing_round
            - include: escaped_or_interpolated

    - match: ~[A-Z]
      comment: with sigil and without interpolation
      scope: meta.string.elixir storage.type.string.elixir
      push:
        - match: (?="""|''')
          set:
            - match: (?<="""|''')
              set: string_modifiers_and_pop
            - include: heredoc_string_raw
        - match: (?=[/|"'])
          set:
            - meta_scope: meta.string.elixir
            # (?<=[A-Z]) avoids matching again after the closing delimiter. E.g.: ~S||//
            - match: (?<=[A-Z])([/|"'])
              captures:
                1: string.quoted.other.literal.upper.elixir punctuation.definition.string.begin.elixir
              push:
                - meta_content_scope: string.quoted.other.literal.upper.elixir
                - match: \\.
                - match: \1
                  scope: string.quoted.other.literal.upper.elixir punctuation.definition.string.end.elixir
                  pop: 1
            - match: ''
              set: string_modifiers_and_pop
        - match: \{
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.quoted.other.literal.upper.elixir
            - include: string_closing_curly
        - match: \[
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.quoted.other.literal.upper.elixir
            - include: string_closing_square
        - match: \<
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.quoted.other.literal.upper.elixir
            - include: string_closing_angle
        - match: \(
          scope: punctuation.definition.string.begin.elixir
          set:
            - meta_scope: meta.string.elixir string.quoted.other.literal.upper.elixir
            - include: string_closing_round

  string_closing_dquote:
    - match: \"
      scope: punctuation.definition.string.end.elixir
      set: string_modifiers_and_pop

  string_closing_squote:
    - match: \'
      scope: punctuation.definition.string.end.elixir
      set: string_modifiers_and_pop

  string_closing_pipe:
    - match: \|
      scope: punctuation.definition.string.end.elixir
      set: string_modifiers_and_pop

  string_closing_slash:
    - match: \/
      scope: punctuation.definition.string.end.elixir
      set: string_modifiers_and_pop

  string_closing_curly:
    - match: \\\}
      scope: constant.character.escape.char.elixir
    - match: \}
      scope: punctuation.definition.string.end.elixir
      set: string_modifiers_and_pop

  string_closing_square:
    - match: \\\]
      scope: constant.character.escape.char.elixir
    - match: \]
      scope: punctuation.definition.string.end.elixir
      set: string_modifiers_and_pop

  string_closing_angle:
    - match: \\\>
      scope: constant.character.escape.char.elixir
    - match: \>
      scope: punctuation.definition.string.end.elixir
      set: string_modifiers_and_pop

  string_closing_round:
    - match: \\\)
      scope: constant.character.escape.char.elixir
    - match: \)
      scope: punctuation.definition.string.end.elixir
      set: string_modifiers_and_pop

  string_modifiers:
    - match: '[a-zA-Z]+'
      scope: meta.string.elixir string.quoted.modifiers.elixir storage.type.string.elixir
    - match: \w+
      scope: invalid.illegal.non-ascii-modifier.elixir

  string_modifiers_and_pop:
    - include: string_modifiers
    - include: if_empty_pop

  ## Declarations

  def_blocks:
    - match: |
        (?x)
        (?>(def(?>macro|guard|n)?p) | (def(?>macro|delegate|guard|n)?))
        {{no_suffix_then_arguments}}
      captures:
        1: keyword.declaration.function.private.elixir
        2: keyword.declaration.function.public.elixir
      push: function_header_pop
    - match: defmodule{{no_suffix_then_arguments}}
      scope: keyword.declaration.module.elixir
      push:
        - meta_scope: meta.namespace.module.elixir
        - include: defmodule_body_pop
    - match: defimpl{{no_suffix_then_arguments}}
      scope: keyword.declaration.implementation.elixir
      push:
        - meta_scope: meta.namespace.implementation.elixir
        - include: arguments_paren_or_ws_pop
    - match: defprotocol{{no_suffix_then_arguments}}
      scope: keyword.declaration.protocol.elixir
      push:
        - meta_scope: meta.namespace.protocol.elixir
        - include: defmodule_body_pop

  defmodule_body_pop:
    - match: \(
      scope: punctuation.section.arguments.begin.elixir
      set:
        - meta_scope: meta.function-call.arguments.elixir
        - include: arguments_closing_pop
        - include: defmodule_arguments
    - match: \s?
      scope: punctuation.section.arguments.begin.elixir
      set:
        - meta_scope: meta.function-call.arguments.elixir
        - include: arguments_ws_closing_pop
        - include: defmodule_arguments

  defmodule_arguments:
    - match: (?={{atom_symbol}}:(?!:))|(?<=[^:]:)
      push: arguments_ws_rest_pop
    - include: block_or_keyword
    - match: |
        (?x)(?=
          (?:(?<=%>)\.)? (?>{{module_name}}{{no_key_suffix}} | __MODULE__{{no_id_key_suffix}})
        | :(?>{{atom_symbol}}|['"])
        )
      push:
        - meta_scope: meta.namespace.elixir
        - match: '{{module_name}}{{no_key_suffix}}(?!\s*\.(?!\.))'
          scope: entity.name.namespace.elixir
        - include: atom_namespace_entity
        - include: special_form
        - include: module_name
        - include: atom_symbol
        - include: unquote_call
        - include: dot_operator
        - include: if_non_space_or_eol_pop
    - include: arg_comma_and_skip_ws
    - include: core_syntax_or_if_closing_pop

  defrecord:
    - match: (?:(Record)\s*(\.)\s*)?(defrecordp?){{no_suffix_then_arguments}}
      captures:
        1: constant.other.module.elixir
        2: punctuation.accessor.dot.elixir
        3: keyword.declaration.elixir
      push: defrecord_pop

  defrecord_pop:
    - match: \(
      scope: punctuation.section.arguments.begin.elixir
      set:
        - meta_scope: meta.function-call.arguments.elixir
        - include: arguments_closing_pop
        - include: defrecord_arguments
    - match: \s?
      scope: punctuation.section.arguments.begin.elixir
      set:
        - meta_scope: meta.function-call.arguments.elixir
        - include: arguments_ws_closing_pop
        - include: defrecord_arguments

  defrecord_arguments:
    - include: atom_function_entity
    - match: (?=,)
      push: arguments_ws_rest_pop
    - include: arg_comma_and_skip_ws
    - include: core_syntax_or_if_closing_pop

  atom_function_entity:
    - match: (:)({{atom_symbol}})(?!\s*\.(?!\.))
      scope: constant.other.symbol.elixir
      captures:
        1: punctuation.definition.constant.begin.elixir
        2: entity.name.function.elixir

  atom_namespace_entity:
    - match: (:)({{atom_symbol}})(?!\s*\.(?!\.))
      scope: constant.other.symbol.elixir
      captures:
        1: punctuation.definition.constant.begin.elixir
        2: entity.name.namespace.elixir

  block_or_keyword:
    - include: atom_keyword
    - include: fn_block
    - include: do_block
    - include: elixir_keywords

  do_block_pop:
    - match: do{{no_id_key_suffix}}
      scope: punctuation.section.block.begin.elixir keyword.context.block.do.elixir
      set:
        - meta_scope: meta.block.elixir
        - match: else{{no_id_key_suffix}}
          comment: the else-block contains arrow clauses only in error handling
          scope: keyword.control.conditional.else.elixir
        - match: (?=%>)
          pop: 1
        - include: error_handling_clauses
        - include: core_syntax_or_block_end_pop

  func_do_block_pop:
    - match: do{{no_id_key_suffix}}
      scope: punctuation.section.block.begin.elixir keyword.context.block.do.elixir
      set:
        - meta_scope: meta.block.elixir
        - include: error_handling_clauses
        - include: core_syntax_or_block_end_pop

  error_handling_clauses:
    - match: else{{no_id_key_suffix}}
      scope: keyword.control.exception.else.elixir
      push: arrow_clauses_body_pop
    - match: after{{no_id_key_suffix}}
      scope: keyword.control.exception.after.elixir
      push: core_syntax_or_if_closing_pop
    - match: rescue{{no_id_key_suffix}}
      scope: keyword.control.exception.rescue.elixir
      push: arrow_clauses_body_pop
    - match: catch{{no_id_key_suffix}}
      scope: keyword.control.exception.catch.elixir
      push: arrow_clauses_body_pop

  do_block:
    # NB: Commented out due to EEx templates: <%= if x do %>...<% end %>
    # - match: end{{no_id_key_suffix}}
    #   scope: invalid.illegal.block-end.elixir
    - match: (?=do{{no_id_key_suffix}})
      push: do_block_pop

  block_end_pop:
    - match: end{{no_id_key_suffix}}
      scope: punctuation.section.block.end.elixir keyword.context.block.end.elixir
      pop: 1

  last_arg_block_end_pop:
    - match: end{{no_id_key_suffix}}
      scope: punctuation.section.block.end.elixir keyword.context.block.end.elixir
      set:
        - match: \s|\n|
          scope: punctuation.section.arguments.end.elixir
          pop: 1

  core_syntax_or_block_end_pop:
    - include: block_end_pop
    - include: core_syntax

  core_syntax_or_if_closing_pop:
    - include: if_closing_token_pop
    - include: core_syntax

  ## Identifiers

  module_name:
    - match: '{{module_name}}{{no_key_suffix}}'
      scope: constant.other.module.elixir
    - match: (?x)(:(["'])) ((?>\\\\|\\\2|(?!\2).)*?) (\2) (?=\s*\.(?!\.))
      captures:
        1: punctuation.definition.constant.begin.elixir
        3: constant.other.module.elixir
        4: punctuation.definition.constant.end.elixir
    - match: (:)({{atom_symbol}})(?=\s*\.(?!\.))
      captures:
        1: punctuation.definition.constant.begin.elixir
        2: constant.other.module.elixir
  module_name_pop:
    - match: '{{module_name}}{{no_key_suffix}}'
      scope: constant.other.module.elixir
      pop: 1

  module_function_call_pop:
    - match: ({{module_name}})\s*(\.(?!\.))
      comment: always a function call after a module
      captures:
        1: constant.other.module.elixir
        2: punctuation.accessor.dot.elixir
      set: member_call_pop

  member_call_pop:
    # - include: sql_or_fragment
    - include: quoted_remote_call_pop
    - include: quoted_member_pop
    - include: unquote_call_pop
    - match: '{{member}}'
      scope: variable.function.elixir
      set: arguments_or_pop
    - include: tuple_call_pop
    - include: if_non_space_or_eol_pop

  id_or_operator_call_pop:
    - match: ({{member}})(?=\s*\.\s*\(|{{has_arguments}})
      scope: variable.function.elixir
      set: arguments_paren_or_ws_pop

  quoted_remote_call_pop:
    - match: \"(?=(?>\\[\\"]|.)*?"{{has_arguments}})
      scope: punctuation.definition.constant.begin.elixir
      set:
        - meta_scope: meta.function-call.elixir
        - match: \"
          scope: punctuation.definition.constant.end.elixir
          set: arguments_or_pop
        - match: (\\[\\"])|[^"]
          scope: variable.function.elixir
          captures:
            1: constant.character.escape.char.elixir
    - match: \'(?=(?>\\[\\']|.)*?'{{has_arguments}})
      scope: punctuation.definition.constant.begin.elixir
      set:
        - meta_scope: meta.function-call.elixir
        - match: \'
          scope: punctuation.definition.constant.end.elixir
          set: arguments_or_pop
        - match: (\\[\\'])|[^']
          scope: variable.function.elixir
          captures:
            1: constant.character.escape.char.elixir

  quoted_member_pop:
    - match: \"
      scope: punctuation.definition.constant.begin.elixir
      set:
        - meta_scope: meta.member.elixir
        - match: \"
          scope: punctuation.definition.constant.end.elixir
          pop: 1
        - match: (\\[\\"])|[^"]
          scope: variable.other.member.elixir
          captures:
            1: constant.character.escape.char.elixir
    - match: \'
      scope: punctuation.definition.constant.begin.elixir
      set:
        - meta_scope: meta.member.elixir
        - match: \'
          scope: punctuation.definition.constant.end.elixir
          pop: 1
        - match: (\\[\\'])|[^']
          scope: variable.other.member.elixir
          captures:
            1: constant.character.escape.char.elixir

  modules_or_ids_or_calls:
    - match: (?x)(:(["'])) ((?>\\\\|\\\2|(?!\2).)*?) (\2) (?=\s*\.(?!\.))
      captures:
        1: punctuation.definition.constant.begin.elixir
        3: constant.other.module.elixir
        4: punctuation.definition.constant.end.elixir
      push: member_call_pop
    - match: (:)({{atom_symbol}})\s*(\.(?!\.))
      captures:
        1: punctuation.definition.constant.begin.elixir
        2: constant.other.module.elixir
        3: punctuation.accessor.dot.elixir
      push: member_call_pop
    - match: (?={{module_name}}|{{identifier}})
      push:
        - include: module_function_call_pop
        - include: id_or_operator_call_pop
        - include: module_name_pop
        - include: identifier_pop

  identifier_pop:
    - match: ((?=_)(?<!%>){{identifier}})|({{identifier}})
      captures:
        1: variable.other.unused.elixir
        2: variable.other.elixir
      pop: 1

  id_member_pop:
    - match: '{{member}}'
      scope: variable.other.member.elixir
      pop: 1

  tuple_call_pop:
    - match: \{
      scope: punctuation.section.braces.begin.elixir
      set:
        - match: \}
          scope: punctuation.section.braces.end.elixir
          pop: 1
        - include: core_syntax_or_if_closing_pop

  dot_accessor:
    - match: \.(?!\.)
      scope: punctuation.accessor.dot.elixir
      push:
        - include: atom_keyword
        - include: module_function_call_pop
        - include: unquote_call_pop
        - include: id_or_operator_call_pop
        - include: id_member_pop
        - include: module_name_pop
        - include: arguments_pop
        - include: quoted_remote_call_pop
        - include: quoted_member_pop
        - include: tuple_call_pop
        - include: if_non_space_or_eol_pop

  ## Module attributes

  module_attribute:
    - match: '@'
      scope: keyword.operator.attribute.elixir
      push: module_attribute_pop

  module_attribute_pop:
    - match: (?!{{identifier}}(?!{{atom_id_suffix}}:(?!:)))
      set:
        - match: '[A-Z]'
          scope: invalid.illegal.attribute.elixir
        - include: if_empty_pop

    # Special attributes:
    - match: (?>(?>module|type|short)?doc){{no_suffix_then_arguments}}
      scope: support.attr.doc.elixir
      set:
        - match: \(
          scope: punctuation.section.arguments.begin.elixir
          set:
            - meta_scope: meta.doc.elixir
            - include: arguments_closing_pop
            - match: ''
              push:
                - include: if_closing_token_pop
                - include: arg_comma_and_skip_ws
                - include: markdown_comment
                - include: core_syntax
        - match: \s?
          scope: punctuation.section.arguments.begin.elixir
          set:
            - meta_scope: meta.doc.elixir
            - include: arguments_ws_closing_pop
            - include: arg_comma_and_skip_ws
            - include: markdown_comment
            - include: core_syntax

    - match: (?>spec|(?:macro)?callback)(?={{no_suffix_then_arguments}}|\s*{{operator}})
      scope: keyword.declaration.type.elixir
      set:
        - meta_scope: meta.type.elixir
        - match: \(
          scope: punctuation.section.arguments.begin.elixir
          set:
            - include: arguments_closing_pop
            - match: ''
              push:
                - match: (?=not{{no_id_key_suffix}})
                  set: spec_header_pop
                - include: block_or_keyword
                - include: spec_header_pop
        - match: \s?
          scope: punctuation.section.arguments.begin.elixir
          set:
            - match: (?=not{{no_id_key_suffix}})
              set: spec_header_pop
            - include: block_or_keyword
            - include: spec_header_pop
            - include: if_non_space_pop

    - match: (?>typep?|opaque){{no_suffix_then_arguments}}
      scope: keyword.declaration.type.elixir
      set:
        - meta_scope: meta.type.elixir
        - match: \(
          scope: punctuation.section.arguments.begin.elixir
          set:
            - meta_scope: meta.type.elixir meta.function-call.arguments.elixir
            - include: block_or_keyword
            - match: (?=unquote\()
              push: [type_definition_pop, arguments_pop, unquote_pop]
            - match: '{{identifier}}'
              scope: entity.name.type.elixir
              push: type_definition_pop
            - include: arguments_rest_pop
        - match: \s?
          scope: punctuation.section.arguments.begin.elixir
          set:
            - meta_scope: meta.type.elixir meta.function-call.arguments.elixir
            - include: block_or_keyword
            - match: (?=unquote\()
              set: [type_definition_pop, arguments_pop, unquote_pop]
            - match: '{{identifier}}'
              scope: entity.name.type.elixir
              set: type_definition_pop
            - include: if_non_space_pop

    - match: |
        (?x)(?>
          impl | deprecated | (?>before_|after_)?compile | behaviour | enforce_keys
        | fallback_to_any | optional_callbacks | file | dialyzer | derive
        | external_resource | vsn | on_(?>definition|load)
        ){{no_suffix_then_arguments}}
      scope: support.attr.elixir
      set: arguments_paren_or_ws_pop

    - include: block_or_keyword

    # Regular attributes:
    - match: '{{identifier}}{{has_arguments}}'
      comment: definition
      scope: entity.name.constant.elixir
      set: arguments_paren_or_ws_pop

    - match: '{{identifier}}'
      comment: reference
      scope: variable.other.constant.elixir
      pop: 1

  spec_after_unquote_pop:
    - match: (?=(?!::){{operator}})
      set: spec_op_definition_pop
    - match: ''
      set: spec_definition_pop

  spec_header_pop:
    - match: (?=unquote\()
      set: [spec_after_unquote_pop, if_non_space_or_eol_pop, arguments_pop, unquote_pop]
    - match: '{{identifier}}(?!\s*(?!::){{operator}})'
      scope: variable.other.spec.elixir
      set: spec_definition_pop
    - match: (?=\S)
      set: spec_op_definition_pop

  ## Documentation in Markdown

  markdown_comment:
    # NB: no need to support `~s"""` which is basically `"""`.
    - match: (""")(.*)(\n)
      captures:
        1: string.quoted.triple.double.elixir punctuation.definition.string.begin.elixir
        2: invalid.illegal.opening-heredoc.elixir
        3: string.quoted.triple.double.elixir
      push:
        - meta_scope: meta.string.elixir
        - match: (?=^\s*""")
          set:
            - meta_scope: meta.string.elixir string.quoted.triple.double.elixir
            - include: heredoc_string_closing_double_pop
        - include: escaped_or_interpolated
        # - include: elixir_in_markdown
        - match: ''
          embed: scope:text.html.markdown
          embed_scope: source.markdown.embedded.elixir
          # escape: (?=^\s*"""|\\|#{|{{is_markdown_elixir}})
          escape: (?=^\s*"""|\\|#{)

    - match: \"
      scope: string.quoted.double.elixir punctuation.definition.string.begin.elixir
      push:
        - meta_scope: meta.string.elixir
        - match: \"
          scope: string.quoted.double.elixir punctuation.definition.string.end.elixir
          pop: 1
        - include: escaped_or_interpolated
        - match: ''
          embed: scope:text.html.markdown
          embed_scope: source.markdown.embedded.elixir
          escape: (?="|\\|#{)

    - match: (~S)(""")(.*)(\n)
      captures:
        1: storage.type.string.elixir
        2: string.quoted.triple.double.elixir punctuation.definition.string.begin.elixir
        3: invalid.illegal.opening-heredoc.elixir
        4: string.quoted.triple.double.elixir
      push:
        - meta_scope: meta.string.elixir
        - match: (?<=""")
          set: string_modifiers_and_pop
        - match: (?=^\s*""")
          push:
            - meta_scope: string.quoted.triple.double.elixir
            - include: heredoc_string_closing_double_pop
        # - include: elixir_in_markdown
        - match: ''
          embed: scope:text.html.markdown
          embed_scope: source.markdown.embedded.elixir
          # escape: (?=^\s*"""|{{is_markdown_elixir}})
          escape: (?=^\s*""")

    - match: (~S)(''')(.*)(\n)
      captures:
        1: storage.type.string.elixir
        2: string.quoted.triple.single.elixir punctuation.definition.string.begin.elixir
        3: invalid.illegal.opening-heredoc.elixir
        4: string.quoted.triple.single.elixir
      push:
        - meta_scope: meta.string.elixir
        - match: (?<=''')
          set: string_modifiers_and_pop
        - match: (?=^\s*''')
          push:
            - meta_scope: string.quoted.triple.single.elixir
            - include: heredoc_string_closing_single_pop
        # - include: elixir_in_markdown
        - match: ''
          embed: scope:text.html.markdown
          embed_scope: source.markdown.embedded.elixir
          # escape: (?=^\s*'''|{{is_markdown_elixir}})
          escape: (?=^\s*''')

  elixir_in_markdown:
    - match: (?<=\s{4})(iex)(?:\((.*?)\))?(>)
      captures:
        1: keyword.other.iex.elixir
        2: constant.other.elixir
        3: keyword.other.iex-angle.elixir punctuation.definition.iex.begin.elixir
      push:
        - clear_scopes: true
        - meta_scope: meta.interpolation.elixir markup.raw.block.markdown markup.raw.block.elixir
        - match: \n|$
          scope: punctuation.definition.iex.end.elixir
          pop: 1
        - include: core_syntax

    - match: (?={{is_markdown_elixir}})
      push:
        - clear_scopes: true
        - meta_scope: meta.interpolation.elixir markup.raw.block.markdown markup.raw.block.elixir
        - match: \n|$
          scope: punctuation.section.arguments.end.elixir
          pop: 1
        - include: core_syntax

  iex_dots:
    - match: ^(\s*)(\.{3})(?:\((.*?)\))?(>)
      comment: iex prompt continuation
      captures:
        1: meta.string.elixir
        2: keyword.other.iex-dots.elixir
        3: constant.other.elixir
        4: keyword.other.iex-angle.elixir punctuation.definition.iex.begin.elixir

  comma_and_skip_ws:
    - include: invalid_comma
    - match: \,
      scope: punctuation.separator.sequence.elixir
      push: invalid_comma_or_non_space_pop

  arg_comma_and_skip_ws:
    - match: \,
      scope: punctuation.separator.arguments.elixir
      push: invalid_comma_or_non_space_pop

  invalid_comma:
    - match: (?=,\s*(?>[,;)]|%>|->(?!:)|(?>do|when){{no_id_key_suffix}}|{{closing_keyword}}))
      push: invalid_comma_or_non_space_pop

  invalid_comma_or_non_space_pop:
    - include: comment
    - match: \,
      scope: invalid.illegal.separator.elixir
    - include: if_non_space_pop

  ## Operators

  operator:
    - match: |
        (?x)(?> # Ordered by likeliness of occurrence.
          (=(?![=>~]))
        | (->|<-)
        | (<?\|>|<~>|<?<~|~>>?) # Operators with pipe precedence.
        | ((?>[!=]=|[<>])(?![<>])=?)
        | (=>)
        | (<>)
        | (-(?!-)|\+(?!\+)|//?|\*\*?)
        | (\+\+(?!\+))
        | (\|(?!\|))
        | (::(?!:))
        | (\^(?!\^\^))
        | (\|\|(?!\|)|&&(?!&)|!)
        | (\.\.\.) # Can appear in @type declarations.
        | (\.\.)
        | (=~)
        | (\\\\)
        | (<<<|>>>|&&&|\|\|\||~~~|\^\^\^)
        | (---|\+\+\+)
        | (--(?!-))
        | (;)
        )
      captures:
        1: keyword.operator.match.elixir
        2: keyword.operator.arrow.elixir
        3: keyword.operator.pipe.elixir
        4: keyword.operator.comparison.elixir
        5: keyword.operator.map-pair.elixir
        6: keyword.operator.binary-concat.elixir
        7: keyword.operator.arithmetic.elixir
        8: keyword.operator.list-concat.elixir
        9: keyword.operator.union.elixir
        10: keyword.operator.colon.elixir
        11: keyword.operator.pin.elixir
        12: keyword.operator.logical.elixir
        13: keyword.operator.ellipsis.elixir
        14: keyword.operator.range.elixir
        15: keyword.operator.regex.elixir
        16: keyword.operator.default.elixir
        17: keyword.operator.bitwise.elixir
        18: keyword.operator.reserved.elixir
        19: keyword.operator.list-diff.elixir
        20: keyword.operator.semicolon.elixir

  # Separate to make @type matching possible.
  cons_operator:
    - match: \|(?![:|>])
      scope: keyword.operator.cons.elixir

  dot_operator:
    - match: \.(?!\.)
      scope: punctuation.accessor.dot.elixir

  ## Literals

  tuple:
    - match: \}
      scope: invalid.illegal.stray-closing-brace.elixir
    - match: \{
      scope: punctuation.section.sequence.begin.elixir
      push:
        - meta_scope: meta.sequence.tuple.elixir
        - match: \}
          scope: punctuation.section.sequence.end.elixir
          pop: 1
        - include: core_syntax_or_if_closing_pop

  item_access:
    - match: |
        (?x)
        (?<=["'\w\])}]|\w[?!]|%>)
        (?<!\bdo|\bwhen|\band|\bin|\belse|\bafter|\bor|\brescue|\bnot|\bcatch)
        \[
      scope: punctuation.section.access.begin.elixir
      push:
        - meta_scope: meta.access.elixir
        - match: \]
          scope: punctuation.section.access.end.elixir
          pop: 1
        - include: core_syntax_or_if_closing_pop

  list:
    - match: \]
      scope: invalid.illegal.stray-closing-bracket.elixir
    - match: \[
      scope: punctuation.section.brackets.begin.elixir
      push:
        - meta_scope: meta.brackets.elixir
        - match: \]
          scope: punctuation.section.brackets.end.elixir
          pop: 1
        - include: cons_operator
        - include: core_syntax_or_if_closing_pop

  map:
    - match: \%
      scope: punctuation.section.mapping.begin.elixir
      push:
        - meta_scope: meta.mapping.elixir
        - match: (?=unquote\()
          set: [map_body_pop, arguments_pop, unquote_pop]
        - include: alias_names
        - match: \^
          scope: keyword.operator.pin.elixir
        - match: ((?=_)(?<!%>){{identifier}})|({{identifier}})
          captures:
            1: variable.other.unused.elixir
            2: variable.other.elixir
          set: map_body_pop
        - include: map_body_pop

  map_body_pop:
    - match: \{
      scope: punctuation.section.mapping.begin.elixir
      set:
        - include: map_closing_pop
        - include: cons_operator
        - include: core_syntax_or_if_closing_pop
    - include: if_closing_token_pop
    - include: if_non_space_or_eol_pop

  paren:
    - match: (?<=%>)(?=\()
      push: arguments_pop
    - match: \)
      scope: invalid.illegal.stray-closing-parenthesis.elixir
    - match: \(
      scope: punctuation.section.group.begin.elixir
      push:
        - meta_scope: meta.parens.elixir
        - match: \)
          scope: punctuation.section.group.end.elixir
          pop: 1
        - include: core_syntax_or_if_closing_pop

  ## Alias/Require statement

  alias:
    - match: (?>alias|require){{no_suffix_then_arguments}}
      scope: keyword.control.import.elixir
      push:
        # alias(X, as: Y)
        - match: \(
          scope: punctuation.section.arguments.begin.elixir
          set:
            - meta_scope: meta.function-call.arguments.elixir
            - include: arguments_closing_pop
            - include: alias_names
            - match: (?=,)
              set:
                - include: alias_as_arg
                - include: arguments_rest_pop
            - include: core_syntax
        # alias X, as: Y
        - match: \s?
          scope: punctuation.section.arguments.begin.elixir
          set:
            - meta_scope: meta.function-call.arguments.elixir
            - include: arguments_ws_closing_pop
            - match: ''
              push:
                - include: alias_names
                - match: (?=,)
                  set:
                    - include: alias_as_arg
                    - include: arg_comma_and_skip_ws
                    - include: if_ws_closing_token_or_eol_pop
                    - include: core_syntax
                - include: if_ws_closing_token_or_eol_pop
                - include: core_syntax

  alias_as_arg:
    - match: as(:)(?!:)
      scope: constant.other.keyword.elixir
      captures:
        1: punctuation.definition.constant.elixir
      push: [alias_as_name_pop, invalid_comma_or_non_space_pop]

  alias_as_name_pop:
    - match: '{{module_name}}{{no_key_suffix}}'
      scope: entity.name.namespace.elixir
    - include: if_non_space_or_eol_pop

  alias_names:
    - match: |
        (?x)(?=
          (?:(?<=%>)\.)? (?>{{module_name}}{{no_key_suffix}} | __MODULE__{{no_id_key_suffix}})
        | :(?>{{atom_symbol}}|['"])
        )
      push:
        - include: module_name
        - include: dot_accessor
        - include: unquote_call_pop
        - include: special_form
        - include: atom_symbol
        - include: if_non_space_or_eol_pop

  ## Atoms

  atom_keyword:
    - match: |
        (?x)
        (?! \?: | ::: | %:(?:{{atom_id}}|['"]) ) # No ::: and %:XYZ{} (it's a map)
        (?>{{atom_symbol}}|(?<=%>)) (:) {{no_colon_suffix}}
      comment: keyword symbol
      scope: constant.other.keyword.elixir
      captures:
        1: punctuation.definition.constant.end.elixir
    # Look for ':' behind the closing double/single quote.
    - match: (?x)(?=(["']) (?>\\. | (?!\1).)* \1:{{no_colon_suffix}})
      comment: keyword string
      push:
        - match: (.)
          scope: punctuation.definition.constant.begin.elixir
          set:
            - meta_scope: meta.string.elixir constant.other.keyword.elixir
            - include: escaped_or_interpolated
            - match: '\1:'
              scope: punctuation.definition.constant.end.elixir
              pop: 1

  atom_symbol:
    - match: :(?!:(?!:))
      scope: constant.other.symbol.elixir punctuation.definition.constant.begin.elixir
      push:
        - match: (?=<%)|{{atom_symbol}}
          scope: constant.other.symbol.elixir
          pop: 1
        - match: (["'])
          scope: punctuation.definition.constant.begin.elixir
          set:
            - meta_scope: constant.other.symbol.quoted.elixir
            - match: \1
              scope: punctuation.definition.constant.end.elixir
              pop: 1
            - include: escaped_or_interpolated
        - include: if_empty_pop

  ## Captures

  capture:
    - match: (&)\s*(\d+)(?!\.(?![.\D]))
      captures:
        1: punctuation.definition.capture.elixir constant.other.capture.elixir
        2: constant.other.capture.elixir
    - match: \&
      scope: keyword.operator.capture.elixir
      push:
        - match: (?=(?>do|fn|true|false|nil){{no_id_key_suffix}})
          pop: 1
        - include: if_closing_token_pop
        - match: (?=\.(?!\.))
          set:
            - include: dot_operator
            - include: arguments_pop
            - include: capture_name_pop
            - include: id_or_operator_call_pop
            - include: quoted_remote_call_pop
            - include: quoted_member_pop
            - match: (?={{member}}{{no_id_key_suffix}})
              push: id_member_pop
            - include: tuple_call_pop
            - include: if_closing_token_pop
            - include: if_non_space_or_eol_pop
        - include: capture_name_pop
        - include: special_form
        - match: (?={{identifier}}\s*\.(?!\.|\s*\())
          push: identifier_pop
        - include: if_non_space_or_eol_pop

  capture_name_pop:
    - match: |
        (?x)(?=
          (?>
            {{module_name}}{{no_key_suffix}}
          | :{{atom_symbol}}
          | (?>:(["'])) (?>\\\\|\\\1|(?!\1).)*? (\1)
          )
          \s* (?!\. (?!\. | \s*\())
        | (?>:{{atom_symbol}}) \s*\.\s* {{member}} \s*\(
        )
      comment: exit if module/atom is not followed by `.` or `.(`
      pop: 1
    - include: atom_symbol
    - include: atom_keyword
    - include: module_name
    - include: unquote_call
    - match: (/)\s*(\d+)(?!\.(?![.\D]))
      captures:
        1: punctuation.accessor.arity.elixir
        2: constant.numeric.arity.elixir
      pop: 1
    - match: |
        (?x)
        (?>(/(?=/)|{{member}}) | (["'])((?>\\\\|\\\2|(?!\2).)*?)(\2))
        (?=\s*(/)\s*(\d+)(?!\.(?![.\D])))
      captures:
        1: variable.other.capture.elixir
        2: punctuation.definition.constant.begin.elixir
        3: variable.other.capture.elixir
        4: punctuation.definition.constant.end.elixir

  ## SQL

  sql_or_fragment:
    - match: (?>(fragment)|(sql))(\()
      captures:
        1: support.function.elixir
        2: variable.function.elixir
        3: punctuation.section.arguments.begin.elixir
      push:
        - meta_scope: meta.function-call.arguments.elixir
        - match: (?=unquote\()
          set: [arguments_rest_pop, sql_unquote_pop, unquote_pop]
        - include: sql_or_fragment_args_pop

  sql_unquote_pop:
    - match: \(
      scope: punctuation.section.arguments.begin.elixir
      set:
        - meta_scope: meta.function-call.arguments.elixir
        - include: sql_or_fragment_args_pop

  sql_or_fragment_args_pop:
    - match: (?=")
      set: [arguments_rest_pop, sql_string_pop]
    - include: comment
    - match: (?=\S)
      set: arguments_rest_pop

  sql_string_pop:
    - match: (""")(.*)\n
      captures:
        1: string.quoted.triple.double.elixir punctuation.definition.string.begin.elixir
        2: invalid.illegal.opening-heredoc.elixir
      set:
        - meta_scope: meta.string.elixir
        - match: '"""'
          scope: string.quoted.triple.double.elixir punctuation.definition.string.end.elixir
          pop: 1
        - match: ''
          push: scope:source.ex.sql
          with_prototype:
            - match: (?=""")
              pop: 1
            - include: sql_or_elixir_escaped_pop
    - match: \"
      scope: string.quoted.double.elixir punctuation.definition.string.begin.elixir
      set:
        - meta_scope: meta.string.elixir
        - match: \"
          scope: string.quoted.double.elixir punctuation.definition.string.end.elixir
          pop: 1
        - match: ''
          push: scope:source.ex.sql
          with_prototype:
            - match: (?=")
              pop: 1
            - include: sql_or_elixir_escaped_pop

  sql_or_elixir_escaped_pop:
    - include: interpolated_elixir
    - include: line_continuation
    - match: (?=\\(?!\\?\?))
      push:
        - meta_scope: source.ex.sql
        - match: (?=\\\\?\?)
          comment: let source.ex.sql match "\\?"
          pop: 1
        - include: escaped_char
        - include: if_empty_pop

  opaque_struct:
    # Attempt highlighting e.g. #PID<0.1.0>, #Decimal<1.0> etc.
    - match: '#(?={{module_name}}(?:\s*\.\s*{{module_name}})*<)'
      scope: punctuation.section.mapping.elixir
      comment: opaque struct
      push:
        - include: module_name
        - include: dot_operator
        - match: \<
          scope: punctuation.section.mapping.begin.elixir
          set:
            - match: (?<=\S|^)\>
              scope: punctuation.section.mapping.end.elixir
              pop: 1
            # Text between <> brackets isn't always valid Elixir code.
            # - include: core_syntax

  comment:
    # - include: opaque_struct
    - match: \#
      scope: punctuation.definition.comment.elixir
      push:
        - meta_scope: comment.line.number-sign.elixir
        - match: \n
          pop: 1

  comments:
    - match: (?=#)
      push:
        - include: comment
        - include: if_non_space_pop

  char_literal:
    - match: (\?$)|(\?)(?>\\[\S\s]|\S|(\s))
      comment: character literal as an integer
      scope: constant.numeric.char.elixir
      captures:
        1: invalid.illegal.character-literal.elixir
        2: punctuation.definition.numeric.elixir
        3: invalid.illegal.character-literal.elixir

  built_ins:
    - match: |
        (?x)(?> # Ordered by likeliness of occurrence.
          is_(?>
            binary|list|atom|integer|function|nil|map(?:_key)?|tuple|number
          | boolean|float|bitstring|pid|exception|port|reference|struct
          )
        | inspect|to_string|elem|length|apply|self|byte_size|div|send|rem|put_(?>elem|in)
        | (?>function|macro)_exported\?|update_in|binary_part|hd|struct!?|max|trunc|then
        | to_charlist|get(?:_and_update)?_in|map_size|min|tl|node|make_ref|bit_size
        | tuple_size|spawn(?>_link|_monitor)?|pop_in|abs|round|sum|floor|ceil|tap
        ){{no_suffix_then_arguments}}
      scope: variable.function.built-in.elixir
      push: arguments_paren_or_ws_pop

  ## Arguments

  arguments_paren_or_ws_pop:
    - include: arguments_pop
    - match: ''
      set: [arguments_ws_closing_or_empty_pop, arguments_ws_pop]

  arguments_or_pop:
    - match: '{{has_arguments}}'
      set: arguments_paren_or_ws_pop
    - include: if_empty_pop

  arguments_pop:
    - match: \(
      scope: punctuation.section.arguments.begin.elixir
      set:
        - meta_scope: meta.function-call.arguments.elixir
        - include: arguments_rest_pop

  arguments_closing_pop:
    - match: \)
      scope: punctuation.section.arguments.end.elixir
      pop: 1

  map_closing_pop:
    - match: \}
      scope: meta.mapping.elixir punctuation.section.mapping.end.elixir
      pop: 1

  arguments_rest_pop:
    - include: arguments_closing_pop
    - include: if_closing_token_pop
    - include: arg_comma_and_skip_ws
    - include: core_syntax

  arguments_ws_closing_pop:
    - match: |
        (?x)
          \n | \s? (?=\s*{{closing_token}})
        | (?<=::\bend\b) | (?<=([^:])\bend\b) | (?<=^end\b)
      scope: punctuation.section.arguments.end.elixir
      pop: 1

  arguments_ws_closing_or_empty_pop:
    - include: arguments_ws_closing_pop
    - include: if_empty_pop

  arguments_ws:
    - match: ''
      push: arguments_ws_pop

  arguments_ws_pop:
    - match: \s?
      comment: arguments list without parentheses
      scope: punctuation.section.arguments.begin.elixir
      set:
        - meta_scope: meta.function-call.arguments.elixir
        - include: arguments_ws_rest_pop

  arguments_ws_rest_pop:
    - include: invalid_comma
    - include: arg_comma_and_skip_ws
    - match: (?=\?:\s*{{closing_token}})
      set:
        - include: char_literal
        - include: if_ws_closing_token_or_eol_pop
    - include: atom_keyword
    - match: (?=(?>when|and|in|or|not){{no_id_key_suffix}})
      push:
        - include: if_closing_token_pop
        - match: (?!(?>when|and|in|or|not){{no_id_key_suffix}}|[,\s]|(?<!,))
          pop: 1
        - include: elixir_keywords
        - include: invalid_comma_or_non_space_pop
    - match: (?x) (?=[-+*/=!|;] | && | \\\\ | ~(?![a-zA-Z]) | <(?!<(?!<)) | >(?!>(?!>)) | \.\.(?!\.) | ::(?!:))
      push:
        - include: if_closing_token_pop
        - include: operator
        - include: invalid_comma_or_non_space_pop
    - match: (?<=[^:]:)\s
      push: invalid_comma_or_non_space_pop
    - include: do_block_pop
    - include: fn_block
    - include: if_ws_closing_token_or_eol_pop
    - include: elixir_keywords
    - include: special_form
    - include: last_id_argument
    - include: core_syntax

  last_id_argument:
    - match: |
        (?x)(?=
          (?>
            (?:@\s*)?{{identifier}}
          | \.\s*(?:{{member}} | "(?>\\[\\"]|.)*?" | '(?>\\[\\']|.)*?')
          )
          \s*do{{no_id_key_suffix}}
        )
      comment: prevent matching last argument as a function call, e.g. `func first, second, last do end`
      push:
        - match: (?=do{{no_id_key_suffix}})
          pop: 1
        - match: (@)\s*({{identifier}})
          comment: reference
          captures:
            1: keyword.operator.attribute.elixir
            2: variable.other.constant.elixir
        - include: identifier_pop
        - match: \.(?!\.)
          scope: punctuation.accessor.dot.elixir
          set:
            - include: id_member_pop
            - include: quoted_member_pop
            - include: if_non_space_or_eol_pop
        - include: if_non_space_or_eol_pop

  special_form:
    - match: __(?>MODULE|ENV|DIR|CALLER|STACKTRACE)__{{no_id_key_suffix}}
      scope: variable.language.special-form.elixir

  elixir_keywords:
    - match: when\b(?![?!])
      scope: keyword.operator.when.elixir
      comment: for `when`-clauses that are placed below the function header
      push:
        - match: (?=(?><-|->)(?!:))
          pop: 1
        - include: arguments_ws_rest_pop
    - match: |
        (?x)(?> # Ordered by likeliness of occurrence.
        # (do) | (fn) # Handled by do_block and fn_block.
        # (when)
          (true|false|nil)
        | (and|in|or|not)
        | (else)
        | (after)
        | (rescue)
        | (catch)
        | (end) # Normally in EEx: <% end %>
        )\b(?![?!])
      comment: fully reserved keywords
      captures:
        1: constant.language.elixir
        2: keyword.operator.logical.elixir
        3: keyword.control.conditional.else.elixir
        4: keyword.control.exception.after.elixir
        5: keyword.control.exception.rescue.elixir
        6: keyword.control.exception.catch.elixir
        7: keyword.context.block.end.elixir

  case_macro_call:
    - match: case(?!\(){{no_suffix_then_arguments}}
      scope: keyword.control.conditional.elixir
      push:
        - match: \s?
          scope: punctuation.section.arguments.begin.elixir
          set:
            - meta_scope: meta.function-call.arguments.elixir
            - match: do{{no_id_key_suffix}}
              scope: punctuation.section.block.begin.elixir keyword.context.block.do.elixir
              push:
                - meta_scope: meta.block.elixir
                - include: arrow_clauses_body_pop
            - include: last_arg_block_end_pop
            - include: arguments_ws_rest_pop

  one_left_arrow_clause:
    - match: (?=\S)
      push:
        - match: (?=<-(?!:)|,|(?>when|do){{no_id_key_suffix}}|do:(?!:))
          set:
            - match: (?=,|do{{no_id_key_suffix}}|do:(?!:))
              pop: 1
            - include: arguments_ws_rest_pop
        - include: parameters_or_if_closing_pop

  with_macro_call:
    - match: with(?!\(){{no_suffix_then_arguments}}
      scope: keyword.control.conditional.elixir
      push:
        - match: \s?
          scope: punctuation.section.arguments.begin.elixir
          set:
            - meta_scope: meta.function-call.arguments.elixir
            - match: (?=do:(?!:))
              set: [arguments_ws_closing_pop, arguments_ws_rest_pop]
            - match: do{{no_id_key_suffix}}
              scope: punctuation.section.block.begin.elixir keyword.context.block.do.elixir
              set:
                - meta_scope: meta.block.elixir
                - match: else{{no_id_key_suffix}}
                  scope: keyword.control.conditional.else.elixir
                  push: arrow_clauses_body_pop
                - include: last_arg_block_end_pop
                - include: core_syntax
            - include: arguments_ws_closing_pop
            - include: arg_comma_and_skip_ws
            - include: one_left_arrow_clause

  for_macro_call:
    - match: for(?!\(){{no_suffix_then_arguments}}
      scope: keyword.control.loop.for.elixir
      push:
        - match: \s?
          scope: punctuation.section.arguments.begin.elixir
          set:
            - meta_scope: meta.function-call.arguments.elixir
            - match: (?=reduce:(?!:))
              set:
                - match: do{{no_id_key_suffix}}
                  scope: punctuation.section.block.begin.elixir keyword.context.block.do.elixir
                  set:
                    - meta_scope: meta.block.elixir
                    - include: last_arg_block_end_pop
                    - match: ''
                      push: arrow_clauses_body_pop
                - include: arg_comma_and_skip_ws
                - include: arguments_ws_rest_pop
            - match: (?={{atom_symbol}}:(?!:))|(?<=[^:]:)
              set: [arguments_ws_closing_pop, arguments_ws_rest_pop]
            - include: arguments_ws_closing_pop
            - include: arg_comma_and_skip_ws
            - include: one_left_arrow_clause

  try_macro_call:
    - match: try(?!\(){{no_suffix_then_arguments}}
      scope: keyword.control.exception.try.elixir
      push:
        - match: \s?
          scope: punctuation.section.arguments.begin.elixir
          set:
            - meta_scope: meta.function-call.arguments.elixir
            - match: do{{no_id_key_suffix}}
              scope: punctuation.section.block.begin.elixir keyword.context.block.do.elixir
              set:
                - meta_scope: meta.block.elixir
                - include: error_handling_clauses
                - include: last_arg_block_end_pop
                - include: core_syntax
            - include: arguments_ws_closing_pop
            - include: arg_comma_and_skip_ws
            - include: core_syntax

  receive_macro_call:
    - match: receive(?!\(){{no_suffix_then_arguments}}
      scope: keyword.control.loop.receive.elixir
      push:
        - match: \s?
          scope: punctuation.section.arguments.begin.elixir
          set:
            - meta_scope: meta.function-call.arguments.elixir
            - match: do{{no_id_key_suffix}}
              scope: punctuation.section.block.begin.elixir keyword.context.block.do.elixir
              set:
                - meta_scope: meta.block.elixir
                - match: after{{no_id_key_suffix}}
                  scope: keyword.control.exception.catch.elixir
                  push: arrow_clauses_body_pop
                - include: last_arg_block_end_pop
                - match: ''
                  push: arrow_clauses_body_pop
            - include: arguments_ws_closing_pop
            - include: arg_comma_and_skip_ws
            - include: core_syntax

  elixir_functions:
    - include: unquote_call
    - include: case_macro_call
    - include: with_macro_call
    - include: for_macro_call
    - include: try_macro_call
    - include: receive_macro_call
    - match: |
        (?x)(?> # Ordered by likeliness of occurrence.
          (case|if|with|cond|unless)
        | (use|import|require)
        | (quote)
        | (raise|reraise|throw)
        | (for)
        | (defstruct|defexception|defoverridable)
        | (try)
        | (receive)
        | (exit)
        | (super)
        | (destructure)
        ){{no_suffix_then_arguments}}
      captures:
        1: keyword.control.conditional.elixir
        2: keyword.control.import.elixir
        3: keyword.other.quote.elixir
        4: keyword.control.flow.throw.elixir
        5: keyword.control.loop.for.elixir
        6: keyword.declaration.elixir
        7: keyword.control.exception.try.elixir
        8: keyword.control.loop.receive.elixir
        9: keyword.control.flow.exit.elixir
        10: keyword.other.super.elixir
        11: keyword.other.destructure.elixir
      push: arguments_paren_or_ws_pop
    - include: defrecord

  unquote_pop:
    - match: unquote(?:_splicing)?
      scope: keyword.other.unquote.elixir
      pop: 1

  unquote_call_pop:
    - match: (?=unquote(?:_splicing)?\()
      set: [unquote_post_args_pop, arguments_pop, unquote_pop]
    - match: (?=unquote(?:_splicing)?{{no_suffix_then_arguments}})
      set: [arguments_ws_pop, unquote_pop]

  unquote_post_args_pop:
    - match: (?=\()
      set: arguments_pop
    - include: if_empty_pop

  unquote_call:
    - match: (?=unquote(?:_splicing)?{{no_suffix_then_arguments}})
      push: unquote_call_pop

  line_continuation:
    - match: \\\n
      scope: punctuation.separator.continuation.elixir

  ## Helpers

  if_closing_token_pop:
    - match: (?=[)}\]]|>>(?!>)|%>|{{closing_keyword}})
      pop: 1

  if_ws_closing_token_pop:
    - match: (?=\s*(?>[)}\];]|>>(?!>)|%>|{{closing_keyword}}))
      pop: 1

  if_ws_closing_token_or_eol_pop:
    - include: if_ws_closing_token_pop
    - match: (?=\s*(?>#|$))
      pop: 1

  if_closing_paren_pop:
    - match: (?=\))
      pop: 1

  if_non_space_pop:
    - match: (?=\S)
      pop: 1

  if_non_space_or_eol_pop:
    - match: (?=\S|$)
      pop: 1

  if_empty_pop:
    - match: ''
      pop: 1