summaryrefslogtreecommitdiff
path: root/mod/tex/context/third/rst/rst_parser.lua
blob: 6aeeb7d584af40b01e67557c57098eb616d86946 (plain)
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
#!/usr/bin/env texlua
--------------------------------------------------------------------------------
--         FILE:  rst_parser.lua
--        USAGE:  refer to doc/documentation.rst
--  DESCRIPTION:  https://bitbucket.org/phg/context-rst/overview
--       AUTHOR:  Philipp Gesang (Phg), <megas.kapaneus@gmail.com>
--      VERSION:  0.3
--      CHANGED:  2011-05-08 17:55:54+0200
--------------------------------------------------------------------------------
--

local rst
local optional_setups

thirddata             = thirddata or { }
thirddata.rst         = { }
thirddata.rst_helpers = { }
local helpers         = thirddata.rst_helpers
if context then
    --thirddata.rst_setups  = optional_setups

    environment.loadluafile("rst_helpers")
    environment.loadluafile("rst_directives")
    environment.loadluafile("rst_setups" )
    environment.loadluafile("rst_context")

    rst             = rst_context
    optional_setups = thirddata.rst_setups
else
    --helpers         = require "rst_helpers"
    require "rst_helpers"
    rst_directives  = require "rst_directives"
    optional_setups = require "rst_setups"
    rst             = require "rst_context"
end

helpers.rst_debug = false

local warn = function(str, ...)
    if not helpers.rst_debug then return false end
    local slen = #str + 3
    str = "*["..str.."]"
    for i,j in ipairs({...}) do
        if 80 - i * 8 - slen < 0 then
            local indent = ""
            for i=1, slen do
                indent = indent .. " "
            end
            str = str .. "\n" .. indent
        end
        str = str .. string.format(" |%6s", string.strip(tostring(j)))
    end
    io.write(str .. " |\n")
    return 0
end

local C, Cb, Cc, Cg, Cmt, Cp, Cs, Ct 
    = lpeg.C, lpeg.Cb, lpeg.Cc, lpeg.Cg, lpeg.Cmt, lpeg.Cp, lpeg.Cs, lpeg.Ct

local P, R, S, V, match 
    = lpeg.P, lpeg.R, lpeg.S, lpeg.V, lpeg.match

local utf = unicode.utf8

local eol = P"\n"

state = {}
state.depth = 0
state.bullets = {} -- mapping bullet forms to depth
state.bullets.max = 0
state.lastbullet = ""
state.lastbullets = {}
state.roman_cache = {} -- storing roman numerals that were already converted
state.currentindent  = "" -- used in definition lists and elsewhere
state.previousindent = "" -- for literal blocks included in paragraphs to restore the paragraph indent
state.currentwidth   = 0 -- table layout
state.currentlayout  = {} -- table layout
state.previousadorn  = nil -- section underlining and overlining

state.footnotes = {}
state.footnotes.autonumber = 0
state.footnotes.numbered   = {}
state.footnotes.labeled    = {}
state.footnotes.autolabel  = {}
state.footnotes.symbol     = {}

state.addme = {}

do
    local first_adornment = ""
    local valid_adornment = P{
        [1] = "adorncheck",
        adorncheck  = V"check_first" * V"check_other"^1 * -P(1),

    --  check_first = Cg(V"adornment_char", "first"),   -- This *should* work but but due to some heavenly
                                                        -- intervention the governing rules of the universe
                                                        -- have been altered so as to annoy everybody 
                                                        -- trying to deploy it.

        check_first = Cmt(V"adornment_char", function(_,_, first)
                            first_adornment = first
                            return true
                        end)
                    ,
        check_other = Cmt(V"adornment_char", function(_,_, char)
                            local prev = first_adornment
                            return char == prev
                        end)
                    ,
        adornment_char = S[[!"#$%&'()*+,-./:;<=>?@[]^_`{|}~]] + P[[\\]], 
    }
    state.valid_adornment = valid_adornment
end

local enclosed_mapping = {
    ["'"] = "'",
    ['"'] = '"',
    ["("] = ")",
    ["["] = "]",
    ["{"] = "}",
    ["<"] = ">",
}

local utfchar = P{ -- from l-lpeg.lua, modified to use as grammar
    [1] = "utfchar",
    utf8byte      = R("\128\191"),
    utf8one       = R("\000\127"),
    utf8two       = R("\194\223") * V"utf8byte",
    utf8three     = R("\224\239") * V"utf8byte" * V"utf8byte",
    utf8four      = R("\240\244") * V"utf8byte" * V"utf8byte" * V"utf8byte",
    utfchar       = V"utf8one" + V"utf8two" + V"utf8three" + V"utf8four",
}



local parser = P{
    [1] = V"document",

    document = V"blank_line"^0 * Cs(V"block"^1),

--------------------------------------------------------------------------------
-- Blocks
--------------------------------------------------------------------------------

    block = V"explicit_markup"
          + Cs(V"section")     / rst.escape
          + V"target_block"
          + V"literal_block"
          + Cs(V"list")        / rst.escape
          + Cs(V"line_block")  / rst.escape
          + Cs(V"table_block") / rst.escape
          + V"transition"    --/ rst.escape
          + V"comment_block"
          + Cs(V"block_quote") / rst.escape
          + Cs(V"paragraph")   / rst.escape
          ,

--------------------------------------------------------------------------------
-- Explicit markup block
--------------------------------------------------------------------------------

    explicit_markup_start = V"double_dot" * V"whitespace",

    explicit_markup = V"footnote_block"
                    + V"directive_block"
                    + V"substitution_definition"
                    ,

    explicit_markup_block = V"explicit_markup"^1
                          ,

--------------------------------------------------------------------------------
-- Directives block
--------------------------------------------------------------------------------

    directive_block = V"directive"
                    --* (V"blank_line"^-1 * V"directive")^0
                    * V"end_block"
                    ,

    directive = V"explicit_markup_start"
              * C(((V"escaped_colon" + (1 - V"colon" - V"eol")) - V"substitution_text")^1)
              * V"double_colon"
              * (V"directive_block_multi" + V"directive_block_single")
              / rst.directive
              ,

    directive_block_multi = C((1 - V"eol")^0) * V"eol"
                          * V"directive_indented_lines"
                          ,

    directive_block_single = C((1 - V"eol")^1) * V"eol",

--------------------------------------------------------------------------------
-- Substitution definition block
--------------------------------------------------------------------------------

    substitution_definition = V"explicit_markup_start"
                            * V"substitution_text"
                            * V"whitespace"
                            * C((1 - V"colon" - V"space" - V"eol")^1) -- directive
                            * V"double_colon"
                            * Ct(V"data_directive_block")
                            * V"end_block"^-1
                            / rst.substitution_definition
                            ,

    substitution_text = V"bar"
                      * C((1 - V"bar" - V"eol")^1)
                      * V"bar"
                      ,
                     
    data_directive_block = V"data_directive_block_long"
                         + V"data_directive_block_short"
                         ,
    data_directive_block_short = C((1 - V"eol")^0) * V"eol",

    data_directive_block_long  = C((1 - V"eol")^0) * V"eol"
                               * V"directive_indented_lines"
                               ,

    directive_indented_lines = V"directive_indented_first"
                             * V"directive_indented_other"^0
                             ,


    directive_indented_first = Cmt(V"space"^1, function(s,i,indent)
                                    warn("sub-i", #indent, i)
                                    state.currentindent = indent
                                    return true
                                end)
                             * C((1 - V"eol")^1) * V"eol"
                             ,

    directive_indented_other = Cmt(V"space"^1, function(s,i,indent)
                                    warn("sub-m", #state.currentindent <= #indent, #indent, #state.currentindent, i)
                                    return #state.currentindent <= #indent
                                end)
                             * C((1 - V"eol")^1) * V"eol"
                             ,


--------------------------------------------------------------------------------
-- Explicit markup footnote block
--------------------------------------------------------------------------------

    footnote_block = V"footnote"^1 * V"end_block",

    footnote = V"explicit_markup_start"
             * (V"footnote_marker" + V"citation_reference_label")
             * C(V"footnote_content")
             * (V"blank_line" - V"end_block")^-1
             / rst.footnote
             ,

    footnote_marker = V"lsquare" * C(V"footnote_label") * V"rsquare" * V"whitespace"^0
                    ,

    citation_reference_label = V"lsquare" * C(V"letter" * (1 - V"rsquare")^1) * V"rsquare" * V"whitespace"^0,

    footnote_label = V"digit"^1
                   + (V"gartenzaun" * V"letter"^1)
                   + V"gartenzaun"
                   + V"asterisk"
                   ,

    footnote_content = V"footnote_long" -- single line
                     + V"footnote_simple"
                     ,

    footnote_simple = (1 - V"eol")^1 * V"eol"
                    ,

    footnote_long = (1 - V"eol")^1 * V"eol"
                  * V"footnote_body"
                  ,

    footnote_body = V"fn_body_first"
                  * (V"fn_body_other" + V"fn_body_other_block")^0
                  ,

    fn_body_first = Cmt(V"space"^1, function(s, i, indent)
                        warn("fn-in", true, #indent)
                        state.currentindent = indent
                        return true
                    end)
                  * (1 - V"eol")^1 * V"eol"
                    ,
    
    fn_matchindent = Cmt(V"space"^1, function(s, i, indent)
                        local tc = state.currentindent
                        warn("fn-ma", tc == indent, #tc, #indent, i)
                        return tc == indent
                    end)
                   ,

    fn_body_other = V"fn_body_other_regular"
                  * (V"blank_line" * V"fn_body_other_regular")^0
                  ,

    fn_body_other_regular = V"fn_matchindent"
                          * (1 - V"eol")^1 * V"eol"
                          ,

    -- TODO find a way to get those to work in footnotes!
    fn_body_other_block = V"line_block"
                        + V"table_block"
                        + V"transition"
                        + V"block_quote"
                        + V"list"
                        ,

--------------------------------------------------------------------------------
-- Table block
--------------------------------------------------------------------------------

    table_block = V"simple_table"
                + V"grid_table"
                ,

--------------------------------------------------------------------------------
-- Simple tables
--------------------------------------------------------------------------------

    simple_table = Ct(V"st_first_row"
                    * V"st_other_rows")
                 * V"end_block"
                 / function (tab)
                     return rst.simple_table(helpers.table.simple(tab))
                 end
                 ,

    st_first_row = V"st_setindent"
                 * C(V"st_setlayout")
                 * V"space"^0 
                 * V"eol"
                 ,

    st_setindent = Cmt(V"space"^0, function(s, i, indent)
                        warn("sta-i", "true",  #indent, "set", i)
                        state.currentindent = indent
                        return true
                    end)
                 ,

    st_matchindent = Cmt(V"space"^0, function(s, i, indent)
                          warn("sta-m", state.currentindent == indent, #indent, #state.currentindent, i)
                          return state.currentindent == indent
                      end)
                   ,

    st_setlayout = Cmt((V"equals"^1) * (V"spaces" * V"equals"^1)^1, function(s, i, layout)
                        local tc = state.currentlayout
                        warn("sta-l", #layout, "set", "", i)
                        tc.raw = layout
                        tc.bounds = helpers.get_st_boundaries(layout)
                        return true
                    end)
                 ,

    st_other_rows = (V"st_content"^1 * V"st_separator")^1,

    st_content = V"blank_line"^-1
               * C(V"st_matchlayout"),       

    st_matchlayout = -#V"st_separator" * Cmt((1 - V"eol")^1, function (s, i, content)
                        -- Don't check for matching indent but if the rest is
                        -- fine then the line should be sane. This allows
                        -- cells starting with spaces.
                        content = content:sub(#state.currentindent)
                        local tcb = state.currentlayout.bounds
                        local n = 1
                        local spaces_only = P" "^1
                        while n < #tcb.slices do
                            local from = tcb.slices[n]  .stop
                            local to   = tcb.slices[n+1].start
                            local between = spaces_only:match(content, from)
                            if not between then -- Cell spanning more than one row.
                                -- pass
                                warn("sta-c", "span", from, to, i)
                            elseif not (between >= to) then
                                warn("sta-c", "false", from, to, i)
                                return false
                            end
                            n = n + 1
                        end
                        warn("sta-c", "true", #tcb.slices, "", i)
                        return true
                     end)
                     * V"eol"
                   ,

    st_separator = V"st_matchindent"
                 * C(V"st_normal_sep" + V"st_colspan_sep")
                 * V"eol"
                 ,

    st_normal_sep = Cmt((V"equals"^1) * (V"spaces" * V"equals"^1)^1, function(s, i, layout)
                        warn("sta-s", state.currentlayout.raw == layout, #layout, #state.currentlayout.raw, i)
                        return state.currentlayout.raw == layout
                    end)
                  ,
                        
    st_colspan_sep = Cmt(V"dash"^1 * (V"spaces" * V"dash"^1)^0, function(s, i, layout)
                         local tcb = state.currentlayout.bounds
                         local this = helpers.get_st_boundaries (layout)
                         local start_valid = false
                         for start, _ in next, this.starts do
                             if tcb.starts[start] then
                                 start_valid = true
                                 local stop_valid = false
                                 for stop, _ in next, this.stops do
                                     if tcb.stops[stop] then -- bingo
                                         stop_valid = true
                                     end
                                 end
                                 if not stop_valid then
                                     warn("sta-x", stop_valid, #layout, #state.currentlayout.raw, i)
                                     return false
                                 end
                             end
                         end
                         warn("sta-x", start_valid, #layout, #state.currentlayout.raw, i)
                         return start_valid
                     end)
                   ,


--------------------------------------------------------------------------------
-- Grid tables
--------------------------------------------------------------------------------

    grid_table = Ct(V"gt_first_row"
                  * V"gt_other_rows")
               * V"blank_line"^1
               / function(tab)
                   return rst.grid_table(helpers.table.create(tab))
               end
               ,

    gt_first_row = V"gt_setindent"
                 * C(V"gt_sethorizontal")
                 * V"eol"
                 ,

    gt_setindent = Cmt(V"space"^0, function(s, i, indent)
                        warn("tab-i", true, #indent, "set", i)
                        state.currentindent = indent
                        return true
                    end)
                 ,

    gt_layoutmarkers = V"table_intersection" + V"table_hline" + V"table_header_hline",

    gt_sethorizontal = Cmt(V"gt_layoutmarkers"^3, function (s, i, width)
                             warn("tab-h", "width", "true", #width, "set", i)
                             state.currentwidth = #width
                             return true
                         end)
                     ,

    gt_other_rows = V"gt_head"^-1
                  * V"gt_body"
                  ,

    gt_matchindent = Cmt(V"space"^0, function (s, i, this)
        local matchme = state.currentindent
        warn("tab-m", "indent", #this == #matchme, #this, #matchme, i)
        return #this == #matchme
    end)
    ,


    gt_cell = (V"gt_content_cell" + V"gt_line_cell") 
    * (V"table_intersection" + V"table_vline")
    ,

    gt_content_cell = ((1 - V"table_vline" - V"table_intersection" - V"eol")^1),

    gt_line_cell = V"table_hline"^1,

    gt_contentrow = V"gt_matchindent"
                   * C((V"table_intersection" + V"table_vline")
                     * V"gt_cell"^1)
                   * V"whitespace"^-1 * V"eol"
                  ,

    gt_body = ((V"gt_contentrow" - V"gt_bodysep")^1 * V"gt_bodysep")^1,

    gt_bodysep = V"gt_matchindent" 
               * C(Cmt(V"table_intersection"
                     * (V"table_hline"^1 * V"table_intersection")^1, function(s, i, separator)
                          local matchme = state.currentwidth
                          warn("tab-m", "body", #separator == matchme, #separator, matchme, i)
                          return #separator == matchme
                      end))
               * V"whitespace"^-1 * V"eol"
               ,

    gt_head = V"gt_contentrow"^1
            * V"gt_headsep"
            ,

    gt_headsep = V"gt_matchindent" 
               * C(Cmt(V"table_intersection" 
                    * (V"table_header_hline"^1 * V"table_intersection")^1, function(s, i, separator)
                          local matchme = state.currentwidth
                          warn("tab-s", "head", #separator == matchme, #separator, matchme, i)
                          return #separator == matchme
                      end))
               * V"whitespace"^-1 * V"eol"
               ,


--------------------------------------------------------------------------------
-- Block quotes
--------------------------------------------------------------------------------

    block_quote = Ct(Cs(V"block_quote_first"
                   * V"block_quote_other"^0
                   * (V"blank_line" * V"block_quote_other"^1)^0)
                   * (V"blank_line"
                   *  Cs(V"block_quote_attri"))^-1)
                * V"end_block"
                / rst.block_quote
                ,

    block_quote_first = Cmt(V"space"^1, function (s, i, indent)
                             warn("bkq-i", #indent, "", indent, "", i)
                             state.currentindent = indent
                             return true
                         end) / ""
                      * -V"attrib_dash"
                      * (1 - V"eol")^1
                      * V"eol"
                      ,
                    
    block_quote_other = Cmt(V"space"^1, function (s, i, indent)
                            warn("bkq-m", #indent, #state.currentindent, 
                                           indent,  state.currentindent, i)
                            return state.currentindent == indent
                        end) / ""
                      * -V"attrib_dash"
                      * (1 - V"eol")^1
                      * V"eol"
                      ,

    block_quote_attri = V"block_quote_attri_first"
                      * V"block_quote_attri_other"^0,

    block_quote_attri_first = Cmt(V"space"^1 * V"attrib_dash" * V"space", function (s, i, indent)
                                   local t = state
                                   warn("bqa-i", utf.len(indent), #t.currentindent, 
                                                 indent,           t.currentindent, i)
                                   local ret = indent:match(" *") == t.currentindent
                                   t.currentindent = ret and indent or t.currentindent
                                   return ret
                               end) / ""
                            * (1 - V"eol")^1
                            * V"eol"
                            ,
                    
    block_quote_attri_other = Cmt(V"space"^1, function (s, i, indent)
                                  warn("bqa-m", #indent, utf.len(state.currentindent),
                                                 indent,  state.currentindent, i)
                                  return utf.len(state.currentindent) == #indent
                              end) / ""
                            * (1 - V"eol")^1
                            * V"eol"
                            ,

--------------------------------------------------------------------------------
-- Line blocks
--------------------------------------------------------------------------------

    line_block = Cs(V"line_block_first"
                  * (V"line_block_other"
                   + V"line_block_empty")^1)
               --* V"blank_line"
               * V"end_block"
               / rst.line_block
               ,

    line_block_marker = V"space"^0 * V"bar" * V"space",

    line_block_empty_marker = V"space"^0 * V"bar" * V"space"^0 * V"eol",


    line_block_first = Cmt(V"line_block_marker", function(s, i, marker)
                            warn("lbk-i", #marker, "", marker, "", i)
                            state.currentindent = marker
                            return true
                        end) / ""
                     * V"line_block_line"
                     ,

    line_block_empty = Cmt(V"line_block_empty_marker", function(s, i, marker)
                            warn("lbk-e", #marker, #state.currentindent, marker, state.currentindent, i)
                            marker = marker:gsub("|.*", "| ")
                            return state.currentindent == marker
                        end) / ""
                     / rst.line_block_empty
                     ,

    line_block_other = Cmt(V"line_block_marker", function(s, i, marker)
                            warn("lbk-m", #marker, #state.currentindent, marker, state.currentindent, i)
                            return state.currentindent == marker
                        end) / ""
                     * V"line_block_line"
                     ,

    line_block_line = Cs((1 - V"eol")^1
                       * V"line_block_cont"^0
                       * V"eol")
                    / rst.line_block_line
                    ,

    line_block_cont = (V"eol" - V"line_block_marker")
                    * Cmt(V"space"^1, function(s, i, spaces)
                            warn("lbk-c", #spaces, #state.currentindent, spaces, state.currentindent, i)
                            return #spaces >= #state.currentindent
                        end) / ""
                    * (1 - V"eol")^1
                    ,

--------------------------------------------------------------------------------
-- Literal blocks
--------------------------------------------------------------------------------

    literal_block = V"literal_block_marker"
                    * Cs(V"literal_block_lines")
                    * V"end_block"
                    / rst.literal_block,

    literal_block_marker = V"double_colon" * V"whitespace"^0 * V"eol" * V"blank_line",

    literal_block_lines = V"unquoted_literal_block_lines"
                        + V"quoted_literal_block_lines",

    unquoted_literal_block_lines = V"literal_block_first"
                                 * (V"blank_line"^-1 * V"literal_block_other")^0
                                 ,
                                 
    quoted_literal_block_lines =   V"quoted_literal_block_first"
                                 * (V"blank_line"^-1 * V"quoted_literal_block_other")^0
                                 ,

    literal_block_first = Cmt(V"space"^1, function (s, i, indent)
                        warn("lbk-f", #indent, "", "", i)
                        if not indent or
                            indent == "" then
                            return false
                        end
                        if state.currentindent and #state.currentindent < #indent then
                            state.currentindent = state.currentindent .. " "
                            return true
                        else
                            state.currentindent = " "
                            return true
                        end
                    end)
                   * V"rest_of_line"
                   * V"eol",

    literal_block_other = Cmt(V"space"^1, function (s, i, indent)
                        warn("lbk-m", 
                             #indent,
                             #state.currentindent,
                             #indent >= #state.currentindent, 
                             i)
                        return #indent >= #state.currentindent
                    end)
                   * V"rest_of_line"
                   * V"eol",

    quoted_literal_block_first = Cmt(V"adornment_char", function (s, i, indent)
                        warn("lbk-f", #indent, "", "", i)
                        if not indent    or
                            indent == "" then
                            return false
                        end
                        state.currentindent = indent
                        return true
                    end)
                   * V"rest_of_line"
                   * V"eol",

    quoted_literal_block_other = Cmt(V"adornment_char", function (s, i, indent)
                        warn("lbk-m", 
                             #indent,
                             #state.currentindent,
                             #indent >= #state.currentindent, 
                             i)
                        return #indent >= #state.currentindent
                    end)
                   * V"rest_of_line"
                   * V"eol",

--------------------------------------------------------------------------------
-- Lists
--------------------------------------------------------------------------------

    list = (V"option_list"
          + V"bullet_list"
          + V"definition_list"
          + V"field_list")
         - V"explicit_markup_start"
         ,

--------------------------------------------------------------------------------
-- Option lists
--------------------------------------------------------------------------------

    option_list = Cs((V"option_list_item"
                   * V"blank_line"^-1)^1)
                /rst.option_list,

    option_list_item = Ct(C(V"option_group")
                        * Cs(V"option_description"))
                     / rst.option_item,

    option_description = V"option_desc_next"
                       + V"option_desc_more"
                       + V"option_desc_single",

    option_desc_single = V"space"^2
                       --* V"rest_of_line"
                       * (1 - V"eol")^1
                       * V"eol",

    option_desc_more = V"space"^2
                     * (1 - V"eol")^1
                     * V"eol"
                     * V"indented_lines"
                     * (V"blank_line" * V"indented_lines")^0,

    option_desc_next = V"eol"
                     * V"indented_lines"
                     * (V"blank_line" * V"indented_lines")^0,

    option_group = V"option"
                 * (V"comma" * V"space" * V"option")^0,

    option = (V"option_posixlong"
            + V"option_posixshort"
            + V"option_dos_vms")
            * V"option_arg"^-1,

    option_arg = (V"equals" + V"space") 
               * ((V"letter" * (V"letter" + V"digit")^1)
                + (V"angle_left" * (1 - V"angle_right")^1 * V"angle_right")),

    option_posixshort = V"dash" * (V"letter" + V"digit"),

    option_posixlong = V"double_dash"
                     * V"letter"
                     * (V"letter" + V"digit" + V"dash")^1,

    option_dos_vms = V"slash"
                   * V"letter"^1,

--------------------------------------------------------------------------------
-- Field lists (for bibliographies etc.)
--------------------------------------------------------------------------------

    field_list = Cs(V"field"
                  * (V"blank_line"^-1 * V"field")^0)
               * V"end_block"
               / rst.field_list,

    field = Ct(V"field_marker"
             * V"field_body")
          / rst.field,

    field_marker = V"colon"
                 * C(V"field_name")
                 * V"colon",

    field_name = (V"escaped_colon" + (1 - V"colon"))^1,

    field_body = V"field_single" + V"field_multi",

    field_single = C((1 -V"eol")^1) * V"eol",

    field_multi = C((1 - V"eol")^0 * V"eol"
                  * V"indented_lines"^-1),

--------------------------------------------------------------------------------
-- Definition lists
--------------------------------------------------------------------------------

    definition_list = Ct((V"definition_item" - V"comment")
                      * (V"blank_line" * V"definition_item")^0)
                    * V"end_block"
                    / rst.deflist
                    ,

    definition_item = Ct(C(V"definition_term")
                       * V"definition_classifiers"
                       * V"eol"
                       * Ct(V"definition_def"))
                    ,

    definition_term = #(1 - V"space" - V"field_marker") 
                    * (1 - V"eol" - V"definition_classifier_separator")^1
                    ,

    definition_classifier_separator = V"space" * V"colon" * V"space",

    definition_classifiers = V"definition_classifier"^0,

    definition_classifier = V"definition_classifier_separator"
                          * C((1 - V"eol" - V"definition_classifier_separator")^1)
                          ,

    definition_def = C(V"definition_firstpar") * C(V"definition_par")^0
                   ,

    definition_indent = Cmt(V"space"^1, function(s, i, indent)
                            warn("def-i", #indent, #state.currentindent, indent == state.currentindent, i)
                            state.currentindent = indent
                            return true
                        end),

    definition_firstpar = V"definition_parinit"
                        * (V"definition_parline" - V"blank_line")^0
                        ,

    definition_par = V"blank_line"
                   * (V"definition_parline" - V"blank_line")^1
                   ,

    definition_parinit = V"definition_indent"
                       * (1 - V"eol")^1
                       * V"eol"
                       ,

    definition_parline = V"definition_match"
                       * (1 - V"eol")^1
                       * V"eol"
                       ,

    definition_match = Cmt(V"space"^1, function (s, i, this)
                            warn("def-m", #this, #state.currentindent, this == state.currentindent, i)
                            return this == state.currentindent
                        end),

--------------------------------------------------------------------------------
-- Bullet lists and enumerations
--------------------------------------------------------------------------------

    -- the next rule handles enumerations as well
    bullet_list = V"bullet_init"
                * (V"blank_line"^-1 * (V"bullet_list" + V"bullet_continue"))^1
                * V"bullet_stop"
                * Cmt(Cc(nil), function (s, i)
                    local t = state
                    warn("close", t.depth)
                    t.bullets[t.depth] = nil -- “pop”
                    t.depth = t.depth - 1
                    t.lastbullet = t.lastbullets[t.depth]
                    return true
                end),

    bullet_stop = V"end_block" / rst.stopitemize,

    bullet_init = Ct(C(V"bullet_first") * V"bullet_itemrest")
                / rst.bullet_item
                ,

    bullet_first = #Cmt(V"bullet_indent", function (s, i, bullet)
                        local t = state
                        local oldbullet = t.bullets[t.depth]
                        local n_spaces = match(P" "^0, bullet)
                        warn("first", 
                            t.depth, 
                            (t.depth == 0 and n_spaces >= 1) or
                            (t.depth >  0 and n_spaces >  1), 
                            bullet,
                            oldbullet,
                            helpers.list.conversion(bullet))

                        if t.depth == 0 and n_spaces >= 1 then -- first level
                            t.depth = 1             -- “push”
                            t.bullets[1] = bullet
                            t.lastbullet = bullet
                            t.bullets.max = t.bullets.max < t.depth and t.depth or t.bullets.max
                            return true
                        elseif t.depth > 0 and n_spaces > 1 then    -- sublist (of sublist)^0
                            if n_spaces >= utf.len(oldbullet) then
                                t.lastbullets[t.depth] = t.lastbullet
                                t.depth = t.depth + 1
                                t.bullets[t.depth] = bullet
                                t.lastbullet = bullet
                                t.bullets.max = t.bullets.max < t.depth and t.depth or t.bullets.max
                                return true
                            end
                        end
                        return false
                    end)
                    * V"bullet_indent"
                    / rst.startitemize
                    ,

    bullet_indent = V"space"^0 * V"bullet_expr" * V"space"^1,

    bullet_cont  = Cmt(V"bullet_indent", function (s, i, bullet)
                        local t = state
                        local conversion = helpers.list.conversion
                        warn("conti", 
                                t.depth, 
                                bullet == t.bullets[t.depth],
                                bullet, 
                                t.bullets[t.depth],
                                t.lastbullets[t.depth],
                                conversion(t.lastbullet),
                                conversion(bullet)
                                )

                        if utf.len(t.bullets[t.depth]) ~= utf.len(bullet) then
                            return false
                        elseif not conversion(bullet) and t.bullets[t.depth] == bullet then
                            return true
                        elseif conversion(t.lastbullet) == conversion(bullet) then -- same type
                            local autoconv  = conversion(bullet) == "auto"
                            local greater   = helpers.list.greater  (bullet, t.lastbullet)
                            t.lastbullet = bullet
                            return autoconv or successor or greater
                        end
                    end),

    bullet_continue = Ct(C(V"bullet_cont") * V"bullet_itemrest")
                    /rst.bullet_item
                    ,

    bullet_itemrest = C(V"bullet_rest"                               -- first line
                       * ((V"bullet_match" * V"bullet_rest")^0        -- any successive lines
                        * (V"blank_line"
                         * (V"bullet_match" * (V"bullet_rest" - V"bullet_indent"))^1)^0))
                    ,
                         --                                     ^^^^^^^^^^^^^
                         --                                     otherwise matches bullet_first
 
    bullet_rest = (1 - V"eol")^1 * V"eol",  -- rest of one line

    bullet_next  = V"space"^1
                 ,

    bullet_match = Cmt(V"bullet_next", function (s, i, this)
                         local t = state
                         warn("match", 
                                t.depth, 
                                string.len(this) == utf.len(t.bullets[t.depth]),
                                utf.len(t.bullets[t.depth]), string.len(this) )
                         return string.len(this) == utf.len(t.bullets[t.depth])
                     end)
                 ,

    bullet_expr = V"bullet_char"
                + (P"(" * V"number_char" * P")")
                +        (V"number_char" * P")")
                + (V"number_char" * V"dot") * #V"space"
                + (V"number_char" * #V"space")
                ,

    number_char = V"roman_numeral"
                + V"Roman_numeral"
                + P"#"
                + V"digit"^1 
                + R"AZ"
                + R"az"
                ,

--------------------------------------------------------------------------------
-- Transitions
--------------------------------------------------------------------------------

    transition_line = C(V"adornment_char"^4),

    transition = V"transition_line" * V"eol"
               * V"end_block"
               / rst.transition
               ,

--------------------------------------------------------------------------------
-- Sectioning
--------------------------------------------------------------------------------

    section_adorn = V"adornment_char"^1,

    section = ((V"section_text" * V"section_once")
             + (V"section_before" * V"section_text" * V"section_after"))
            / rst.section
            * (V"end_block" + V"blank_line")
            ,

    -- The whitespace handling after the overline is necessary because headings
    -- without overline aren't allowed to be indented.
    section_before = C(Cmt(V"section_adorn", function(s,i, adorn)
                          state.previousadorn = adorn
                          warn ("sec-f", state.valid_adornment:match(adorn), adorn:sub(1,2) .. "...", "", i)
                          if state.valid_adornment:match(adorn) then
                              return true
                          end
                          return false
                      end))
                   * V"whitespace"^0
                   * V"eol"
                   * V"whitespace"^0
                   ,

    section_text = C((1 - V"space" - V"eol") * (1 - V"eol")^1) * V"eol",

    section_after = C(Cmt(V"section_adorn", function(s,i, adorn)
                         local tests = false
                         tests = state.valid_adornment:match(adorn) and true
                         if state.previousadorn then
                             tests = tests and adorn == state.previousadorn
                         end
                         warn ("sec-o", tests, adorn:sub(1,2) .. "…", "", i)
                         state.previousadorn = nil
                         return tests
                     end))
                    * V"whitespace"^0
                    ,

    section_once = C(Cmt(V"section_adorn", function(s,i, adorn)
                         local tests = false
                         tests = state.valid_adornment:match(adorn) and true
                         warn ("sec-o", tests, adorn:sub(1,2) .. "…", "", i)
                         state.previousadorn = nil
                         return tests
                     end))
                    * V"whitespace"^0
                    ,

--------------------------------------------------------------------------------
-- Target Blocks
--------------------------------------------------------------------------------

    tname_normal = C((V"escaped_colon" + 1 - V"colon")^1)
                 * V"colon",

    tname_bareia = C(V"bareia"
                    * (1 - V"eol" - V"bareia")^1
                    * V"bareia")
                 * V"colon",

    target_name = V"double_dot"
                * V"space"
                * V"underscore"
                * (V"tname_bareia" + V"tname_normal"),

    target_firstindent = V"eol" * Cg(V"space"^1, "indent"),

    target_nextindent  = V"eol" * C(V"space"^1),

    target_indentmatch = Cmt(V"target_nextindent" -- I ♡ LPEG!
                           * Cb("indent"), function (s, i, a, b)
                                return a == b 
                            end),

    target_link  = ( V"space"^0 * V"target_firstindent"
                 * Ct(C(1 - V"whitespace" - V"eol")^1 
                    * (V"target_indentmatch"
                     * C(1 - V"whitespace" - V"eol")^1)^0)
                 * V"eol" * #(1 - V"whitespace" - "eol")) / rst.joinindented
                 + C((1 - V"eol")^1) * V"eol" * #(V"double_dot" + V"double_underscore" + V"eol")
                 + (1 - V"end_block")^0 * Cc(""),

    target       = Ct((V"target_name" * (V"space"^0 * V"eol" * V"target_name")^0)
                 * V"space"^0
                 * V"target_link")
                 / rst.target,

    anonymous_prefix = (V"double_dot" * V"space" * V"double_underscore" * V"colon")
                     + (V"double_underscore")
                     ,

    anonymous_target = V"anonymous_prefix"
                     * V"space"^0
                     * Ct(Cc"" * V"target_link")
                     / rst.target
                     ,

    target_block = (V"anonymous_target" + V"target")^1
                 * V"end_block",

--------------------------------------------------------------------------------
-- Paragraphs * Inline Markup
--------------------------------------------------------------------------------

    paragraph = Ct(V"par_first"
                 * V"par_other"^0) / rst.paragraph
              * V"end_block"
              * V"reset_depth"
              ,

    par_first = V"par_setindent" * C((1 - V"literal_block_shorthand" - V"eol")^1) * V"eol",

    par_other = V"par_matchindent" 
              * C((1 - V"literal_block_shorthand" - V"eol")^1)
              * (V"included_literal_block" + V"eol")
              ,

    par_setindent = Cmt(V"space"^0, function (s, i, indent)
                        warn("par-i", #indent, "", "", i)
                        state.previousindent = state.currentindent
                        state.currentindent = indent
                        return true
                    end),

    par_matchindent = Cmt(V"space"^0, function (s, i, indent)
                          warn("par-m", state.currentindent == indent, #indent, #state.currentindent, i)
                          return state.currentindent == indent
                      end),

    link_standalone = C(V"uri")
                    / rst.link_standalone,

    reference = Cs(V"_reference")
              / rst.reference,

    _reference = (1 - V"underscore" - V"spacing" - V"eol" - V"punctuation" - V"groupchars")^1 * V"underscore",

    included_literal_block = V"literal_block_shorthand"
                           * V"literal_block_markerless"
                           * Cmt(Cp(), function (s, i, _)
                                  warn("par-s", "", #state.previousindent, #state.currentindent, i)
                                  state.currentindent = state.previousindent
                                  return true
                              end)
                           ,

    literal_block_shorthand = Cs(((V"colon" * V"space" * V"double_colon")
                              + V"double_colon")
                             * V"whitespace"^0
                             * V"eol" 
                             * V"blank_line") 
                             -- The \unskip is necessary because the lines of a
                             -- paragraph get concatenated from a table with a
                             -- space as separator. And the literal block is
                             -- treated as one such line, hence it would be
                             -- preceded by a space. As the ":" character
                             -- always  follows a non-space this should be a
                             -- safe, albeit unpleasant, hack. If you don't
                             -- agree then file a bug report and I'll look into
                             -- it.
                             / "\\\\unskip:"
                            ,

    literal_block_markerless = Cs(V"literal_block_lines"
                               * (V"blank_line"^1 * V"literal_block_lines")^0)
                             * V"blank_line"
                             / rst.included_literal_block
                             ,

    -- This is needed because lpeg.Cmt() patterns are evaluated even
    -- if they are part of a larger pattern that doesn’t match. The
    -- result is that they confuse the nesting.
    -- Resetting the current nesting depth at every end of block
    -- should be safe because this pattern always matches last.
    reset_depth = Cmt(Cc("nothing") / "", function (s,i, something)
                        state.depth = 0
                        return true
                    end)
                ,

--------------------------------------------------------------------------------
-- Comments
--------------------------------------------------------------------------------

    comment_block = V"comment"^1
                  * V"end_block"
                  ,

    comment = V"double_dot" / ""
            * (V"block_comment" + V"line_comment")
            ,

    block_comment = V"eol"
                  * Cs(V"indented_lines")
                  / rst.block_comment,

    line_comment = V"whitespace"^1
                 * Cs((1 - V"eol")^0 * V"eol")
                 / rst.line_comment
                 ,

--------------------------------------------------------------------------------
-- Generic indented block
--------------------------------------------------------------------------------

    indented_lines = V"indented_first"
                   * (V"indented_other"^0
                    * (V"blank_line" * V"indented_other"^1)^0)
                   ,

    indented_first = Cmt(V"space"^1, function (s, i, indent)
                        warn("idt-f", indent, i)
                        state.currentindent = indent
                        return true
                    end)
                   * (1 - V"eol")^1
                   * V"eol",

    indented_other = Cmt(V"space"^1, function (s, i, indent)
                        warn("idt-m", #indent, #state.currentindent, #indent == #state.currentindent, i)
                        return indent == state.currentindent
                    end)
                   * (1 - V"eol")^1
                   * V"eol",

--------------------------------------------------------------------------------
-- Urls
--------------------------------------------------------------------------------
    uri = V"url_protocol" * V"url_domain" * (V"slash" * V"url_path")^0,

    url_protocol = (P"http" + P"ftp" + P"shttp" + P"sftp") * P"://",
    url_domain_char = 1 - V"dot" - V"spacing" - V"eol" - V"punctuation",
    url_domain = V"url_domain_char"^1 * (V"dot" * V"url_domain_char"^1)^0,
    url_path_char = R("az", "AZ", "09") + S"-_.!~*'()",
    url_path = V"slash" * (V"url_path_char"^1 * V"slash"^-1)^1,

--------------------------------------------------------------------------------
-- Terminal Symbols and Low-Level Elements
--------------------------------------------------------------------------------

    word = (1 - V"punctuation" - V"end_block" - V"spacing" - V"eol")^1, -- TODO : no punctuation (later)

    asterisk = P"*",
    double_asterisk = V"asterisk" * V"asterisk",

    bareia = P"`",
    double_bareia = V"bareia" * V"bareia",
    escaped_bareia = (Cs(V"backslash") / "" * V"bareia") + 1,

    slash = P"/",
    doubleslash = V"slash" * V"slash",

    backslash = P"\\",
    bar = P"|",

    groupchars = S"()[]{}",

    --- Punctuation
    -- Some of the following are used for markup as well as for punctuation.

    comma = P",",
    colon = P":",
    double_colon = V"colon" * V"colon",
    escaped_colon = V"backslash" * V"colon",
    dot = P".",
    period = V"dot",
    double_dot = V"dot" * V"dot",
    interpunct = P"·",
    underscore = P"_",
    double_underscore = V"underscore" * V"underscore",
    dash = P"-",
    double_dash = V"dash" * V"dash",
    triple_dash = V"double_dash" * V"dash",
    emdash = P"—",
    attrib_dash = V"triple_dash" + V"double_dash" + V"emdash", -- begins quote attribution blocks
    dashes = V"dash" + P"‒" + P"–" + V"emdash" + P"―",
    hyphen = P"‐",
    semicolon = P";",
    questionmark = P"?",
    exclamationmark = P"!",
    inverted_exclamationmark = P"¡",
    inverted_questionmark = P"¿",
    interrobang = P"‽",

    apostrophe = P"’" + P"'",
    --brackets   = P"[ ], (",, { }, ⟨ ⟩ )
    lsquare = P"[",
    rsquare = P"]",
    ellipsis   = P"…" + P"...",
    guillemets = P"«" + P"»",
    quotationmarks= P"‘" + P"’" + P"“" + P"”",
    solidus= P"⁄",


    punctuation = V"apostrophe"
                + V"colon" 
                + V"comma" 
                + V"dashes"
                + V"dot" 
                + V"ellipsis"
                + V"exclamationmark"
                + V"guillemets"
                + V"hyphen"
                + V"interpunct"
                + V"interrobang"
                + V"questionmark" 
                + V"quotationmarks"
                + V"semicolon" 
                + V"slash"
                + V"solidus"
                + V"underscore"
                ,

    -- These are treated separately as the might begin a paragraph (sigh!).
    inverted_punctuation = V"inverted_exclamationmark"
                         + V"inverted_questionmark",

    -- End punctuation

    letter = R"az" + R"AZ",

    equals = P"=",

    space = P" ",
    spaces = V"space"^1,
    whitespace = (P" " + Cs(P"\t") / "        " + Cs(S"\v") / " "),
    spacing = V"whitespace"^1,
    blank_line = V"space"^0 * V"eol",

    rest_of_line = (1 - V"eol")^1,

    eol = P"\n",
    eof = V"eol"^0 * -P(1),

    end_block = V"blank_line"^1
              + V"eof"
              + (V"whitespace"^0 * V"eol"
               * (V"whitespace"^0 * V"eol")^0 * V"eof")
              ,

    -- diverse markup character sets
    delimiters = P"‐" + P"‑" + P"‒" + P"–" + V"emdash" + V"space",   -- inline markup
    adornment_char = S[[!"#$%&'()*+,-./:;<=>?@[]^_`{|}~]] + P[[\\]], -- headings
    bullet_char = S"*+-" + P"•" + P"‣" + P"⁃",                       -- bullet lists
    argument_char = V"double_dash" * V"dash" * V"slash",             -- option lists

    digit = R"09",
    roman_numeral = S"ivxlcdm"^1,
    Roman_numeral = S"IVXLCDM"^1,

    inline_delimiter = P"**" + P"``" + S"*`",
    angle_left       = P"<",
    angle_right      = P">",
    enclosed_open    = S[['"([{<]],
    enclosed_close   = S[['")]}>]],

    gartenzaun = P"#",

    table_intersection = P"+",
    table_hline = V"dash",
    table_vline = V"bar",
    table_header_hline = P"=",
}

local function load_file (name)
    f = assert(io.open(name, "r"), "Not a file!")
    if not f then return 1 end
    local tmp = f:read("*all")
    f:close()
    return tmp
end

local function save_file (name, data)
    f = assert(io.open(name, "w"), "Could not open file "..name.." for writing! Check its permissions")
    if not f then return 1 end
    f:write(data)
    f:close()
    return 0
end

local function get_setups (inline)
    local optional_setups = optional_setups -- might expect lots of calls
    local setups = [[
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
%{                           Setups                            }%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% General                                                       %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\setupcolors[state=start]
\setupinteraction[state=start,focus=standard,color=darkgreen,contrastcolor=darkgreen]
\setupbodyfontenvironment [default]  [em=italic]
\sethyphenatedurlnormal{:=?&}
\sethyphenatedurlbefore{?&}
\sethyphenatedurlafter {:=/-}

\doifundefined{startparagraph}{% -->mkii
  \enableregime[utf]
  \let\startparagraph\relax
  \let\stopparagraph\endgraf
}

]]

    for item, _ in next, state.addme do
        local f = optional_setups[item]
        setups = f and setups .. f() or setups
    end
    setups = setups .. [[


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
%{                           Main                              }%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

]]
    if not inline then
        setups = setups .. "\n\\starttext"
    end
    return setups
end

local function main()
    local testdata = load_file(arg[1])
    if testdata == 1 then return 1 end

    local processeddata = parser:match(testdata)
    local setups = get_setups()

    processeddata = setups .. processeddata .. [[

\stoptext

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
%{                        End of Document                      }%
%~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% vim:ft=context:tw=65:shiftwidth=2:tabstop=2:set expandtab
]]

    if processeddata then
        save_file(arg[2], processeddata)
    else
        return 1
    end
    return 0
end

do
    local Cs, P = lpeg.Cs, lpeg.P
    local percent = P"\%"
    local eol     = P"\n"
    local comment = percent * (1 - eol)^0 * eol / "\n"
    strip_comments = Cs((comment + 1)^0)
end

function thirddata.rst.do_rst_file(fname)
    local rst_parser = parser
    local raw_data   = load_file(fname)
    local processed  = rst_parser:match(raw_data)
    local setups     = get_setups(false)
    local tmp_file   = tex.jobname .. "–rst_temporary.tex.tmp"

    if processed then
        processed = strip_comments:match(setups..processed.."\n\\stoptext\n")
        save_file (tmp_file,processed)
        context.input("./"..tmp_file)
    end
end

function thirddata.rst.do_rst_snippet(txt)
    local processed  = parser:match(txt)
    local setups     = get_setups(true)
    local tmp_file   = tex.jobname .. "–rst_temporary.tex.tmp"

    if processed then
        warn("·cs·",txt)
        processed = strip_comments:match(setups..processed)
        save_file (tmp_file,processed)
        context.input("./"..tmp_file)
    else
        warn("·cs·",txt)
        context.par()
        context("{\\bf context-rst could not process snippet.\\par}")
        context.type(txt)
        context.par()
    end
end


if not context then
    return main()
end