parse.peg 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, end) }
  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 <- tableKeyComp (tableKeySep tableKeyComp)*
  42. tableKeyComp <- key { p.AddTableKey() }
  43. tableKeySep <- ws '.' ws
  44. inlineTableValSep <- ws ',' ws
  45. integer <- [\-+]? int
  46. int <- [1-9] (digit / '_' digit)+ / digit
  47. float <- integer (frac exp? / frac? exp)
  48. frac <- '.' digit (digit / '_' digit)*
  49. exp <- [eE] [\-+]? digit (digit / '_' digit)*
  50. string <- (
  51. mlLiteralString
  52. / literalString
  53. / mlBasicString
  54. / basicString
  55. )
  56. basicString <- <'"' basicChar* '"'> { p.SetBasicString(p.buffer, begin, end) }
  57. basicChar <- basicUnescaped / escaped
  58. escaped <- escape ([btnfr"/\\] / 'u' hexQuad / 'U' hexQuad hexQuad)
  59. basicUnescaped <- [ -!#-\[\]-\0x10FFFF]
  60. escape <- '\\'
  61. mlBasicString <- '"""' mlBasicBody '"""' { p.SetMultilineString() }
  62. mlBasicBody <- (
  63. <basicChar / newline> { p.AddMultilineBasicBody(p.buffer, begin, end) }
  64. / escape newline wsnl
  65. )*
  66. literalString <- "'" <literalChar*> "'" { p.SetLiteralString(p.buffer, begin, end) }
  67. literalChar <- [\t -&(-\0x10FFFF]
  68. mlLiteralString <- "'''" <mlLiteralBody> "'''" { p.SetMultilineLiteralString(p.buffer, begin, end) }
  69. mlLiteralBody <- (!"'''" (mlLiteralChar / newline))*
  70. mlLiteralChar <- [\t -\0x10FFFF]
  71. hexdigit <- [0-9A-Fa-f]
  72. hexQuad <- hexdigit hexdigit hexdigit hexdigit
  73. boolean <- 'true' / 'false'
  74. dateFullYear <- digitQuad
  75. dateMonth <- digitDual
  76. dateMDay <- digitDual
  77. timeHour <- digitDual
  78. timeMinute <- digitDual
  79. timeSecond <- digitDual
  80. timeSecfrac <- '.' digit+
  81. timeNumoffset <- [\-+] timeHour ':' timeMinute
  82. timeOffset <- 'Z' / timeNumoffset
  83. partialTime <- timeHour ':' timeMinute ':' timeSecond timeSecfrac?
  84. fullDate <- dateFullYear '-' dateMonth '-' dateMDay
  85. fullTime <- partialTime timeOffset?
  86. datetime <- (fullDate ('T' fullTime)?) / partialTime
  87. digit <- [0-9]
  88. digitDual <- digit digit
  89. digitQuad <- digitDual digitDual
  90. array <- (
  91. '[' { p.StartArray() }
  92. wsnl arrayValues? wsnl
  93. ']'
  94. )
  95. arrayValues <- (
  96. val { p.AddArrayVal() }
  97. (
  98. wsnl comment?
  99. wsnl arraySep
  100. wsnl comment?
  101. wsnl val { p.AddArrayVal() }
  102. )*
  103. wsnl arraySep?
  104. wsnl comment?
  105. )
  106. arraySep <- ','