parse.peg 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package toml
  2. type tomlParser Peg {
  3. toml
  4. }
  5. TOML <- Expression (newline Expression)* newline? !. { _ = buffer }
  6. Expression <- (
  7. <ws table ws comment? (wsnl keyval ws comment?)*> { p.SetTableString(begin, end) }
  8. / ws keyval ws comment?
  9. / ws comment?
  10. / ws
  11. )
  12. newline <- <[\r\n]+> { p.AddLineCount(end - begin) }
  13. ws <- [ \t]*
  14. wsnl <- (
  15. [ \t]
  16. / <[\r\n]> { p.AddLineCount(end - begin) }
  17. )*
  18. comment <- '#' <[\t -\0x10FFFF]*>
  19. keyval <- key ws '=' ws val { p.AddKeyValue() }
  20. key <- bareKey / quotedKey
  21. bareKey <- <[0-9A-Za-z\-_]+> { p.SetKey(p.buffer, begin, end) }
  22. quotedKey <- '"' <basicChar+> '"' { p.SetKey(p.buffer, begin-1, end+1) }
  23. val <- (
  24. <datetime> { p.SetTime(begin, end) }
  25. / <float> { p.SetFloat64(begin, end) }
  26. / <integer> { p.SetInt64(begin, end) }
  27. / <string> { p.SetString(begin, end) }
  28. / <boolean> { p.SetBool(begin, end) }
  29. / <array> { p.SetArray(begin, end) }
  30. / inlineTable
  31. )
  32. table <- stdTable / arrayTable
  33. stdTable <- '[' ws <tableKey> ws ']' { p.SetTable(p.buffer, begin, end) }
  34. arrayTable <- '[[' ws <tableKey> ws ']]' { p.SetArrayTable(p.buffer, begin, end) }
  35. inlineTable <- (
  36. '{' { p.StartInlineTable() }
  37. ws inlineTableKeyValues ws
  38. '}' { p.EndInlineTable() }
  39. )
  40. inlineTableKeyValues <- (keyval inlineTableValSep?)*
  41. tableKey <- key (tableKeySep key)*
  42. tableKeySep <- ws '.' ws
  43. inlineTableValSep <- ws ',' ws
  44. integer <- [\-+]? int
  45. int <- [1-9] (digit / '_' digit)+ / digit
  46. float <- integer (frac exp? / frac? exp)
  47. frac <- '.' digit (digit / '_' digit)*
  48. exp <- [eE] [\-+]? digit (digit / '_' digit)*
  49. string <- (
  50. mlLiteralString
  51. / literalString
  52. / mlBasicString
  53. / basicString
  54. )
  55. basicString <- <'"' basicChar* '"'> { p.SetBasicString(p.buffer, begin, end) }
  56. basicChar <- basicUnescaped / escaped
  57. escaped <- escape ([btnfr"/\\] / 'u' hexQuad / 'U' hexQuad hexQuad)
  58. basicUnescaped <- [ -!#-\[\]-\0x10FFFF]
  59. escape <- '\\'
  60. mlBasicString <- '"""' mlBasicBody '"""' { p.SetMultilineString() }
  61. mlBasicBody <- (
  62. <basicChar / newline> { p.AddMultilineBasicBody(p.buffer, begin, end) }
  63. / escape newline wsnl
  64. )*
  65. literalString <- "'" <literalChar*> "'" { p.SetLiteralString(p.buffer, begin, end) }
  66. literalChar <- [\t -&(-\0x10FFFF]
  67. mlLiteralString <- "'''" <mlLiteralBody> "'''" { p.SetMultilineLiteralString(p.buffer, begin, end) }
  68. mlLiteralBody <- (!"'''" (mlLiteralChar / newline))*
  69. mlLiteralChar <- [\t -\0x10FFFF]
  70. hexdigit <- [0-9A-Fa-f]
  71. hexQuad <- hexdigit hexdigit hexdigit hexdigit
  72. boolean <- 'true' / 'false'
  73. dateFullYear <- digitQuad
  74. dateMonth <- digitDual
  75. dateMDay <- digitDual
  76. timeHour <- digitDual
  77. timeMinute <- digitDual
  78. timeSecond <- digitDual
  79. timeSecfrac <- '.' digit+
  80. timeNumoffset <- [\-+] timeHour ':' timeMinute
  81. timeOffset <- 'Z' / timeNumoffset
  82. partialTime <- timeHour ':' timeMinute ':' timeSecond timeSecfrac?
  83. fullDate <- dateFullYear '-' dateMonth '-' dateMDay
  84. fullTime <- partialTime timeOffset
  85. datetime <- (fullDate ('T' fullTime)?) / partialTime
  86. digit <- [0-9]
  87. digitDual <- digit digit
  88. digitQuad <- digitDual digitDual
  89. array <- (
  90. '[' { p.StartArray() }
  91. wsnl arrayValues? wsnl
  92. ']'
  93. )
  94. arrayValues <- (
  95. val { p.AddArrayVal() }
  96. (
  97. wsnl comment?
  98. wsnl arraySep
  99. wsnl comment?
  100. wsnl val { p.AddArrayVal() }
  101. )*
  102. wsnl arraySep?
  103. wsnl comment?
  104. )
  105. arraySep <- ','