|
Packit Service |
a2ae7a |
module Json =
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* A generic lens for Json files *)
|
|
Packit Service |
a2ae7a |
(* Based on the following grammar from http://www.json.org/ *)
|
|
Packit Service |
a2ae7a |
(* Object ::= '{'Members ? '}' *)
|
|
Packit Service |
a2ae7a |
(* Members ::= Pair+ *)
|
|
Packit Service |
a2ae7a |
(* Pair ::= String ':' Value *)
|
|
Packit Service |
a2ae7a |
(* Array ::= '[' Elements ']' *)
|
|
Packit Service |
a2ae7a |
(* Elements ::= Value ( "," Value )* *)
|
|
Packit Service |
a2ae7a |
(* Value ::= String | Number | Object | Array | "true" | "false" | "null" *)
|
|
Packit Service |
a2ae7a |
(* String ::= "\"" Char* "\"" *)
|
|
Packit Service |
a2ae7a |
(* Number ::= /-?[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?/ *)
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
let ws = del /[ \t\n]*/ ""
|
|
Packit Service |
a2ae7a |
let comment = Util.empty_c_style | Util.comment_c_style | Util.comment_multiline
|
|
Packit Service |
a2ae7a |
let comments = comment* . Sep.opt_space
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
let comma = Util.del_str "," . comments
|
|
Packit Service |
a2ae7a |
let colon = Util.del_str ":" . comments
|
|
Packit Service |
a2ae7a |
let lbrace = Util.del_str "{" . comments
|
|
Packit Service |
a2ae7a |
let rbrace = Util.del_str "}"
|
|
Packit Service |
a2ae7a |
let lbrack = Util.del_str "[" . comments
|
|
Packit Service |
a2ae7a |
let rbrack = Util.del_str "]"
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* This follows the definition of 'string' at https://www.json.org/
|
|
Packit Service |
a2ae7a |
It's a little wider than what's allowed there as it would accept
|
|
Packit Service |
a2ae7a |
nonsensical \u escapes *)
|
|
Packit Service |
a2ae7a |
let str_store = Quote.dquote . store /([^\\"]|\\\\["\/bfnrtu\\])*/ . Quote.dquote
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
let number = [ label "number" . store /-?[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?/
|
|
Packit Service |
a2ae7a |
. comments ]
|
|
Packit Service |
a2ae7a |
let str = [ label "string" . str_store . comments ]
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
let const (r:regexp) = [ label "const" . store r . comments ]
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
let fix_value (value:lens) =
|
|
Packit Service |
a2ae7a |
let array = [ label "array" . lbrack
|
|
Packit Service |
a2ae7a |
. ( ( Build.opt_list value comma . rbrack . comments )
|
|
Packit Service |
a2ae7a |
| (rbrack . ws) ) ]
|
|
Packit Service |
a2ae7a |
in let pair = [ label "entry" . str_store . ws . colon . value ]
|
|
Packit Service |
a2ae7a |
in let obj = [ label "dict" . lbrace
|
|
Packit Service |
a2ae7a |
. ( ( Build.opt_list pair comma. rbrace . comments )
|
|
Packit Service |
a2ae7a |
| (rbrace . ws ) ) ]
|
|
Packit Service |
a2ae7a |
in (str | number | obj | array | const /true|false|null/)
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
(* Process arbitrarily deeply nested JSON objects *)
|
|
Packit Service |
a2ae7a |
let rec rlns = fix_value rlns
|
|
Packit Service |
a2ae7a |
|
|
Packit Service |
a2ae7a |
let lns = comments . rlns
|