parse.peg 3.3 KB
Newer Older
H
hongming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
package toml

type tomlParser Peg {
    toml
}

TOML <- Expression (newline Expression)* newline? !. { _ = buffer }

Expression <- (
    <ws table ws comment? (wsnl keyval ws comment?)*> { p.SetTableString(begin, end) }
  / ws keyval ws comment?
  / ws comment?
  / ws
)

newline <- <[\r\n]+> { p.AddLineCount(end - begin) }

ws <- [ \t]*
wsnl <- (
    [ \t]
  / <[\r\n]> { p.AddLineCount(end - begin) }
)*

comment <- '#' <[\t -\0x10FFFF]*>

keyval <- key ws '=' ws val { p.AddKeyValue() }

key <- bareKey / quotedKey

bareKey <- <[0-9A-Za-z\-_]+> { p.SetKey(p.buffer, begin, end) }

quotedKey <- < '"' basicChar* '"' > { p.SetKey(p.buffer, begin, end) }

val <- (
    <datetime>    { p.SetTime(begin, end) }
  / <float>       { p.SetFloat64(begin, end) }
  / <integer>     { p.SetInt64(begin, end) }
  / <string>      { p.SetString(begin, end) }
  / <boolean>     { p.SetBool(begin, end) }
  / <array>       { p.SetArray(begin, end) }
  / inlineTable
)

table <- stdTable / arrayTable

stdTable <- '[' ws <tableKey> ws ']' { p.SetTable(p.buffer, begin, end) }

arrayTable <- '[[' ws <tableKey> ws ']]' { p.SetArrayTable(p.buffer, begin, end) }

inlineTable <- (
    '{' { p.StartInlineTable() }
    ws inlineTableKeyValues ws
    '}' { p.EndInlineTable() }
)

inlineTableKeyValues <- (keyval inlineTableValSep?)*

tableKey <- tableKeyComp (tableKeySep tableKeyComp)*

tableKeyComp <- key { p.AddTableKey() }

tableKeySep <- ws '.' ws

inlineTableValSep <- ws ',' ws

integer <- [\-+]? int
int <- [1-9] (digit / '_' digit)+ / digit

float <- integer (frac exp? / frac? exp)
frac <- '.' digit (digit / '_' digit)*
exp <- [eE] [\-+]? digit (digit / '_' digit)*

string <- (
    mlLiteralString
  / literalString
  / mlBasicString
  / basicString
)

basicString <- <'"' basicChar* '"'> { p.SetBasicString(p.buffer, begin, end) }

basicChar <- basicUnescaped / escaped
escaped <- escape ([btnfr"/\\] / 'u' hexQuad / 'U' hexQuad hexQuad)

basicUnescaped <- [ -!#-\[\]-\0x10FFFF]

escape <- '\\'

mlBasicString <- '"""' mlBasicBody '"""' { p.SetMultilineString() }

mlBasicBody <- (
    <basicChar / newline> { p.AddMultilineBasicBody(p.buffer, begin, end) }
  / escape newline wsnl
)*

literalString <- "'" <literalChar*> "'" { p.SetLiteralString(p.buffer, begin, end) }

literalChar <- [\t -&(-\0x10FFFF]

mlLiteralString <- "'''" <mlLiteralBody> "'''" { p.SetMultilineLiteralString(p.buffer, begin, end) }

mlLiteralBody <- (!"'''" (mlLiteralChar / newline))*

mlLiteralChar <- [\t -\0x10FFFF]

hexdigit <- [0-9A-Fa-f]
hexQuad <- hexdigit hexdigit hexdigit hexdigit

boolean <- 'true' / 'false'

dateFullYear <- digitQuad
dateMonth <- digitDual
dateMDay <- digitDual
timeHour <- digitDual
timeMinute <- digitDual
timeSecond <- digitDual
timeSecfrac <- '.' digit+
timeNumoffset <- [\-+] timeHour ':' timeMinute
timeOffset <- 'Z' / timeNumoffset
partialTime <- timeHour ':' timeMinute ':' timeSecond timeSecfrac?
fullDate <- dateFullYear '-' dateMonth '-' dateMDay
fullTime <- partialTime timeOffset
datetime <- (fullDate ('T' fullTime)?) / partialTime

digit <- [0-9]
digitDual <- digit digit
digitQuad <- digitDual digitDual

array <- (
    '[' { p.StartArray() }
    wsnl arrayValues? wsnl
    ']'
)

arrayValues <- (
    val { p.AddArrayVal() }
    (
        wsnl comment?
        wsnl arraySep
        wsnl comment?
        wsnl val { p.AddArrayVal() }
    )*
    wsnl arraySep?
    wsnl comment?
)

arraySep <- ','