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
include Types
open Rresult
open R.Infix
module Stream = Stream
open Stream
let scalar ?anchor ?tag ?(plain_implicit=true) ?(quoted_implicit=false)
?(style=`Plain) value =
{ anchor; tag; plain_implicit; quoted_implicit; style; value }
let yaml_scalar_to_json t =
match t with
| "null" | "NULL" | "" | "Null" | "~" -> `Null
| "y"|"Y"|"yes"|"Yes"|"YES"
| "true"|"True"|"TRUE"
| "on"|"On"|"ON" -> `Bool true
| "n"|"N"|"no"|"No"|"NO"
| "false"|"False"|"FALSE"
| "off"|"Off"|"OFF" -> `Bool false
| "-.inf" -> `Float neg_infinity
| ".inf" -> `Float infinity
| ".nan"|".NaN"|".NAN" -> `Float nan
| s -> (try `Float (float_of_string s) with _ -> `String s)
let to_json v =
let rec fn = function
| `Scalar {value; quoted_implicit=true} -> `String value
| `Scalar {value} -> yaml_scalar_to_json value
| `Alias _ -> failwith "Anchors are not supported when serialising to JSON"
| `A l -> `A (List.map fn l)
| `O l -> `O (List.map (fun ({anchor;value},v) -> value, (fn v)) l)
in
match fn v with
| r -> Ok r
| exception (Failure msg) -> R.error_msg msg
let of_json (v:value) =
let rec fn = function
| `Null -> `Scalar (scalar "")
| `Bool b -> `Scalar (scalar (string_of_bool b))
| `Float f -> `Scalar (scalar (string_of_float f))
| `String value -> `Scalar (scalar value)
| `A l -> `A (List.map fn l)
| `O l -> `O (List.map (fun (k,v) -> scalar k, (fn v)) l)
in match fn v with
| r -> Ok r
| exception (Failure msg) -> R.error_msg msg
let to_string ?len ?(encoding=`Utf8) ?scalar_style ?layout_style (v:value) =
emitter ?len () >>= fun t ->
stream_start t encoding >>= fun () ->
document_start t >>= fun () ->
let rec iter = function
|`Null -> Stream.scalar (scalar "") t
|`String s -> Stream.scalar (scalar ?style:scalar_style s) t
|`Float s -> Stream.scalar (scalar (Printf.sprintf "%.16g" s)) t
|`Bool s -> Stream.scalar (scalar (string_of_bool s)) t
|`A l ->
sequence_start ?style:layout_style t >>= fun () ->
let rec fn = function
| [] -> sequence_end t
| hd::tl -> iter hd >>= fun () -> fn tl
in fn l
|`O l ->
mapping_start ?style:layout_style t >>= fun () ->
let rec fn = function
| [] -> mapping_end t
| (k,v)::tl -> iter (`String k) >>= fun () -> iter v >>= fun () -> fn tl
in fn l
in
iter v >>= fun () ->
document_end t >>= fun () ->
stream_end t >>= fun () ->
let r = Stream.emitter_buf t in
Ok (Bytes.to_string r)
let to_string_exn ?len ?encoding ?scalar_style ?layout_style s =
match to_string ?len ?encoding ?scalar_style ?layout_style s with
| Ok s -> s
| Error (`Msg m) -> raise (Invalid_argument m)
let yaml_to_string ?(encoding=`Utf8) ?scalar_style ?layout_style v =
emitter () >>= fun t ->
stream_start t encoding >>= fun () ->
document_start t >>= fun () ->
let rec iter = function
|`Scalar s -> Stream.scalar s t
|`Alias anchor -> alias t anchor
|`A l ->
sequence_start ?style:layout_style t >>= fun () ->
let rec fn = function
| [] -> sequence_end t
| hd::tl -> iter hd >>= fun () -> fn tl
in fn l
|`O l ->
mapping_start ?style:layout_style t >>= fun () ->
let rec fn = function
| [] -> mapping_end t
| (k,v)::tl -> iter (`Scalar k) >>= fun () -> iter v >>= fun () -> fn tl
in fn l
in
iter v >>= fun () ->
document_end t >>= fun () ->
stream_end t >>= fun () ->
let r = Stream.emitter_buf t in
Ok (Bytes.to_string r)
let yaml_of_string s =
let open Event in
parser s >>= fun t ->
let next () =
do_parse t >>= fun (e, pos) ->
Logs.debug (fun l -> l "event %s\n%!" (sexp_of_t e |> Sexplib.Sexp.to_string_hum));
Ok (e,pos) in
next () >>= fun (e,pos) ->
match e with
| Stream_start _ -> begin
next () >>= fun (e,pos) ->
match e with
| Document_start _ -> begin
let rec parse_v (e,pos) =
match e with
| Sequence_start _ ->
next () >>=
parse_seq [] >>= fun s ->
Ok (`A s)
| Scalar scalar -> Ok (`Scalar scalar)
| Alias {anchor} -> Ok (`Alias anchor)
| Mapping_start _ ->
next () >>=
parse_map [] >>= fun s ->
Ok (`O s)
| e -> R.error_msg (Fmt.strf "todo %s (%s)" (sexp_of_t e |> Sexplib.Sexp.to_string_hum) (sexp_of_pos pos |> Sexplib.Sexp.to_string_hum))
and parse_seq acc (e,pos) =
match e with
| Sequence_end -> Ok (List.rev acc)
| e ->
parse_v (e,pos) >>= fun v ->
next () >>=
parse_seq (v :: acc)
and parse_map acc (e,pos) =
match e with
| Mapping_end -> Ok (List.rev acc)
| e -> begin
parse_v (e,pos) >>= fun v ->
begin match v with
| `Scalar k ->
next () >>=
parse_v >>= fun v ->
next () >>=
parse_map ((k,v)::acc)
| _ -> R.error_msg (Fmt.strf "only string keys are supported (%s)" (sexp_of_pos pos |> Sexplib.Sexp.to_string_hum))
end
end
in
next () >>=
parse_v
end
| Stream_end -> Ok (`Scalar (scalar ""))
| e -> R.error_msg (Fmt.strf "Not document start: %s" (sexp_of_t e |> Sexplib.Sexp.to_string_hum))
end
| _ -> R.error_msg "Not stream start"
let of_string s = yaml_of_string s >>= to_json
let of_string_exn s =
match of_string s with
| Ok s -> s
| Error (`Msg m) -> raise (Invalid_argument m)
let pp ppf s =
match to_string s with
| Ok s -> Format.pp_print_string ppf s
| Error (`Msg m) -> Format.pp_print_string ppf (Printf.sprintf "(error (%s))" m)
let rec equal v1 v2 =
match v1, v2 with
| `Null, `Null -> true
| `Bool x1, `Bool x2 -> ((=) : bool -> bool -> bool) x1 x2
| `Float x1, `Float x2 -> ((=) : float -> float -> bool) x1 x2
| `String x1, `String x2 -> String.equal x1 x2
| `A xs1, `A xs2 -> List.for_all2 equal xs1 xs2
| `O xs1, `O xs2 ->
List.for_all2 (fun (k1, v1) (k2, v2) ->
String.equal k1 k2 && equal v1 v2) xs1 xs2
| _ -> false