@@ -64,54 +64,46 @@ module Table = struct
64
64
65
65
let valid_align_row lx = List. map valid_align lx |> Option. join_list
66
66
67
- let create ~header ~ data ~align : Ast. table =
67
+ let create ~grid ~align : Ast. table =
68
68
let to_block x = Loc. at x.Loc. location (`Paragraph [ x ]) in
69
- let cell_to_block = List. map to_block in
69
+ let cell_to_block ( x , k ) = ( List. map to_block x, k) in
70
70
let row_to_block = List. map cell_to_block in
71
71
let grid_to_block = List. map row_to_block in
72
- ((row_to_block header, grid_to_block data , align), `Light )
72
+ ((grid_to_block grid , align), `Light )
73
73
74
- let from_grid (grid : _ Ast.grid ) : Ast.table =
74
+ let with_kind kind : 'a with_location list list -> 'a Ast.row =
75
+ List. map (fun c -> (c, kind))
76
+
77
+ let from_grid grid : Ast.table =
75
78
match grid with
76
- | [] -> create ~header: [] ~data :[] ~align: []
79
+ | [] -> create ~grid : [] ~align: []
77
80
| row1 :: rows2_N -> (
78
81
match valid_align_row row1 with
79
82
(* If the first line is the align row, everything else is data. *)
80
- | Some align -> create ~header: [] ~data: rows2_N ~align
83
+ | Some align ->
84
+ create ~grid: (List. map (with_kind `Data ) rows2_N) ~align
81
85
| None -> (
82
86
match rows2_N with
83
87
(* Only 1 line, if this is not the align row this is data. *)
84
- | [] -> create ~header: [] ~data: [ row1 ] ~align: []
88
+ | [] -> create ~grid: [ with_kind `Data row1 ] ~align: []
85
89
| row2 :: rows3_N -> (
86
90
match valid_align_row row2 with
87
91
(* If the second line is the align row, the first one is the
88
92
header and the rest is data. *)
89
- | Some align -> create ~header: row1 ~data: rows3_N ~align
93
+ | Some align ->
94
+ let header = with_kind `Header row1 in
95
+ let data = List. map (with_kind `Data ) rows3_N in
96
+ create ~grid: (header :: data) ~align
90
97
(* No align row in the first 2 lines, everything is considered
91
98
data. *)
92
- | None -> create ~header: [] ~data: grid ~align: [] )))
99
+ | None ->
100
+ create ~grid: (List. map (with_kind `Data ) grid) ~align: [] ))
101
+ )
93
102
end
94
103
95
104
module Heavy_syntax = struct
96
- let create ~header ~data : Ast. table = ((header, data, [] ), `Heavy )
97
-
98
- let valid_header_row row =
99
- List. map (function `Header , x -> Some x | `Data , _ -> None ) row
100
- |> Option. join_list
101
-
102
- let from_grid grid : Ast.table =
103
- match grid with
104
- | [] -> create ~header: [] ~data: []
105
- | row1 :: rows2_N ->
106
- let header, data =
107
- (* If the first line is the header row, everything else is data. *)
108
- match valid_header_row row1 with
109
- | Some header -> (header, rows2_N)
110
- (* Otherwise everything is considered data. *)
111
- | None -> ([] , grid)
112
- in
113
- let data = List. map (List. map snd) data in
114
- create ~header ~data
105
+ let create ~grid : Ast. table = ((grid, [] ), `Heavy )
106
+ let from_grid grid : Ast.table = create ~grid
115
107
end
116
108
end
117
109
@@ -475,7 +467,7 @@ let paragraph : input -> Ast.nestable_block_element with_location =
475
467
(* {3 Helper types} *)
476
468
477
469
(* The interpretation of tokens in the block parser depends on where on a line
478
- each token appears. The seven possible "locations" are:
470
+ each token appears. The six possible "locations" are:
479
471
480
472
- [`At_start_of_line], when only whitespace has been read on the current
481
473
line.
@@ -485,8 +477,7 @@ let paragraph : input -> Ast.nestable_block_element with_location =
485
477
[-], has been read, and only whitespace has been read since.
486
478
- [`After_explicit_list_bullet], when a valid explicit bullet, such as [{li],
487
479
has been read, and only whitespace has been read since.
488
- - [`After_table_header], when a table header opening markup ('{th') has been read.
489
- - [`After_table_cell], when a table cell opening markup ('{td') has been read.
480
+ - [`After_table_cell], when a table cell opening markup ('{th' or '{td') has been read.
490
481
- [`After_text], when any other valid non-whitespace token has already been
491
482
read on the current line.
492
483
@@ -510,7 +501,6 @@ type where_in_line =
510
501
| `After_tag
511
502
| `After_shorthand_bullet
512
503
| `After_explicit_list_bullet
513
- | `After_table_header
514
504
| `After_table_cell
515
505
| `After_text ]
516
506
@@ -565,7 +555,6 @@ type ('block, 'stops_at_which_tokens) context =
565
555
| Top_level : (Ast .block_element , stops_at_delimiters ) context
566
556
| In_shorthand_list : (Ast .nestable_block_element , stopped_implicitly ) context
567
557
| In_explicit_list : (Ast .nestable_block_element , stops_at_delimiters ) context
568
- | In_table_header : (Ast .nestable_block_element , stops_at_delimiters ) context
569
558
| In_table_cell : (Ast .nestable_block_element , stops_at_delimiters ) context
570
559
| In_tag : (Ast .nestable_block_element , Token .t ) context
571
560
@@ -581,7 +570,6 @@ let accepted_in_all_contexts :
581
570
| Top_level -> (block :> Ast.block_element )
582
571
| In_shorthand_list -> block
583
572
| In_explicit_list -> block
584
- | In_table_header -> block
585
573
| In_table_cell -> block
586
574
| In_tag -> block
587
575
@@ -674,7 +662,6 @@ let rec block_element_list :
674
662
| Top_level -> (List. rev acc, next_token, where_in_line)
675
663
| In_shorthand_list -> (List. rev acc, next_token, where_in_line)
676
664
| In_explicit_list -> (List. rev acc, next_token, where_in_line)
677
- | In_table_header -> (List. rev acc, next_token, where_in_line)
678
665
| In_table_cell -> (List. rev acc, next_token, where_in_line)
679
666
| In_tag -> (List. rev acc, next_token, where_in_line))
680
667
(* Whitespace. This can terminate some kinds of block elements. It is also
@@ -726,8 +713,7 @@ let rec block_element_list :
726
713
consume_block_elements ~parsed_a_tag where_in_line acc
727
714
(* Table cells ([{th ...}] and [{td ...}]) can never appear directly
728
715
in block content. They can only appear inside [{tr ...}]. *)
729
- | { value = (`Begin_table_header | `Begin_table_data ) as token; location }
730
- ->
716
+ | { value = `Begin_table_cell _ as token ; location } ->
731
717
let suggestion =
732
718
Printf. sprintf " move %s into %s." (Token. print token)
733
719
(Token. describe `Begin_table_row )
@@ -777,7 +763,6 @@ let rec block_element_list :
777
763
if where_in_line = `At_start_of_line then
778
764
(List. rev acc, next_token, where_in_line)
779
765
else recover_when_not_at_top_level context
780
- | In_table_header -> recover_when_not_at_top_level context
781
766
| In_table_cell -> recover_when_not_at_top_level context
782
767
| In_tag ->
783
768
if where_in_line = `At_start_of_line then
@@ -1028,7 +1013,6 @@ let rec block_element_list :
1028
1013
(List. rev acc, next_token, where_in_line)
1029
1014
else recover_when_not_at_top_level context
1030
1015
| In_explicit_list -> recover_when_not_at_top_level context
1031
- | In_table_header -> recover_when_not_at_top_level context
1032
1016
| In_table_cell -> recover_when_not_at_top_level context
1033
1017
| In_tag -> recover_when_not_at_top_level context
1034
1018
| Top_level ->
@@ -1089,7 +1073,6 @@ let rec block_element_list :
1089
1073
| Top_level -> `At_start_of_line
1090
1074
| In_shorthand_list -> `After_shorthand_bullet
1091
1075
| In_explicit_list -> `After_explicit_list_bullet
1092
- | In_table_header -> `After_table_header
1093
1076
| In_table_cell -> `After_table_cell
1094
1077
| In_tag -> `After_tag
1095
1078
in
@@ -1334,7 +1317,7 @@ and heavy_table ~parent_markup ~parent_markup_location input =
1334
1317
(Table.Heavy_syntax. from_grid grid, brace_location)
1335
1318
1336
1319
(* Consumes a sequence of table cells (starting with '{th ...}' or '{td ... }',
1337
- which are represented by [`Begin_table_header] [`Begin_table_data ] tokens).
1320
+ which are represented by [`Begin_table_cell ] tokens).
1338
1321
1339
1322
This function is called immediately after '{tr' ([`Begin_table_row]) is
1340
1323
read. The only "valid" way to exit is by reading a [`Right_brace] token,
@@ -1343,13 +1326,7 @@ and heavy_table_row ~parent_markup input =
1343
1326
let rec consume_cell_items acc =
1344
1327
Reader. until_rbrace input acc >>> fun next_token ->
1345
1328
match next_token.Loc. value with
1346
- | `Begin_table_header as token ->
1347
- junk input;
1348
- let content, _brace_location =
1349
- heavy_table_header input ~parent_markup: token
1350
- in
1351
- consume_cell_items ((`Header , content) :: acc)
1352
- | `Begin_table_data as token ->
1329
+ | `Begin_table_cell kind as token ->
1353
1330
junk input;
1354
1331
let content, token_after_list_item, _where_in_line =
1355
1332
block_element_list In_table_cell ~parent_markup: token input
@@ -1360,7 +1337,7 @@ and heavy_table_row ~parent_markup input =
1360
1337
Parse_error. not_allowed token_after_list_item.location
1361
1338
~what: (Token. describe `End ) ~in_what: (Token. describe token)
1362
1339
|> add_warning input);
1363
- consume_cell_items ((`Data , content ) :: acc)
1340
+ consume_cell_items ((content, kind ) :: acc)
1364
1341
| token ->
1365
1342
Parse_error. not_allowed next_token.location ~what: (Token. describe token)
1366
1343
~in_what: (Token. describe parent_markup)
@@ -1370,28 +1347,6 @@ and heavy_table_row ~parent_markup input =
1370
1347
in
1371
1348
consume_cell_items []
1372
1349
1373
- (* Consumes a table header.
1374
-
1375
- This function is called immediately after '{th' ([`Begin_table_header]) is
1376
- read. The only "valid" way to exit is by reading a [`Right_brace] token,
1377
- which is consumed. *)
1378
- and heavy_table_header ~parent_markup input =
1379
- let rec consume_items acc =
1380
- Reader. until_rbrace input acc >>> fun next_token ->
1381
- (match acc with
1382
- | _ :: _ ->
1383
- Parse_error. not_allowed next_token.location
1384
- ~what: (Token. describe next_token.value)
1385
- ~in_what: (Token. describe parent_markup)
1386
- |> add_warning input
1387
- | [] -> () );
1388
- let content, _token_after_list_item, _where_in_line =
1389
- block_element_list In_table_header ~parent_markup input
1390
- in
1391
- consume_items content
1392
- in
1393
- consume_items []
1394
-
1395
1350
(* {2 Entry point} *)
1396
1351
1397
1352
let parse warnings tokens =
0 commit comments