3 (* A generic lens for Json files *)
4 (* Based on the following grammar from http://www.json.org/ *)
5 (* Object ::= '{'Members ? '}' *)
6 (* Members ::= Pair+ *)
7 (* Pair ::= String ':' Value *)
8 (* Array ::= '[' Elements ']' *)
9 (* Elements ::= Value ( "," Value )* *)
10 (* Value ::= String | Number | Object | Array | "true" | "false" | "null" *)
11 (* String ::= "\"" Char* "\"" *)
12 (* Number ::= /-?[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?/ *)
15 let ws = del /[ \t\n]*/ ""
16 let comment = Util.empty_c_style | Util.comment_c_style | Util.comment_multiline
17 let comments = comment* . Sep.opt_space
19 let comma = Util.del_str "," . comments
20 let colon = Util.del_str ":" . comments
21 let lbrace = Util.del_str "{" . comments
22 let rbrace = Util.del_str "}"
23 let lbrack = Util.del_str "[" . comments
24 let rbrack = Util.del_str "]"
26 let str_store = Quote.dquote . store /([^\\\\"]|\\\\("|n|r|t|\\\\))*/ . Quote.dquote (* " Emacs, relax *)
28 let number = [ label "number" . store /-?[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?/
30 let str = [ label "string" . str_store . comments ]
32 let const (r:regexp) = [ label "const" . store r . comments ]
34 let fix_value (value:lens) =
35 let array = [ label "array" . lbrack
36 . ( ( Build.opt_list value comma . rbrack . comments )
38 in let pair = [ label "entry" . str_store . ws . colon . value ]
39 in let obj = [ label "dict" . lbrace
40 . ( ( Build.opt_list pair comma. rbrace . comments )
42 in (str | number | obj | array | const /true|false|null/)
44 (* Process arbitrarily deeply nested JSON objects *)
45 let rec rlns = fix_value rlns
47 let lns = comments . rlns