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