toml.abnf 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. ;; It is possible to try this grammar interactively, using instaparse.
  8. ;; http://instaparse.mojombo.com/
  9. ;;
  10. ;; To do so, in the lower right, click on Options and change `:input-format` to
  11. ;; ':abnf'. Then paste this entire ABNF document into the grammar entry box
  12. ;; (above the options). Then you can type or paste a sample TOML document into
  13. ;; the beige box on the left. Tada!
  14. ;; Overall Structure
  15. toml = expression *( newline expression )
  16. expression = ws [ comment ]
  17. expression =/ ws keyval ws [ comment ]
  18. expression =/ ws table ws [ comment ]
  19. ;; Whitespace
  20. ws = *wschar
  21. wschar = %x20 ; Space
  22. wschar =/ %x09 ; Horizontal tab
  23. ;; Newline
  24. newline = %x0A ; LF
  25. newline =/ %x0D.0A ; CRLF
  26. ;; Comment
  27. comment-start-symbol = %x23 ; #
  28. non-ascii = %x80-D7FF / %xE000-10FFFF
  29. non-eol = %x09 / %x20-7F / non-ascii
  30. comment = comment-start-symbol *non-eol
  31. ;; Key-Value pairs
  32. keyval = key keyval-sep val
  33. key = simple-key / dotted-key
  34. simple-key = quoted-key / unquoted-key
  35. unquoted-key = 1*( ALPHA / DIGIT / %x2D / %x5F ) ; A-Z / a-z / 0-9 / - / _
  36. quoted-key = basic-string / literal-string
  37. dotted-key = simple-key 1*( dot-sep simple-key )
  38. dot-sep = ws %x2E ws ; . Period
  39. keyval-sep = ws %x3D ws ; =
  40. val = string / boolean / array / inline-table / date-time / float / integer
  41. ;; String
  42. string = ml-basic-string / basic-string / ml-literal-string / literal-string
  43. ;; Basic String
  44. basic-string = quotation-mark *basic-char quotation-mark
  45. quotation-mark = %x22 ; "
  46. basic-char = basic-unescaped / escaped
  47. basic-unescaped = wschar / %x21 / %x23-5B / %x5D-7E / non-ascii
  48. escaped = escape escape-seq-char
  49. escape = %x5C ; \
  50. escape-seq-char = %x22 ; " quotation mark U+0022
  51. escape-seq-char =/ %x5C ; \ reverse solidus U+005C
  52. escape-seq-char =/ %x62 ; b backspace U+0008
  53. escape-seq-char =/ %x66 ; f form feed U+000C
  54. escape-seq-char =/ %x6E ; n line feed U+000A
  55. escape-seq-char =/ %x72 ; r carriage return U+000D
  56. escape-seq-char =/ %x74 ; t tab U+0009
  57. escape-seq-char =/ %x75 4HEXDIG ; uXXXX U+XXXX
  58. escape-seq-char =/ %x55 8HEXDIG ; UXXXXXXXX U+XXXXXXXX
  59. ;; Multiline Basic String
  60. ml-basic-string = ml-basic-string-delim [ newline ] ml-basic-body
  61. ml-basic-string-delim
  62. ml-basic-string-delim = 3quotation-mark
  63. ml-basic-body = *mlb-content *( mlb-quotes 1*mlb-content ) [ mlb-quotes ]
  64. mlb-content = mlb-char / newline / mlb-escaped-nl
  65. mlb-char = mlb-unescaped / escaped
  66. mlb-quotes = 1*2quotation-mark
  67. mlb-unescaped = wschar / %x21 / %x23-5B / %x5D-7E / non-ascii
  68. mlb-escaped-nl = escape ws newline *( wschar / newline )
  69. ;; Literal String
  70. literal-string = apostrophe *literal-char apostrophe
  71. apostrophe = %x27 ; ' apostrophe
  72. literal-char = %x09 / %x20-26 / %x28-7E / non-ascii
  73. ;; Multiline Literal String
  74. ml-literal-string = ml-literal-string-delim [ newline ] ml-literal-body
  75. ml-literal-string-delim
  76. ml-literal-string-delim = 3apostrophe
  77. ml-literal-body = *mll-content *( mll-quotes 1*mll-content ) [ mll-quotes ]
  78. mll-content = mll-char / newline
  79. mll-char = %x09 / %x20-26 / %x28-7E / non-ascii
  80. mll-quotes = 1*2apostrophe
  81. ;; Integer
  82. integer = dec-int / hex-int / oct-int / bin-int
  83. minus = %x2D ; -
  84. plus = %x2B ; +
  85. underscore = %x5F ; _
  86. digit1-9 = %x31-39 ; 1-9
  87. digit0-7 = %x30-37 ; 0-7
  88. digit0-1 = %x30-31 ; 0-1
  89. hex-prefix = %x30.78 ; 0x
  90. oct-prefix = %x30.6F ; 0o
  91. bin-prefix = %x30.62 ; 0b
  92. dec-int = [ minus / plus ] unsigned-dec-int
  93. unsigned-dec-int = DIGIT / digit1-9 1*( DIGIT / underscore DIGIT )
  94. hex-int = hex-prefix HEXDIG *( HEXDIG / underscore HEXDIG )
  95. oct-int = oct-prefix digit0-7 *( digit0-7 / underscore digit0-7 )
  96. bin-int = bin-prefix digit0-1 *( digit0-1 / underscore digit0-1 )
  97. ;; Float
  98. float = float-int-part ( exp / frac [ exp ] )
  99. float =/ special-float
  100. float-int-part = dec-int
  101. frac = decimal-point zero-prefixable-int
  102. decimal-point = %x2E ; .
  103. zero-prefixable-int = DIGIT *( DIGIT / underscore DIGIT )
  104. exp = "e" float-exp-part
  105. float-exp-part = [ minus / plus ] zero-prefixable-int
  106. special-float = [ minus / plus ] ( inf / nan )
  107. inf = %x69.6e.66 ; inf
  108. nan = %x6e.61.6e ; nan
  109. ;; Boolean
  110. boolean = true / false
  111. true = %x74.72.75.65 ; true
  112. false = %x66.61.6C.73.65 ; false
  113. ;; Date and Time (as defined in RFC 3339)
  114. date-time = offset-date-time / local-date-time / local-date / local-time
  115. date-fullyear = 4DIGIT
  116. date-month = 2DIGIT ; 01-12
  117. date-mday = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on month/year
  118. time-delim = "T" / %x20 ; T, t, or space
  119. time-hour = 2DIGIT ; 00-23
  120. time-minute = 2DIGIT ; 00-59
  121. time-second = 2DIGIT ; 00-58, 00-59, 00-60 based on leap second rules
  122. time-secfrac = "." 1*DIGIT
  123. time-numoffset = ( "+" / "-" ) time-hour ":" time-minute
  124. time-offset = "Z" / time-numoffset
  125. partial-time = time-hour ":" time-minute ":" time-second [ time-secfrac ]
  126. full-date = date-fullyear "-" date-month "-" date-mday
  127. full-time = partial-time time-offset
  128. ;; Offset Date-Time
  129. offset-date-time = full-date time-delim full-time
  130. ;; Local Date-Time
  131. local-date-time = full-date time-delim partial-time
  132. ;; Local Date
  133. local-date = full-date
  134. ;; Local Time
  135. local-time = partial-time
  136. ;; Array
  137. array = array-open [ array-values ] ws-comment-newline array-close
  138. array-open = %x5B ; [
  139. array-close = %x5D ; ]
  140. array-values = ws-comment-newline val ws-comment-newline array-sep array-values
  141. array-values =/ ws-comment-newline val ws-comment-newline [ array-sep ]
  142. array-sep = %x2C ; , Comma
  143. ws-comment-newline = *( wschar / [ comment ] newline )
  144. ;; Table
  145. table = std-table / array-table
  146. ;; Standard Table
  147. std-table = std-table-open key std-table-close
  148. std-table-open = %x5B ws ; [ Left square bracket
  149. std-table-close = ws %x5D ; ] Right square bracket
  150. ;; Inline Table
  151. inline-table = inline-table-open [ inline-table-keyvals ] inline-table-close
  152. inline-table-open = %x7B ws ; {
  153. inline-table-close = ws %x7D ; }
  154. inline-table-sep = ws %x2C ws ; , Comma
  155. inline-table-keyvals = keyval [ inline-table-sep inline-table-keyvals ]
  156. ;; Array Table
  157. array-table = array-table-open key array-table-close
  158. array-table-open = %x5B.5B ws ; [[ Double left square bracket
  159. array-table-close = ws %x5D.5D ; ]] Double right square bracket
  160. ;; Built-in ABNF terms, reproduced here for clarity
  161. ALPHA = %x41-5A / %x61-7A ; A-Z / a-z
  162. DIGIT = %x30-39 ; 0-9
  163. HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"