chiark / gitweb /
b142e6c86cef3ae1f0acaeaffe1aa5233605cfc0
[nailing-cargo.git] / toml.abnf
1 ;; WARNING: This document is a work-in-progress and should not be considered
2 ;; authoritative until further notice.
3
4 ;; This is an attempt to define TOML in ABNF according to the grammar defined
5 ;; in RFC 5234 (http://www.ietf.org/rfc/rfc5234.txt).
6
7 ;; You can try out this grammar using http://instaparse.mojombo.com/
8 ;; To do so, in the lower right, click on Options and change `:input-format` to
9 ;; ':abnf'. Then paste this entire ABNF document into the grammar entry box
10 ;; (above the options). Then you can type or paste a sample TOML document into
11 ;; the beige box on the left. Tada!
12
13 ;; Overall Structure
14
15 toml = expression *( newline expression )
16
17 expression =  ws [ comment ]
18 expression =/ ws keyval ws [ comment ]
19 expression =/ ws table ws [ comment ]
20
21 ;; Whitespace
22
23 ws = *wschar
24 wschar =  %x20  ; Space
25 wschar =/ %x09  ; Horizontal tab
26
27 ;; Newline
28
29 newline =  %x0A     ; LF
30 newline =/ %x0D.0A  ; CRLF
31
32 ;; Comment
33
34 comment-start-symbol = %x23 ; #
35 non-ascii = %x80-D7FF / %xE000-10FFFF
36 non-eol = %x09 / %x20-7F / non-ascii
37
38 comment = comment-start-symbol *non-eol
39
40 ;; Key-Value pairs
41
42 keyval = key keyval-sep val
43
44 key = simple-key / dotted-key
45 simple-key = quoted-key / unquoted-key
46
47 unquoted-key = 1*( ALPHA / DIGIT / %x2D / %x5F ) ; A-Z / a-z / 0-9 / - / _
48 quoted-key = basic-string / literal-string
49 dotted-key = simple-key 1*( dot-sep simple-key )
50
51 dot-sep   = ws %x2E ws  ; . Period
52 keyval-sep = ws %x3D ws ; =
53
54 val = string / boolean / array / inline-table / date-time / float / integer
55
56 ;; String
57
58 string = ml-basic-string / basic-string / ml-literal-string / literal-string
59
60 ;; Basic String
61
62 basic-string = quotation-mark *basic-char quotation-mark
63
64 quotation-mark = %x22            ; "
65
66 basic-char = basic-unescaped / escaped
67 basic-unescaped = wschar / %x21 / %x23-5B / %x5D-7E / non-ascii
68 escaped = escape escape-seq-char
69
70 escape = %x5C                   ; \
71 escape-seq-char =  %x22         ; "    quotation mark  U+0022
72 escape-seq-char =/ %x5C         ; \    reverse solidus U+005C
73 escape-seq-char =/ %x62         ; b    backspace       U+0008
74 escape-seq-char =/ %x66         ; f    form feed       U+000C
75 escape-seq-char =/ %x6E         ; n    line feed       U+000A
76 escape-seq-char =/ %x72         ; r    carriage return U+000D
77 escape-seq-char =/ %x74         ; t    tab             U+0009
78 escape-seq-char =/ %x75 4HEXDIG ; uXXXX                U+XXXX
79 escape-seq-char =/ %x55 8HEXDIG ; UXXXXXXXX            U+XXXXXXXX
80
81 ;; Multiline Basic String
82
83 ml-basic-string = ml-basic-string-delim ml-basic-body ml-basic-string-delim
84 ml-basic-string-delim = 3quotation-mark
85 ml-basic-body = *mlb-content *( mlb-quotes 1*mlb-content ) [ mlb-quotes ]
86
87 mlb-content = mlb-char / newline / mlb-escaped-nl
88 mlb-char = mlb-unescaped / escaped
89 mlb-quotes = 1*2quotation-mark
90 mlb-unescaped = wschar / %x21 / %x23-5B / %x5D-7E / non-ascii
91 mlb-escaped-nl = escape ws newline *( wschar / newline )
92
93 ;; Literal String
94
95 literal-string = apostrophe *literal-char apostrophe
96
97 apostrophe = %x27 ; ' apostrophe
98
99 literal-char = %x09 / %x20-26 / %x28-7E / non-ascii
100
101 ;; Multiline Literal String
102
103 ml-literal-string = ml-literal-string-delim ml-literal-body ml-literal-string-delim
104 ml-literal-string-delim = 3apostrophe
105 ml-literal-body = *mll-content *( mll-quotes 1*mll-content ) [ mll-quotes ]
106
107 mll-content = mll-char / newline
108 mll-char = %x09 / %x20-26 / %x28-7E / non-ascii
109 mll-quotes = 1*2apostrophe
110
111 ;; Integer
112
113 integer = dec-int / hex-int / oct-int / bin-int
114
115 minus = %x2D                       ; -
116 plus = %x2B                        ; +
117 underscore = %x5F                  ; _
118 digit1-9 = %x31-39                 ; 1-9
119 digit0-7 = %x30-37                 ; 0-7
120 digit0-1 = %x30-31                 ; 0-1
121
122 hex-prefix = %x30.78               ; 0x
123 oct-prefix = %x30.6f               ; 0o
124 bin-prefix = %x30.62               ; 0b
125
126 dec-int = [ minus / plus ] unsigned-dec-int
127 unsigned-dec-int = DIGIT / digit1-9 1*( DIGIT / underscore DIGIT )
128
129 hex-int = hex-prefix HEXDIG *( HEXDIG / underscore HEXDIG )
130 oct-int = oct-prefix digit0-7 *( digit0-7 / underscore digit0-7 )
131 bin-int = bin-prefix digit0-1 *( digit0-1 / underscore digit0-1 )
132
133 ;; Float
134
135 float = float-int-part ( exp / frac [ exp ] )
136 float =/ special-float
137
138 float-int-part = dec-int
139 frac = decimal-point zero-prefixable-int
140 decimal-point = %x2E               ; .
141 zero-prefixable-int = DIGIT *( DIGIT / underscore DIGIT )
142
143 exp = "e" float-exp-part
144 float-exp-part = [ minus / plus ] zero-prefixable-int
145
146 special-float = [ minus / plus ] ( inf / nan )
147 inf = %x69.6e.66  ; inf
148 nan = %x6e.61.6e  ; nan
149
150 ;; Boolean
151
152 boolean = true / false
153
154 true    = %x74.72.75.65     ; true
155 false   = %x66.61.6C.73.65  ; false
156
157 ;; Date and Time (as defined in RFC 3339)
158
159 date-time      = offset-date-time / local-date-time / local-date / local-time
160
161 date-fullyear  = 4DIGIT
162 date-month     = 2DIGIT  ; 01-12
163 date-mday      = 2DIGIT  ; 01-28, 01-29, 01-30, 01-31 based on month/year
164 time-delim     = "T" / %x20 ; T, t, or space
165 time-hour      = 2DIGIT  ; 00-23
166 time-minute    = 2DIGIT  ; 00-59
167 time-second    = 2DIGIT  ; 00-58, 00-59, 00-60 based on leap second rules
168 time-secfrac   = "." 1*DIGIT
169 time-numoffset = ( "+" / "-" ) time-hour ":" time-minute
170 time-offset    = "Z" / time-numoffset
171
172 partial-time   = time-hour ":" time-minute ":" time-second [ time-secfrac ]
173 full-date      = date-fullyear "-" date-month "-" date-mday
174 full-time      = partial-time time-offset
175
176 ;; Offset Date-Time
177
178 offset-date-time = full-date time-delim full-time
179
180 ;; Local Date-Time
181
182 local-date-time = full-date time-delim partial-time
183
184 ;; Local Date
185
186 local-date = full-date
187
188 ;; Local Time
189
190 local-time = partial-time
191
192 ;; Array
193
194 array = array-open [ array-values ] ws-comment-newline array-close
195
196 array-open =  %x5B ; [
197 array-close = %x5D ; ]
198
199 array-values =  ws-comment-newline val ws array-sep array-values
200 array-values =/ ws-comment-newline val ws [ array-sep ]
201
202 array-sep = %x2C  ; , Comma
203
204 ws-comment-newline = *( wschar / [ comment ] newline )
205
206 ;; Table
207
208 table = std-table / array-table
209
210 ;; Standard Table
211
212 std-table = std-table-open key std-table-close
213
214 std-table-open  = %x5B ws     ; [ Left square bracket
215 std-table-close = ws %x5D     ; ] Right square bracket
216
217 ;; Inline Table
218
219 inline-table = inline-table-open [ inline-table-keyvals ] inline-table-close
220
221 inline-table-open  = %x7B ws     ; {
222 inline-table-close = ws %x7D     ; }
223 inline-table-sep   = ws %x2C ws  ; , Comma
224
225 inline-table-keyvals = keyval [ inline-table-sep inline-table-keyvals ]
226
227 ;; Array Table
228
229 array-table = array-table-open key array-table-close
230
231 array-table-open  = %x5B.5B ws  ; [[ Double left square bracket
232 array-table-close = ws %x5D.5D  ; ]] Double right square bracket
233
234 ;; Built-in ABNF terms, reproduced here for clarity
235
236 ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
237 DIGIT = %x30-39 ; 0-9
238 HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"