Bump to 1.14.1
[platform/upstream/augeas.git] / lenses / toml.aug
1 (*
2 Module: Toml
3   Parses TOML files
4
5 Author: Raphael Pinson <raphael.pinson@camptocamp.com>
6
7 About: Reference
8   https://toml.io/en/v1.0.0
9
10 About: License
11    This file is licensed under the LGPL v2+, like the rest of Augeas.
12
13 About: Lens Usage
14    To be documented
15
16 About: Configuration files
17    This lens applies to TOML files.
18
19 About: Examples
20    The <Test_Toml> file contains various examples and tests.
21 *)
22
23 module Toml =
24
25 (* Group: base definitions *)
26
27 (* View: comment
28      A simple comment *)
29 let comment  = IniFile.comment "#" "#"
30
31 (* View: empty
32      An empty line *)
33 let empty = Util.empty_dos
34
35 (* View: eol
36      An end of line *)
37 let eol = Util.doseol
38
39
40 (* Group: value entries *)
41
42 let bare_re_noquot = (/[^][", \t\r\n]/ - "#")
43 let bare_re = (/[^][,\r=]/ - "#")+
44 let no_quot = /[^]["\r\n]*/
45 let bare = Quote.do_dquote_opt_nil (store (bare_re_noquot . (bare_re* . bare_re_noquot)?))
46 let quoted = Quote.do_dquote (store (/[^"]/ . "#"* . /[^"]/))
47
48 let ws = del /[ \t\n]*/ ""
49
50 let space_or_empty = [ del /[ \t\n]+/ " " ]
51
52 let comma = Util.del_str "," . (space_or_empty | comment)?
53 let lbrace = Util.del_str "{" . (space_or_empty | comment)?
54 let rbrace = Util.del_str "}"
55 let lbrack = Util.del_str "[" . (space_or_empty | comment)?
56 let rbrack = Util.del_str "]"
57
58 (* This follows the definition of 'string' at https://www.json.org/
59    It's a little wider than what's allowed there as it would accept
60    nonsensical \u escapes *)
61 let triple_dquote = Util.del_str "\"\"\""
62 let str_store = Quote.dquote . store /([^\\"]|\\\\["\/bfnrtu\\])*/ . Quote.dquote
63
64 let str_store_multi = triple_dquote . eol
65                   . store /([^\\"]|\\\\["\/bfnrtu\\])*/
66                   . del /\n[ \t]*/ "\n" . triple_dquote
67
68 let str_store_literal = Quote.squote . store /([^\\']|\\\\['\/bfnrtu\\])*/ . Quote.squote
69
70 let integer =
71      let base10 = /[+-]?[0-9_]+/
72   in let hex = /0x[A-Za-z0-9]+/
73   in let oct = /0o[0-7]+/
74   in let bin = /0b[01]+/
75   in [ label "integer" . store (base10 | hex | oct | bin) ]
76
77 let float =
78      let n = /[0-9_]+/
79   in let pm = /[+-]?/
80   in let z = pm . n
81   in let decim = "." . n
82   in let exp = /[Ee]/ . z
83   in let num = z . decim | z . exp | z . decim . exp
84   in let inf = pm . "inf"
85   in let nan = pm . "nan"
86   in [ label "float" . store (num | inf | nan) ]
87
88 let str = [ label "string" . str_store ]
89
90 let str_multi = [ label "string_multi" . str_store_multi ]
91
92 let str_literal = [ label "string_literal" . str_store_literal ]
93
94 let bool (r:regexp) = [ label "bool" . store r ]
95
96
97 let date_re = /[0-9]{4}-[0-9]{2}-[0-9]{2}/
98 let time_re = /[0-9]{1,2}:[0-9]{2}:[0-9]{2}(\.[0-9]+)?[A-Z]*/
99
100 let datetime = [ label "datetime" . store (date_re . /[T ]/ . time_re) ]
101 let date = [ label "date" . store date_re ]
102 let time = [ label "time" . store time_re ]
103
104 let norec = str | str_multi | str_literal
105           | integer | float | bool /true|false/
106           | datetime | date |  time
107
108 let array (value:lens) = [ label "array" . lbrack
109                . ( ( Build.opt_list value comma . (del /,?/ "") . (space_or_empty | comment)? . rbrack )
110                    | rbrack ) ]
111
112 let array_norec = array norec
113
114 (* This is actually no real recursive array, instead it is one or two dimensional
115    For more info on this see https://github.com/hercules-team/augeas/issues/715 *)
116 let array_rec = array (norec | array_norec)
117
118 let entry_base (value:lens) = [ label "entry" . store Rx.word . Sep.space_equal . value ]
119
120 let inline_table (value:lens) = [ label "inline_table" . lbrace
121                    . ( (Build.opt_list (entry_base value) comma . space_or_empty? . rbrace)
122                       | rbrace ) ]
123
124 let entry = [ label "entry" . Util.indent . store Rx.word . Sep.space_equal
125             . (norec | array_rec | inline_table (norec|array_norec)) . (eol | comment) ]
126
127 (* Group: tables *)
128
129 (* View: table_gen
130      A generic table *)
131 let table_gen (name:string) (lbrack:string) (rbrack:string) =
132      let title = Util.indent . label name
133                . Util.del_str lbrack
134                . store /[^]\r\n.]+(\.[^]\r\n.]+)*/
135                . Util.del_str rbrack . eol
136   in [ title . (entry|empty|comment)* ]
137
138 (* View: table
139      A table or array of tables *)
140 let table = table_gen "table" "[" "]"
141           | table_gen "@table" "[[" "]]"
142
143 (* Group: lens *)
144
145 (* View: lns
146      The Toml lens *)
147 let lns = (entry | empty | comment)* . table*