aboutsummaryrefslogtreecommitdiff
path: root/src/mdx/mdx_parser.zig
blob: 93dee1980a4dac19f77da0436a8af24be8e791d5 (plain) (blame)
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
const std = @import("std");
const logger = @import("../logger.zig");
const mdx_lexer = @import("./mdx_lexer.zig");
const Lexer = mdx_lexer.Lexer;
const importRecord = @import("../import_record.zig");
const js_ast = @import("../js_ast.zig");
const JSParser = @import("../js_parser/js_parser.zig").MDXParser;
const ParseStatementOptions = @import("../js_parser/js_parser.zig").ParseStatementOptions;

const options = @import("../options.zig");

const fs = @import("../fs.zig");
const _global = @import("../global.zig");
const string = _global.string;
const Output = _global.Output;
const Global = _global.Global;
const Environment = _global.Environment;
const strings = _global.strings;
const MutableString = _global.MutableString;
const stringZ = _global.stringZ;
const default_allocator = _global.default_allocator;
const C = _global.C;
const expect = std.testing.expect;
const ImportKind = importRecord.ImportKind;
const BindingNodeIndex = js_ast.BindingNodeIndex;
const Define = @import("../defines.zig").Define;
const js_lexer = @import("../js_lexer.zig");
const StmtNodeIndex = js_ast.StmtNodeIndex;
const ExprNodeIndex = js_ast.ExprNodeIndex;
const ExprNodeList = js_ast.ExprNodeList;
const StmtNodeList = js_ast.StmtNodeList;
const BindingNodeList = js_ast.BindingNodeList;
const ParserOptions = @import("../js_parser/js_parser.zig").Parser.Options;
const runVisitPassAndFinish = @import("../js_parser/js_parser.zig").Parser.runVisitPassAndFinish;
const Ref = @import("../ast/base.zig").Ref;
const assert = std.debug.assert;
const BabyList = js_ast.BabyList;

const LocRef = js_ast.LocRef;
const S = js_ast.S;
const B = js_ast.B;
const G = js_ast.G;
const T = mdx_lexer.T;
const E = js_ast.E;
const Stmt = js_ast.Stmt;
const Expr = js_ast.Expr;
const Binding = js_ast.Binding;
const Symbol = js_ast.Symbol;
const Level = js_ast.Op.Level;
const Op = js_ast.Op;
const Scope = js_ast.Scope;
const Range = logger.Range;

pub const Container = struct {
    ch: u8 = 0,
    is_loose: bool = false,
    is_task: bool = false,
    start: u32 = 0,
    mark_indent: u32 = 0,
    contents_indent: u32 = 0,
    block_index: u32 = 0,
    task_mark_off: u32 = 0,
};

pub const Block = struct {
    tag: Tag = Tag.html,
    flags: Block.Flags.Set = Block.Flags.Set{},
    data: u32 = 0,
    /// Leaf blocks:     Count of lines (MD_LINE or MD_VERBATIMLINE) on the block.
    /// LI:     Task mark offset in the input doc.
    /// OL:     Start item number.
    ///
    line_count: u32 = 0,
    line_offset: u32 = 0,
    detail: Block.Detail = Block.Detail{ .none = .{} },

    pub inline fn lines(this: Block, lines_: BabyList(Line)) []Line {
        return lines_.ptr[this.line_offset .. this.line_offset + this.line_count];
    }

    pub inline fn verbatimLines(this: Block, lines_: BabyList(Line.Verbatim)) []Line.Verbatim {
        return lines_.ptr[this.line_offset .. this.line_offset + this.line_count];
    }

    pub const Data = u32;

    pub const Flags = enum(u3) {
        container_opener = 0,
        container_closer = 1,
        loose_list = 2,
        setext_header = 3,

        pub const Set = std.enums.EnumSet(Block.Flags);
    };

    pub inline fn isContainer(this: Block) bool {
        return this.flags.contains(.container_opener) or this.flags.contains(.container_closer);
    }

    pub const Tag = enum {
        /// <body>...</body>      
        doc,

        /// <blockquote>...</blockquote>      
        quote,

        /// <ul>...</ul>
        ///Detail: Structure ul_detail.      
        ul,

        /// <ol>...</ol>
        ///Detail: Structure ol_detail.      
        ol,

        /// <li>...</li>
        ///Detail: Structure li_detail.      
        li,

        /// <hr>      
        hr,

        /// <h1>...</h1> (for levels up to 6)
        ///Detail: Structure h_detail.      
        h,

        /// <pre><code>...</code></pre>
        ///Note the text lines within code blocks are terminated with '\n'
        ///instead of explicit MD_TEXT_BR.      
        code,

        /// Raw HTML block. This itself does not correspond to any particular HTML
        ///tag. The contents of it _is_ raw HTML source intended to be put
        ///in verbatim form to the HTML output.      
        html,

        /// <p>...</p>      
        p,

        /// <table>...</table> and its contents.
        ///Detail: Structure table_detail (for table),
        ///        structure td_detail (for th and td)
        ///Note all of these are used only if extension MD_FLAG_TABLES is enabled.      
        table,
        thead,
        tbody,
        tr,
        th,
        td,
    };

    pub const UL = struct {
        tight: bool = false,
        mark: u8 = '*',
    };

    pub const OL = struct {
        start: u32 = 0,
        tight: bool = false,
        mark: u8 = '*',
    };

    pub const LI = struct {
        /// Can be non-zero only with MD_FLAG_TASKLISTS
        task: bool = false,
        /// is_task, then one of 'x', 'X' or ' '. Undefined otherwise.
        task_mark: u8 = 'x',
        /// If is_task, then offset in the input of the char between '[' and ']'. 
        task_mark_off: u32 = 0,
    };

    pub const Header = u4;

    pub const Code = struct {
        info: Attribute = .{},
        lang: Attribute = .{},
        /// character used for fenced code block; or zero for indented code block. *
        fence: u8 = '`',
    };

    pub const Table = struct {
        /// Count of columns in the table. 
        column_count: u32 = 0,
        /// Count of rows in the table header (currently always 1) 
        head_row_count: u32 = 1,
        /// Count of rows in the table body 
        body_row_count: u32 = 0,
    };

    pub const Detail = union {
        none: void,
        ul: UL,
        ol: OL,
        li: LI,
    };

    pub const TD = struct {
        alignment: Align = Align.default,
    };
};
pub const Span = struct {
    pub const Tag = enum {
        /// <em>...</em>
        em,

        /// <strong>...</strong>
        strong,

        /// <a href="xxx">...</a>
        /// Detail: Structure a_detail.
        a,

        /// <img src="xxx">...</a>
        /// Detail: Structure img_detail.
        /// Note: Image text can contain nested spans and even nested images.
        /// If rendered into ALT attribute of HTML <IMG> tag, it's responsibility
        /// of the parser to deal with it.
        img,

        /// <code>...</code>
        code,

        /// <del>...</del>
        /// Note: Recognized only when MD_FLAG_STRIKETHROUGH is enabled.
        del,

        /// For recognizing inline ($) and display ($$) equations
        /// Note: Recognized only when MD_FLAG_LATEXMATHSPANS is enabled.
        latexmath,
        latexmath_display,

        /// Wiki links
        /// Note: Recognized only when MD_FLAG_WIKILINKS is enabled.
        wikilink,

        /// <u>...</u>
        /// Note: Recognized only when MD_FLAG_UNDERLINE is enabled.
        u,
    };

    pub const Link = struct {
        src: Attribute = .{},
        title: Attribute = .{},
    };

    pub const Image = Link;

    pub const Wikilink = struct {
        target: Attribute = .{},
    };
};

pub const Text = enum {
    /// Normal text. 
    normal,
    /// NULL character. CommonMark requires replacing NULL character with
    /// the replacement char U+FFFD, so this allows caller to do that easily. 
    nullchar,
    /// Line breaks.
    /// Note these are not sent from blocks with verbatim output (MD_BLOCK_CODE
    /// or MD_BLOCK_HTML). In such cases, '\n' is part of the text itself. 
    /// <br> (hard break) 
    br,
    /// '\n' in source text where it is not semantically meaningful (soft break) 
    softbr,
    /// Entity.
    /// (a) Named entity, e.g. &nbsp; 
    ///     (Note MD4C does not have a list of known entities.
    ///     Anything matching the regexp /&[A-Za-z][A-Za-z0-9]{1,47};/ is
    ///     treated as a named entity.)
    /// (b) Numerical entity, e.g. &#1234;
    /// (c) Hexadecimal entity, e.g. &#x12AB;
    ///
    /// As MD4C is mostly encoding agnostic, application gets the verbatim
    /// entity text into the MD_PARSER::text_callback(). 
    entity,
    /// Text in a code block (inside MD_BLOCK_CODE) or inlined code (`code`).
    /// If it is inside MD_BLOCK_CODE, it includes spaces for indentation and
    /// '\n' for new lines. br and softbr are not sent for this
    /// kind of text. 
    code,
    /// Text is a raw HTML. If it is contents of a raw HTML block (i.e. not
    /// an inline raw HTML), then br and softbr are not used.
    /// The text contains verbatim '\n' for the new lines. 
    html,
    /// Text is inside an equation. This is processed the same way as inlined code
    /// spans (`code`). 
    latexmath,
};
pub const Align = enum(u3) {
    default = 0,
    left = 1,
    center = 2,
    right = 3,
};

/// String attribute.
///
/// This wraps strings which are outside of a normal text flow and which are
/// propagated within various detailed structures, but which still may contain
/// string portions of different types like e.g. entities.
///
/// So, for example, lets consider this image:
///
///     ![image alt text](http://example.org/image.png 'foo &quot; bar')
///
/// The image alt text is propagated as a normal text via the MD_PARSER::text()
/// callback. However, the image title ('foo &quot; bar') is propagated as
/// MD_ATTRIBUTE in MD_SPAN_IMG_DETAIL::title.
///
/// Then the attribute MD_SPAN_IMG_DETAIL::title shall provide the following:
///  -- [0]: "foo "   (substr_types[0] == MD_TEXT_NORMAL; substr_offsets[0] == 0)
///  -- [1]: "&quot;" (substr_types[1] == MD_TEXT_ENTITY; substr_offsets[1] == 4)
///  -- [2]: " bar"   (substr_types[2] == MD_TEXT_NORMAL; substr_offsets[2] == 10)
///  -- [3]: (n/a)    (n/a                              ; substr_offsets[3] == 14)
///
/// Note that these invariants are always guaranteed:
///  -- substr_offsets[0] == 0
///  -- substr_offsets[LAST+1] == size
///  -- Currently, only MD_TEXT_NORMAL, MD_TEXT_ENTITY, MD_TEXT_NULLCHAR
///     substrings can appear. This could change only of the specification
///     changes.
///
pub const Attribute = struct {
    text: []const u8 = "",
    substring: Substring.List = .{},
};
pub const Substring = struct {
    offset: u32,
    tag: Text,

    pub const List = std.MultiArrayList(Substring);
    pub const ListPool = ObjectPool(List);
};

pub const Mark = struct {
    position: Ref = Ref{},
    prev: u32 = std.math.maxInt(u32),
    next: u32 = std.math.maxInt(u32),
    ch: u8 = 0,
    flags: u16 = 0,

    /// Maybe closer. 
    pub const potential_closer = 0x02;
    /// Maybe opener. 
    pub const potential_opener = 0x01;
    /// Definitely opener. 
    pub const opener = 0x04;
    /// Definitely closer. 
    pub const closer = 0x08;
    /// Resolved in any definite way. 
    pub const resolved = 0x10;

    /// Helper for the "rule of 3". */
    pub const emph_intraword = 0x20;
    pub const emph_mod3_0 = 0x40;
    pub const emph_mod3_1 = 0x80;
    pub const emph_mod3_2 = (0x40 | 0x80);
    pub const emph_mod3_mask = (0x40 | 0x80);
    /// Distinguisher for '<', '>'. */
    pub const autolink = 0x20;
    /// For permissive autolinks. */
    pub const validpermissiveautolink = 0x20;
    /// For '[' to rule out invalid link labels early */
    pub const hasnestedbrackets = 0x20;

    /// During analyzes of inline marks, we need to manage some "mark chains",
    /// of (yet unresolved) openers. This structure holds start/end of the chain.
    /// The chain internals are then realized through MD_MARK::prev and ::next.
    pub const Chain = struct {
        head: u32 = std.math.maxInt(u32),
        tail: u32 = std.math.maxInt(u32),

        pub const List = struct {
            data: [13]Chain = [13]Chain{ .{}, .{}, .{}, .{}, .{}, .{}, .{}, .{}, .{}, .{}, .{}, .{} },
            pub inline fn ptr_chain(this: *List) *Chain {
                return &this.data[0];
            }
            pub inline fn tablecellboundaries(this: *List) *Chain {
                return &this.data[1];
            }
            pub inline fn asterisk_openers_extraword_mod3_0(this: *List) *Chain {
                return &this.data[2];
            }
            pub inline fn asterisk_openers_extraword_mod3_1(this: *List) *Chain {
                return &this.data[3];
            }
            pub inline fn asterisk_openers_extraword_mod3_2(this: *List) *Chain {
                return &this.data[4];
            }
            pub inline fn asterisk_openers_intraword_mod3_0(this: *List) *Chain {
                return &this.data[5];
            }
            pub inline fn asterisk_openers_intraword_mod3_1(this: *List) *Chain {
                return &this.data[6];
            }
            pub inline fn asterisk_openers_intraword_mod3_2(this: *List) *Chain {
                return &this.data[7];
            }
            pub inline fn underscore_openers(this: *List) *Chain {
                return &this.data[8];
            }
            pub inline fn tilde_openers_1(this: *List) *Chain {
                return &this.data[9];
            }
            pub inline fn tilde_openers_2(this: *List) *Chain {
                return &this.data[10];
            }
            pub inline fn bracket_openers(this: *List) *Chain {
                return &this.data[11];
            }
            pub inline fn dollar_openers(this: *List) *Chain {
                return &this.data[12];
            }
        };
    };
};

pub const Line = struct {
    beg: u32 = 0,
    end: u32 = 0,

    pub const Tag = enum(u32) {
        blank,
        hr,
        atx_header,
        setext_header,
        setext_underline,
        indented_code,
        fenced_code,
        html,
        text,
        table,
        table_underline,
    };
    pub const Analysis = packed struct {
        tag: Tag = Tag.blank,
        beg: u32 = 0,
        end: u32 = 0,
        indent: u32 = 0,
        data: u32 = 0,

        pub const blank = Analysis{};
        pub fn eql(a: Analysis, b: Analysis) bool {
            return strings.eqlLong(std.mem.asBytes(&a), std.mem.asBytes(&b), false);
        }
    };

    pub const Verbatim = struct {
        line: Line = Line{},
        indent: u32 = 0,
    };
};

pub const MDParser = struct {
    marks: BabyList(Mark) = .{},
    chain: Mark.Chain.List = .{},
    source: logger.Source,
    flags: Flags.Set = Flags.commonmark,
    allocator: std.mem.Allocator,
    mdx: *MDX,
    mark_char_map: [255]u1 = undefined,
    doc_ends_with_newline: bool = false,
    size: u32 = 0,

    lines: BabyList(Line) = .{},
    verbatim_lines: BabyList(Line.Verbatim) = .{},

    containers: BabyList(Container) = .{},
    blocks: BabyList(Block) = .{},
    current_block: ?*Block = null,
    current_block_index: u32 = 0,

    code_fence_length: u32 = 0,
    code_indent_offset: u32 = std.math.maxInt(u32),
    last_line_has_list_loosening_effect: bool = false,
    last_list_item_starts_with_two_blank_lines: bool = false,

    pub const Flags = enum {
        /// In MD_TEXT_NORMAL, collapse non-trivial whitespace into single ' ' 
        collapse_whitespace,
        /// Do not require space in ATX headers ( ###header ) 
        permissive_atxheaders,
        /// Recognize URLs as autolinks even without '<', '>' 
        permissive_url_autolinks,
        /// Recognize e-mails as autolinks even without '<', '>' and 'mailto:' 
        permissive_email_autolinks,
        /// Disable indented code blocks. (Only fenced code works.)  
        noindented_codeblocks,
        /// Disable raw HTML blocks. 
        no_html_blocks,
        /// Disable raw HTML (inline). 
        no_html_spans,
        /// Enable tables extension. 
        tables,
        /// Enable strikethrough extension. 
        strikethrough,
        /// Enable WWW autolinks (even without any scheme prefix, if they begin with 'www.') 
        permissive_www_autolinks,
        /// Enable task list extension.
        tasklists,
        /// Enable $ and $$ containing LaTeX equations. 
        latex_mathspans,
        /// Enable wiki links extension.  
        wikilinks,
        /// Enable underline extension (and disables '_' for normal emphasis). 
        underline,

        pub const Set = std.enums.EnumSet(Flags);
        pub const permissive_autolinks = Set.init(.{ .permissive_email_autolinks = true, .permissive_url_autolinks = true });
        pub const no_email = Set.init(.{ .no_html_blocks = true, .no_html_spans = true });
        pub const github = Set.init(.{ .tables = true, .permissive_autolinks = true, .strikethrough = true, .tasklists = true });
        pub const commonmark: i32 = Set{};
    };

    fn buildCharMap(this: *MDParser) void {
        @memset(&this.mark_char_map, 0, this.mark_char_map.len);

        this.mark_char_map['\\'] = 1;
        this.mark_char_map['*'] = 1;
        this.mark_char_map['_'] = 1;
        this.mark_char_map['`'] = 1;
        this.mark_char_map['&'] = 1;
        this.mark_char_map[';'] = 1;
        this.mark_char_map['<'] = 1;
        this.mark_char_map['>'] = 1;
        this.mark_char_map['['] = 1;
        this.mark_char_map['!'] = 1;
        this.mark_char_map[']'] = 1;
        this.mark_char_map[0] = 1;

        // whitespace
        this.mark_char_map[' '] = 1;
        this.mark_char_map['\t'] = 1;
        this.mark_char_map['\r'] = 1;
        this.mark_char_map['\n'] = 1;

        // form feed
        this.mark_char_map[0xC] = 1;
        // vertical tab
        this.mark_char_map[0xB] = 1;

        if (this.flags.contains(.strikethrough)) {
            this.mark_char_map['~'] = 1;
        }

        if (this.flags.contains(.latex_mathspans)) {
            this.mark_char_map['$'] = 1;
        }

        if (this.flags.contains(.permissive_email_autolinks)) {
            this.mark_char_map['@'] = 1;
        }

        if (this.flags.contains(.permissive_url_autolinks)) {
            this.mark_char_map[':'] = 1;
        }

        if (this.flags.contains(.permissive_www_autolinks)) {
            this.mark_char_map['.'] = 1;
        }

        if (this.flags.contains(.tables)) {
            this.mark_char_map['.'] = 1;
        }
    }
    pub fn init(allocator: std.mem.Allocator, source: logger.Source, flags: Flags.Set, mdx: *MDX) MDParser {
        var parser = MDParser{
            .allocator = allocator,
            .source = source,
            .flags = flags,
            .mdx = mdx,
            .size = @truncate(u32, source.contents.len),
        };
        parser.buildCharMap();
        parser.doc_ends_with_newline = source.contents.len.len > 0 and source.contents[source.contents.len - 1] == '\n';
        return parser;
    }

    fn startNewBlock(this: *MDParser, line: *const Line.Analysis) !void {
        try this.blocks.push(
            this.allocator,
            Block{
                .tag = switch (line.tag) {
                    .hr => Block.Tag.hr,
                    .atx_header, .setext_header => Block.Tag.h,
                    .fenced_code, .indented_code => Block.Tag.code,
                    .text => Block.Tag.p,
                    .html => Block.Tag.html,
                    else => unreachable,
                },
                .data = line.data,
                .line_count = 0,
                .line_offset = switch (line.tag) {
                    .indented_code, .html, .fenced_code => this.verbatim_lines.len,
                    else => this.lines.len,
                },
            },
        );
    }

    inline fn charAt(this: *const MDParser, index: u32) u8 {
        return this.source.contents[index];
    }

    inline fn isNewline(this: *const MDParser, index: u32) bool {
        return switch (this.charAt(index)) {
            '\n', '\r' => true,
            else => false,
        };
    }

    inline fn isAnyOf2(this: *const MDParser, index: u32, comptime first: u8, comptime second: u8) bool {
        return isAnyOf2_(this.charAt(index), first, second);
    }

    inline fn isAnyOf2_(char: u8, comptime first: u8, comptime second: u8) bool {
        return switch (char) {
            first, second => true,
            else => false,
        };
    }

    inline fn isAnyOf(this: *const MDParser, index: u32, comptime values: []const u8) bool {
        return isCharAnyOf(this.charAt(index), values);
    }

    inline fn isCharAnyOf(char: u8, comptime values: []const u8) bool {
        inline for (values) |val| {
            if (val == char) return true;
        }
        return false;
    }

    inline fn isBlank(char: u8) bool {
        return isCharAnyOf(char, &[_]u8{ ' ', '\t' });
    }

    inline fn isWhitespace(char: u8) bool {
        return isCharAnyOf(char, &[_]u8{ ' ', '\t', 0xC, 0xB });
    }

    pub fn getIndent(this: *MDParser, total_indent: u32, beg: u32, end: *u32) u32 {
        var off = beg;
        var indent = total_indent;
        while (off < this.size and isBlank(this.charAt(off))) {
            if (this.charAt(off) == '\t') {
                indent = (indent + 4) & ~3;
            } else {
                indent += 1;
            }
            off += 1;
        }
        end.* = off;
        return indent - total_indent;
    }

    pub fn isContainerMark(this: *MDParser, indent: u32, beg: u32, end: *u32, container: *Container) bool {
        var off = beg;
        var max_end: u32 = undefined;

        if (off >= this.size or indent >= this.code_indent_offset)
            return false;

        if (this.charAt(off) == '>') {
            off += 1;
            container.ch = '>';
            container.is_loose = false;
            container.is_task = false;
            container.mark_indent = indent;
            container.contents_indent = indent + 1;
            end.* = off;
            return true;
        }

        // Check for list item bullet mark.
        if (this.isAnyOf(off, "-+*") and (off + 1 >= this.size or isBlank(this.charAt(off + 1)) or this.isNewline(off + 1))) {
            container.ch = this.charAt(off);
            container.is_loose = false;
            container.is_task = false;
            container.mark_indent = indent;
            container.contents_indent = indent + 1;
            end.* = off + 1;
            return true;
        }

        // Check for ordered list item marks
        max_end = @minimum(off + 9, this.size);
        container.start = 0;
        while (off < max_end and std.ascii.isDigit(this.charAt(off))) {
            container.start = container.start * 10 + (this.charAt(off) - '0');
            off += 1;
        }

        if (off > beg and
            off < this.size and
            (this.isAnyOf2(off, '.', ')')) and
            (off + 1 >= this.size or
            this.isBlank(this.charAt(off + 1) or
            this.isNewline(off + 1))))
        {
            container.ch = this.charAt(off);
            container.is_loose = false;
            container.is_task = false;
            container.mark_indent = indent;
            container.contents_indent = indent + off - beg + 1;
            end.* = off + 1;
            return true;
        }

        return false;
    }

    fn analyzeLine(this: *MDParser, beg: u32, end: *u32, pivot_line: *const Line.Analysis, line: *Line.Analysis) !void {
        _ = this;
        _ = beg;
        _ = end;
        _ = pivot_line;
        _ = line;
        var off = beg;
        var hr_killer: u32 = 0;
        var prev_line_has_list_loosening_effect = this.last_line_has_list_loosening_effect;
        var container = Container{};
        _ = hr_killer;
        _ = prev_line_has_list_loosening_effect;
        _ = container;
        var total_indent: u32 = 0;
        var n_parents: u32 = 0;
        var n_brothers: u32 = 0;
        var n_children: u32 = 0;

        // Given the indentation and block quote marks '>', determine how many of
        // the current containers are our parents.
        while (n_parents < this.containers.len) {
            var c: *Container = this.containers.ptr + n_parents;

            if (c.ch == '>' and line.indent < this.code_indent_offset and off < this.size and this.charAt(off) == '>') {
                off += 1;
                total_indent += 1;
                line.indent = this.getIndent(total_indent, off, &off);
                total_indent += line.indent;

                // The optional 1st space after '>' is part of the block quote mark.
                line.indent -|= line.indent;
                line.beg = off;
            } else if (c.ch != '>' and line.indent >= c.contents_indent) {
                line.indent -|= c.contents_indent;
            } else {
                break;
            }

            n_parents += 1;
        }

        if (off >= this.size or this.isNewline(off)) {
            // Blank line does not need any real indentation to be nested inside a list
            if (n_brothers + n_children == 0) {
                while (n_parents < this.containers.len and this.containers.ptr[n_parents].ch == '>') {
                    n_parents += 1;
                }
            }
        }

        while (true) {
            switch (pivot_line.tag) {
                .fencedcode => {
                    // Check whether we are fenced code continuation.
                    line.beg = off;

                    // We are another MD_LINE_FENCEDCODE unless we are closing fence
                    // which we transform into MD_LINE_BLANK.
                    if (line.indent < this.code_indent_offset) {
                        if (this.isClosingCodeFence(this.charAt(pivot_line.beg), off, &off)) {
                            line.tag = .blank;
                            this.last_line_has_list_loosening_effect = false;
                            break;
                        }
                    }

                    // Change indentation accordingly to the initial code fence.
                    if (n_parents == this.containers.len) {
                        line.indent -|= pivot_line.indent;
                        line.tag = .fenced_code;
                        break;
                    }
                },

                .indentedcode => {},
                .text => {},

                .html => {},
                else => {},
            }

            // Check for blank line.
            if (off >= this.size or this.isNewline(off)) {
                if (pivot_line.tag == .indented_code and n_parents == this.containers.len) {
                    line.tag = .indented_code;
                    line.indent -|= this.code_indent_offset;
                    this.last_line_has_list_loosening_effect = false;
                } else {
                    line.tag = .blank;
                    this.last_line_has_list_loosening_effect = n_parents > 0 and
                        n_brothers + n_children == 0 and
                        this.containers.ptr[n_parents - 1].ch != '>';

                    // See https://github.com/mity/md4c/issues/6
                    //
                    // This ugly checking tests we are in (yet empty) list item but
                    // not its very first line (i.e. not the line with the list
                    // item mark).
                    //
                    // If we are such a blank line, then any following non-blank
                    // line which would be part of the list item actually has to
                    // end the list because according to the specification, "a list
                    // item can begin with at most one blank line."
                    //
                    if (n_parents > 0 and this.containers.ptr[n_parents - 1].ch != '>' and n_brothers + n_children == 0 and this.current_block == null and this.blocks.len > 0) {
                        var top_block = this.blocks.last().?;
                        if (top_block.tag == .li) {
                            this.last_list_item_starts_with_two_blank_lines = true;
                        }
                    }
                }
                break;
            } else {
                // This is the 2nd half of the hack. If the flag is set (i.e. there
                // was a 2nd blank line at the beginning of the list item) and if
                // we would otherwise still belong to the list item, we enforce
                // the end of the list.
                this.last_line_has_list_loosening_effect = false;
                if (this.last_list_item_starts_with_two_blank_lines) {
                    if (n_parents > 0 and
                        this.containers.ptr[n_parents - 1].ch != '>' and
                        n_brothers + n_children == 0 and
                        this.current_block == null and this.blocks.len > 1)
                    {
                        var top = this.blocks.last().?;
                        if (top.tag == .li) {
                            n_parents -|= 1;
                        }
                    }
                    this.last_line_has_list_loosening_effect = true;
                }
            }

            // Check whether we are Setext underline.
            if (line.indent < this.code_indent_offset and
                pivot_line.tag == .text and
                off < this.size and
                this.isAnyOf2(off, '=', '-') and
                n_parents == this.containers.len)
            {
                var level: u4 = 0;
                if (this.isSetextUnderline(off, &off, &level)) {
                    line.tag = .setext_underline;
                    line.data = level;
                    break;
                }
            }

            // Check for a thematic break line
            if (line.indent < this.code_indent_offset and off < this.size and off >= hr_killer and this.isAnyOf(off, "-_*")) {
                if (this.isHRLine(off, &off, &hr_killer)) {
                    line.tag = .hr;
                    break;
                }
            }

            // Check for "brother" container. I.e. whether we are another list item
            //in already started list.
            if (n_parents < this.containers.len and n_brothers + n_children == 0) {
                var tmp: u32 = undefined;

                if (this.isContainerMark(line.indent, off, &tmp, &container) and
                    isContainerCompatible(&this.containers.ptr[n_parents], &container))
                {
                    pivot_line.* = Line.Analysis.blank;
                    off = tmp;

                    total_indent += container.contents_indent - container.mark_indent;
                    line.indent = this.getIndent(total_indent, off, &off);
                    total_indent += line.indent;
                    line.beg = off;

                    //  Some of the following whitespace actually still belongs to the mark.
                    if (off >= this.size or this.isNewline(off)) {
                        container.contents_indent += 1;
                    } else if (line.indent <= this.code_indent_offset) {
                        container.contents_indent += line.indent;
                        line.indent = 0;
                    } else {
                        container.contents_indent += 1;
                        line.indent -= 1;
                    }

                    this.containers.ptr[n_parents].mark_indent = container.mark_indent;
                    this.containers.ptr[n_parents].contents_indent = container.contents_indent;
                    n_brothers += 1;
                    continue;
                }
            }

            // Check for indented code
            // Note: indented code block cannot interrupt a paragrpah
            if (line.indent >= this.code_indent_offset and
                (pivot_line.tag == .blank or
                pivot_line.tag == .indented_code))
            {
                line.tag = .indented_code;
                std.debug.assert(line.indent >= this.code_indent_offset);
                line.indent -|= this.code_indent_offset;
                line.data = 0;
                break;
            }

            // Check for start of a new container block
            if (line.indent < this.code_indent_offset and
                this.isContainerMark(line.indent, off, &off, &container))
            {
                if (pivot_line.tag == .text and
                    n_parents == this.n_containers and
                    (off >= this.size or this.isNewline(off)) and
                    container.ch != '>')
                {
                    // Noop. List mark followed by a blank line cannot interrupt a paragraph.
                } else if (pivot_line.tag == .text and
                    n_parents == this.containers.len and
                    isAnyOf2_(container.ch, '.', ')'))
                {
                    // Noop. Ordered list cannot interrupt a paragraph unless the start index is 1.
                } else {
                    total_indent += container.contents_indent - container.mark_indent;
                    line.indent = this.getIndent(total_indent, off, &off);
                    total_indent += line.indent;

                    line.beg = off;
                    line.data = container.ch;

                    // Some of the following whitespace actually still belongs to the mark.
                    if (off >= this.size or this.isNewline(off)) {
                        container.contents_indent += 1;
                    } else if (line.indent <= this.code_indent_offset) {
                        container.contents_indent += line.indent;
                        line.indent = 0;
                    } else {
                        container.contents_indent += 1;
                        line.indent -= 1;
                    }

                    if (n_brothers + n_children == 0) {
                        pivot_line.* = Line.Analysis.blank;
                    }

                    if (n_children == 0) {
                        try this.leaveChildContainers(n_parents + n_brothers);
                    }

                    n_children += 1;
                    try this.pushContainer(container);
                    continue;
                }
            }

            // heck whether we are table continuation.
            if (pivot_line.tag == .table and n_parents == this.n_containers) {
                line.tag = .table;
                break;
            }

            // heck for ATX header.
            if (line.indent < this.code_indent_offset and off < this.size and this.isAnyOf(off, '#')) {
                var level: u4 = 0;
                if (this.isATXHeaderLine(off, &line.beg, &off, &level)) {
                    line.tag = .atx_header;
                    line.data = level;
                    break;
                }
            }

            // Check whether we are starting code fence.
            if (off < this.size and this.isAnyOf2(off, '`', '~')) {
                if (this.isOpeningCodeFence(off, &off)) {
                    line.tag = .fenced_code;
                    line.data = 1;
                    break;
                }
            }

            // Check for start of raw HTML block.
            if (off < this.size and !this.flags.contains(.no_html_blocks) and this.charAt(off) == '<') {}

            // Check for table underline.
            if (this.flags.contains(.tables) and pivot_line.tag == .text and off < this.size and this.isAnyOf(off, "|-:") and n_parents == this.containers.len) {
                var col_count: u32 = undefined;

                if (this.current_block != null and this.current_block.?.line_count == 1 and this.isTableUnderline(off, &off, &col_count)) {
                    line.data = col_count;
                    line.tag = .table_underline;
                    break;
                }
            }

            //  By default, we are normal text line.
            line.tag = .text;
            if (pivot_line.tag == .text and n_brothers + n_children == 0) {
                // lazy continuation
                n_parents = this.containers.len;
            }

            // Check for task mark.
            if (this.flags.contains(.tasklists) and
                n_brothers + n_children > 0 and
                off < this.size and
                isCharAnyOf(this.containers.last().?.ch, "-+*.)"))
            {
                var tmp: u32 = off;

                while (tmp < this.size and tmp < off + 3 and isBlank(tmp)) {
                    tmp += 1;
                }

                if ((tmp + 2 < this.size and
                    this.charAt(tmp) == '[' and
                    this.isAnyOf(tmp + 1, "xX ") and
                    this.charAt(tmp + 2) == ']') and
                    (tmp + 3 == this.size or
                    isBlank(this.charAt(tmp + 3)) or
                    this.isNewline(tmp + 3)))
                {
                    var task_container: *Container = if (n_children > 0) this.containers.last().? else &container;
                    task_container.is_task = true;
                    task_container.task_mark_off = tmp + 1;
                    off = tmp + 3;
                    while (off < this.size and isWhitespace(this.charAt(off))) {
                        off += 1;
                    }
                    if (off == this.size) break;
                    line.beg = off;
                }
            }

            break;
        }

        // Scan for end of the line.
        while (off + 3 < this.size and
            !(strings.eqlComptimeIgnoreLen(this.source.contents.ptr[off..][0..4], "\n\n\n\n") or
            strings.eqlComptimeIgnoreLen(this.source.contents.ptr[off..][0..4], "\r\n\r\n")))
        {
            off += 4;
        }

        while (off < this.size and !this.isNewline(off)) {
            off += 1;
        }

        // Set end of line
        line.end = off;

        // ut for ATX header, we should exclude the optional trailing mark.
        if (line.type == .atx_header) {
            var tmp = line.end;
            while (tmp > line.beg and this.charAt(tmp - 1) == ' ') {
                tmp -= 1;
            }

            while (tmp > line.beg and this.charAt(tmp - 1) == '#') {
                tmp -= 1;
            }

            if (tmp == line.beg or this.charAt(tmp - 1) == ' ' or this.flags.contains(.permissive_atxheaders)) {
                line.end = tmp;
            }
        }

        // Trim trailing spaces.
        switch (line.tag) {
            .indented_code, .fenced_code => {},
            else => {
                while (line.end > line.beg and this.charAt(line.end - 1) == ' ') {
                    line.end -= 1;
                }
            },
        }

        // Eat also the new line
        if (off < this.size and this.charAt(off) == '\r') {
            off += 1;
        }

        if (off < this.size and this.charAt(off) == '\n') {
            off += 1;
        }

        end.* = off;

        // If we belong to a list after seeing a blank line, the list is loose.
        if (prev_line_has_list_loosening_effect and line.tag != .blank and n_parents + n_brothers > 0) {
            var c: *Container = this.containers.ptr[n_parents + n_brothers - 1];
            if (c.ch != '>') {
                var block: *Block = this.blocks.ptr[c.block_index];
                block.flags.insert(.loose_list);
            }
        }

        // Leave any containers we are not part of anymore.
        if (n_children == 0 and n_parents + n_brothers < this.containers.len) {
            try this.leaveChildContainers(n_parents + n_brothers);
        }

        // Enter any container we found a mark for
        if (n_brothers > 0) {
            std.debug.assert(n_brothers == 0);
            try this.pushContainerBytes(
                Block.Tag.li,
                this.containers.ptr[n_parents].task_mark_off,
                if (this.containers.ptr[n_parents].is_task) this.charAt(this.containers.ptr[n_parents].task_mark_off) else 0,
                Block.Flags.container_closer,
            );
            try this.pushContainerBytes(
                Block.Tag.li,
                container.task_mark_off,
                if (container.is_task) this.charAt(container.task_mark_off) else 0,
                Block.Flags.container_opener,
            );
            this.containers.ptr[n_parents].is_task = container.is_task;
            this.containers.ptr[n_parents].task_mark_off = container.task_mark_off;
        }

        if (n_children > 0) {
            try this.enterChildContainers(n_children);
        }
    }
    fn processLine(this: *MDParser, p_pivot_line: **const Line.Analysis, line: *Line.Analysis) !void {
        var pivot_line = p_pivot_line.*;

        switch (line.tag) {
            .blank => {
                // Blank line ends current leaf block.
                try this.endCurrentBlock();
                p_pivot_line.* = Line.Analysis.blank;
            },
            .hr, .atx_header => {
                try this.endCurrentBlock();

                // Add our single-line block
                try this.startNewBlock(line);
                try this.addLineIntoCurrentBlock(line);
                try this.endCurrentBlock();
                p_pivot_line.* = &Line.Analysis.blank;
            },
            .setext_underline => {
                this.current_block.?.tag = .table;
                this.current_block.?.data = line.data;
                this.current_block.?.flags.insert(.setext_header);
                try this.addLineIntoCurrentBlock(line);
                try this.endCurrentBlock();
                if (this.current_block == null) {
                    p_pivot_line.* = &Line.Analysis.blank;
                } else {
                    // This happens if we have consumed all the body as link ref. defs.
                    //and downgraded the underline into start of a new paragraph block.
                    line.tag = .text;
                    p_pivot_line.* = line;
                }
            },
            // MD_LINE_TABLEUNDERLINE changes meaning of the current block.
            .table_underline => {
                var current_block = this.current_block.?;
                std.debug.assert(current_block.line_count == 1);
                current_block.tag = .table;
                current_block.data = line.data;
                std.debug.assert(pivot_line != &Line.Analysis.blank);
                @intToPtr(*Line.Analysis, @ptrToInt(p_pivot_line.*)).tag = .table;
                try this.addLineIntoCurrentBlock(line);
            },
            else => {
                // The current block also ends if the line has different type.
                if (line.tag != pivot_line.tag) {
                    try this.endCurrentBlock();
                }

                // The current line may start a new block.
                if (this.current_block == null) {
                    try this.startNewBlock(line);
                    p_pivot_line.* = line;
                }

                // In all other cases the line is just a continuation of the current block.
                try this.addLineIntoCurrentBlock(line);
            },
        }
    }
    fn consumeLinkReferenceDefinitions(this: *MDParser) !void {
        _ = this;
    }
    fn addLineIntoCurrentBlock(this: *MDParser, analysis: *const Line.Analysis) !void {
        var current_block = this.current_block.?;

        switch (current_block.tag) {
            .code, .html => {
                if (current_block.line_count > 0)
                    std.debug.assert(
                        this.verbatim_lines.len == current_block.line_count + current_block.line_offset,
                    );
                if (current_block.line_count == 0) {
                    current_block.line_offset = this.verbatim_lines.len;
                }

                try this.verbatim_lines.push(this.allocator, Line.Verbatim{
                    .indent = analysis.indent,
                    .line = .{
                        .beg = analysis.beg,
                        .end = analysis.end,
                    },
                });
            },
            else => {
                if (current_block.line_count > 0)
                    std.debug.assert(
                        this.lines.len == current_block.line_count + current_block.line_offset,
                    );
                if (current_block.line_count == 0) {
                    current_block.line_offset = this.lines.len;
                }
                this.lines.push(this.allocator, .{ .beg = analysis.beg, .end = analysis.end });
            },
        }

        current_block.line_count += 1;
    }
    fn endCurrentBlock(this: *MDParser) !void {
        _ = this;

        var block = this.current_block orelse return;
        // Check whether there is a reference definition. (We do this here instead
        // of in md_analyze_line() because reference definition can take multiple
        // lines.) */
        if ((block.tag == .p or block.tag == .h) and block.flags.contains(.setext_header)) {
            var lines = block.lines(this.lines);
            if (lines[0].beg == '[') {
                try this.consumeLinkReferenceDefinitions();
                block = this.current_block orelse return;
            }
        }

        if (block.tag == .h and block.flags.contains(.setext_header)) {
            var n_lines = block.line_count;
            if (n_lines > 1) {
                // get rid of the underline
                if (this.lines.len == block.line_count + block.line_offset) {
                    this.lines.len -= 1;
                }
                block.line_count -= 1;
            } else {
                // Only the underline has left after eating the ref. defs.
                // Keep the line as beginning of a new ordinary paragraph. */
                block.tag = .p;
            }
        }

        // Mark we are not building any block anymore.
        this.current_block = null;
        this.current_block_index -|= 1;
    }
    fn buildRefDefHashTable(this: *MDParser) !void {
        _ = this;
    }
    fn leaveChildContainers(this: *MDParser, keep: u32) !void {
        _ = this;
        while (this.containers.len > keep) {
            var c = this.containers.last().?;
            var is_ordered_list = false;
            switch (c.ch) {
                ')', '.' => {
                    is_ordered_list = true;
                },
                '-', '+', '*' => {
                    try this.pushContainerBytes(
                        Block.Tag.li,
                        c.task_mark_off,
                        if (c.is_task) this.charAt(c.task_mark_off) else 0,
                        Block.Flags.container_closer,
                    );
                    try this.pushContainerBytes(
                        if (is_ordered_list) Block.Tag.ol else Block.Tag.ul,
                        c.ch,
                        if (c.is_task) this.charAt(c.task_mark_off) else 0,
                        Block.Flags.container_closer,
                    );
                },
                '>' => {
                    try this.pushContainerBytes(
                        Block.Tag.quote,
                        0,
                        0,
                        Block.Flags.container_closer,
                    );
                },
                else => unreachable,
            }

            this.containers.len -= 1;
        }
    }
    fn enterChildContainers(this: *MDParser, keep: u32) !void {
        _ = this;
        var i: u32 = this.containers.len - keep;
        while (i < this.containers.len) : (i += 1) {
            var c: *Container = this.containers.ptr[i];
            var is_ordered_list = false;

            switch (c.ch) {
                ')', '.' => {
                    is_ordered_list = true;
                },
                '-', '+', '*' => {
                    //  Remember offset in ctx.block_bytes so we can revisit the
                    // block if we detect it is a loose list.
                    try this.endCurrentBlock();
                    c.block_index = this.blocks.len;

                    try this.pushContainerBytes(
                        if (is_ordered_list) Block.Tag.ol else Block.Tag.ul,
                        c.start,
                        c.ch,
                        Block.Flags.container_opener,
                    );
                    try this.pushContainerBytes(
                        Block.Tag.li,
                        c.task_mark_off,
                        if (c.is_task) this.charAt(c.task_mark_off) else 0,
                        Block.Flags.container_opener,
                    );
                },
                '>' => {
                    try this.pushContainerBytes(
                        Block.Tag.quote,
                        0,
                        0,
                        Block.Flags.container_opener,
                    );
                },
                else => unreachable,
            }
        }
    }
    fn pushContainer(this: *MDParser, container: Container) !void {
        try this.containers.push(this.allocator, container);
    }

    const LeafBlockDetail = union {
        none: void,
        h: Block.Header,
        code: Block.Code,
        table: Block.Table,
    };

    fn processLeafBlockWithType(this: *MDParser, comptime tag: Block.Tag, block: *Block) anyerror!void {
        const BlockDetailType = comptime switch (tag) {
            Block.Tag.h => Block.Header,
            Block.Tag.code => Block.Code,
            Block.Tag.table => Block.Table,
            else => void,
        };

        const is_in_tight_list = if (this.containers.len == 0)
            false
        else
            !this.containers.ptr[this.containers.len - 1].is_loose;

        const detail: BlockDetailType = switch (comptime tag) {
            Block.Tag.h => @truncate(Block.Header, block.data),
            Block.Tag.code => try this.setupFencedCodeDetail(block),
            Block.Tag.table => .{
                .col_count = block.data,
                .head_row_count = 1,
                .body_row_count = block.line_count -| 2,
            },
            else => void{},
        };

        if (!is_in_tight_list or comptime tag != .p) {
            try this.mdx.onEnterBlock(block.tag, BlockDetailType, detail);
        }

        defer {
            if (comptime tag == Block.Tag.code) {}
        }
    }
    fn processLeafBlock(this: *MDParser, block: *Block) anyerror!void {
        return switch (block.tag) {
            .doc => try this.processLeafBlockWithType(Block.Tag.doc, block),
            .quote => try this.processLeafBlockWithType(Block.Tag.quote, block),
            .ul => try this.processLeafBlockWithType(Block.Tag.ul, block),
            .ol => try this.processLeafBlockWithType(Block.Tag.ol, block),
            .li => try this.processLeafBlockWithType(Block.Tag.li, block),
            .hr => try this.processLeafBlockWithType(Block.Tag.hr, block),
            .h => try this.processLeafBlockWithType(Block.Tag.h, block),
            .code => try this.processLeafBlockWithType(Block.Tag.code, block),
            .html => try this.processLeafBlockWithType(Block.Tag.html, block),
            .p => try this.processLeafBlockWithType(Block.Tag.p, block),
            .table => try this.processLeafBlockWithType(Block.Tag.table, block),
            .thead => try this.processLeafBlockWithType(Block.Tag.thead, block),
            .tbody => try this.processLeafBlockWithType(Block.Tag.tbody, block),
            .tr => try this.processLeafBlockWithType(Block.Tag.tr, block),
            .th => try this.processLeafBlockWithType(Block.Tag.th, block),
            .td => try this.processLeafBlockWithType(Block.Tag.td, block),
        };
    }
    fn pushContainerBytes(this: *MDParser, block_type: Block.Tag, start: u32, data: u32, flag: Block.Flags) !void {
        try this.endCurrentBlock();
        var block = Block{
            .tag = block_type,
            .line_count = start,
            .data = data,
        };
        block.flags.insert(flag);
        var prev_block: ?Block = null;
        if (this.current_block) |curr| {
            prev_block = curr.*;
        }

        try this.blocks.push(this.allocator, block);
        if (prev_block != null) {
            this.current_block = this.blocks.ptr[this.current_block_index];
        }
    }
    fn processAllBlocks(this: *MDParser) !void {
        _ = this;

        // ctx->containers now is not needed for detection of lists and list items
        // so we reuse it for tracking what lists are loose or tight. We rely
        // on the fact the vector is large enough to hold the deepest nesting
        // level of lists.
        this.containers.len = 0;
        var blocks = this.blocks.slice();
        for (blocks) |*block| {
            const detail: Block.Detail =
                switch (block.tag) {
                .ul => Block.Detail{
                    .ul = .{
                        .is_tight = !block.flags.contains(.loose_list),
                        .mark = @truncate(u8, block.data),
                    },
                },
                .ol => Block.Detail{
                    .ol = .{
                        .start = block.line_count,
                        .is_tight = !block.flags.contains(.loose_list),
                        .mark_delimiter = @truncate(u8, block.data),
                    },
                },
                .li => Block.Detail{
                    .li = .{
                        .is_task = block.data != 0,
                        .task_mark = @truncate(u8, block.data),
                        .task_mark_offset = @intCast(u32, block.line_count),
                    },
                },
                else => Block.Detail{ .none = .{} },
            };

            if (block.flags.contains(.container)) {
                if (block.flags.contains(.container_closer)) {
                    switch (block.tag) {
                        .li => try this.mdx.onLeaveBlock(block.tag, Block.LI, detail.li),
                        .ul => try this.mdx.onLeaveBlock(block.tag, Block.UL, detail.ul),
                        .ol => try this.mdx.onLeaveBlock(block.tag, Block.OL, detail.ol),
                        else => try this.mdx.onLeaveBlock(block.tag, void, void{}),
                    }
                    this.containers.len -|= switch (block.tag) {
                        .ul, .ol, .blockquote => 1,
                        else => 0,
                    };
                }

                if (block.flags.contains(.container_opener)) {
                    switch (block.tag) {
                        .li => try this.mdx.onEnterBlock(block.tag, Block.LI, detail.li),
                        .ul => try this.mdx.onEnterBlock(block.tag, Block.UL, detail.ul),
                        .ol => try this.mdx.onEnterBlock(block.tag, Block.OL, detail.ol),
                        else => try this.mdx.onEnterBlock(block.tag, void, void{}),
                    }

                    switch (block.tag) {
                        .ul, .ol => {
                            this.containers.ptr[this.containers.len].is_loose = block.flags.contains(.loose_list);
                            this.containers.len += 1;
                        },
                        .blockquote => {
                            //  This causes that any text in a block quote, even if
                            // nested inside a tight list item, is wrapped with
                            // <p>...</p>. */
                            this.containers.ptr[this.containers.len].is_loose = true;
                            this.containers.len += 1;
                        },
                        else => {},
                    }
                }
            } else {
                try this.processLeafBlock(block);
            }
        }
    }
    fn isContainerCompatible(pivot: *const Container, container: *const Container) bool {
        // Block quote has no "items" like lists.
        if (container.ch == '>') return false;

        if (container.ch != pivot.ch)
            return false;

        if (container.mark_indent > pivot.contents_indent)
            return false;
        return true;
    }

    fn isHRLine(this: *MDParser, beg: u32, end: *u32, hr_killer: *u32) bool {
        var off = beg + 1;
        var n: u32 = 1;

        while (off < this.size and (this.charAt(off) == this.charAt(beg) or this.charAt(off) == ' ' or this.charAt(off) == '\t')) {
            if (this.charAt(off) == this.charAt(beg))
                n += 1;
            off += 1;
        }

        if (n < 3) {
            hr_killer.* = off;
            return false;
        }

        // Nothing else can be present on the line. */
        if (off < this.size and !this.isNewline(off)) {
            hr_killer.* = off;
            return false;
        }

        end.* = off;
        return true;
    }

    fn isSetextUnderline(this: *MDParser, beg: u32, end: *u32, level: *u4) bool {
        var off = beg + 1;
        while (off < this.size and this.charAt(off) == this.charAt(beg))
            off += 1;

        // Optionally, space(s) can follow. */
        while (off < this.size and this.charAt(off) == ' ')
            off += 1;

        // But nothing more is allowed on the line.
        if (off < this.size and !this.isNewline(off))
            return false;
        level.* = if (this.charAt(beg) == '=') 1 else 2;
        end.* = off;
        return true;
    }

    fn isATXHeaderLine(this: *MDParser, beg: u32, p_beg: *u32, end: *u32, level: *u4) bool {
        var n: i32 = undefined;
        var off: u32 = beg + 1;

        while (off < this.size and this.charAt(off) == '#' and off - beg < 7) {
            off += 1;
        }
        n = off - beg;

        if (n > 6)
            return false;
        level.* = @intCast(u4, n);

        if (!(this.flags.contains(.permissive_atxheaders)) and off < this.size and
            this.charAt(off) != ' ' and this.charAt(off) != '\t' and !this.isNewline(off))
            return false;

        while (off < this.size and this.charAt(off) == ' ') {
            off += 1;
        }

        p_beg.* = off;
        end.* = off;

        return true;
    }

    fn isTableUnderline(this: *MDParser, beg: u32, end: *u32, column_column: *u32) bool {
        _ = this;
        _ = end;
        _ = column_column;

        var off = beg;
        var found_pipe = false;
        var col_count: u32 = 0;

        if (off < this.size and this.charAt(off) == '|') {
            found_pipe = true;
            off += 1;
            while (off < this.size and isWhitespace(this.charAt(off))) {
                off += 1;
            }
        }

        while (true) {
            var delimited = false;

            // Cell underline ("-----", ":----", "----:" or ":----:")if(off < this.size  and  this.charAt(off) == _T(':'))
            off += 1;
            if (off >= this.size or this.charAt(off) != '-')
                return false;
            while (off < this.size and this.charAt(off) == '-')
                off += 1;
            if (off < this.size and this.charAt(off) == ':')
                off += 1;

            col_count += 1;

            // Pipe delimiter (optional at the end of line). */
            while (off < this.size and isWhitespace(this.charAt(off)))
                off += 1;
            if (off < this.size and this.charAt(off) == '|') {
                delimited = true;
                found_pipe = true;
                off += 1;
                while (off < this.size and isWhitespace(this.charAt(off)))
                    off += 1;
            }

            // Success, if we reach end of line.
            if (off >= this.size or this.isNewline(off))
                break;

            if (!delimited)
                return false;
        }

        if (!found_pipe)
            return false;

        column_column.* = col_count;
        end.* = off;
        return true;
    }

    fn isOpeningCodeFence(this: *MDParser, beg: u8, end: *u32) bool {
        var off = beg;
        const first = this.charAt(beg);

        while (off < this.size and this.charAt(off) == first) {
            off += 1;
        }

        // Fence must have at least three characters.
        if (off - beg < 3)
            return false;

        // Optionally, space(s) can follow
        while (off < this.size and this.charAt(off) == ' ') {
            off += 1;
        }

        // Optionally, an info string can follow.
        while (off < this.size and !this.isNewline(this.charAt(off))) {
            // Backtick-based fence must not contain '`' in the info string.
            if (first == '`' and this.charAt(off) == '`')
                return false;
            off += 1;
        }

        end.* = off;
        return true;
    }

    fn isClosingCodeFence(this: *MDParser, ch: u8, beg: u8, end: *u32) bool {
        var off = beg;

        defer {
            end.* = off;
        }

        while (off < this.size and this.charAt(off) == ch) {
            off += 1;
        }

        if (off - beg < this.code_fence_length) {
            return false;
        }

        // Optionally, space(s) can follow
        while (off < this.size and this.charAt(off) == ' ') {
            off += 1;
        }

        // But nothing more is allowed on the line.
        if (off < this.size and !this.isNewline(this.charAt(off)))
            return false;

        return true;
    }

    pub fn parse(this: *MDParser) anyerror!void {
        var pivot_line = &Line.Analysis.blank;
        var line_buf: [2]Line.Analysis = undefined;
        var line = &line_buf[0];
        var offset: u32 = 0;

        try this.mdx.onEnterBlock(.doc, void, void{});

        const len: u32 = this.size;
        while (offset < len) {
            if (line == pivot_line) {
                line = if (line == &line_buf[0]) &line_buf[1] else &line_buf[0];
            }

            try this.analyzeLine(offset, &offset, pivot_line, line);
            try this.processLine(&pivot_line, line);
        }

        this.endCurrentBlock();

        try this.buildRefDefHashTable();

        this.leaveChildContainers(0);
        this.processAllBlocks();
        try this.mdx.onLeaveBlock(.doc, void, void{});
    }
};

pub const MDX = struct {
    parser: JSParser,
    log: *logger.Log,
    allocator: std.mem.Allocator,
    stmts: std.ArrayListUnmanaged(js_ast.Stmt) = .{},

    pub const Options = struct {};

    pub fn onEnterBlock(this: *MDX, tag: Block.Tag, comptime Detail: type, detail: Detail) anyerror!void {
        _ = tag;
        _ = detail;
        _ = this;
    }

    pub fn onLeaveBlock(this: *MDX, tag: Block.Tag, comptime Detail: type, detail: Detail) anyerror!void {
        _ = tag;
        _ = detail;
        _ = this;
    }

    pub fn onEnterSpan(this: *MDX, tag: Span.Tag, comptime Detail: type, detail: Detail) anyerror!void {
        _ = tag;
        _ = detail;
        _ = this;
    }

    pub fn onLeaveSpan(this: *MDX, tag: Span.Tag, comptime Detail: type, detail: Detail) anyerror!void {
        _ = tag;
        _ = detail;
        _ = this;
    }

    pub fn onText(this: *MDX, tag: Text, text: []const u8) anyerror!void {
        _ = tag;
        _ = text;
        _ = this;
    }

    pub inline fn source(p: *const MDX) *const logger.Source {
        return &p.lexer.source;
    }

    pub fn e(_: *MDX, t: anytype, loc: logger.Loc) Expr {
        const Type = @TypeOf(t);
        if (@typeInfo(Type) == .Pointer) {
            return Expr.init(std.meta.Child(Type), t.*, loc);
        } else {
            return Expr.init(Type, t, loc);
        }
    }

    pub fn s(_: *MDX, t: anytype, loc: logger.Loc) Stmt {
        const Type = @TypeOf(t);
        if (@typeInfo(Type) == .Pointer) {
            return Stmt.init(std.meta.Child(Type), t.*, loc);
        } else {
            return Stmt.alloc(Type, t, loc);
        }
    }

    pub fn setup(
        this: *MDX,
        _options: ParserOptions,
        log: *logger.Log,
        source_: *const logger.Source,
        define: *Define,
        allocator: std.mem.Allocator,
    ) !void {
        try JSParser.init(
            allocator,
            log,
            source_,
            define,
            js_lexer.Lexer.initNoAutoStep(log, source_.*, allocator),
            _options,
            &this.parser,
        );
        this.lexer = try Lexer.init(&this.parser.lexer);
        this.allocator = allocator;
        this.log = log;
        this.stmts = .{};
    }

    pub fn parse(this: *MDX) !js_ast.Result {
        try this._parse();
        return try runVisitPassAndFinish(JSParser, &this.parser, this.stmts.toOwnedSlice(this.allocator));
    }

    fn run(this: *MDX) anyerror!logger.Loc {
        _ = this;
        return logger.Loc.Empty;
    }

    fn _parse(this: *MDX) anyerror!void {
        var root_children = std.ArrayListUnmanaged(Expr){};
        var first_loc = try run(this, &root_children);

        first_loc.start = @maximum(first_loc.start, 0);
        const args_loc = first_loc;
        first_loc.start += 1;
        const body_loc = first_loc;

        // We need to simulate a function that was parsed
        _ = try this.parser.pushScopeForParsePass(.function_args, args_loc);

        _ = try this.parser.pushScopeForParsePass(.function_body, body_loc);

        const root = this.e(E.JSXElement{
            .tag = this.e(E.JSXElement.Tag.map.get(E.JSXElement.Tag.main), body_loc),
            .children = ExprNodeList.fromList(root_children),
        }, body_loc);

        var root_stmts = try this.allocator.alloc(Stmt, 1);
        root_stmts[0] = this.s(S.Return{ .value = root }, body_loc);

        try this.stmts.append(
            this.allocator,

            this.s(S.ExportDefault{
                .default_name = try this.parser.createDefaultName(args_loc),
                .value = .{
                    .expr = this.e(E.Arrow{
                        .body = G.FnBody{
                            .stmts = root_stmts,
                            .loc = body_loc,
                        },
                        .args = &[_]G.Arg{},
                        .prefer_expr = true,
                    }, args_loc),
                },
            }, args_loc),
        );
    }
};