Imported Upstream version 1.7.0
[platform/upstream/augeas.git] / lenses / json.aug
1 module Json =
2
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]+)?/                      *)
13
14
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
18
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 "]"
25
26 let str_store = Quote.dquote . store /([^\\\\"]|\\\\("|n|r|t|\\\\))*/ . Quote.dquote  (* " Emacs, relax *)
27
28 let number = [ label "number" . store /-?[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?/
29              . comments ]
30 let str = [ label "string" . str_store . comments ]
31
32 let const (r:regexp) = [ label "const" . store r . comments ]
33
34 let fix_value (value:lens) =
35      let array = [ label "array" . lbrack
36                . ( ( Build.opt_list value comma . rbrack . comments )
37                    | (rbrack . ws) ) ]
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 )
41                  | (rbrace . ws ) ) ]
42   in (str | number | obj | array | const /true|false|null/)
43
44 (* Process arbitrarily deeply nested JSON objects *)
45 let rec rlns = fix_value rlns
46
47 let lns = comments . rlns