@@ -40,33 +40,80 @@ let peek input =
40
40
41
41
module Table = struct
42
42
module Light_syntax = struct
43
+ let split_on_plus w =
44
+ let len = String. length w in
45
+ match w with
46
+ | "+" -> [ " " ]
47
+ | _ when len > 1 ->
48
+ let plus = function '+' -> true | _ -> false in
49
+ let w =
50
+ if plus (String. get w 0 ) then String. sub w 1 (len - 1 ) else w
51
+ in
52
+ let len = String. length w in
53
+ let w =
54
+ if plus (String. get w (len - 1 )) then String. sub w 0 (len - 1 )
55
+ else w
56
+ in
57
+ String. split_on_char '+' w
58
+ | _ -> [ w ]
59
+
60
+ let % expect_test _ =
61
+ let f x =
62
+ let pp x = Printf. printf " %S " x in
63
+ List. iter pp (split_on_plus x)
64
+ in
65
+ f " " ;
66
+ [% expect {| " " | }];
67
+ f " +" ;
68
+ [% expect {| " " | }];
69
+ f " ++" ;
70
+ [% expect {| " " | }];
71
+ f " +--+" ;
72
+ [% expect {| " --" | }];
73
+ f " --" ;
74
+ [% expect {| " --" | }];
75
+ f " --+--+--" ;
76
+ [% expect {| " --" " --" " --" | }];
77
+ f " +----+----+----+" ;
78
+ [% expect {| " ----" " ----" " ----" | }]
79
+
43
80
let valid_align = function
44
- | [ { Loc. value = `Word w; _ } ] -> (
45
- match String. length w with
46
- | 0 -> `Valid None
47
- | 1 -> (
48
- match w with
49
- | "-" -> `Valid None
50
- | ":" -> `Valid (Some `Center )
51
- | _ -> `Invalid )
52
- | len ->
53
- if String. for_all (Char. equal '-' ) (String. sub w 1 (len - 2 )) then
54
- match (String. get w 0 , String. get w (len - 1 )) with
55
- | ':' , ':' -> `Valid (Some `Center )
56
- | ':' , '-' -> `Valid (Some `Left )
57
- | '-' , ':' -> `Valid (Some `Right )
58
- | '-' , '-' -> `Valid None
59
- | _ -> `Invalid
60
- else `Invalid )
61
- | _ -> `Invalid
81
+ | [ { Loc. value = `Word w; _ } ] ->
82
+ (* We consider [+----+----+----+] a valid row, as it is a common format. *)
83
+ let valid_word w =
84
+ match String. length w with
85
+ | 0 -> `Valid None
86
+ | 1 -> (
87
+ match w with
88
+ | "-" -> `Valid None
89
+ | ":" -> `Valid (Some `Center )
90
+ | _ -> `Invalid )
91
+ | len ->
92
+ if String. for_all (Char. equal '-' ) (String. sub w 1 (len - 2 ))
93
+ then
94
+ match (String. get w 0 , String. get w (len - 1 )) with
95
+ | ':' , ':' -> `Valid (Some `Center )
96
+ | ':' , '-' -> `Valid (Some `Left )
97
+ | '-' , ':' -> `Valid (Some `Right )
98
+ | '-' , '-' -> `Valid None
99
+ | _ -> `Invalid
100
+ else `Invalid
101
+ in
102
+ List. map valid_word (split_on_plus w)
103
+ | _ -> [ `Invalid ]
62
104
63
105
let valid_align_row lx =
64
106
let rec loop acc = function
65
107
| [] -> Some (List. rev acc)
66
- | x :: q -> (
67
- match valid_align x with
68
- | `Invalid -> None
69
- | `Valid alignment -> loop (alignment :: acc) q)
108
+ | x :: q ->
109
+ let all_aligns = valid_align x in
110
+ let valid_aligns =
111
+ List. filter_map
112
+ (function `Valid a -> Some a | `Invalid -> None )
113
+ all_aligns
114
+ in
115
+ if List. (length valid_aligns < length all_aligns) then None
116
+ else loop (List. rev_append valid_aligns acc) q
70
117
in
71
118
loop [] lx
72
119
0 commit comments