Skip to content

Commit 64b788b

Browse files
gpetiotjonludlam
authored andcommitted
Simplify alignment
1 parent 76ac20e commit 64b788b

File tree

6 files changed

+29
-27
lines changed

6 files changed

+29
-27
lines changed

src/ast.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type inline_element =
3333
type 'a cell = 'a with_location list
3434
type 'a row = 'a cell list
3535
type 'a grid = 'a row list
36-
type 'a abstract_table = 'a row * 'a grid * alignment option list
36+
type 'a abstract_table = 'a row * 'a grid * alignment list
3737

3838
type nestable_block_element =
3939
[ `Paragraph of inline_element with_location list

src/compat.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module Option = struct
77
| None -> failwith "Option.value_exn None"
88
| Some x -> x
99

10+
let value ~default = function None -> default | Some x -> x
11+
1012
let join_list l =
1113
if List.for_all is_some l then Some (List.map value_exn l) else None
1214
end

src/compat.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module Option : sig
55
val is_some : 'a option -> bool
66
(** [is_some o] is [true] if and only if [o] is [Some o]. *)
77

8+
val value : default:'a -> 'a option -> 'a
89
val join_list : 'a option list -> 'a list option
910
end
1011

src/syntax.ml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,24 @@ let peek input =
4040

4141
module Table = struct
4242
module Light_syntax = struct
43+
let default_align = `Center
44+
4345
let valid_align = function
4446
| [ { Loc.value = `Word w; _ } ] -> (
4547
match String.length w with
46-
| 0 -> Some None
48+
| 0 -> Some default_align
4749
| 1 -> (
4850
match w with
49-
| "-" -> Some None
50-
| ":" -> Some (Some `Center)
51+
| "-" -> Some default_align
52+
| ":" -> Some `Center
5153
| _ -> None)
5254
| len ->
5355
if String.for_all (Char.equal '-') (String.sub w 1 (len - 2)) then
5456
match (String.get w 0, String.get w (len - 1)) with
55-
| ':', ':' -> Some (Some `Center)
56-
| ':', '-' -> Some (Some `Left)
57-
| '-', ':' -> Some (Some `Right)
58-
| '-', '-' -> Some None
57+
| ':', ':' -> Some `Center
58+
| ':', '-' -> Some `Left
59+
| '-', ':' -> Some `Right
60+
| '-', '-' -> Some default_align
5961
| _ -> None
6062
else None)
6163
| _ -> None
@@ -90,8 +92,7 @@ module Table = struct
9092
let valid_header_row row =
9193
List.map
9294
(function
93-
| `Header (Some x), y -> Some (Some x, y)
94-
| `Header None, y -> Some (None, y)
95+
| `Header align, x -> Some (Option.value align ~default:`Center, x)
9596
| `Data, _ -> None)
9697
row
9798
|> Option.join_list

test/test.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ module Ast_to_sexp = struct
9797
map "cell" cell @@ at.at (f at) );
9898
( map "data" data @@ fun row ->
9999
map "row" row @@ fun cell -> map "cell" cell @@ at.at (f at) );
100-
(map "align" align @@ function
101-
| Some a -> alignment a
102-
| None -> Atom "none");
100+
map "align" align @@ alignment;
103101
]
104102
in
105103
match t with

test/test_tables.ml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let%expect_test _ =
3737
{|
3838
((output
3939
(((f.ml (1 0) (1 17))
40-
(table (syntax heavy) (header ((cell ()))) (data ()) (align (none))))))
40+
(table (syntax heavy) (header ((cell ()))) (data ()) (align (center))))))
4141
(warnings ())) |}]
4242

4343
let multiple_headers =
@@ -47,7 +47,7 @@ let%expect_test _ =
4747
((output
4848
(((f.ml (1 0) (1 37))
4949
(table (syntax heavy) (header ((cell ())))
50-
(data ((row ((cell ()))) (row ((cell ()))))) (align (none))))))
50+
(data ((row ((cell ()))) (row ((cell ()))))) (align (center))))))
5151
(warnings ())) |}]
5252

5353
let complex_table =
@@ -129,8 +129,8 @@ let%expect_test _ =
129129
((cell (((f.ml (24 17) (24 18)) (word 1))))
130130
(cell (((f.ml (24 21) (24 22)) (word 2))))
131131
(cell (((f.ml (24 25) (24 26)) (word 3))))))))
132-
(align (none none none))))))))))
133-
(align (none none))))))
132+
(align (center center center))))))))))
133+
(align (center center))))))
134134
(warnings ())) |}]
135135

136136
let align =
@@ -243,7 +243,7 @@ let%expect_test _ =
243243
((row
244244
((cell (((f.ml (4 9) (4 10)) (word x))))
245245
(cell (((f.ml (4 13) (4 14)) (word y))))))))
246-
(align (none none))))))
246+
(align (center center))))))
247247
(warnings ())) |}]
248248

249249
let no_align =
@@ -278,7 +278,7 @@ let%expect_test _ =
278278
{|
279279
((output
280280
(((f.ml (2 6) (4 7))
281-
(table (syntax light) (header ()) (data ()) (align (none none))))))
281+
(table (syntax light) (header ()) (data ()) (align (center center))))))
282282
(warnings ())) |}]
283283

284284
let no_data =
@@ -296,7 +296,7 @@ let%expect_test _ =
296296
(header
297297
((cell (((f.ml (3 9) (3 10)) (word x))))
298298
(cell (((f.ml (3 13) (3 14)) (word y))))))
299-
(data ()) (align (none none))))))
299+
(data ()) (align (center center))))))
300300
(warnings ())) |}]
301301

302302
let alignment =
@@ -317,7 +317,7 @@ let%expect_test _ =
317317
(cell (((f.ml (3 13) (3 14)) (word b))))
318318
(cell (((f.ml (3 17) (3 18)) (word c))))
319319
(cell (((f.ml (3 21) (3 22)) (word d))))))
320-
(data ()) (align (none left right center))))))
320+
(data ()) (align (center left right center))))))
321321
(warnings ())) |}]
322322

323323
let no_bars =
@@ -345,7 +345,7 @@ let%expect_test _ =
345345
(cell (((f.ml (5 12) (5 13)) (word b))))
346346
(cell (((f.ml (5 16) (5 17)) (word c))))
347347
(cell (((f.ml (5 20) (5 21)) (word d))))))))
348-
(align (none left right center))))))
348+
(align (center left right center))))))
349349
(warnings ())) |}]
350350

351351
let light_table_new_lines =
@@ -377,7 +377,7 @@ let%expect_test _ =
377377
(cell (((f.ml (8 13) (8 14)) (word b))))
378378
(cell (((f.ml (8 17) (8 18)) (word c))))
379379
(cell (((f.ml (8 21) (8 22)) (word d))))))))
380-
(align (none none none none))))))
380+
(align (center center center center))))))
381381
(warnings ())) |}]
382382

383383
let light_table_markup =
@@ -406,7 +406,7 @@ let%expect_test _ =
406406
(cell
407407
(((f.ml (3 60) (3 65)) (bold (((f.ml (3 63) (3 64)) (word d)))))
408408
((f.ml (3 66) (3 71)) (code_span foo))))))
409-
(data ()) (align (none none none none))))))
409+
(data ()) (align (center center center center))))))
410410
(warnings ())) |}]
411411

412412
let no_space =
@@ -427,7 +427,7 @@ let%expect_test _ =
427427
(cell (((f.ml (3 15) (3 16)) (word b))))
428428
(cell (((f.ml (3 18) (3 19)) (word c))))
429429
(cell (((f.ml (3 21) (3 22)) (word d))))))
430-
(data ()) (align (none right left center))))))
430+
(data ()) (align (center right left center))))))
431431
(warnings ())) |}]
432432

433433
let multiple_headers =
@@ -484,7 +484,7 @@ let%expect_test _ =
484484
(((f.ml (2 11) (5 12))
485485
(table (syntax light)
486486
(header ((cell ()) (cell (((f.ml (3 23) (3 24)) (word b)))))) (data ())
487-
(align (none none))))))
487+
(align (center center))))))
488488
(warnings
489489
( "File \"f.ml\", line 3, characters 13-20:\
490490
\n'{[...]}' (code block) is not allowed in '{t ...}' (table)."))) |}]
@@ -506,7 +506,7 @@ let%expect_test _ =
506506
(header
507507
((cell (((f.ml (4 13) (4 14)) (word a))))
508508
(cell (((f.ml (4 17) (4 18)) (word b))))))
509-
(data ()) (align (none none))))))
509+
(data ()) (align (center center))))))
510510
(warnings
511511
( "File \"f.ml\", line 3, characters 11-18:\
512512
\n'{[...]}' (code block) is not allowed in '{t ...}' (table)."))) |}]

0 commit comments

Comments
 (0)