summary refs log tree commit diff stats
path: root/nim/rst.pas
blob: 89ef2c501a170b1fe763f281654db34b7032e95f (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
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
//
//
//           The Nimrod Compiler
//        (c) Copyright 2009 Andreas Rumpf
//
//    See the file "copying.txt", included in this
//    distribution, for details about the copyright.
//
unit rst;

// This module implements a *reStructuredText* parser. A larget
// subset is provided.

interface

{$include 'config.inc'}

uses
  nsystem, nos, msgs, strutils, platform, nhashes, ropes, charsets, options;

type
  TRstNodeKind = (
    rnInner,       // an inner node or a root
    rnHeadline,    // a headline
    rnOverline,    // an over- and underlined headline
    rnTransition,  // a transition (the ------------- <hr> thingie)
    rnParagraph,   // a paragraph

    rnBulletList,  // a bullet list
    rnBulletItem,  // a bullet item
    rnEnumList,    // an enumerated list
    rnEnumItem,    // an enumerated item

    rnDefList,     // a definition list
    rnDefItem,     // an item of a definition list consisting of ...
    rnDefName,     // ... a name part ...
    rnDefBody,     // ... and a body part ...

    rnFieldList,   // a field list
    rnField,       // a field item
    rnFieldName,   // consisting of a field name ...
    rnFieldBody,   // ... and a field body

    rnOptionList,
    rnOptionListItem,
    rnOptionGroup,
    rnOption,
    rnOptionString,
    rnOptionArgument,
    rnDescription,

    rnLiteralBlock,
    rnQuotedLiteralBlock,

    rnLineBlock,   // the | thingie
    rnLineBlockItem, // sons of the | thing

    rnBlockQuote,  // text just indented

    rnTable,
    rnGridTable,
    rnTableRow,
    rnTableHeaderCell,
    rnTableDataCell,

    rnLabel,       // used for footnotes and other things
    rnFootnote,    // a footnote

    rnCitation,    // similar to footnote

    rnStandaloneHyperlink,
    rnHyperlink,
    rnRef,
    rnDirective,   // a directive
    rnDirArg,
    rnRaw,
    rnTitle,
    rnContents,
    rnImage,
    rnFigure,
    rnCodeBlock,
    rnContainer,   // ``container`` directive
    rnIndex,       // index directve:
                   // .. index::
                   //   key
                   //     * `file#id <file#id>`_
                   //     * `file#id <file#id>'_

    rnSubstitutionDef,  // a definition of a substitution

    rnGeneralRole,
    // Inline markup:
    rnSub,
    rnSup,
    rnIdx,
    rnEmphasis,         // "*"
    rnStrongEmphasis,   // "**"
    rnInterpretedText,  // "`"
    rnInlineLiteral,    // "``"
    rnSubstitutionReferences, // "|"

    rnLeaf              // a leaf; the node's text field contains the leaf val
  );
const
  rstnodekindToStr: array [TRstNodeKind] of string = (
    'Inner', 'Headline', 'Overline', 'Transition', 'Paragraph',
    'BulletList', 'BulletItem', 'EnumList', 'EnumItem', 'DefList', 'DefItem',
    'DefName', 'DefBody', 'FieldList', 'Field', 'FieldName', 'FieldBody',
    'OptionList', 'OptionListItem', 'OptionGroup', 'Option', 'OptionString',
    'OptionArgument', 'Description', 'LiteralBlock', 'QuotedLiteralBlock',
    'LineBlock', 'LineBlockItem', 'BlockQuote', 'Table', 'GridTable',
    'TableRow', 'TableHeaderCell', 'TableDataCell', 'Label', 'Footnote',
    'Citation', 'StandaloneHyperlink', 'Hyperlink', 'Ref', 'Directive',
    'DirArg', 'Raw', 'Title', 'Contents', 'Image', 'Figure', 'CodeBlock',
    'Container', 'Index', 'SubstitutionDef', 'GeneralRole', 
    'Sub', 'Sup', 'Idx', 'Emphasis', 'StrongEmphasis', 'InterpretedText', 
    'InlineLiteral', 'SubstitutionReferences', 'Leaf'
  );

type
  // the syntax tree of RST:
  PRSTNode = ^TRstNode;
  TRstNodeSeq = array of PRstNode;
  TRSTNode = record
    kind: TRstNodeKind;
    text: string;      // valid for leafs in the AST; and the title of
                       // the document or the section
    level: int;        // valid for some node kinds
    sons: TRstNodeSeq; // the node's sons
  end {@acyclic};


function rstParse(const text: string; // the text to be parsed
                  skipPounds: bool;
                  const filename: string; // for error messages
                  line, column: int;
                  var hasToc: bool): PRstNode;
function rsonsLen(n: PRstNode): int;
function newRstNode(kind: TRstNodeKind): PRstNode; overload;
function newRstNode(kind: TRstNodeKind; const s: string): PRstNode; overload;
procedure addSon(father, son: PRstNode);

function rstnodeToRefname(n: PRstNode): string;

function addNodes(n: PRstNode): string;

function getFieldValue(n: PRstNode; const fieldname: string): string;
function getArgument(n: PRstNode): string;

// index handling:
procedure setIndexPair(index, key, val: PRstNode);
procedure sortIndex(a: PRstNode);
procedure clearIndex(index: PRstNode; const filename: string);


implementation

// ----------------------------- scanner part --------------------------------

const
  SymChars: TCharSet = ['a'..'z', 'A'..'Z', '0'..'9', #128..#255];

type
  TTokType = (tkEof, tkIndent, tkWhite, tkWord, tkAdornment, tkPunct, tkOther);
  TToken = record            // a RST token
    kind: TTokType;          // the type of the token
    ival: int;               // the indentation or parsed integer value
    symbol: string;          // the parsed symbol as string
    line, col: int;          // line and column of the token
  end;
  TTokenSeq = array of TToken;
  TLexer = object(NObject)
    buf: PChar;
    bufpos: int;
    line, col, baseIndent: int;
    skipPounds: bool;
  end;

procedure getThing(var L: TLexer; var tok: TToken; const s: TCharSet);
var
  pos: int;
begin
  tok.kind := tkWord;
  tok.line := L.line;
  tok.col := L.col;
  pos := L.bufpos;
  while True do begin
    addChar(tok.symbol, L.buf[pos]);
    inc(pos);
    if not (L.buf[pos] in s) then break
  end;
  inc(L.col, pos - L.bufpos);
  L.bufpos := pos;
end;

procedure getAdornment(var L: TLexer; var tok: TToken);
var
  pos: int;
  c: char;
begin
  tok.kind := tkAdornment;
  tok.line := L.line;
  tok.col := L.col;
  pos := L.bufpos;
  c := L.buf[pos];
  while True do begin
    addChar(tok.symbol, L.buf[pos]);
    inc(pos);
    if L.buf[pos] <> c then break
  end;
  inc(L.col, pos - L.bufpos);
  L.bufpos := pos
end;

function getIndentAux(var L: TLexer; start: int): int;
var
  buf: PChar;
  pos: int;
begin
  pos := start;
  buf := L.buf;
  // skip the newline (but include it in the token!)
  if buf[pos] = #13 then begin
    if buf[pos+1] = #10 then inc(pos, 2) else inc(pos);
  end
  else if buf[pos] = #10 then inc(pos);
  if L.skipPounds then begin
    if buf[pos] = '#' then inc(pos);
    if buf[pos] = '#' then inc(pos);
  end;
  result := 0;
  while True do begin
    case buf[pos] of
      ' ', #11, #12: begin
        inc(pos);
        inc(result);
      end;
      #9: begin
        inc(pos);
        result := result - (result mod 8) + 8;
      end;
      else break; // EndOfFile also leaves the loop
    end;
  end;
  if buf[pos] = #0 then result := 0
  else if (buf[pos] = #10) or (buf[pos] = #13) then begin
    // look at the next line for proper indentation:
    result := getIndentAux(L, pos);
  end;
  L.bufpos := pos; // no need to set back buf
end;

procedure getIndent(var L: TLexer; var tok: TToken);
begin
  inc(L.line);
  tok.line := L.line;
  tok.col := 0;
  tok.kind := tkIndent;
  // skip the newline (but include it in the token!)
  tok.ival := getIndentAux(L, L.bufpos);
  L.col := tok.ival;
  tok.ival := max(tok.ival - L.baseIndent, 0);
  tok.symbol := nl +{&} repeatChar(tok.ival);
end;

procedure rawGetTok(var L: TLexer; var tok: TToken);
var
  c: Char;
begin
  tok.symbol := '';
  tok.ival := 0;
  c := L.buf[L.bufpos];
  case c of
    'a'..'z', 'A'..'Z', #128..#255, '0'..'9': getThing(L, tok, SymChars);
    ' ', #9, #11, #12: begin
      getThing(L, tok, {@set}[' ', #9]);
      tok.kind := tkWhite;
      if L.buf[L.bufpos] in [#13, #10] then
        rawGetTok(L, tok); // ignore spaces before \n
    end;
    #13, #10: getIndent(L, tok);
    '!', '"', '#', '$', '%', '&', '''',
    '(', ')', '*', '+', ',', '-', '.', '/',
    ':', ';', '<', '=', '>', '?', '@', '[', '\', ']',
    '^', '_', '`', '{', '|', '}', '~': begin
      getAdornment(L, tok);
      if length(tok.symbol) <= 3 then tok.kind := tkPunct;
    end;
    else begin
      tok.line := L.line;
      tok.col := L.col;
      if c = #0 then
        tok.kind := tkEof
      else begin
        tok.kind := tkOther;
        addChar(tok.symbol, c);
        inc(L.bufpos);
        inc(L.col);
      end
    end
  end;
  tok.col := max(tok.col - L.baseIndent, 0);
end;

procedure getTokens(const buffer: string; skipPounds: bool;
                    var tokens: TTokenSeq);
var
  L: TLexer;
  len: int;
begin
{@ignore}
  fillChar(L, sizeof(L), 0);
{@emit}
  len := length(tokens);
  L.buf := PChar(buffer);
  L.line := 1;
  // skip UTF-8 BOM
  if (L.buf[0] = #239) and (L.buf[1] = #187) and (L.buf[2] = #191) then 
    inc(L.bufpos, 3);
  L.skipPounds := skipPounds;
  if skipPounds then begin
    if L.buf[L.bufpos] = '#' then inc(L.bufpos);
    if L.buf[L.bufpos] = '#' then inc(L.bufpos);
    L.baseIndent := 0;
    while L.buf[L.bufpos] = ' ' do begin
      inc(L.bufpos);
      inc(L.baseIndent);
    end
  end;
  while true do begin
    inc(len);
    setLength(tokens, len);
    rawGetTok(L, tokens[len-1]);
    if tokens[len-1].kind = tkEof then break;
  end;
  if tokens[0].kind = tkWhite then begin // BUGFIX
    tokens[0].ival := length(tokens[0].symbol);
    tokens[0].kind := tkIndent
  end
end;

// --------------------------------------------------------------------------

procedure addSon(father, son: PRstNode);
var
  L: int;
begin
  L := length(father.sons);
  setLength(father.sons, L+1);
  father.sons[L] := son;
end;

procedure addSonIfNotNil(father, son: PRstNode);
begin
  if son <> nil then addSon(father, son);
end;

function rsonsLen(n: PRstNode): int;
begin
  result := length(n.sons)
end;

function newRstNode(kind: TRstNodeKind): PRstNode; overload;
begin
  new(result);
{@ignore}
  fillChar(result^, sizeof(result^), 0);
{@emit
  result.sons := @[];
}
  result.kind := kind;
end;

function newRstNode(kind: TRstNodeKind; const s: string): PRstNode; overload;
begin
  result := newRstNode(kind);
  result.text := s;
end;

// ---------------------------------------------------------------------------
type
  TLevelMap = array [Char] of int;
  TSubstitution = record
    key: string;
    value: PRstNode;
  end;
  TSharedState = record
    uLevel, oLevel: int; // counters for the section levels
    subs: array of TSubstitution; // substitutions
    refs: array of TSubstitution; // references
    underlineToLevel: TLevelMap;
    // Saves for each possible title adornment character its level in the
    // current document. This is for single underline adornments.
    overlineToLevel: TLevelMap;
    // Saves for each possible title adornment character its level in the
    // current document. This is for over-underline adornments.
  end;
  PSharedState = ^TSharedState;
  TRstParser = object(NObject)
    idx: int;
    tok: TTokenSeq;
    s: PSharedState;
    indentStack: array of int;
    filename: string;
    line, col: int;
    hasToc: bool;
  end;

function newSharedState(): PSharedState;
begin
  new(result);
{@ignore}
  fillChar(result^, sizeof(result^), 0);
{@emit}
  {@emit
  result.subs := @[];}
  {@emit
  result.refs := @[];}
end;

function tokInfo(const p: TRstParser; const tok: TToken): TLineInfo;
begin
  result := newLineInfo(p.filename, p.line+tok.line, p.col+tok.col);
end;

procedure rstMessage(const p: TRstParser; msgKind: TMsgKind;
                     const arg: string); overload;
begin
  liMessage(tokInfo(p, p.tok[p.idx]), msgKind, arg);
end;

procedure rstMessage(const p: TRstParser; msgKind: TMsgKind); overload;
begin
  liMessage(tokInfo(p, p.tok[p.idx]), msgKind, p.tok[p.idx].symbol);
end;

function currInd(const p: TRstParser): int;
begin
  result := p.indentStack[high(p.indentStack)];
end;

procedure pushInd(var p: TRstParser; ind: int);
var
  len: int;
begin
  len := length(p.indentStack);
  setLength(p.indentStack, len+1);
  p.indentStack[len] := ind;
end;

procedure popInd(var p: TRstParser);
begin
  if length(p.indentStack) > 1 then
    setLength(p.indentStack, length(p.indentStack)-1);
end;

procedure initParser(var p: TRstParser; sharedState: PSharedState);
begin
  {@ignore}
  fillChar(p, sizeof(p), 0);
  p.tok := nil;
  p.indentStack := nil;
  pushInd(p, 0);
  {@emit
  p.indentStack := @[0];}
  {@emit
  p.tok := @[];}
  p.idx := 0;
  p.filename := '';
  p.hasToc := false;
  p.col := 0;
  p.line := 1;
  p.s := sharedState;
end;

// ---------------------------------------------------------------

procedure addNodesAux(n: PRstNode; var result: string);
var
  i: int;
begin
  if n.kind = rnLeaf then
    add(result, n.text)
  else begin
    for i := 0 to rsonsLen(n)-1 do
      addNodesAux(n.sons[i], result)
  end
end;

function addNodes(n: PRstNode): string;
begin
  result := '';
  addNodesAux(n, result);
end;

procedure rstnodeToRefnameAux(n: PRstNode; var r: string; var b: bool);
var
  i: int;
begin
  if n.kind = rnLeaf then begin
    for i := strStart to length(n.text)+strStart-1 do begin
      case n.text[i] of
        '0'..'9': begin
          if b then begin addChar(r, '-'); b := false; end;
          // BUGFIX: HTML id's cannot start with a digit
          if length(r) = 0 then addChar(r, 'Z');
          addChar(r, n.text[i])
        end;
        'a'..'z': begin
          if b then begin addChar(r, '-'); b := false; end;
          addChar(r, n.text[i])
        end;
        'A'..'Z': begin
          if b then begin addChar(r, '-'); b := false; end;
          addChar(r, chr(ord(n.text[i]) - ord('A') + ord('a')));
        end;
        else if (length(r) > 0) then b := true;
      end
    end
  end
  else begin
    for i := 0 to rsonsLen(n)-1 do rstnodeToRefnameAux(n.sons[i], r, b)
  end
end;

function rstnodeToRefname(n: PRstNode): string;
var
  b: bool;
begin
  result := '';
  b := false;
  rstnodeToRefnameAux(n, result, b);
end;

function findSub(var p: TRstParser; n: PRstNode): int;
var
  key: string;
  i: int;
begin
  key := addNodes(n);
  // the spec says: if no exact match, try one without case distinction:
  for i := 0 to high(p.s.subs) do
    if key = p.s.subs[i].key then begin
      result := i; exit
    end;
  for i := 0 to high(p.s.subs) do
    if cmpIgnoreStyle(key, p.s.subs[i].key) = 0 then begin
      result := i; exit
    end;
  result := -1
end;

procedure setSub(var p: TRstParser; const key: string; value: PRstNode);
var
  i, len: int;
begin
  len := length(p.s.subs);
  for i := 0 to len-1 do
    if key = p.s.subs[i].key then begin
      p.s.subs[i].value := value; exit
    end;
  setLength(p.s.subs, len+1);
  p.s.subs[len].key := key;
  p.s.subs[len].value := value;
end;

procedure setRef(var p: TRstParser; const key: string; value: PRstNode);
var
  i, len: int;
begin
  len := length(p.s.refs);
  for i := 0 to len-1 do
    if key = p.s.refs[i].key then begin
      p.s.refs[i].value := value;
      rstMessage(p, warnRedefinitionOfLabel, key);
      exit
    end;
  setLength(p.s.refs, len+1);
  p.s.refs[len].key := key;
  p.s.refs[len].value := value;
end;

function findRef(var p: TRstParser; const key: string): PRstNode;
var
  i: int;
begin
  for i := 0 to high(p.s.refs) do
    if key = p.s.refs[i].key then begin
      result := p.s.refs[i].value; exit
    end;
  result := nil
end;

function cmpNodes(a, b: PRstNode): int;
var
  x, y: PRstNode;
begin
  assert(a.kind = rnDefItem);
  assert(b.kind = rnDefItem);
  x := a.sons[0];
  y := b.sons[0];
  result := cmpIgnoreStyle(addNodes(x), addNodes(y))
end;

procedure sortIndex(a: PRstNode);
// we use shellsort here; fast and simple
var
  N, i, j, h: int;
  v: PRstNode;
begin
  assert(a.kind = rnDefList);
  N := rsonsLen(a);
  h := 1; repeat h := 3*h+1; until h > N;
  repeat
    h := h div 3;
    for i := h to N-1 do begin
      v := a.sons[i]; j := i;
      while cmpNodes(a.sons[j-h], v) >= 0 do begin
        a.sons[j] := a.sons[j-h]; j := j - h;
        if j < h then break
      end;
      a.sons[j] := v;
    end;
  until h = 1
end;

function eqRstNodes(a, b: PRstNode): bool;
var
  i: int;
begin
  result := false;
  if a.kind <> b.kind then exit;
  if a.kind = rnLeaf then
    result := a.text = b.text
  else begin
    if rsonsLen(a) <> rsonsLen(b) then exit;
    for i := 0 to rsonsLen(a)-1 do
      if not eqRstNodes(a.sons[i], b.sons[i]) then exit;
    result := true
  end
end;

function matchesHyperlink(h: PRstNode; const filename: string): bool;
var
  s: string;
begin
  if h.kind = rnInner then begin
    assert(rsonsLen(h) = 1);
    result := matchesHyperlink(h.sons[0], filename)
  end
  else if h.kind = rnHyperlink then begin
    s := addNodes(h.sons[1]);
    if startsWith(s, filename) and (s[length(filename)+strStart] = '#') then
      result := true
    else
      result := false
  end
  else // this may happen in broken indexes!
    result := false
end;

procedure clearIndex(index: PRstNode; const filename: string);
var
  i, j, k, items, lastItem: int;
  val: PRstNode;
begin
  assert(index.kind = rnDefList);
  for i := 0 to rsonsLen(index)-1 do begin
    assert(index.sons[i].sons[1].kind = rnDefBody);
    val := index.sons[i].sons[1].sons[0];
    if val.kind = rnInner then val := val.sons[0];
    if val.kind = rnBulletList then begin
      items := rsonsLen(val);
      lastItem := -1; // save the last valid item index
      for j := 0 to rsonsLen(val)-1 do begin
        if val.sons[j] = nil then
          dec(items)
        else if matchesHyperlink(val.sons[j].sons[0], filename) then begin
          val.sons[j] := nil;
          dec(items)
        end
        else lastItem := j
      end;
      if items = 1 then // remove bullet list:
        index.sons[i].sons[1].sons[0] := val.sons[lastItem].sons[0]
      else if items = 0 then
        index.sons[i] := nil
    end
    else if matchesHyperlink(val, filename) then
      index.sons[i] := nil
  end;
  // remove nil nodes:
  k := 0;
  for i := 0 to rsonsLen(index)-1 do begin
    if index.sons[i] <> nil then begin
      if k <> i then index.sons[k] := index.sons[i];
      inc(k)
    end
  end;
  setLength(index.sons, k);
end;

procedure setIndexPair(index, key, val: PRstNode);
var
  i: int;
  e, a, b: PRstNode;
begin
  // writeln(rstnodekindToStr[key.kind], ': ', rstnodekindToStr[val.kind]);
  assert(index.kind = rnDefList);
  assert(key.kind <> rnDefName);
  a := newRstNode(rnDefName);
  addSon(a, key);

  for i := 0 to rsonsLen(index)-1 do begin
    if eqRstNodes(index.sons[i].sons[0], a) then begin
      assert(index.sons[i].sons[1].kind = rnDefBody);
      e := index.sons[i].sons[1].sons[0];
      if e.kind <> rnBulletList then begin
        e := newRstNode(rnBulletList);
        b := newRstNode(rnBulletItem);
        addSon(b, index.sons[i].sons[1].sons[0]);
        addSon(e, b);
        index.sons[i].sons[1].sons[0] := e;
      end;
      b := newRstNode(rnBulletItem);
      addSon(b, val);
      addSon(e, b);

      exit // key already exists
    end
  end;
  e := newRstNode(rnDefItem);
  assert(val.kind <> rnDefBody);
  b := newRstNode(rnDefBody);
  addSon(b, val);
  addSon(e, a);
  addSon(e, b);
  addSon(index, e);
end;

// ---------------------------------------------------------------------------

function newLeaf(var p: TRstParser): PRstNode;
begin
  result := newRstNode(rnLeaf, p.tok[p.idx].symbol)
end;

function getReferenceName(var p: TRstParser; const endStr: string): PRstNode;
var
  res: PRstNode;
begin
  res := newRstNode(rnInner);
  while true do begin
    case p.tok[p.idx].kind of
      tkWord, tkOther, tkWhite: addSon(res, newLeaf(p));
      tkPunct:
        if p.tok[p.idx].symbol = endStr then begin inc(p.idx); break end
        else addSon(res, newLeaf(p));
      else begin
        rstMessage(p, errXexpected, endStr);
        break
      end
    end;
    inc(p.idx);
  end;
  result := res;
end;

function untilEol(var p: TRstParser): PRstNode;
begin
  result := newRstNode(rnInner);
  while not (p.tok[p.idx].kind in [tkIndent, tkEof]) do begin
    addSon(result, newLeaf(p)); inc(p.idx);
  end
end;

procedure expect(var p: TRstParser; const tok: string);
begin
  if p.tok[p.idx].symbol = tok then inc(p.idx)
  else rstMessage(p, errXexpected, tok)
end;

(*
  From the specification:

  The inline markup start-string and end-string recognition rules are as
  follows. If any of the conditions are not met, the start-string or end-string
  will not be recognized or processed.

   1. Inline markup start-strings must start a text block or be immediately
      preceded by whitespace or one of:  ' " ( [ { < - / :
   2. Inline markup start-strings must be immediately followed by
      non-whitespace.
   3. Inline markup end-strings must be immediately preceded by non-whitespace.
   4. Inline markup end-strings must end a text block or be immediately
      followed by whitespace or one of: ' " ) ] } > - / : . , ; ! ? \
   5. If an inline markup start-string is immediately preceded by a single or
      double quote, "(", "[", "{", or "<", it must not be immediately followed
      by the corresponding single or double quote, ")", "]", "}", or ">".
   6. An inline markup end-string must be separated by at least one character
      from the start-string.
   7. An unescaped backslash preceding a start-string or end-string will
      disable markup recognition, except for the end-string of inline literals.
      See Escaping Mechanism above for details.
*)
function isInlineMarkupEnd(const p: TRstParser; const markup: string): bool;
begin
  result := p.tok[p.idx].symbol = markup;
  if not result then exit;
  // Rule 3:
  result := not (p.tok[p.idx-1].kind in [tkIndent, tkWhite]);
  if not result then exit;
  // Rule 4:
  result := (p.tok[p.idx+1].kind in [tkIndent, tkWhite, tkEof])
    or (p.tok[p.idx+1].symbol[strStart] in ['''', '"', ')', ']', '}', '>',
                                            '-', '/', '\', ':', '.', ',',
                                            ';', '!', '?', '_']);
  if not result then exit;
  // Rule 7:
  if p.idx > 0 then begin
    if (markup <> '``') and (p.tok[p.idx-1].symbol = '\'+'') then begin
      result := false
    end
  end
end;

function isInlineMarkupStart(const p: TRstParser; const markup: string): bool;
var
  c, d: Char;
begin
  result := p.tok[p.idx].symbol = markup;
  if not result then exit;
  // Rule 1:
  result := (p.idx = 0) or (p.tok[p.idx-1].kind in [tkIndent, tkWhite])
    or (p.tok[p.idx-1].symbol[strStart] in ['''', '"', '(', '[', '{', '<',
                                            '-', '/', ':', '_']);
  if not result then exit;
  // Rule 2:
  result := not (p.tok[p.idx+1].kind in [tkIndent, tkWhite, tkEof]);
  if not result then exit;
  // Rule 5 & 7:
  if p.idx > 0 then begin
    if p.tok[p.idx-1].symbol = '\'+'' then
      result := false
    else begin
      c := p.tok[p.idx-1].symbol[strStart];
      case c of
        '''', '"': d := c;
        '(': d := ')';
        '[': d := ']';
        '{': d := '}';
        '<': d := '>';
        else d := #0;
      end;
      if d <> #0 then
        result := p.tok[p.idx+1].symbol[strStart] <> d;
    end
  end
end;

procedure parseBackslash(var p: TRstParser; father: PRstNode);
begin
  assert(p.tok[p.idx].kind = tkPunct);
  if p.tok[p.idx].symbol = '\\' then begin
    addSon(father, newRstNode(rnLeaf, '\'+''));
    inc(p.idx);
  end
  else if p.tok[p.idx].symbol = '\'+'' then begin
    // XXX: Unicode?
    inc(p.idx);
    if p.tok[p.idx].kind <> tkWhite then addSon(father, newLeaf(p));
    inc(p.idx);
  end
  else begin
    addSon(father, newLeaf(p));
    inc(p.idx)
  end
end;

function match(const p: TRstParser; start: int; const expr: string): bool;
// regular expressions are:
// special char     exact match
// 'w'              tkWord
// ' '              tkWhite
// 'a'              tkAdornment
// 'i'              tkIndent
// 'p'              tkPunct
// 'T'              always true
// 'E'              whitespace, indent or eof
// 'e'              tkWord or '#' (for enumeration lists)
var
  i, j, last, len: int;
  c: char;
begin
  i := strStart;
  j := start;
  last := length(expr)+strStart-1;
  while i <= last do begin
    case expr[i] of
      'w': result := p.tok[j].kind = tkWord;
      ' ': result := p.tok[j].kind = tkWhite;
      'i': result := p.tok[j].kind = tkIndent;
      'p': result := p.tok[j].kind = tkPunct;
      'a': result := p.tok[j].kind = tkAdornment;
      'o': result := p.tok[j].kind = tkOther;
      'T': result := true;
      'E': result := p.tok[j].kind in [tkEof, tkWhite, tkIndent];
      'e': begin
        result := (p.tok[j].kind = tkWord) or (p.tok[j].symbol = '#'+'');
        if result then
          case p.tok[j].symbol[strStart] of
            'a'..'z', 'A'..'Z': result := length(p.tok[j].symbol) = 1;
            '0'..'9': result := allCharsInSet(p.tok[j].symbol, ['0'..'9']);
            else begin end
          end
      end
      else begin
        c := expr[i];
        len := 0;
        while (i <= last) and (expr[i] = c) do begin inc(i); inc(len) end;
        dec(i);
        result := (p.tok[j].kind in [tkPunct, tkAdornment])
          and (length(p.tok[j].symbol) = len)
          and (p.tok[j].symbol[strStart] = c);
      end
    end;
    if not result then exit;
    inc(j);
    inc(i)
  end;
  result := true
end;

procedure fixupEmbeddedRef(n, a, b: PRstNode);
var
  i, sep, incr: int;
begin
  sep := -1;
  for i := rsonsLen(n)-2 downto 0 do
    if n.sons[i].text = '<'+'' then begin sep := i; break end;
  if (sep > 0) and (n.sons[sep-1].text[strStart] = ' ') then incr := 2
  else incr := 1;
  for i := 0 to sep-incr do addSon(a, n.sons[i]);
  for i := sep+1 to rsonsLen(n)-2 do addSon(b, n.sons[i]);
end;

function parsePostfix(var p: TRstParser; n: PRstNode): PRstNode;
var
  a, b: PRstNode;
begin
  result := n;
  if isInlineMarkupEnd(p, '_'+'') then begin
    inc(p.idx);
    if (p.tok[p.idx-2].symbol ='`'+'')
    and (p.tok[p.idx-3].symbol = '>'+'') then begin
      a := newRstNode(rnInner);
      b := newRstNode(rnInner);
      fixupEmbeddedRef(n, a, b);
      if rsonsLen(a) = 0 then begin
        result := newRstNode(rnStandaloneHyperlink);
        addSon(result, b);
      end
      else begin
        result := newRstNode(rnHyperlink);
        addSon(result, a);
        addSon(result, b);
        setRef(p, rstnodeToRefname(a), b);
      end
    end
    else if n.kind = rnInterpretedText then
      n.kind := rnRef
    else begin
      result := newRstNode(rnRef);
      addSon(result, n);
    end;
  end
  else if match(p, p.idx, ':w:') then begin
    // a role:
    if p.tok[p.idx+1].symbol = 'idx' then
      n.kind := rnIdx
    else if p.tok[p.idx+1].symbol = 'literal' then
      n.kind := rnInlineLiteral
    else if p.tok[p.idx+1].symbol = 'strong' then
      n.kind := rnStrongEmphasis
    else if p.tok[p.idx+1].symbol = 'emphasis' then
      n.kind := rnEmphasis
    else if (p.tok[p.idx+1].symbol = 'sub')
         or (p.tok[p.idx+1].symbol = 'subscript') then
      n.kind := rnSub
    else if (p.tok[p.idx+1].symbol = 'sup')
         or (p.tok[p.idx+1].symbol = 'supscript') then
      n.kind := rnSup
    else begin
      result := newRstNode(rnGeneralRole);
      n.kind := rnInner;
      addSon(result, n);
      addSon(result, newRstNode(rnLeaf, p.tok[p.idx+1].symbol));
    end;
    inc(p.idx, 3)
  end
end;

function isURL(const p: TRstParser; i: int): bool;
begin
  result := (p.tok[i+1].symbol = ':'+'') and (p.tok[i+2].symbol = '//')
    and (p.tok[i+3].kind = tkWord) and (p.tok[i+4].symbol = '.'+'')
end;

procedure parseURL(var p: TRstParser; father: PRstNode);
var
  n: PRstNode;
begin
  //if p.tok[p.idx].symbol[strStart] = '<' then begin
  if isURL(p, p.idx) then begin
    n := newRstNode(rnStandaloneHyperlink);
    while true do begin
      case p.tok[p.idx].kind of
        tkWord, tkAdornment, tkOther: begin end;
        tkPunct: begin
          if not (p.tok[p.idx+1].kind in [tkWord, tkAdornment, tkOther, tkPunct])
          then break
        end
        else break
      end;
      addSon(n, newLeaf(p));
      inc(p.idx);
    end;
    addSon(father, n);
  end
  else begin
    n := newLeaf(p);
    inc(p.idx);
    if p.tok[p.idx].symbol = '_'+'' then n := parsePostfix(p, n);
    addSon(father, n);
  end
end;

procedure parseUntil(var p: TRstParser; father: PRstNode;
                     const postfix: string; interpretBackslash: bool);
begin
  while true do begin
    case p.tok[p.idx].kind of
      tkPunct: begin
        if isInlineMarkupEnd(p, postfix) then begin
          inc(p.idx);
          break;
        end
        else if interpretBackslash then
          parseBackslash(p, father)
        else begin
          addSon(father, newLeaf(p));
          inc(p.idx);
        end
      end;
      tkAdornment, tkWord, tkOther: begin
        addSon(father, newLeaf(p));
        inc(p.idx);
      end;
      tkIndent: begin
        addSon(father, newRstNode(rnLeaf, ' '+''));
        inc(p.idx);
        if p.tok[p.idx].kind = tkIndent then begin
          rstMessage(p, errXExpected, postfix);
          break
        end
      end;
      tkWhite: begin
        addSon(father, newRstNode(rnLeaf, ' '+''));
        inc(p.idx);
      end
      else
        rstMessage(p, errXExpected, postfix);
    end
  end
end;

procedure parseInline(var p: TRstParser; father: PRstNode);
var
  n: PRstNode;
begin
  case p.tok[p.idx].kind of
    tkPunct: begin
      if isInlineMarkupStart(p, '**') then begin
        inc(p.idx);
        n := newRstNode(rnStrongEmphasis);
        parseUntil(p, n, '**', true);
        addSon(father, n);
      end
      else if isInlineMarkupStart(p, '*'+'') then begin
        inc(p.idx);
        n := newRstNode(rnEmphasis);
        parseUntil(p, n, '*'+'', true);
        addSon(father, n);
      end
      else if isInlineMarkupStart(p, '``') then begin
        inc(p.idx);
        n := newRstNode(rnInlineLiteral);
        parseUntil(p, n, '``', false);
        addSon(father, n);
      end
      else if isInlineMarkupStart(p, '`'+'') then begin
        inc(p.idx);
        n := newRstNode(rnInterpretedText);
        parseUntil(p, n, '`'+'', true);
        n := parsePostfix(p, n);
        addSon(father, n);
      end
      else if isInlineMarkupStart(p, '|'+'') then begin
        inc(p.idx);
        n := newRstNode(rnSubstitutionReferences);
        parseUntil(p, n, '|'+'', false);
        addSon(father, n);
      end
      else begin
        parseBackslash(p, father);
      end;
    end;
    tkWord: parseURL(p, father);
    tkAdornment, tkOther, tkWhite: begin
      addSon(father, newLeaf(p));
      inc(p.idx);
    end
    else assert(false);
  end
end;

function getDirective(var p: TRstParser): string;
var
  j: int;
begin
  if (p.tok[p.idx].kind = tkWhite) and (p.tok[p.idx+1].kind = tkWord) then begin
    j := p.idx;
    inc(p.idx);
    result := p.tok[p.idx].symbol;
    inc(p.idx);
    while p.tok[p.idx].kind in [tkWord, tkPunct, tkAdornment, tkOther] do begin
      if p.tok[p.idx].symbol = '::' then break;
      add(result, p.tok[p.idx].symbol);
      inc(p.idx);
    end;
    if (p.tok[p.idx].kind = tkWhite) then inc(p.idx);
    if p.tok[p.idx].symbol = '::' then begin
      inc(p.idx);
      if (p.tok[p.idx].kind = tkWhite) then inc(p.idx);
    end
    else begin
      p.idx := j; // set back
      result := '' // error
    end
  end
  else
    result := '';
end;

function parseComment(var p: TRstParser): PRstNode;
var
  indent: int;
begin
  case p.tok[p.idx].kind of
    tkIndent, tkEof: begin
      if p.tok[p.idx+1].kind = tkIndent then begin
        inc(p.idx);
        // empty comment
      end
      else begin
        indent := p.tok[p.idx].ival;
        while True do begin
          case p.tok[p.idx].kind of
            tkEof: break;
            tkIndent: begin
              if (p.tok[p.idx].ival < indent) then break;
            end
            else begin end
          end;
          inc(p.idx)
        end
      end
    end
    else
      while not (p.tok[p.idx].kind in [tkIndent, tkEof]) do inc(p.idx);
  end;
  result := nil;
end;

type
  TDirKind = ( // must be ordered alphabetically!
    dkNone, dkAuthor, dkAuthors, dkCodeBlock, dkContainer,
    dkContents, dkFigure, dkImage, dkInclude, dkIndex, dkRaw, dkTitle
  );
const
  DirIds: array [0..11] of string = (
    '', 'author', 'authors', 'code-block', 'container',
    'contents', 'figure', 'image', 'include', 'index', 'raw', 'title'
  );

function getDirKind(const s: string): TDirKind;
var
  i: int;
begin
  i := binaryStrSearch(DirIds, s);
  if i >= 0 then result := TDirKind(i)
  else result := dkNone
end;

procedure parseLine(var p: TRstParser; father: PRstNode);
begin
  while True do begin
    case p.tok[p.idx].kind of
      tkWhite, tkWord, tkOther, tkPunct: parseInline(p, father);
      else break;
    end
  end
end;

procedure parseSection(var p: TRstParser; result: PRstNode); forward;

function parseField(var p: TRstParser): PRstNode;
var
  col, indent: int;
  fieldname, fieldbody: PRstNode;
begin
  result := newRstNode(rnField);
  col := p.tok[p.idx].col;
  inc(p.idx); // skip :
  fieldname := newRstNode(rnFieldname);
  parseUntil(p, fieldname, ':'+'', false);
  fieldbody := newRstNode(rnFieldbody);

  if p.tok[p.idx].kind <> tkIndent then
    parseLine(p, fieldbody);
  if p.tok[p.idx].kind = tkIndent then begin
    indent := p.tok[p.idx].ival;
    if indent > col then begin
      pushInd(p, indent);
      parseSection(p, fieldbody);
      popInd(p);
    end
  end;
  addSon(result, fieldname);
  addSon(result, fieldbody);
end;

function parseFields(var p: TRstParser): PRstNode;
var
  col: int;
begin
  result := nil;
  if (p.tok[p.idx].kind = tkIndent)
  and (p.tok[p.idx+1].symbol = ':'+'') then begin
    col := p.tok[p.idx].ival; // BUGFIX!
    result := newRstNode(rnFieldList);
    inc(p.idx);
    while true do begin
      addSon(result, parseField(p));
      if (p.tok[p.idx].kind = tkIndent) and (p.tok[p.idx].ival = col)
      and (p.tok[p.idx+1].symbol = ':'+'') then inc(p.idx)
      else break
    end
  end
end;

function getFieldValue(n: PRstNode; const fieldname: string): string;
var
  i: int;
  f: PRstNode;
begin
  result := '';
  if n.sons[1] = nil then exit;
  if (n.sons[1].kind <> rnFieldList) then
    InternalError('getFieldValue (2): ' + rstnodeKindToStr[n.sons[1].kind]);
  for i := 0 to rsonsLen(n.sons[1])-1 do begin
    f := n.sons[1].sons[i];
    if cmpIgnoreStyle(addNodes(f.sons[0]), fieldname) = 0 then begin
      result := addNodes(f.sons[1]);
      if result = '' then result := #1#1; // indicates that the field exists
      exit
    end
  end
end;

function getArgument(n: PRstNode): string;
begin
  if n.sons[0] = nil then result := ''
  else result := addNodes(n.sons[0]);
end;

function parseDotDot(var p: TRstParser): PRstNode; forward;

function parseLiteralBlock(var p: TRstParser): PRstNode;
var
  indent: int;
  n: PRstNode;
begin
  result := newRstNode(rnLiteralBlock);
  n := newRstNode(rnLeaf, '');
  if p.tok[p.idx].kind = tkIndent then begin
    indent := p.tok[p.idx].ival;
    inc(p.idx);
    while True do begin
      case p.tok[p.idx].kind of
        tkEof: break;
        tkIndent: begin
          if (p.tok[p.idx].ival < indent) then begin
            break;
          end
          else begin
            add(n.text, nl);
            add(n.text, repeatChar(p.tok[p.idx].ival - indent));
            inc(p.idx)
          end
        end
        else begin
          add(n.text, p.tok[p.idx].symbol);
          inc(p.idx)
        end
      end
    end
  end
  else begin
    while not (p.tok[p.idx].kind in [tkIndent, tkEof]) do begin
      add(n.text, p.tok[p.idx].symbol);
      inc(p.idx)
    end
  end;
  addSon(result, n);
end;

function getLevel(var map: TLevelMap; var lvl: int; c: Char): int;
begin
  if map[c] = 0 then begin
    inc(lvl);
    map[c] := lvl;
  end;
  result := map[c]
end;

function tokenAfterNewline(const p: TRstParser): int;
begin
  result := p.idx;
  while true do
    case p.tok[result].kind of
      tkEof: break;
      tkIndent: begin inc(result); break end;
      else inc(result)
    end
end;

// ---------------------------------------------------------------------------

function isLineBlock(const p: TRstParser): bool;
var
  j: int;
begin
  j := tokenAfterNewline(p);
  result := (p.tok[p.idx].col = p.tok[j].col) and (p.tok[j].symbol = '|'+'')
    or (p.tok[j].col > p.tok[p.idx].col)
end;

function predNL(const p: TRstParser): bool;
begin
  result := true;
  if (p.idx > 0) then
    result := (p.tok[p.idx-1].kind = tkIndent)
         and (p.tok[p.idx-1].ival = currInd(p))
end;

function isDefList(const p: TRstParser): bool;
var
  j: int;
begin
  j := tokenAfterNewline(p);
  result := (p.tok[p.idx].col < p.tok[j].col)
    and (p.tok[j].kind in [tkWord, tkOther, tkPunct])
    and (p.tok[j-2].symbol <> '::');
end;

function whichSection(const p: TRstParser): TRstNodeKind;
begin
  case p.tok[p.idx].kind of
    tkAdornment: begin
      if match(p, p.idx+1, 'ii') then result := rnTransition
      else if match(p, p.idx+1, ' a') then result := rnTable
      else if match(p, p.idx+1, 'i'+'') then result := rnOverline
      else result := rnLeaf
    end;
    tkPunct: begin
      if match(p, tokenAfterNewLine(p), 'ai') then
        result := rnHeadline
      else if p.tok[p.idx].symbol = '::' then
        result := rnLiteralBlock
      else if predNL(p)
          and ((p.tok[p.idx].symbol = '+'+'') or
          (p.tok[p.idx].symbol = '*'+'') or
          (p.tok[p.idx].symbol = '-'+''))
          and (p.tok[p.idx+1].kind = tkWhite) then
        result := rnBulletList
      else if (p.tok[p.idx].symbol = '|'+'') and isLineBlock(p) then
        result := rnLineBlock
      else if (p.tok[p.idx].symbol = '..') and predNL(p) then
        result := rnDirective
      else if (p.tok[p.idx].symbol = ':'+'') and predNL(p) then
        result := rnFieldList
      else if match(p, p.idx, '(e) ') then
        result := rnEnumList
      else if match(p, p.idx, '+a+') then begin
        result := rnGridTable;
        rstMessage(p, errGridTableNotImplemented);
      end
      else if isDefList(p) then
        result := rnDefList
      else if match(p, p.idx, '-w') or match(p, p.idx, '--w')
           or match(p, p.idx, '/w') then
        result := rnOptionList
      else
        result := rnParagraph
    end;
    tkWord, tkOther, tkWhite: begin
      if match(p, tokenAfterNewLine(p), 'ai') then
        result := rnHeadline
      else if isDefList(p) then
        result := rnDefList
      else if match(p, p.idx, 'e) ') or match(p, p.idx, 'e. ') then
        result := rnEnumList
      else
        result := rnParagraph;
    end;
    else result := rnLeaf;
  end
end;

function parseLineBlock(var p: TRstParser): PRstNode;
var
  col: int;
  item: PRstNode;
begin
  result := nil;
  if p.tok[p.idx+1].kind = tkWhite then begin
    col := p.tok[p.idx].col;
    result := newRstNode(rnLineBlock);
    pushInd(p, p.tok[p.idx+2].col);
    inc(p.idx, 2);
    while true do begin
      item := newRstNode(rnLineBlockItem);
      parseSection(p, item);
      addSon(result, item);
      if (p.tok[p.idx].kind = tkIndent) and (p.tok[p.idx].ival = col)
      and (p.tok[p.idx+1].symbol = '|'+'')
      and (p.tok[p.idx+2].kind = tkWhite) then inc(p.idx, 3)
      else break;
    end;
    popInd(p);
  end;
end;

procedure parseParagraph(var p: TRstParser; result: PRstNode);
begin
  while True do begin
    case p.tok[p.idx].kind of
      tkIndent: begin
        if p.tok[p.idx+1].kind = tkIndent then begin
          inc(p.idx);
          break
        end
        else if (p.tok[p.idx].ival = currInd(p)) then begin
          inc(p.idx);
          case whichSection(p) of
            rnParagraph, rnLeaf, rnHeadline, rnOverline, rnDirective:
              addSon(result, newRstNode(rnLeaf, ' '+''));
            rnLineBlock: addSonIfNotNil(result, parseLineBlock(p));
            else break;
          end;
        end
        else break
      end;
      tkPunct: begin
        if (p.tok[p.idx].symbol = '::') and (p.tok[p.idx+1].kind = tkIndent)
        and (currInd(p) < p.tok[p.idx+1].ival) then begin
          addSon(result, newRstNode(rnLeaf, ':'+''));
          inc(p.idx); // skip '::'
          addSon(result, parseLiteralBlock(p));
          break
        end
        else
          parseInline(p, result)
      end;
      tkWhite, tkWord, tkAdornment, tkOther:
        parseInline(p, result);
      else break;
    end
  end
end;

function parseParagraphWrapper(var p: TRstParser): PRstNode;
begin
  result := newRstNode(rnParagraph);
  parseParagraph(p, result);
end;

function parseHeadline(var p: TRstParser): PRstNode;
var
  c: Char;
begin
  result := newRstNode(rnHeadline);
  parseLine(p, result);
  assert(p.tok[p.idx].kind = tkIndent);
  assert(p.tok[p.idx+1].kind = tkAdornment);
  c := p.tok[p.idx+1].symbol[strStart];
  inc(p.idx, 2);
  result.level := getLevel(p.s.underlineToLevel, p.s.uLevel, c);
end;

type
  TIntSeq = array of int;

function tokEnd(const p: TRstParser): int;
begin
  result := p.tok[p.idx].col + length(p.tok[p.idx].symbol) - 1;
end;

procedure getColumns(var p: TRstParser; var cols: TIntSeq);
var
  L: int;
begin
  L := 0;
  while true do begin
    inc(L);
    setLength(cols, L);
    cols[L-1] := tokEnd(p);
    assert(p.tok[p.idx].kind = tkAdornment);
    inc(p.idx);
    if p.tok[p.idx].kind <> tkWhite then break;
    inc(p.idx);
    if p.tok[p.idx].kind <> tkAdornment then break
  end;
  if p.tok[p.idx].kind = tkIndent then inc(p.idx);
  // last column has no limit:
  cols[L-1] := 32000;
end;

function parseDoc(var p: TRstParser): PRstNode; forward;

function parseSimpleTable(var p: TRstParser): PRstNode;
var
  cols: TIntSeq;
  row: array of string;
  j, i, last, line: int;
  c: Char;
  q: TRstParser;
  a, b: PRstNode;
begin
  result := newRstNode(rnTable);
{@ignore}
  cols := nil;
  row := nil;
{@emit
  cols := @[];}
{@emit
  row := @[];}
  a := nil;
  c := p.tok[p.idx].symbol[strStart];
  while true do begin
    if p.tok[p.idx].kind = tkAdornment then begin
      last := tokenAfterNewline(p);
      if p.tok[last].kind in [tkEof, tkIndent] then begin
        // skip last adornment line:
        p.idx := last; break
      end;
      getColumns(p, cols);
      setLength(row, length(cols));
      if a <> nil then
        for j := 0 to rsonsLen(a)-1 do a.sons[j].kind := rnTableHeaderCell;
    end;
    if p.tok[p.idx].kind = tkEof then break;
    for j := 0 to high(row) do row[j] := '';
    // the following while loop iterates over the lines a single cell may span:
    line := p.tok[p.idx].line;
    while true do begin
      i := 0;
      while not (p.tok[p.idx].kind in [tkIndent, tkEof]) do begin
        if (tokEnd(p) <= cols[i]) then begin
          add(row[i], p.tok[p.idx].symbol);
          inc(p.idx);
        end
        else begin
          if p.tok[p.idx].kind = tkWhite then inc(p.idx);
          inc(i)
        end
      end;
      if p.tok[p.idx].kind = tkIndent then inc(p.idx);
      if tokEnd(p) <= cols[0] then break;
      if p.tok[p.idx].kind in [tkEof, tkAdornment] then break;
      for j := 1 to high(row) do addChar(row[j], #10);
    end;
    // process all the cells:
    a := newRstNode(rnTableRow);
    for j := 0 to high(row) do begin
      initParser(q, p.s);
      q.col := cols[j];
      q.line := line-1;
      q.filename := p.filename;
      getTokens(row[j], false, q.tok);
      b := newRstNode(rnTableDataCell);
      addSon(b, parseDoc(q));
      addSon(a, b);
    end;
    addSon(result, a);
  end;
end;

function parseTransition(var p: TRstParser): PRstNode;
begin
  result := newRstNode(rnTransition);
  inc(p.idx);
  if p.tok[p.idx].kind = tkIndent then inc(p.idx);
  if p.tok[p.idx].kind = tkIndent then inc(p.idx);
end;

function parseOverline(var p: TRstParser): PRstNode;
var
  c: char;
begin
  c := p.tok[p.idx].symbol[strStart];
  inc(p.idx, 2);
  result := newRstNode(rnOverline);
  while true do begin
    parseLine(p, result);
    if p.tok[p.idx].kind = tkIndent then begin
      inc(p.idx);
      if p.tok[p.idx-1].ival > currInd(p) then
        addSon(result, newRstNode(rnLeaf, ' '+''))
      else
        break
    end
    else break
  end;
  result.level := getLevel(p.s.overlineToLevel, p.s.oLevel, c);
  if p.tok[p.idx].kind = tkAdornment then begin
    inc(p.idx); // XXX: check?
    if p.tok[p.idx].kind = tkIndent then inc(p.idx);
  end
end;

function parseBulletList(var p: TRstParser): PRstNode;
var
  bullet: string;
  col: int;
  item: PRstNode;
begin
  result := nil;
  if p.tok[p.idx+1].kind = tkWhite then begin
    bullet := p.tok[p.idx].symbol;
    col := p.tok[p.idx].col;
    result := newRstNode(rnBulletList);
    pushInd(p, p.tok[p.idx+2].col);
    inc(p.idx, 2);
    while true do begin
      item := newRstNode(rnBulletItem);
      parseSection(p, item);
      addSon(result, item);
      if (p.tok[p.idx].kind = tkIndent) and (p.tok[p.idx].ival = col)
      and (p.tok[p.idx+1].symbol = bullet)
      and (p.tok[p.idx+2].kind = tkWhite) then inc(p.idx, 3)
      else break;
    end;
    popInd(p);
  end;
end;

function parseOptionList(var p: TRstParser): PRstNode;
var
  a, b, c: PRstNode;
  j: int;
begin
  result := newRstNode(rnOptionList);
  while true do begin
    if match(p, p.idx, '-w')
    or match(p, p.idx, '--w')
    or match(p, p.idx, '/w') then begin
      a := newRstNode(rnOptionGroup);
      b := newRstNode(rnDescription);
      c := newRstNode(rnOptionListItem);
      while not (p.tok[p.idx].kind in [tkIndent, tkEof]) do begin
        if (p.tok[p.idx].kind = tkWhite)
        and (length(p.tok[p.idx].symbol) > 1) then begin
          inc(p.idx); break
        end;
        addSon(a, newLeaf(p));
        inc(p.idx);
      end;
      j := tokenAfterNewline(p);
      if (j > 0) and (p.tok[j-1].kind = tkIndent)
      and (p.tok[j-1].ival > currInd(p)) then begin
        pushInd(p, p.tok[j-1].ival);
        parseSection(p, b);
        popInd(p);
      end
      else begin
        parseLine(p, b);
      end;
      if (p.tok[p.idx].kind = tkIndent) then inc(p.idx);
      addSon(c, a);
      addSon(c, b);
      addSon(result, c);
    end
    else break;
  end
end;

function parseDefinitionList(var p: TRstParser): PRstNode;
var
  j, col: int;
  a, b, c: PRstNode;
begin
  result := nil;
  j := tokenAfterNewLine(p)-1;
  if (j >= 1) and (p.tok[j].kind = tkIndent)
  and (p.tok[j].ival > currInd(p)) and (p.tok[j-1].symbol <> '::') then begin
    col := p.tok[p.idx].col;
    result := newRstNode(rnDefList);
    while true do begin
      j := p.idx;
      a := newRstNode(rnDefName);
      parseLine(p, a);
      //writeln('after def line: ', p.tok[p.idx].ival :1, '  ', col : 1);
      if (p.tok[p.idx].kind = tkIndent)
      and (p.tok[p.idx].ival > currInd(p))
      and (p.tok[p.idx+1].symbol <> '::')
      and not (p.tok[p.idx+1].kind in [tkIndent, tkEof]) then begin
        pushInd(p, p.tok[p.idx].ival);
        b := newRstNode(rnDefBody);
        parseSection(p, b);
        c := newRstNode(rnDefItem);
        addSon(c, a);
        addSon(c, b);
        addSon(result, c);
        popInd(p);
      end
      else begin
        p.idx := j;
        break
      end;
      if (p.tok[p.idx].kind = tkIndent) and (p.tok[p.idx].ival = col) then begin
        inc(p.idx);
        j := tokenAfterNewLine(p)-1;
        if (j >= 1) and (p.tok[j].kind = tkIndent)
        and (p.tok[j].ival > col)
        and (p.tok[j-1].symbol <> '::')
        and (p.tok[j+1].kind <> tkIndent) then begin end
        else break
      end
    end;
    if rsonsLen(result) = 0 then result := nil
  end
end;

function parseEnumList(var p: TRstParser): PRstNode;
const
  wildcards: array [0..2] of string = ('(e) ', 'e) ', 'e. ');
  wildpos: array [0..2] of int = (1, 0, 0);
var
  w, col, j: int;
  item: PRstNode;
begin
  result := nil;
  w := 0;
  while w <= 2 do begin
    if match(p, p.idx, wildcards[w]) then break;
    inc(w);
  end;
  if w <= 2 then begin
    col := p.tok[p.idx].col;
    result := newRstNode(rnEnumList);
    inc(p.idx, wildpos[w]+3);
    j := tokenAfterNewLine(p);
    if (p.tok[j].col = p.tok[p.idx].col) or match(p, j, wildcards[w]) then begin
      pushInd(p, p.tok[p.idx].col);
      while true do begin
        item := newRstNode(rnEnumItem);
        parseSection(p, item);
        addSon(result, item);
        if (p.tok[p.idx].kind = tkIndent)
        and (p.tok[p.idx].ival = col)
        and match(p, p.idx+1, wildcards[w]) then
          inc(p.idx, wildpos[w]+4)
        else
          break
      end;
      popInd(p);
    end
    else begin
      dec(p.idx, wildpos[w]+3);
      result := nil
    end
  end
end;

function sonKind(father: PRstNode; i: int): TRstNodeKind;
begin
  result := rnLeaf;
  if i < rsonsLen(father) then result := father.sons[i].kind;
end;

procedure parseSection(var p: TRstParser; result: PRstNode);
var
  a: PRstNode;
  k: TRstNodeKind;
  leave: bool;
begin
  while true do begin
    leave := false;
    assert(p.idx >= 0);
    while p.tok[p.idx].kind = tkIndent do begin
      if currInd(p) = p.tok[p.idx].ival then begin
        inc(p.idx);
      end
      else if p.tok[p.idx].ival > currInd(p) then begin
        pushInd(p, p.tok[p.idx].ival);
        a := newRstNode(rnBlockQuote);
        parseSection(p, a);
        addSon(result, a);
        popInd(p);
      end
      else begin
        leave := true;
        break;
      end
    end;
    if leave then break;
    if p.tok[p.idx].kind = tkEof then break;
    a := nil;
    k := whichSection(p);
    case k of
      rnLiteralBlock: begin
        inc(p.idx); // skip '::'
        a := parseLiteralBlock(p);
      end;
      rnBulletList: a := parseBulletList(p);
      rnLineblock: a := parseLineBlock(p);
      rnDirective: a := parseDotDot(p);
      rnEnumList: a := parseEnumList(p);
      rnLeaf: begin
        rstMessage(p, errNewSectionExpected);
      end;
      rnParagraph: begin end;
      rnDefList: a := parseDefinitionList(p);
      rnFieldList: begin
        dec(p.idx);
        a := parseFields(p);
      end;
      rnTransition: a := parseTransition(p);
      rnHeadline: a := parseHeadline(p);
      rnOverline: a := parseOverline(p);
      rnTable: a := parseSimpleTable(p);
      rnOptionList: a := parseOptionList(p);
      else InternalError('rst.parseSection()');
    end;
    if (a = nil) and (k <> rnDirective) then begin
      a := newRstNode(rnParagraph);
      parseParagraph(p, a);
    end;
    addSonIfNotNil(result, a);
  end;
  if (sonKind(result, 0) = rnParagraph)
  and (sonKind(result, 1) <> rnParagraph) then
    result.sons[0].kind := rnInner;
end;

function parseSectionWrapper(var p: TRstParser): PRstNode;
begin
  result := newRstNode(rnInner);
  parseSection(p, result);
  while (result.kind = rnInner) and (rsonsLen(result) = 1) do
    result := result.sons[0]
end;

function parseDoc(var p: TRstParser): PRstNode;
begin
  result := parseSectionWrapper(p);
  if p.tok[p.idx].kind <> tkEof then
    rstMessage(p, errGeneralParseError);
end;

type
  TDirFlag = (hasArg, hasOptions, argIsFile);
  TDirFlags = set of TDirFlag;
  TSectionParser = function (var p: TRstParser): PRstNode;

function parseDirective(var p: TRstParser; flags: TDirFlags;
                        contentParser: TSectionParser): PRstNode;
var
  args, options, content: PRstNode;
begin
  result := newRstNode(rnDirective);
  args := nil;
  options := nil;
  if hasArg in flags then begin
    args := newRstNode(rnDirArg);
    if argIsFile in flags then begin
      while True do begin
        case p.tok[p.idx].kind of
          tkWord, tkOther, tkPunct, tkAdornment: begin
            addSon(args, newLeaf(p));
            inc(p.idx);
          end;
          else break;
        end
      end
    end
    else begin
      parseLine(p, args);
    end
  end;
  addSon(result, args);
  if hasOptions in flags then begin
    if (p.tok[p.idx].kind = tkIndent) and (p.tok[p.idx].ival >= 3)
    and (p.tok[p.idx+1].symbol = ':'+'') then
      options := parseFields(p);
  end;
  addSon(result, options);
  if (assigned(contentParser)) and (p.tok[p.idx].kind = tkIndent)
  and (p.tok[p.idx].ival > currInd(p)) then begin
    pushInd(p, p.tok[p.idx].ival);
    content := contentParser(p);
    popInd(p);
    addSon(result, content)
  end
  else
    addSon(result, nil);
end;

function dirInclude(var p: TRstParser): PRstNode;
(*
The following options are recognized:

start-after : text to find in the external data file
    Only the content after the first occurrence of the specified text will
    be included.
end-before : text to find in the external data file
    Only the content before the first occurrence of the specified text
    (but after any after text) will be included.
literal : flag (empty)
    The entire included text is inserted into the document as a single
    literal block (useful for program listings).
encoding : name of text encoding
    The text encoding of the external data file. Defaults to the document's
    encoding (if specified).
*)
var
  n: PRstNode;
  filename, path: string;
  q: TRstParser;
begin
  result := nil;
  n := parseDirective(p, {@set}[hasArg, argIsFile, hasOptions], nil);
  filename := strip(addNodes(n.sons[0]));
  path := findFile(filename);
  if path = '' then
    rstMessage(p, errCannotOpenFile, filename)
  else begin
    // XXX: error handling; recursive file inclusion!
    if getFieldValue(n, 'literal') <> '' then begin
      result := newRstNode(rnLiteralBlock);
      addSon(result, newRstNode(rnLeaf, readFile(path)));
    end
    else begin
      initParser(q, p.s);
      q.filename := filename;
      getTokens(readFile(path), false, q.tok);
      // workaround a GCC bug: 
      if find(q.tok[high(q.tok)].symbol, #0#1#2) > 0 then begin
        InternalError('Too many binary zeros in include file');
      end;
      result := parseDoc(q);
    end
  end
end;

function dirCodeBlock(var p: TRstParser): PRstNode;
var
  n: PRstNode;
  filename, path: string;
begin
  result := parseDirective(p, {@set}[hasArg, hasOptions], parseLiteralBlock);
  filename := strip(getFieldValue(result, 'file'));
  if filename <> '' then begin
    path := findFile(filename);
    if path = '' then rstMessage(p, errCannotOpenFile, filename);
    n := newRstNode(rnLiteralBlock);
    addSon(n, newRstNode(rnLeaf, readFile(path)));
    result.sons[2] := n;
  end;
  result.kind := rnCodeBlock;
end;

function dirContainer(var p: TRstParser): PRstNode;
begin
  result := parseDirective(p, {@set}[hasArg], parseSectionWrapper);
  assert(result.kind = rnDirective);
  assert(rsonsLen(result) = 3);
  result.kind := rnContainer;
end;

function dirImage(var p: TRstParser): PRstNode;
begin
  result := parseDirective(p, {@set}[hasOptions, hasArg, argIsFile], nil);
  result.kind := rnImage
end;

function dirFigure(var p: TRstParser): PRstNode;
begin
  result := parseDirective(p, {@set}[hasOptions, hasArg, argIsFile],
                           parseSectionWrapper);
  result.kind := rnFigure
end;

function dirTitle(var p: TRstParser): PRstNode;
begin
  result := parseDirective(p, {@set}[hasArg], nil);
  result.kind := rnTitle
end;

function dirContents(var p: TRstParser): PRstNode;
begin
  result := parseDirective(p, {@set}[hasArg], nil);
  result.kind := rnContents
end;

function dirIndex(var p: TRstParser): PRstNode;
begin
  result := parseDirective(p, {@set}[], parseSectionWrapper);
  result.kind := rnIndex
end;

function dirRaw(var p: TRstParser): PRstNode;
(*
The following options are recognized:

file : string (newlines removed)
    The local filesystem path of a raw data file to be included.
url : string (whitespace removed)
    An Internet URL reference to a raw data file to be included.
encoding : name of text encoding
    The text encoding of the external raw data (file or URL).
    Defaults to the document's encoding (if specified).
*)
var
  filename, path, f: string;
begin
  result := parseDirective(p, {@set}[hasOptions], parseSectionWrapper);
  result.kind := rnRaw;
  filename := getFieldValue(result, 'file');
  if filename <> '' then begin
    path := findFile(filename);
    if path = '' then
      rstMessage(p, errCannotOpenFile, filename)
    else begin
      f := readFile(path);
      result := newRstNode(rnRaw);
      addSon(result, newRstNode(rnLeaf, f));
    end
  end
end;

function parseDotDot(var p: TRstParser): PRstNode;
var
  d: string;
  col: int;
  a, b: PRstNode;
begin
  result := nil;
  col := p.tok[p.idx].col;
  inc(p.idx);
  d := getDirective(p);
  if d <> '' then begin
    pushInd(p, col);
    case getDirKind(d) of
      dkInclude:    result := dirInclude(p);
      dkImage:      result := dirImage(p);
      dkFigure:     result := dirFigure(p);
      dkTitle:      result := dirTitle(p);
      dkContainer:  result := dirContainer(p);
      dkContents:   result := dirContents(p);
      dkRaw:        result := dirRaw(p);
      dkCodeblock:  result := dirCodeBlock(p);
      dkIndex:      result := dirIndex(p);
      else          rstMessage(p, errInvalidDirectiveX, d);
    end;
    popInd(p);
  end
  else if match(p, p.idx, ' _') then begin
    // hyperlink target:
    inc(p.idx, 2);
    a := getReferenceName(p, ':'+'');
    if p.tok[p.idx].kind = tkWhite then inc(p.idx);
    b := untilEol(p);
    setRef(p, rstnodeToRefname(a), b);
  end
  else if match(p, p.idx, ' |') then begin
    // substitution definitions:
    inc(p.idx, 2);
    a := getReferenceName(p, '|'+'');
    if p.tok[p.idx].kind = tkWhite then inc(p.idx);
    if cmpIgnoreStyle(p.tok[p.idx].symbol, 'replace') = 0 then begin
      inc(p.idx);
      expect(p, '::');
      b := untilEol(p);
    end
    else if cmpIgnoreStyle(p.tok[p.idx].symbol, 'image') = 0 then begin
      inc(p.idx);
      b := dirImage(p);
    end
    else
      rstMessage(p, errInvalidDirectiveX, p.tok[p.idx].symbol);
    setSub(p, addNodes(a), b);
  end
  else if match(p, p.idx, ' [') then begin
    // footnotes, citations
    inc(p.idx, 2);
    a := getReferenceName(p, ']'+'');
    if p.tok[p.idx].kind = tkWhite then inc(p.idx);
    b := untilEol(p);
    setRef(p, rstnodeToRefname(a), b);
  end
  else
    result := parseComment(p);
end;

function resolveSubs(var p: TRstParser; n: PRstNode): PRstNode;
var
  i, x: int;
  y: PRstNode;
  e, key: string;
begin
  result := n;
  if n = nil then exit;
  case n.kind of
    rnSubstitutionReferences: begin
      x := findSub(p, n);
      if x >= 0 then result := p.s.subs[x].value
      else begin
        key := addNodes(n);
        e := getEnv(key);
        if e <> '' then result := newRstNode(rnLeaf, e)
        else rstMessage(p, warnUnknownSubstitutionX, key);
      end
    end;
    rnRef: begin
      y := findRef(p, rstnodeToRefname(n));
      if y <> nil then begin
        result := newRstNode(rnHyperlink);
        n.kind := rnInner;
        addSon(result, n);
        addSon(result, y);
      end
    end;
    rnLeaf: begin end;
    rnContents: p.hasToc := true;
    else begin
      for i := 0 to rsonsLen(n)-1 do
        n.sons[i] := resolveSubs(p, n.sons[i]);
    end
  end
end;

function rstParse(const text: string; // the text to be parsed
                  skipPounds: bool;
                  const filename: string; // for error messages
                  line, column: int;
                  var hasToc: bool): PRstNode;
var
  p: TRstParser;
begin
  if isNil(text) then
    rawMessage(errCannotOpenFile, filename);
  initParser(p, newSharedState());
  p.filename := filename;
  p.line := line;
  p.col := column;
  getTokens(text, skipPounds, p.tok);
  result := resolveSubs(p, parseDoc(p));
  hasToc := p.hasToc;
end;

end.