;; 该文档描述了 TOML 的语法，使用的是 ABNF 格式（定义于 RFC 5234——https://www.ietf.org/rfc/rfc5234.txt）。
;;
;; 所有有效的 TOML 文档都符合该描述，然而根据辅助性文本说明中描述的语义需要拒绝某些错误的文档。

;; 可以通过 instaparse 交互式地尝试该语法。
;;     http://instaparse.mojombo.com/
;;
;; 要做到这一点，在右下角，点击 Options 并将 `:input-format` 改为 ':abnf'。
;; 然后将整个 ABNF 文档粘贴到语法输入框（在 Options 上方）。
;; 然后您可以将一份 TOML 文档小样输入或粘贴到左侧米黄色的框中。
;; Tada！

;; 总结构

toml = expression *( newline expression )

expression =  ws [ comment ]
expression =/ ws keyval ws [ comment ]
expression =/ ws table ws [ comment ]

;; 空白

ws = *wschar
wschar =  %x20  ; 空格
wschar =/ %x09  ; 水平制表符

;; 换行

newline =  %x0A     ; LF
newline =/ %x0D.0A  ; CRLF

;; 注释

comment-start-symbol = %x23 ; #
non-ascii = %x80-D7FF / %xE000-10FFFF
non-eol = %x09 / %x20-7E / non-ascii

comment = comment-start-symbol *non-eol

;; 键值对

keyval = key keyval-sep val

key = simple-key / dotted-key
simple-key = quoted-key / unquoted-key

unquoted-key = 1*( ALPHA / DIGIT / %x2D / %x5F ) ; A-Z / a-z / 0-9 / - / _
quoted-key = basic-string / literal-string
dotted-key = simple-key 1*( dot-sep simple-key )

dot-sep   = ws %x2E ws  ; . 点
keyval-sep = ws %x3D ws ; =

val = string / boolean / array / inline-table / date-time / float / integer

;; 字符串

string = ml-basic-string / basic-string / ml-literal-string / literal-string

;; 基础字符串

basic-string = quotation-mark *basic-char quotation-mark

quotation-mark = %x22            ; "

basic-char = basic-unescaped / escaped
basic-unescaped = wschar / %x21 / %x23-5B / %x5D-7E / non-ascii
escaped = escape escape-seq-char

escape = %x5C                   ; \
escape-seq-char =  %x22         ; "    双引号          U+0022
escape-seq-char =/ %x5C         ; \    反斜杠          U+005C
escape-seq-char =/ %x62         ; b    退格符          U+0008
escape-seq-char =/ %x66         ; f    换页符          U+000C
escape-seq-char =/ %x6E         ; n    换行符          U+000A
escape-seq-char =/ %x72         ; r    回车符          U+000D
escape-seq-char =/ %x74         ; t    制表符          U+0009
escape-seq-char =/ %x75 4HEXDIG ; uXXXX                U+XXXX
escape-seq-char =/ %x55 8HEXDIG ; UXXXXXXXX            U+XXXXXXXX

;; 多行基础字符串

ml-basic-string = ml-basic-string-delim [ newline ] ml-basic-body
                  ml-basic-string-delim
ml-basic-string-delim = 3quotation-mark
ml-basic-body = *mlb-content *( mlb-quotes 1*mlb-content ) [ mlb-quotes ]

mlb-content = mlb-char / newline / mlb-escaped-nl
mlb-char = mlb-unescaped / escaped
mlb-quotes = 1*2quotation-mark
mlb-unescaped = wschar / %x21 / %x23-5B / %x5D-7E / non-ascii
mlb-escaped-nl = escape ws newline *( wschar / newline )

;; 字面量字符串

literal-string = apostrophe *literal-char apostrophe

apostrophe = %x27 ; ' 单引号

literal-char = %x09 / %x20-26 / %x28-7E / non-ascii

;; 多行字面量字符串

ml-literal-string = ml-literal-string-delim [ newline ] ml-literal-body
                    ml-literal-string-delim
ml-literal-string-delim = 3apostrophe
ml-literal-body = *mll-content *( mll-quotes 1*mll-content ) [ mll-quotes ]

mll-content = mll-char / newline
mll-char = %x09 / %x20-26 / %x28-7E / non-ascii
mll-quotes = 1*2apostrophe

;; 整数

integer = dec-int / hex-int / oct-int / bin-int

minus = %x2D                       ; -
plus = %x2B                        ; +
underscore = %x5F                  ; _
digit1-9 = %x31-39                 ; 1-9
digit0-7 = %x30-37                 ; 0-7
digit0-1 = %x30-31                 ; 0-1

hex-prefix = %x30.78               ; 0x
oct-prefix = %x30.6F               ; 0o
bin-prefix = %x30.62               ; 0b

dec-int = [ minus / plus ] unsigned-dec-int
unsigned-dec-int = DIGIT / digit1-9 1*( DIGIT / underscore DIGIT )

hex-int = hex-prefix HEXDIG *( HEXDIG / underscore HEXDIG )
oct-int = oct-prefix digit0-7 *( digit0-7 / underscore digit0-7 )
bin-int = bin-prefix digit0-1 *( digit0-1 / underscore digit0-1 )

;; 浮点数

float = float-int-part ( exp / frac [ exp ] )
float =/ special-float

float-int-part = dec-int
frac = decimal-point zero-prefixable-int
decimal-point = %x2E               ; .
zero-prefixable-int = DIGIT *( DIGIT / underscore DIGIT )

exp = "e" float-exp-part
float-exp-part = [ minus / plus ] zero-prefixable-int

special-float = [ minus / plus ] ( inf / nan )
inf = %x69.6e.66  ; inf
nan = %x6e.61.6e  ; nan

;; 布尔值

boolean = true / false

true    = %x74.72.75.65     ; true
false   = %x66.61.6C.73.65  ; false

;; 日期和时刻（按照 RFC 3339 中的定义）

date-time      = offset-date-time / local-date-time / local-date / local-time

date-fullyear  = 4DIGIT
date-month     = 2DIGIT  ; 01-12
date-mday      = 2DIGIT  ; 01-28，01-29，01-30，01-31 基于月/年
time-delim     = "T" / %x20 ; T，t，或空格
time-hour      = 2DIGIT  ; 00-23
time-minute    = 2DIGIT  ; 00-59
time-second    = 2DIGIT  ; 00-58，00-59，00-60 基于闰秒规则
time-secfrac   = "." 1*DIGIT
time-numoffset = ( "+" / "-" ) time-hour ":" time-minute
time-offset    = "Z" / time-numoffset

partial-time   = time-hour ":" time-minute ":" time-second [ time-secfrac ]
full-date      = date-fullyear "-" date-month "-" date-mday
full-time      = partial-time time-offset

;; 坐标日期时刻

offset-date-time = full-date time-delim full-time

;; 各地日期时刻

local-date-time = full-date time-delim partial-time

;; 各地日期

local-date = full-date

;; 各地时刻

local-time = partial-time

;; 数组

array = array-open [ array-values ] ws-comment-newline array-close

array-open =  %x5B ; [
array-close = %x5D ; ]

array-values =  ws-comment-newline val ws-comment-newline array-sep array-values
array-values =/ ws-comment-newline val ws-comment-newline [ array-sep ]

array-sep = %x2C  ; , 逗号

ws-comment-newline = *( wschar / [ comment ] newline )

;; 表

table = std-table / array-table

;; 标准表

std-table = std-table-open key std-table-close

std-table-open  = %x5B ws     ; [ 左方括号
std-table-close = ws %x5D     ; ] 右方括号

;; 行内表

inline-table = inline-table-open [ inline-table-keyvals ] inline-table-close

inline-table-open  = %x7B ws     ; {
inline-table-close = ws %x7D     ; }
inline-table-sep   = ws %x2C ws  ; , 逗号

inline-table-keyvals = keyval [ inline-table-sep inline-table-keyvals ]

;; 数组表

array-table = array-table-open key array-table-close

array-table-open  = %x5B.5B ws  ; [[ 双左方括号
array-table-close = ws %x5D.5D  ; ]] 双右方括号

;; ABNF 的内置术语，为清楚起见重新陈列于此

ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
DIGIT = %x30-39 ; 0-9
HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
