Bump to 1.14.1
[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 (* This follows the definition of 'string' at https://www.json.org/
27    It's a little wider than what's allowed there as it would accept
28    nonsensical \u escapes *)
29 let str_store = Quote.dquote . store /([^\\"]|\\\\["\/bfnrtu\\])*/ . Quote.dquote
30
31 let number = [ label "number" . store /-?[0-9]+(\.[0-9]+)?([eE][+-]?[0-9]+)?/
32              . comments ]
33 let str = [ label "string" . str_store . comments ]
34
35 let const (r:regexp) = [ label "const" . store r . comments ]
36
37 let fix_value (value:lens) =
38      let array = [ label "array" . lbrack
39                . ( ( Build.opt_list value comma . rbrack . comments )
40                    | (rbrack . ws) ) ]
41   in let pair = [ label "entry" . str_store . ws . colon . value ]
42   in let obj = [ label "dict" . lbrace
43              . ( ( Build.opt_list pair comma. rbrace . comments )
44                  | (rbrace . ws ) ) ]
45   in (str | number | obj | array | const /true|false|null/)
46
47 (* Process arbitrarily deeply nested JSON objects *)
48 let rec rlns = fix_value rlns
49
50 let lns = comments . rlns