Source file test_token_yaml.ml
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
module Yaml =
struct
type t =
| Scalar of string
| List of t list
| Record of (string * t) list
let scalar str = Scalar str
let list lst = List lst
let record lst = Record lst
module Pretty = Fmlib_pretty.Print
let rec to_doc: t -> Pretty.doc = function
| Scalar str ->
Pretty.text str
| List lst ->
let open Pretty in
lst
|> List.map
(fun y ->
text "- " <+> nest 2 (to_doc y))
|> separated_by cut
| Record lst ->
let open Pretty in
lst
|> List.map
(fun (str,y) ->
text str <+> char ':'
<+> cut
<+> nest 4 (to_doc y))
|> separated_by cut
let to_string (str: string): string =
String.(make 1 '"' ^ escaped str ^ make 1 '"')
let rec to_json: t -> string = function
| Scalar str ->
to_string str
| List lst ->
"["
^
String.concat ", " (List.map to_json lst)
^
"]"
| Record lst ->
"{"
^
String.concat
", "
(List.map
(fun (key, y) -> to_string key ^ ": " ^ to_json y)
lst
)
^
"}"
let write_to_channel (oc: out_channel) (y: t): unit =
let open Pretty in
to_doc y <+> cut
|> layout 80
|> write_to_channel oc
end
module Token =
struct
type t =
| String of string
| Colon
| Dash
let string str = String str
let to_string: t -> string = function
| String str -> "<" ^ str ^ ">"
| Colon -> "':'"
| Dash -> "'-'"
end
module Token_plus =
struct
type t = Position.range * Token.t
end
module Lexer =
struct
module CP = Character.Make (Unit) (Token_plus) (Fmlib_std.Void)
open CP
let : char t =
let* _ = char '#' in
let* _ =
(charp (fun c -> c <> '\n') "comment character")
|> skip_zero_or_more
in
return '#'
let whitespace: int t =
char ' ' </> char '\n' </> comment
|> skip_zero_or_more
|> no_expectations
|> detach
let lexeme (p: 'a t): 'a t =
let* a = p in
let* _ = whitespace in
return a
let colon: Token.t t =
let* _ = char ':' in
return Token.Colon
let dash: Token.t t =
let* _ = char '-' in
return Token.Dash
let raw_string: Token.t t =
let expect = "chars not containing colon and newline" in
let inner c = c <> '\n' && c <> ':' && c <> '#' in
let first c = c <> '"' && inner c
in
(word first inner expect)
|> map (fun str -> String.trim str |> Token.string)
let quoted_string: Token.t t =
let expect = "chars except newline and dquote" in
let ok c = c <> '\n' && c <> '"'
in
let* _ = char '"' in
let* str = (word ok ok expect) </> return "" in
let* _ = char '"'|> lexeme
in
return (Token.string str)
let token: Token_plus.t t =
let* _ = whitespace in
let* tok =
colon </> dash </> raw_string </> quoted_string |> located
in
let* _ = whitespace in
return tok
module Parser =
struct
include CP.Parser
let make (pos: Position.t): t =
make_parser pos () token
let init: t =
make Position.start
let restart (lex: t): t =
assert (has_succeeded lex);
fold_lookahead
(make (position lex))
put
put_end
lex
end
end
module Combinator =
struct
module TP = Token_parser.Make (Unit) (Token) (Yaml) (Fmlib_std.Void)
module Parser = TP.Parser
open TP
let string: string t =
step
"string"
(fun _ _ tok ->
match tok with
| Token.String str ->
Some (str, ())
| _ ->
None
)
let colon: char t =
step
"':'"
(fun _ _ tok ->
match tok with
| Token.Colon ->
Some (':', ())
| _ ->
None
)
let dash: char t =
step
"'-'"
(fun _ _ tok ->
match tok with
| Token.Dash ->
Some ('-', ())
| _ ->
None
)
let scalar: Yaml.t t =
map Yaml.scalar string
module Backtrack = struct
let rec yaml (): Yaml.t t =
sequence_block () </> record_block () </> scalar
and sequence_block (): Yaml.t t =
one_or_more
(
sequence_element ()
<?>
"sequence element: \"- xxxx\""
|> align
)
<?> "sequence of aligned \"- xxxx\""
|> map (fun (a, lst) -> Yaml.list (a :: lst))
and sequence_element (): Yaml.t t =
let* _ = dash in
yaml () |> indent 1
and record_block (): Yaml.t t =
one_or_more
(
record_element ()
<?> "key value pair: \"xxx: yyyy\""
|> align
)
<?> "sequence of aligned \"xxx: yyyy\""
|> map (fun (a, lst) -> Yaml.record (a :: lst))
and record_element (): (string * Yaml.t) t =
let* str =
backtrack
(
let* str = string in
let* _ = colon in
return str
)
"\"xxx:\""
in
let* y =
(sequence_block () |> indent 0)
</>
(record_block () </> scalar |> indent 1)
in
return (str, y)
let parse: Parser.t =
make () (yaml ())
end
module Left_factored = struct
let rec yaml (): Yaml.t t =
sequence_block () </> record_block_or_scalar ()
and value_in_record (): Yaml.t t =
(sequence_block () |> indent 0)
</>
(record_block_or_scalar () |> indent 1)
and sequence_block (): Yaml.t t =
one_or_more
(
sequence_element ()
<?>
"sequence element: \"- xxxx\""
|> align
)
<?> "sequence of aligned \"- xxxx\""
|> map (fun (a, lst) -> Yaml.list (a :: lst))
and sequence_element (): Yaml.t t =
let* _ = dash in
yaml () |> indent 1
and record_block_or_scalar (): Yaml.t t =
let* str, y =
string_or_record_element () |> align
in
match y with
| None ->
Yaml.scalar str |> return
| Some y ->
let* lst =
zero_or_more
(
record_element ()
<?> "key value pair: \"xxx: yyyy\""
|> align
)
in
(str, y) :: lst |> Yaml.record |> return
and string_or_record_element (): (string * Yaml.t option) t =
let* str = string in
(
let* _ = colon in
let* y = value_in_record () in
return (str, Some y)
)
</>
return (str, None)
and record_element (): (string * Yaml.t) t =
let* str = string in
let* _ = colon in
let* y = value_in_record () in
return (str, y)
let parse: Parser.t =
make () (yaml ())
end
let left_factor = true
let parse =
if left_factor then
Left_factored.parse
else
Backtrack.parse
end
module Lex = Lexer.Parser
module Parse = Combinator.Parser
module Void = Fmlib_std.Void
module PL = struct
include Parse_with_lexer.Make (Unit) (Token) (Yaml) (Void) (Lex) (Parse)
let start: t =
make Lex.init Combinator.parse
end
module Pretty = Fmlib_pretty.Print
let write_error (str: string) (p: PL.t): unit =
let module Reporter = Error_reporter.Make (PL) in
Reporter.(
make_syntax p
|> run_on_string str
|> Pretty.layout 50
|> Pretty.write_to_channel stdout
)
let%test _ =
let open PL in
let str = {|
names: - Alice
- "Bob:#"
category: encryption
|}
in
let p = run_on_string str start in
has_succeeded p
&&
(final p |> Yaml.to_json)
=
{|{"names": ["Alice", "Bob:#"], "category": "encryption"}|}
let%test _ =
let open PL in
let str = {|
names: - Alice
- "-Bob"
category: encryption
|}
in
let p = run_on_string str start in
not (has_succeeded p)