Source file 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
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
(* See yaml.mli for the supported subset of YAML. *)

type t =
  | Null
  | Bool of bool
  | Float of float
  | String of string
  | List of t list
  | Assoc of (string * t) list

exception Parse_error of string

let error lnum fmt =
  Printf.ksprintf
    (fun s -> raise (Parse_error (Printf.sprintf "line %d: %s" lnum s)))
    fmt

(* {1 String helpers} *)

let is_blank c = c = ' ' || c = '\t'

let rtrim s =
  let n = ref (String.length s) in
  while !n > 0 && is_blank s.[!n - 1] do
    decr n
  done;
  String.sub s 0 !n

let skip_blanks s i =
  let n = String.length s in
  while !i < n && is_blank s.[!i] do
    incr i
  done

let add_utf8 b cp =
  if cp < 0x80 then Buffer.add_char b (Char.chr cp)
  else if cp < 0x800 then (
    Buffer.add_char b (Char.chr (0xc0 lor (cp lsr 6)));
    Buffer.add_char b (Char.chr (0x80 lor (cp land 0x3f))))
  else if cp < 0x10000 then (
    Buffer.add_char b (Char.chr (0xe0 lor (cp lsr 12)));
    Buffer.add_char b (Char.chr (0x80 lor ((cp lsr 6) land 0x3f)));
    Buffer.add_char b (Char.chr (0x80 lor (cp land 0x3f))))
  else (
    Buffer.add_char b (Char.chr (0xf0 lor (cp lsr 18)));
    Buffer.add_char b (Char.chr (0x80 lor ((cp lsr 12) land 0x3f)));
    Buffer.add_char b (Char.chr (0x80 lor ((cp lsr 6) land 0x3f)));
    Buffer.add_char b (Char.chr (0x80 lor (cp land 0x3f))))

(* {1 Scalars} *)

(** Read [count] hexadecimal digits of [s] starting at [!i]. *)
let read_hex lnum s i count =
  let n = String.length s in
  let v = ref 0 in
  for _ = 1 to count do
    if !i >= n then error lnum "truncated unicode escape sequence";
    let d =
      match s.[!i] with
        | '0' .. '9' as c -> Char.code c - Char.code '0'
        | 'a' .. 'f' as c -> Char.code c - Char.code 'a' + 10
        | 'A' .. 'F' as c -> Char.code c - Char.code 'A' + 10
        | c -> error lnum "invalid hexadecimal digit %C" c
    in
    v := (!v * 16) + d;
    incr i
  done;
  !v

(** Read the double-quoted scalar of [s] starting at [!i]. *)
let read_double lnum s i =
  let n = String.length s in
  let b = Buffer.create 16 in
  let stop = ref false in
  incr i;
  while not !stop do
    if !i >= n then error lnum "unterminated double-quoted string";
    match s.[!i] with
      | '"' ->
          incr i;
          stop := true
      | '\\' -> (
          incr i;
          if !i >= n then error lnum "unterminated escape sequence";
          let e = s.[!i] in
          incr i;
          match e with
            | 'n' -> Buffer.add_char b '\n'
            | 't' -> Buffer.add_char b '\t'
            | 'r' -> Buffer.add_char b '\r'
            | 'b' -> Buffer.add_char b '\b'
            | 'f' -> Buffer.add_char b '\012'
            | 'v' -> Buffer.add_char b '\011'
            | 'a' -> Buffer.add_char b '\007'
            | 'e' -> Buffer.add_char b '\027'
            | '0' -> Buffer.add_char b '\000'
            | '\\' -> Buffer.add_char b '\\'
            | '"' -> Buffer.add_char b '"'
            | '/' -> Buffer.add_char b '/'
            | ' ' -> Buffer.add_char b ' '
            | 'x' | 'u' | 'U' ->
                let count = match e with 'x' -> 2 | 'u' -> 4 | _ -> 8 in
                let cp = read_hex lnum s i count in
                if cp > 0x10ffff then error lnum "invalid unicode code point";
                add_utf8 b cp
            | c -> error lnum "invalid escape sequence \\%c" c)
      | c ->
          Buffer.add_char b c;
          incr i
  done;
  Buffer.contents b

(** Read the single-quoted scalar of [s] starting at [!i]. *)
let read_single lnum s i =
  let n = String.length s in
  let b = Buffer.create 16 in
  let stop = ref false in
  incr i;
  while not !stop do
    if !i >= n then error lnum "unterminated single-quoted string";
    if s.[!i] = '\'' then
      if !i + 1 < n && s.[!i + 1] = '\'' then (
        Buffer.add_char b '\'';
        i := !i + 2)
      else (
        incr i;
        stop := true)
    else (
      Buffer.add_char b s.[!i];
      incr i)
  done;
  Buffer.contents b

(** Whether [s] is a decimal number, as OCaml's [float_of_string] is more
    permissive than YAML (it accepts ["0x1p3"], ["1_0"], ["nan"], ...). *)
let is_number s =
  let n = String.length s in
  let i = ref 0 in
  if !i < n && (s.[!i] = '-' || s.[!i] = '+') then incr i;
  let start = !i in
  while !i < n && s.[!i] >= '0' && s.[!i] <= '9' do
    incr i
  done;
  let int_digits = !i - start in
  let frac_digits = ref 0 in
  if !i < n && s.[!i] = '.' then (
    incr i;
    let start = !i in
    while !i < n && s.[!i] >= '0' && s.[!i] <= '9' do
      incr i
    done;
    frac_digits := !i - start);
  if int_digits = 0 && !frac_digits = 0 then false
  else (
    let ok = ref true in
    if !i < n && (s.[!i] = 'e' || s.[!i] = 'E') then (
      incr i;
      if !i < n && (s.[!i] = '-' || s.[!i] = '+') then incr i;
      let start = !i in
      while !i < n && s.[!i] >= '0' && s.[!i] <= '9' do
        incr i
      done;
      if !i = start then ok := false);
    !ok && !i = n)

(** Type of a plain (unquoted) scalar. *)
let resolve s =
  match s with
    | "" | "~" | "null" | "Null" | "NULL" -> Null
    | "true" | "True" | "TRUE" -> Bool true
    | "false" | "False" | "FALSE" -> Bool false
    | ".inf" | ".Inf" | ".INF" | "+.inf" | "+.Inf" | "+.INF" -> Float infinity
    | "-.inf" | "-.Inf" | "-.INF" -> Float neg_infinity
    | ".nan" | ".NaN" | ".NAN" -> Float nan
    | _ -> if is_number s then Float (float_of_string s) else String s

(** Reject the YAML features we do not implement. *)
let check_unsupported lnum s =
  if s <> "" then (
    match s.[0] with
      | '|' | '>' -> error lnum "block scalars are not supported"
      | '&' -> error lnum "anchors are not supported"
      | '*' -> error lnum "aliases are not supported"
      | '!' -> error lnum "tags are not supported"
      | '%' -> error lnum "directives are not supported"
      | '@' | '`' -> error lnum "%C is a reserved indicator" s.[0]
      | '?' when String.length s = 1 || s.[1] = ' ' ->
          error lnum "complex mapping keys are not supported"
      | _ -> ())

(* {1 Flow collections} *)

(** Read a plain scalar of [s] starting at [!i], in flow context. *)
let read_plain s i =
  let n = String.length s in
  let start = !i in
  let fin = ref (-1) in
  while !fin < 0 && !i < n do
    let c = s.[!i] in
    if c = ',' || c = ']' || c = '}' then fin := !i
    else if
      c = ':'
      && (!i + 1 >= n
         || match s.[!i + 1] with ' ' | ',' | ']' | '}' -> true | _ -> false)
    then fin := !i
    else incr i
  done;
  let fin = if !fin < 0 then n else !fin in
  i := fin;
  rtrim (String.sub s start (fin - start))

let rec flow_value lnum s i =
  skip_blanks s i;
  if !i >= String.length s then error lnum "unexpected end of flow collection";
  match s.[!i] with
    | '[' ->
        incr i;
        flow_seq lnum s i
    | '{' ->
        incr i;
        flow_map lnum s i
    | '"' -> String (read_double lnum s i)
    | '\'' -> String (read_single lnum s i)
    | c ->
        let raw = read_plain s i in
        if raw = "" then error lnum "unexpected %C in flow collection" c;
        check_unsupported lnum raw;
        resolve raw

and flow_seq lnum s i =
  let n = String.length s in
  let items = ref [] in
  let stop = ref false in
  skip_blanks s i;
  if !i < n && s.[!i] = ']' then (
    incr i;
    stop := true);
  while not !stop do
    items := flow_value lnum s i :: !items;
    skip_blanks s i;
    if !i >= n then error lnum "unterminated flow sequence";
    match s.[!i] with
      | ']' ->
          incr i;
          stop := true
      | ',' ->
          incr i;
          skip_blanks s i;
          if !i < n && s.[!i] = ']' then (
            incr i;
            stop := true)
      | c -> error lnum "unexpected %C in flow sequence" c
  done;
  List (List.rev !items)

and flow_map lnum s i =
  let n = String.length s in
  let items = ref [] in
  let stop = ref false in
  skip_blanks s i;
  if !i < n && s.[!i] = '}' then (
    incr i;
    stop := true);
  while not !stop do
    let key = flow_key lnum s i in
    skip_blanks s i;
    let value =
      if !i < n && s.[!i] = ':' then (
        incr i;
        skip_blanks s i;
        if !i >= n then error lnum "unterminated flow mapping"
        else if s.[!i] = ',' || s.[!i] = '}' then Null
        else flow_value lnum s i)
      else Null
    in
    items := (key, value) :: !items;
    skip_blanks s i;
    if !i >= n then error lnum "unterminated flow mapping";
    match s.[!i] with
      | '}' ->
          incr i;
          stop := true
      | ',' ->
          incr i;
          skip_blanks s i;
          if !i < n && s.[!i] = '}' then (
            incr i;
            stop := true)
      | c -> error lnum "unexpected %C in flow mapping" c
  done;
  Assoc (List.rev !items)

and flow_key lnum s i =
  skip_blanks s i;
  if !i >= String.length s then error lnum "unterminated flow mapping";
  match s.[!i] with
    | '"' -> read_double lnum s i
    | '\'' -> read_single lnum s i
    | '[' | '{' -> error lnum "only scalar mapping keys are supported"
    | _ ->
        let raw = read_plain s i in
        if raw = "" then error lnum "empty mapping key";
        check_unsupported lnum raw;
        raw

(* {1 Lines} *)

type line = { mutable indent : int; mutable content : string; lnum : int }

(** Remove a trailing comment, taking quoting into account. *)
let strip_comment s =
  let n = String.length s in
  let i = ref 0 in
  let cut = ref (-1) in
  let quote = ref ' ' in
  while !cut < 0 && !i < n do
    let c = s.[!i] in
    if !quote = '"' then
      if c = '\\' then i := !i + 2
      else (
        if c = '"' then quote := ' ';
        incr i)
    else if !quote = '\'' then (
      if c = '\'' then quote := ' ';
      incr i)
    else if c = '#' && (!i = 0 || is_blank s.[!i - 1]) then cut := !i
    else (
      if c = '"' || c = '\'' then quote := c;
      incr i)
  done;
  if !cut < 0 then s else String.sub s 0 !cut

(** Split the input into non-empty, comment-free lines. *)
let scan src =
  let lines = ref [] in
  List.iteri
    (fun k l ->
      let lnum = k + 1 in
      let l =
        let n = String.length l in
        if n > 0 && l.[n - 1] = '\r' then String.sub l 0 (n - 1) else l
      in
      let l = strip_comment l in
      let n = String.length l in
      let i = ref 0 in
      while !i < n && l.[!i] = ' ' do
        incr i
      done;
      if !i < n && l.[!i] = '\t' then
        error lnum "tabulations cannot be used for indentation";
      let content = rtrim (String.sub l !i (n - !i)) in
      if content <> "" then lines := { indent = !i; content; lnum } :: !lines)
    (String.split_on_char '\n' src);
  Array.of_list (List.rev !lines)

(** Handle the [---] and [...] document markers. *)
let strip_document_markers lines =
  let is_marker m l = l.indent = 0 && l.content = m in
  let lines = Array.to_list lines in
  let lines =
    match lines with l :: rest when is_marker "---" l -> rest | _ -> lines
  in
  let rec take acc = function
    | [] -> List.rev acc
    | l :: rest when is_marker "..." l -> (
        match rest with
          | [] -> List.rev acc
          | l :: _ -> error l.lnum "content after the end of the document")
    | l :: _ when is_marker "---" l ->
        error l.lnum "multiple documents are not supported"
    | l :: rest -> take (l :: acc) rest
  in
  Array.of_list (take [] lines)

(* {1 Block structure} *)

let is_dash c = c = "-" || (String.length c >= 2 && c.[0] = '-' && c.[1] = ' ')

(** Position of the [:] separating a key from its value, if any. *)
let key_sep s =
  let n = String.length s in
  let res = ref None in
  let i = ref 0 in
  let depth = ref 0 in
  let quote = ref ' ' in
  while !res = None && !i < n do
    let c = s.[!i] in
    if !quote = '"' then
      if c = '\\' then i := !i + 2
      else (
        if c = '"' then quote := ' ';
        incr i)
    else if !quote = '\'' then (
      if c = '\'' then quote := ' ';
      incr i)
    else (
      (match c with
        | '"' | '\'' -> quote := c
        | '[' | '{' -> incr depth
        | ']' | '}' -> decr depth
        | ':' when !depth = 0 && (!i + 1 >= n || s.[!i + 1] = ' ') ->
            res := Some !i
        | _ -> ());
      incr i)
  done;
  !res

let parse_key lnum k =
  let k = String.trim k in
  if k = "" then error lnum "empty mapping key";
  check_unsupported lnum k;
  let consumed v i =
    skip_blanks k i;
    if !i <> String.length k then error lnum "unexpected content after key";
    v
  in
  match k.[0] with
    | '"' ->
        let i = ref 0 in
        consumed (read_double lnum k i) i
    | '\'' ->
        let i = ref 0 in
        consumed (read_single lnum k i) i
    | '[' | '{' -> error lnum "only scalar mapping keys are supported"
    | _ -> k

(** Parse a value written on a single line. *)
let parse_inline lnum s =
  let s = String.trim s in
  check_unsupported lnum s;
  let consumed v i =
    skip_blanks s i;
    if !i <> String.length s then error lnum "unexpected content after value";
    v
  in
  if s = "" then Null
  else (
    match s.[0] with
      | '[' | '{' ->
          let i = ref 0 in
          consumed (flow_value lnum s i) i
      | '"' ->
          let i = ref 0 in
          consumed (String (read_double lnum s i)) i
      | '\'' ->
          let i = ref 0 in
          consumed (String (read_single lnum s i)) i
      | _ -> resolve s)

let parse lines =
  let len = Array.length lines in
  let pos = ref 0 in
  (* Parse the block of lines at indentation [indent]. *)
  let rec node indent =
    if !pos >= len then Null
    else (
      let l = lines.(!pos) in
      if l.indent < indent then Null
      else if l.indent > indent then error l.lnum "unexpected indentation"
      else if is_dash l.content then seq indent
      else (
        match key_sep l.content with
          | Some _ -> map indent
          | None ->
              incr pos;
              parse_inline l.lnum l.content))
  (* Parse the block nested under a line at indentation [indent]. *)
  and child indent =
    if !pos < len && lines.(!pos).indent > indent then node lines.(!pos).indent
    else Null
  and seq indent =
    let items = ref [] in
    while
      !pos < len && lines.(!pos).indent = indent && is_dash lines.(!pos).content
    do
      let l = lines.(!pos) in
      let c = l.content in
      let n = String.length c in
      let i = ref 1 in
      while !i < n && c.[!i] = ' ' do
        incr i
      done;
      let v =
        if !i >= n then (
          incr pos;
          child indent)
        else (
          (* Reinterpret what follows the dash as a line of its own, which
             handles ["- 5"], ["- a: 1"] with further entries below, and
             ["- - 1"] uniformly. *)
          l.indent <- indent + !i;
          l.content <- String.sub c !i (n - !i);
          node l.indent)
      in
      items := v :: !items
    done;
    List (List.rev !items)
  and map indent =
    let items = ref [] in
    while !pos < len && lines.(!pos).indent = indent do
      let l = lines.(!pos) in
      if is_dash l.content then
        error l.lnum "unexpected sequence entry in a mapping";
      match key_sep l.content with
        | None -> error l.lnum "expected a mapping entry"
        | Some k ->
            let key = parse_key l.lnum (String.sub l.content 0 k) in
            let n = String.length l.content in
            let rest = String.trim (String.sub l.content (k + 1) (n - k - 1)) in
            incr pos;
            let v =
              if rest <> "" then parse_inline l.lnum rest
              else if
                (* A sequence may be written at the indentation of its key. *)
                !pos < len
                && lines.(!pos).indent = indent
                && is_dash lines.(!pos).content
              then seq indent
              else child indent
            in
            items := (key, v) :: !items
    done;
    Assoc (List.rev !items)
  in
  let v = if len = 0 then Null else node lines.(0).indent in
  if !pos < len then error lines.(!pos).lnum "unexpected content";
  v

let of_string s : (t, string) result =
  try Ok (parse (strip_document_markers (scan s)))
  with Parse_error msg -> Error msg

let of_file f =
  let ic = open_in_bin f in
  let s = really_input_string ic (in_channel_length ic) in
  close_in ic;
  of_string s

(* {1 Printing} *)

let float_to_string f =
  if Float.is_nan f then ".nan"
  else if f = Float.infinity then ".inf"
  else if f = Float.neg_infinity then "-.inf"
  else if Float.is_integer f && Float.abs f < 1e16 then Printf.sprintf "%.0f" f
  else (
    let s = Printf.sprintf "%.15g" f in
    if float_of_string s = f then s else Printf.sprintf "%.17g" f)

(** Whether [s] can be printed without quotes and read back identically. *)
let plain_safe s =
  let n = String.length s in
  n > 0
  && (match resolve s with String _ -> true | _ -> false)
  && s <> "---" && s <> "..."
  && (not (is_blank s.[0]))
  && (not (is_blank s.[n - 1]))
  && (match s.[0] with
    | ',' | '[' | ']' | '{' | '}' | '#' | '&' | '*' | '!' | '|' | '>' | '\''
    | '"' | '%' | '@' | '`' ->
        false
    | '-' | '?' | ':' -> n > 1 && s.[1] <> ' '
    | _ -> true)
  &&
  let ok = ref true in
  String.iteri
    (fun i c ->
      if c < ' ' || c = '\127' then ok := false
      else if c = ':' && (i + 1 >= n || s.[i + 1] = ' ') then ok := false
      else if c = '#' && i > 0 && is_blank s.[i - 1] then ok := false)
    s;
  !ok

let quote s =
  let b = Buffer.create (String.length s + 2) in
  Buffer.add_char b '"';
  String.iter
    (fun c ->
      match c with
        | '"' -> Buffer.add_string b "\\\""
        | '\\' -> Buffer.add_string b "\\\\"
        | '\n' -> Buffer.add_string b "\\n"
        | '\t' -> Buffer.add_string b "\\t"
        | '\r' -> Buffer.add_string b "\\r"
        | c when c < ' ' || c = '\127' ->
            Buffer.add_string b (Printf.sprintf "\\x%02x" (Char.code c))
        | c -> Buffer.add_char b c)
    s;
  Buffer.add_char b '"';
  Buffer.contents b

let render_string s = if plain_safe s then s else quote s

let render_scalar v =
  match v with
    | Null -> "null"
    | Bool b -> if b then "true" else "false"
    | Float f -> float_to_string f
    | String s -> render_string s
    | List [] -> "[]"
    | Assoc [] -> "{}"
    | List _ | Assoc _ -> assert false

(** Print [v] at indentation [indent], assuming the prefix of its first line is
    already printed. Always ends with a newline. *)
let rec write b indent v =
  match v with
    | List (_ :: _ as items) ->
        List.iteri
          (fun i x ->
            if i > 0 then Buffer.add_string b (String.make indent ' ');
            Buffer.add_string b "- ";
            write b (indent + 2) x)
          items
    | Assoc (_ :: _ as items) ->
        List.iteri
          (fun i (k, x) ->
            if i > 0 then Buffer.add_string b (String.make indent ' ');
            Buffer.add_string b (render_string k);
            Buffer.add_char b ':';
            match x with
              | List (_ :: _) | Assoc (_ :: _) ->
                  Buffer.add_char b '\n';
                  Buffer.add_string b (String.make (indent + 2) ' ');
                  write b (indent + 2) x
              | _ ->
                  Buffer.add_char b ' ';
                  write b (indent + 2) x)
          items
    | v ->
        Buffer.add_string b (render_scalar v);
        Buffer.add_char b '\n'

let to_string yaml =
  let b = Buffer.create 256 in
  write b 0 yaml;
  Buffer.contents b

let to_file f yaml =
  let oc = open_out_bin f in
  output_string oc (to_string yaml);
  close_out oc