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
open! Compat
type 'a with_location = 'a Loc.with_location
type input = {
tokens : Token.t Loc.with_location Stream.t;
warnings : Warning.t list ref;
}
let add_warning input warning = input.warnings := warning :: !(input.warnings)
let junk input = Stream.junk input.tokens
let peek input =
match Stream.peek input.tokens with
| Some token -> token
| None -> assert false
module Table = struct
module Light_syntax = struct
let valid_align = function
| [ { Loc.value = `Word w; _ } ] -> (
match String.length w with
| 0 -> `Valid None
| 1 -> (
match w with
| "-" -> `Valid None
| ":" -> `Valid (Some `Center)
| _ -> `Invalid)
| len ->
if String.for_all (Char.equal '-') (String.sub w 1 (len - 2)) then
match (String.get w 0, String.get w (len - 1)) with
| ':', ':' -> `Valid (Some `Center)
| ':', '-' -> `Valid (Some `Left)
| '-', ':' -> `Valid (Some `Right)
| '-', '-' -> `Valid None
| _ -> `Invalid
else `Invalid)
| _ -> `Invalid
let valid_align_row lx =
let rec loop acc = function
| [] -> Some (List.rev acc)
| x :: q -> (
match valid_align x with
| `Invalid -> None
| `Valid alignment -> loop (alignment :: acc) q)
in
loop [] lx
let create ~grid ~align : Ast.table =
let cell_to_block (x, k) =
let whole_loc = Loc.span (List.map (fun x -> x.Loc.location) x) in
match x with
| [] -> ([], k)
| _ -> ([ Loc.at whole_loc (`Paragraph x) ], k)
in
let row_to_block = List.map cell_to_block in
let grid_to_block = List.map row_to_block in
((grid_to_block grid, align), `Light)
let with_kind kind : 'a with_location list list -> 'a Ast.row =
List.map (fun c -> (c, kind))
let from_raw_data grid : Ast.table =
match grid with
| [] -> create ~grid:[] ~align:None
| row1 :: rows2_N -> (
match valid_align_row row1 with
| Some _ as align ->
create ~grid:(List.map (with_kind `Data) rows2_N) ~align
| None -> (
match rows2_N with
| [] -> create ~grid:[ with_kind `Data row1 ] ~align:None
| row2 :: rows3_N -> (
match valid_align_row row2 with
| Some _ as align ->
let = with_kind `Header row1 in
let data = List.map (with_kind `Data) rows3_N in
create ~grid:(header :: data) ~align
| None ->
create ~grid:(List.map (with_kind `Data) grid) ~align:None
)))
end
module Heavy_syntax = struct
let create ~grid : Ast.table = ((grid, None), `Heavy)
let from_grid grid : Ast.table = create ~grid
end
end
module Reader = struct
let until_rbrace_or_eof input acc =
let rec consume () =
let next_token = peek input in
match next_token.value with
| `Right_brace ->
junk input;
`End (acc, next_token.location)
| `End ->
Parse_error.end_not_allowed next_token.location ~in_what:"table"
|> add_warning input;
junk input;
`End (acc, next_token.location)
| `Space _ | `Single_newline _ | `Blank_line _ ->
junk input;
consume ()
| _ -> `Token next_token
in
consume ()
module Infix = struct
let ( >>> ) consume if_token =
match consume with
| `End (ret, loc) -> (ret, loc)
| `Token t -> if_token t
end
end
open Reader.Infix
let npeek n input = Stream.npeek n input.tokens
type style = [ `Bold | `Italic | `Emphasis | `Superscript | `Subscript ]
type token_that_always_begins_an_inline_element =
[ `Word of string
| `Code_span of string
| `Raw_markup of string option * string
| `Begin_style of style
| `Simple_reference of string
| `Begin_reference_with_replacement_text of string
| `Simple_link of string
| `Begin_link_with_replacement_text of string
| `Math_span of string ]
let _check_subset : token_that_always_begins_an_inline_element -> Token.t =
fun t -> (t :> Token.t)
let rec inline_element :
input -> Loc.span -> _ -> Ast.inline_element with_location =
fun input location next_token ->
match next_token with
| `Space _ as token ->
junk input;
Loc.at location token
| `Word _ as token ->
junk input;
Loc.at location token
| `Minus ->
junk input;
Loc.at location (`Word "-")
| `Plus ->
junk input;
Loc.at location (`Word "+")
| `Bar ->
junk input;
Loc.at location (`Word "|")
| (`Code_span _ | `Math_span _ | `Raw_markup _) as token ->
junk input;
Loc.at location token
| `Begin_style s as parent_markup ->
junk input;
let requires_leading_whitespace =
match s with
| `Bold | `Italic | `Emphasis -> true
| `Superscript | `Subscript -> false
in
let content, brace_location =
delimited_inline_element_list ~parent_markup
~parent_markup_location:location ~requires_leading_whitespace input
in
let location = Loc.span [ location; brace_location ] in
if content = [] then
Parse_error.should_not_be_empty
~what:(Token.describe parent_markup)
location
|> add_warning input;
Loc.at location (`Styled (s, content))
| `Simple_reference r ->
junk input;
let r_location = Loc.nudge_start (String.length "{!") location in
let r = Loc.at r_location r in
Loc.at location (`Reference (`Simple, r, []))
| `Begin_reference_with_replacement_text r as parent_markup ->
junk input;
let r_location = Loc.nudge_start (String.length "{{!") location in
let r = Loc.at r_location r in
let content, brace_location =
delimited_inline_element_list ~parent_markup
~parent_markup_location:location ~requires_leading_whitespace:false
input
in
let location = Loc.span [ location; brace_location ] in
if content = [] then
Parse_error.should_not_be_empty
~what:(Token.describe parent_markup)
location
|> add_warning input;
Loc.at location (`Reference (`With_text, r, content))
| `Simple_link u ->
junk input;
let u = String.trim u in
if u = "" then
Parse_error.should_not_be_empty
~what:(Token.describe next_token)
location
|> add_warning input;
Loc.at location (`Link (u, []))
| `Begin_link_with_replacement_text u as parent_markup ->
junk input;
let u = String.trim u in
if u = "" then
Parse_error.should_not_be_empty
~what:(Token.describe parent_markup)
location
|> add_warning input;
let content, brace_location =
delimited_inline_element_list ~parent_markup
~parent_markup_location:location ~requires_leading_whitespace:false
input
in
`Link (u, content) |> Loc.at (Loc.span [ location; brace_location ])
and delimited_inline_element_list :
parent_markup:[< Token.t ] ->
parent_markup_location:Loc.span ->
requires_leading_whitespace:bool ->
input ->
Ast.inline_element with_location list * Loc.span =
fun ~parent_markup ~parent_markup_location ~requires_leading_whitespace input ->
let rec consume_elements :
at_start_of_line:bool ->
Ast.inline_element with_location list ->
Ast.inline_element with_location list * Loc.span =
fun ~at_start_of_line acc ->
let next_token = peek input in
match next_token.value with
| `Right_brace ->
junk input;
(List.rev acc, next_token.location)
| (`Space _ | #token_that_always_begins_an_inline_element) as token ->
let acc = inline_element input next_token.location token :: acc in
consume_elements ~at_start_of_line:false acc
| `Single_newline ws ->
junk input;
let element = Loc.same next_token (`Space ws) in
consume_elements ~at_start_of_line:true (element :: acc)
| `Blank_line ws as blank ->
Parse_error.not_allowed ~what:(Token.describe blank)
~in_what:(Token.describe parent_markup)
next_token.location
|> add_warning input;
junk input;
let element = Loc.same next_token (`Space ws) in
consume_elements ~at_start_of_line:true (element :: acc)
| `Bar as token ->
let acc = inline_element input next_token.location token :: acc in
consume_elements ~at_start_of_line:false acc
| (`Minus | `Plus) as bullet ->
(if at_start_of_line then
let suggestion =
Printf.sprintf "move %s so it isn't the first thing on the line."
(Token.print bullet)
in
Parse_error.not_allowed ~what:(Token.describe bullet)
~in_what:(Token.describe parent_markup)
~suggestion next_token.location
|> add_warning input);
let acc = inline_element input next_token.location bullet :: acc in
consume_elements ~at_start_of_line:false acc
| other_token ->
Parse_error.not_allowed
~what:(Token.describe other_token)
~in_what:(Token.describe parent_markup)
next_token.location
|> add_warning input;
let last_location =
match acc with
| last_token :: _ -> last_token.location
| [] -> parent_markup_location
in
(List.rev acc, last_location)
in
let first_token = peek input in
match first_token.value with
| `Space _ ->
junk input;
consume_elements ~at_start_of_line:false []
| `Single_newline _ ->
junk input;
consume_elements ~at_start_of_line:true []
| `Blank_line _ as blank ->
Parse_error.not_allowed ~what:(Token.describe blank)
~in_what:(Token.describe parent_markup)
first_token.location
|> add_warning input;
junk input;
consume_elements ~at_start_of_line:true []
| `Right_brace ->
junk input;
([], first_token.location)
| _ ->
if requires_leading_whitespace then
Parse_error.should_be_followed_by_whitespace
~what:(Token.print parent_markup)
parent_markup_location
|> add_warning input;
consume_elements ~at_start_of_line:false []
let paragraph : input -> Ast.nestable_block_element with_location =
fun input ->
let rec paragraph_line :
Ast.inline_element with_location list ->
Ast.inline_element with_location list =
fun acc ->
let next_token = peek input in
match next_token.value with
| ( `Space _ | `Minus | `Plus | `Bar
| #token_that_always_begins_an_inline_element ) as token ->
let element = inline_element input next_token.location token in
paragraph_line (element :: acc)
| _ -> acc
in
let rec additional_lines :
Ast.inline_element with_location list ->
Ast.inline_element with_location list =
fun acc ->
match npeek 2 input with
| { value = `Single_newline ws; location }
:: { value = #token_that_always_begins_an_inline_element | `Bar; _ }
:: _ ->
junk input;
let acc = Loc.at location (`Space ws) :: acc in
let acc = paragraph_line acc in
additional_lines acc
| _ -> List.rev acc
in
let elements = paragraph_line [] |> additional_lines in
`Paragraph elements |> Loc.at (Loc.span (List.map Loc.location elements))
type where_in_line =
[ `At_start_of_line
| `After_tag
| `After_shorthand_bullet
| `After_explicit_list_bullet
| `After_table_cell
| `After_text ]
type stops_at_delimiters = [ `End | `Right_brace ]
type code_stop = [ `End | `Right_code_delimiter ]
type stopped_implicitly =
[ `End
| `Blank_line of string
| `Right_brace
| `Minus
| `Plus
| Token.section_heading
| Token.tag ]
let _check_subset : stops_at_delimiters -> Token.t = fun t -> (t :> Token.t)
let _check_subset : stopped_implicitly -> Token.t = fun t -> (t :> Token.t)
type ('block, 'stops_at_which_tokens) context =
| Top_level : (Ast.block_element, stops_at_delimiters) context
| In_shorthand_list : (Ast.nestable_block_element, stopped_implicitly) context
| In_explicit_list : (Ast.nestable_block_element, stops_at_delimiters) context
| In_table_cell : (Ast.nestable_block_element, stops_at_delimiters) context
| In_code_results : (Ast.nestable_block_element, code_stop) context
| In_tag : (Ast.nestable_block_element, Token.t) context
let accepted_in_all_contexts :
type block stops_at_which_tokens.
(block, stops_at_which_tokens) context ->
Ast.nestable_block_element ->
block =
fun context block ->
match context with
| Top_level -> (block :> Ast.block_element)
| In_shorthand_list -> block
| In_explicit_list -> block
| In_table_cell -> block
| In_code_results -> block
| In_tag -> block
let tag_to_words = function
| `Author s -> [ `Word "@author"; `Space " "; `Word s ]
| `Before s -> [ `Word "@before"; `Space " "; `Word s ]
| `Canonical s -> [ `Word "@canonical"; `Space " "; `Word s ]
| `Deprecated -> [ `Word "@deprecated" ]
| `Inline -> [ `Word "@inline" ]
| `Open -> [ `Word "@open" ]
| `Closed -> [ `Word "@closed" ]
| `Hidden -> [ `Word "@hidden" ]
| `Param s -> [ `Word "@param"; `Space " "; `Word s ]
| `Raise s -> [ `Word "@raise"; `Space " "; `Word s ]
| `Return -> [ `Word "@return" ]
| `See (`Document, s) -> [ `Word "@see"; `Space " "; `Word ("\"" ^ s ^ "\"") ]
| `See (`File, s) -> [ `Word "@see"; `Space " "; `Word ("'" ^ s ^ "'") ]
| `See (`Url, s) -> [ `Word "@see"; `Space " "; `Word ("<" ^ s ^ ">") ]
| `Since s -> [ `Word "@since"; `Space " "; `Word s ]
| `Version s -> [ `Word "@version"; `Space " "; `Word s ]
let rec block_element_list :
type block stops_at_which_tokens.
(block, stops_at_which_tokens) context ->
parent_markup:[< Token.t | `Comment ] ->
input ->
block with_location list
* stops_at_which_tokens with_location
* where_in_line =
fun context ~parent_markup input ->
let rec consume_block_elements :
parsed_a_tag:bool ->
where_in_line ->
block with_location list ->
block with_location list
* stops_at_which_tokens with_location
* where_in_line =
fun ~parsed_a_tag where_in_line acc ->
let describe token =
match token with
| #token_that_always_begins_an_inline_element -> "paragraph"
| _ -> Token.describe token
in
let warn_if_after_text { Loc.location; value = token } =
if where_in_line = `After_text then
Parse_error.should_begin_on_its_own_line ~what:(describe token) location
|> add_warning input
in
let warn_if_after_tags { Loc.location; value = token } =
if parsed_a_tag then
let suggestion =
Printf.sprintf "move %s before any tags." (Token.describe token)
in
Parse_error.not_allowed ~what:(describe token)
~in_what:"the tags section" ~suggestion location
|> add_warning input
in
let warn_because_not_at_top_level { Loc.location; value = token } =
let suggestion =
Printf.sprintf "move %s outside of any other markup."
(Token.print token)
in
Parse_error.not_allowed ~what:(Token.describe token)
~in_what:(Token.describe parent_markup)
~suggestion location
|> add_warning input
in
match peek input with
| { value = `End; _ } as next_token -> (
match context with
| Top_level -> (List.rev acc, next_token, where_in_line)
| In_shorthand_list -> (List.rev acc, next_token, where_in_line)
| In_explicit_list -> (List.rev acc, next_token, where_in_line)
| In_tag -> (List.rev acc, next_token, where_in_line)
| In_table_cell -> (List.rev acc, next_token, where_in_line)
| In_code_results -> (List.rev acc, next_token, where_in_line))
| { value = `Right_brace; _ } as next_token -> (
match context with
| Top_level -> (List.rev acc, next_token, where_in_line)
| In_shorthand_list -> (List.rev acc, next_token, where_in_line)
| In_explicit_list -> (List.rev acc, next_token, where_in_line)
| In_table_cell -> (List.rev acc, next_token, where_in_line)
| In_tag -> (List.rev acc, next_token, where_in_line)
| In_code_results ->
junk input;
consume_block_elements ~parsed_a_tag where_in_line acc)
| { value = `Right_code_delimiter; _ } as next_token -> (
match context with
| In_code_results -> (List.rev acc, next_token, where_in_line)
| _ ->
junk input;
consume_block_elements ~parsed_a_tag where_in_line acc)
| { value = `Space _; _ } ->
junk input;
consume_block_elements ~parsed_a_tag where_in_line acc
| { value = `Single_newline _; _ } ->
junk input;
consume_block_elements ~parsed_a_tag `At_start_of_line acc
| { value = `Blank_line _; _ } as next_token -> (
match context with
| In_shorthand_list -> (List.rev acc, next_token, where_in_line)
| _ ->
junk input;
consume_block_elements ~parsed_a_tag `At_start_of_line acc)
| { value = `Begin_list_item _ as token; location } ->
let suggestion =
Printf.sprintf "move %s into %s, or use %s." (Token.print token)
(Token.describe (`Begin_list `Unordered))
(Token.describe `Minus)
in
Parse_error.not_allowed ~what:(Token.describe token)
~in_what:(Token.describe parent_markup)
~suggestion location
|> add_warning input;
junk input;
consume_block_elements ~parsed_a_tag where_in_line acc
| { value = `Begin_table_row as token; location } ->
let suggestion =
Printf.sprintf "move %s into %s." (Token.print token)
(Token.describe `Begin_table_heavy)
in
Parse_error.not_allowed ~what:(Token.describe token)
~in_what:(Token.describe parent_markup)
~suggestion location
|> add_warning input;
junk input;
consume_block_elements ~parsed_a_tag where_in_line acc
| { value = `Begin_table_cell _ as token; location } ->
let suggestion =
Printf.sprintf "move %s into %s." (Token.print token)
(Token.describe `Begin_table_row)
in
Parse_error.not_allowed ~what:(Token.describe token)
~in_what:(Token.describe parent_markup)
~suggestion location
|> add_warning input;
junk input;
consume_block_elements ~parsed_a_tag where_in_line acc
| { value = `Tag tag as token; location } as next_token -> (
let recover_when_not_at_top_level context =
warn_because_not_at_top_level next_token;
junk input;
let words = List.map (Loc.at location) (tag_to_words tag) in
let paragraph =
`Paragraph words
|> accepted_in_all_contexts context
|> Loc.at location
in
consume_block_elements ~parsed_a_tag `At_start_of_line
(paragraph :: acc)
in
match context with
| In_explicit_list -> recover_when_not_at_top_level context
| In_shorthand_list ->
if where_in_line = `At_start_of_line then
(List.rev acc, next_token, where_in_line)
else recover_when_not_at_top_level context
| In_table_cell -> recover_when_not_at_top_level context
| In_tag ->
if where_in_line = `At_start_of_line then
(List.rev acc, next_token, where_in_line)
else recover_when_not_at_top_level context
| In_code_results -> recover_when_not_at_top_level context
| Top_level -> (
if where_in_line <> `At_start_of_line then
Parse_error.should_begin_on_its_own_line
~what:(Token.describe token) location
|> add_warning input;
junk input;
match tag with
| (`Author s | `Since s | `Version s | `Canonical s) as tag ->
let s = String.trim s in
if s = "" then
Parse_error.should_not_be_empty ~what:(Token.describe token)
location
|> add_warning input;
let tag =
match tag with
| `Author _ -> `Author s
| `Since _ -> `Since s
| `Version _ -> `Version s
| `Canonical _ ->
let r_location =
Loc.nudge_start (String.length "@canonical ") location
in
`Canonical (Loc.at r_location s)
in
let tag = Loc.at location (`Tag tag) in
consume_block_elements ~parsed_a_tag:true `After_text
(tag :: acc)
| (`Deprecated | `Return) as tag ->
let content, _stream_head, where_in_line =
block_element_list In_tag ~parent_markup:token input
in
let tag =
match tag with
| `Deprecated -> `Deprecated content
| `Return -> `Return content
in
let location =
location :: List.map Loc.location content |> Loc.span
in
let tag = Loc.at location (`Tag tag) in
consume_block_elements ~parsed_a_tag:true where_in_line
(tag :: acc)
| (`Param _ | `Raise _ | `Before _) as tag ->
let content, _stream_head, where_in_line =
block_element_list In_tag ~parent_markup:token input
in
let tag =
match tag with
| `Param s -> `Param (s, content)
| `Raise s -> `Raise (s, content)
| `Before s -> `Before (s, content)
in
let location =
location :: List.map Loc.location content |> Loc.span
in
let tag = Loc.at location (`Tag tag) in
consume_block_elements ~parsed_a_tag:true where_in_line
(tag :: acc)
| `See (kind, target) ->
let content, _next_token, where_in_line =
block_element_list In_tag ~parent_markup:token input
in
let location =
location :: List.map Loc.location content |> Loc.span
in
let tag = `Tag (`See (kind, target, content)) in
let tag = Loc.at location tag in
consume_block_elements ~parsed_a_tag:true where_in_line
(tag :: acc)
| (`Inline | `Open | `Closed | `Hidden) as tag ->
let tag = Loc.at location (`Tag tag) in
consume_block_elements ~parsed_a_tag:true `After_text
(tag :: acc)))
| ( { value = #token_that_always_begins_an_inline_element; _ }
| { value = `Bar; _ } ) as next_token ->
warn_if_after_tags next_token;
warn_if_after_text next_token;
let block = paragraph input in
let block = Loc.map (accepted_in_all_contexts context) block in
let acc = block :: acc in
consume_block_elements ~parsed_a_tag `After_text acc
| { value = `Verbatim s as token; location } as next_token ->
warn_if_after_tags next_token;
warn_if_after_text next_token;
if s = "" then
Parse_error.should_not_be_empty ~what:(Token.describe token) location
|> add_warning input;
junk input;
let block = accepted_in_all_contexts context token in
let block = Loc.at location block in
let acc = block :: acc in
consume_block_elements ~parsed_a_tag `After_text acc
| { value = `Math_block s as token; location } as next_token ->
warn_if_after_tags next_token;
warn_if_after_text next_token;
if s = "" then
Parse_error.should_not_be_empty ~what:(Token.describe token) location
|> add_warning input;
junk input;
let block = accepted_in_all_contexts context token in
let block = Loc.at location block in
let acc = block :: acc in
consume_block_elements ~parsed_a_tag `After_text acc
| {
value =
`Code_block (meta, delim, { value = s; location = v_loc }, has_outputs)
as token;
location;
} as next_token ->
warn_if_after_tags next_token;
warn_if_after_text next_token;
junk input;
let delimiter = if delim = "" then None else Some delim in
let output, location =
if not has_outputs then (None, location)
else
let content, next_token, _where_in_line =
block_element_list In_code_results ~parent_markup:token input
in
junk input;
let locations =
location :: List.map (fun content -> content.Loc.location) content
in
let location = Loc.span locations in
let location = { location with end_ = next_token.location.end_ } in
(Some content, location)
in
if s = "" then
Parse_error.should_not_be_empty ~what:(Token.describe token) location
|> add_warning input;
let meta =
match meta with
| None -> None
| Some (language, tags) -> Some { Ast.language; tags }
in
let block =
accepted_in_all_contexts context
(`Code_block
{
Ast.meta;
delimiter;
content = { value = s; location = v_loc };
output;
})
in
let block = Loc.at location block in
let acc = block :: acc in
consume_block_elements ~parsed_a_tag `After_text acc
| { value = `Modules s as token; location } as next_token ->
warn_if_after_tags next_token;
warn_if_after_text next_token;
junk input;
let split_string delimiters s =
let rec scan_delimiters acc index =
if index >= String.length s then List.rev acc
else if String.contains delimiters s.[index] then
scan_delimiters acc (index + 1)
else scan_word acc index (index + 1)
and scan_word acc start_index index =
if index >= String.length s then
let word = String.sub s start_index (index - start_index) in
List.rev (word :: acc)
else if String.contains delimiters s.[index] then
let word = String.sub s start_index (index - start_index) in
scan_delimiters (word :: acc) (index + 1)
else scan_word acc start_index (index + 1)
in
scan_delimiters [] 0
in
let modules =
split_string " \t\r\n" s |> List.map (fun r -> Loc.at location r)
in
if modules = [] then
Parse_error.should_not_be_empty ~what:(Token.describe token) location
|> add_warning input;
let block = accepted_in_all_contexts context (`Modules modules) in
let block = Loc.at location block in
let acc = block :: acc in
consume_block_elements ~parsed_a_tag `After_text acc
| { value = `Begin_list kind as token; location } as next_token ->
warn_if_after_tags next_token;
warn_if_after_text next_token;
junk input;
let items, brace_location =
explicit_list_items ~parent_markup:token input
in
if items = [] then
Parse_error.should_not_be_empty ~what:(Token.describe token) location
|> add_warning input;
let location = Loc.span [ location; brace_location ] in
let block = `List (kind, `Heavy, items) in
let block = accepted_in_all_contexts context block in
let block = Loc.at location block in
let acc = block :: acc in
consume_block_elements ~parsed_a_tag `After_text acc
| { value = (`Begin_table_light | `Begin_table_heavy) as token; location }
as next_token ->
warn_if_after_tags next_token;
warn_if_after_text next_token;
junk input;
let block, brace_location =
let parent_markup = token in
let parent_markup_location = location in
match token with
| `Begin_table_light ->
light_table input ~parent_markup ~parent_markup_location
| `Begin_table_heavy ->
heavy_table input ~parent_markup ~parent_markup_location
in
let location = Loc.span [ location; brace_location ] in
let block = accepted_in_all_contexts context (`Table block) in
let block = Loc.at location block in
let acc = block :: acc in
consume_block_elements ~parsed_a_tag `After_text acc
| { value = (`Minus | `Plus) as token; location } as next_token -> (
(match where_in_line with
| `After_text | `After_shorthand_bullet ->
Parse_error.should_begin_on_its_own_line
~what:(Token.describe token) location
|> add_warning input
| _ -> ());
warn_if_after_tags next_token;
match context with
| In_shorthand_list -> (List.rev acc, next_token, where_in_line)
| _ ->
let items, where_in_line =
shorthand_list_items next_token where_in_line input
in
let kind =
match token with `Minus -> `Unordered | `Plus -> `Ordered
in
let location =
location :: List.map Loc.location (List.flatten items) |> Loc.span
in
let block = `List (kind, `Light, items) in
let block = accepted_in_all_contexts context block in
let block = Loc.at location block in
let acc = block :: acc in
consume_block_elements ~parsed_a_tag where_in_line acc)
| { value = `Begin_section_heading (level, label) as token; location } as
next_token -> (
warn_if_after_tags next_token;
let recover_when_not_at_top_level context =
warn_because_not_at_top_level next_token;
junk input;
let content, brace_location =
delimited_inline_element_list ~parent_markup:token
~parent_markup_location:location ~requires_leading_whitespace:true
input
in
let location = Loc.span [ location; brace_location ] in
let paragraph =
`Paragraph content
|> accepted_in_all_contexts context
|> Loc.at location
in
consume_block_elements ~parsed_a_tag `At_start_of_line
(paragraph :: acc)
in
match context with
| In_shorthand_list ->
if where_in_line = `At_start_of_line then
(List.rev acc, next_token, where_in_line)
else recover_when_not_at_top_level context
| In_explicit_list -> recover_when_not_at_top_level context
| In_table_cell -> recover_when_not_at_top_level context
| In_tag -> recover_when_not_at_top_level context
| In_code_results -> recover_when_not_at_top_level context
| Top_level ->
if where_in_line <> `At_start_of_line then
Parse_error.should_begin_on_its_own_line
~what:(Token.describe token) location
|> add_warning input;
let label =
match label with
| Some "" ->
Parse_error.should_not_be_empty ~what:"heading label" location
|> add_warning input;
None
| _ -> label
in
junk input;
let content, brace_location =
delimited_inline_element_list ~parent_markup:token
~parent_markup_location:location
~requires_leading_whitespace:true input
in
if content = [] then
Parse_error.should_not_be_empty ~what:(Token.describe token)
location
|> add_warning input;
let location = Loc.span [ location; brace_location ] in
let heading = `Heading (level, label, content) in
let heading = Loc.at location heading in
let acc = heading :: acc in
consume_block_elements ~parsed_a_tag `After_text acc)
| { value = `Begin_paragraph_style _ as token; location } ->
junk input;
let content, brace_location =
delimited_inline_element_list ~parent_markup:token
~parent_markup_location:location ~requires_leading_whitespace:true
input
in
let location = Loc.span [ location; brace_location ] in
Parse_error.markup_should_not_be_used ~what:(Token.describe token)
location
|> add_warning input;
let paragraph =
`Paragraph content
|> accepted_in_all_contexts context
|> Loc.at location
in
consume_block_elements ~parsed_a_tag `At_start_of_line (paragraph :: acc)
in
let where_in_line =
match context with
| Top_level -> `At_start_of_line
| In_shorthand_list -> `After_shorthand_bullet
| In_explicit_list -> `After_explicit_list_bullet
| In_table_cell -> `After_table_cell
| In_code_results -> `After_tag
| In_tag -> `After_tag
in
consume_block_elements ~parsed_a_tag:false where_in_line []
and shorthand_list_items :
[ `Minus | `Plus ] with_location ->
where_in_line ->
input ->
Ast.nestable_block_element with_location list list * where_in_line =
fun first_token where_in_line input ->
let bullet_token = first_token.value in
let rec consume_list_items :
[> ] with_location ->
where_in_line ->
Ast.nestable_block_element with_location list list ->
Ast.nestable_block_element with_location list list * where_in_line =
fun next_token where_in_line acc ->
match next_token.value with
| `End | `Right_brace | `Blank_line _ | `Tag _ | `Begin_section_heading _ ->
(List.rev acc, where_in_line)
| (`Minus | `Plus) as bullet ->
if bullet = bullet_token then (
junk input;
let content, stream_head, where_in_line =
block_element_list In_shorthand_list ~parent_markup:bullet input
in
if content = [] then
Parse_error.should_not_be_empty ~what:(Token.describe bullet)
next_token.location
|> add_warning input;
let acc = content :: acc in
consume_list_items stream_head where_in_line acc)
else (List.rev acc, where_in_line)
in
consume_list_items
(first_token :> stopped_implicitly with_location)
where_in_line []
and explicit_list_items :
parent_markup:[< Token.t ] ->
input ->
Ast.nestable_block_element with_location list list * Loc.span =
fun ~parent_markup input ->
let rec consume_list_items :
Ast.nestable_block_element with_location list list ->
Ast.nestable_block_element with_location list list * Loc.span =
fun acc ->
let next_token = peek input in
match next_token.value with
| `End ->
Parse_error.end_not_allowed next_token.location
~in_what:(Token.describe parent_markup)
|> add_warning input;
(List.rev acc, next_token.location)
| `Right_brace ->
junk input;
(List.rev acc, next_token.location)
| `Space _ | `Single_newline _ | `Blank_line _ ->
junk input;
consume_list_items acc
| `Begin_list_item kind as token ->
junk input;
(if kind = `Li then
match (peek input).value with
| `Space _ | `Single_newline _ | `Blank_line _ | `Right_brace ->
()
| _ ->
Parse_error.should_be_followed_by_whitespace next_token.location
~what:(Token.print token)
|> add_warning input);
let content, token_after_list_item, _where_in_line =
block_element_list In_explicit_list ~parent_markup:token input
in
if content = [] then
Parse_error.should_not_be_empty next_token.location
~what:(Token.describe token)
|> add_warning input;
(match token_after_list_item.value with
| `Right_brace -> junk input
| `End ->
Parse_error.end_not_allowed token_after_list_item.location
~in_what:(Token.describe token)
|> add_warning input);
let acc = content :: acc in
consume_list_items acc
| token ->
let suggestion =
match token with
| `Begin_section_heading _ | `Tag _ ->
Printf.sprintf "move %s outside the list." (Token.describe token)
| _ ->
Printf.sprintf "move %s into a list item, %s or %s."
(Token.describe token)
(Token.print (`Begin_list_item `Li))
(Token.print (`Begin_list_item `Dash))
in
Parse_error.not_allowed next_token.location ~what:(Token.describe token)
~in_what:(Token.describe parent_markup)
~suggestion
|> add_warning input;
junk input;
consume_list_items acc
in
consume_list_items []
and light_table ~parent_markup ~parent_markup_location input =
let rec consume_rows acc ~last_loc =
Reader.until_rbrace_or_eof input acc >>> fun next_token ->
match next_token.Loc.value with
| `Bar | #token_that_always_begins_an_inline_element -> (
let next, row, last_loc =
light_table_row ~parent_markup ~last_loc input
in
match next with
| `Continue -> consume_rows (row :: acc) ~last_loc
| `Stop -> (row :: acc, last_loc))
| other_token ->
Parse_error.not_allowed next_token.location
~what:(Token.describe other_token)
~in_what:(Token.describe parent_markup)
|> add_warning input;
junk input;
consume_rows acc ~last_loc
in
let rows, brace_location = consume_rows [] ~last_loc:parent_markup_location in
let grid = List.rev rows in
(Table.Light_syntax.from_raw_data grid, brace_location)
and light_table_row ~parent_markup ~last_loc input =
let rec consume_row acc_row acc_cell acc_space ~new_line ~last_loc =
let push_cells row cell =
match cell with [] -> row | _ -> List.rev cell :: row
in
let return row cell = List.rev (push_cells row cell) in
let next_token = peek input in
match next_token.value with
| `End ->
Parse_error.end_not_allowed next_token.location ~in_what:"table"
|> add_warning input;
junk input;
(`Stop, return acc_row acc_cell, next_token.location)
| `Right_brace ->
junk input;
(`Stop, return acc_row acc_cell, next_token.location)
| `Space _ as token ->
junk input;
let i = Loc.at next_token.location token in
consume_row acc_row acc_cell (i :: acc_space) ~new_line ~last_loc
| `Single_newline _ | `Blank_line _ ->
junk input;
(`Continue, return acc_row acc_cell, last_loc)
| `Bar ->
junk input;
let acc_row = if new_line then [] else List.rev acc_cell :: acc_row in
consume_row acc_row [] [] ~new_line:false ~last_loc
| #token_that_always_begins_an_inline_element as token ->
let i = inline_element input next_token.location token in
if Loc.spans_multiple_lines i then
Parse_error.not_allowed
~what:(Token.describe (`Single_newline ""))
~in_what:(Token.describe `Begin_table_light)
i.location
|> add_warning input;
let acc_cell =
if acc_cell = [] then [ i ] else (i :: acc_space) @ acc_cell
in
consume_row acc_row acc_cell [] ~new_line:false
~last_loc:next_token.location
| other_token ->
Parse_error.not_allowed next_token.location
~what:(Token.describe other_token)
~in_what:(Token.describe parent_markup)
|> add_warning input;
junk input;
consume_row acc_row acc_cell acc_space ~new_line ~last_loc
in
consume_row [] [] [] ~new_line:true ~last_loc
and heavy_table ~parent_markup ~parent_markup_location input =
let rec consume_rows acc ~last_loc =
Reader.until_rbrace_or_eof input acc >>> fun next_token ->
match next_token.Loc.value with
| `Begin_table_row as token ->
junk input;
let items, last_loc = heavy_table_row ~parent_markup:token input in
consume_rows (List.rev items :: acc) ~last_loc
| token ->
Parse_error.not_allowed next_token.location ~what:(Token.describe token)
~in_what:(Token.describe parent_markup)
~suggestion:"Move outside of {table ...}, or inside {tr ...}"
|> add_warning input;
junk input;
consume_rows acc ~last_loc
in
let rows, brace_location = consume_rows [] ~last_loc:parent_markup_location in
let grid = List.rev rows in
(Table.Heavy_syntax.from_grid grid, brace_location)
and heavy_table_row ~parent_markup input =
let rec consume_cell_items acc =
Reader.until_rbrace_or_eof input acc >>> fun next_token ->
match next_token.Loc.value with
| `Begin_table_cell kind as token ->
junk input;
let content, token_after_list_item, _where_in_line =
block_element_list In_table_cell ~parent_markup:token input
in
(match token_after_list_item.value with
| `Right_brace -> junk input
| `End ->
Parse_error.not_allowed token_after_list_item.location
~what:(Token.describe `End) ~in_what:(Token.describe token)
|> add_warning input);
consume_cell_items ((content, kind) :: acc)
| token ->
Parse_error.not_allowed next_token.location ~what:(Token.describe token)
~in_what:(Token.describe parent_markup)
~suggestion:
"Move outside of {table ...}, or inside {td ...} or {th ...}"
|> add_warning input;
junk input;
consume_cell_items acc
in
consume_cell_items []
let parse warnings tokens =
let input : input = { tokens; warnings } in
let rec parse_block_elements () =
let elements, last_token, _where_in_line =
block_element_list Top_level ~parent_markup:`Comment input
in
match last_token.value with
| `End -> elements
| `Right_brace ->
Parse_error.unpaired_right_brace last_token.location
|> add_warning input;
let block =
Loc.same last_token (`Paragraph [ Loc.same last_token (`Word "}") ])
in
junk input;
elements @ (block :: parse_block_elements ())
in
let ast = parse_block_elements () in
(ast, List.rev !(input.warnings))