-
Notifications
You must be signed in to change notification settings - Fork 50
/
parse.peg
228 lines (167 loc) · 5.58 KB
/
parse.peg
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package toml
type tomlParser Peg {
toml
}
TOML <- Expression (newline Expression)* newline* !. { _ = buffer }
Expression <- (
<ws table ws comment? (ws newlineRequired wsnl keyval ws comment?)*> { p.SetTableSource(begin, end) }
/ ws keyval ws comment?
/ ws comment?
/ ws
)
val <- (
<datetime> { p.SetTime(begin, end) }
/ <float> { p.SetFloat(begin, end) }
/ <integer> { p.SetInteger(begin, end) }
/ <string> { p.SetString(begin, end) }
/ <boolean> { p.SetBool(begin, end) }
/ <array> { p.SetArray(begin, end) }
/ <inlineTable> { p.SetInlineTableSource(begin, end) }
)
ws <- [ \t]*
wsnl <- ([ \t] / newline)*
comment <- '#' (badControl / [\t -\0x10FFFF])*
newline <- ('\r\n' / '\n') { p.Newline() }
newlineRequired <- (
!newline { p.Error(errNewlineRequired) }
/ newline
)
badControl <- <[\0x7F\0x0B\0xC\0x00-\0x08\0x0E-\0x1F]> {
p.Error(&rawControlError{p.buffer[begin]})
}
# -------------------------------------------------------------------------
# -- Tables
table <- stdTable / arrayTable
stdTable <- '[' ws <tableKey> ws ']' { p.SetTable(p.buffer, begin, end) }
arrayTable <- '[[' ws <tableKey> ws ']]' { p.SetArrayTable(p.buffer, begin, end) }
keyval <- key ws '=' ws val { p.AddKeyValue() }
key <- bareKey / quotedKey
bareKey <- <bareKeyChar+> { p.SetKey(p.buffer, begin, end) }
bareKeyChar <- badControl / [0-9A-Za-z\-_]
quotedKey <- < '"' basicChar* '"' > { p.SetKey(p.buffer, begin, end) }
tableKey <- tableKeyComp (tableKeySep tableKeyComp)*
tableKeyComp <- key { p.AddTableKey() }
tableKeySep <- ws '.' ws
# -------------------------------------------------------------------------
# -- Inline Tables
inlineTable <- (
'{' { p.StartInlineTable() }
ws inlineTableKeyValues? ws
'}' { p.EndInlineTable() }
)
inlineTableKeyValues <- (
keyval
(
ws inlineTableCommaRequired ws
keyval
)*
ws inlineTableCommaForbidden
)
inlineTableCommaForbidden <- (
!','
/ ',' { p.Error(errInlineTableCommaAtEnd) }
)
inlineTableCommaRequired <- (
!',' { p.Error(errInlineTableCommaRequired) }
/ ','
)
# -------------------------------------------------------------------------
# -- Booleans
boolean <- 'true' / 'false'
# -------------------------------------------------------------------------
# -- Numbers
integer <- hexInt / octalInt / binaryInt / decimalInt / ([+\-] decimalInt)
decimalInt <- [1-9] (decimalDigit / '_' decimalDigit)+ / decimalDigit
decimalDigit <- [0-9]
hexInt <- '0x' hexDigit (hexDigit / '_' hexDigit)*
hexDigit <- [[0-9A-F]]
octalInt <- '0o' octalDigit (octalDigit / '_' octalDigit)*
octalDigit <- [0-7]
binaryInt <- '0b' binaryDigit (binaryDigit / '_' octalDigit)*
binaryDigit <- [01]
float <- [+\-]? ('nan' / 'inf' / floatDigits)
floatDigits <- decimalInt (floatFrac floatExp? / floatFrac? floatExp)
floatFrac <- '.' decimalDigit (decimalDigit / '_' decimalDigit)*
floatExp <- [[E]] [\-+]? decimalDigit (decimalDigit / '_' decimalDigit)*
# -------------------------------------------------------------------------
# -- Escape Sequences
escaped <- escape ([btnfr"/\\] / 'u' hexQuad / 'U' hexQuad hexQuad)
escape <- '\\'
hexQuad <- hexDigit hexDigit hexDigit hexDigit
# -------------------------------------------------------------------------
# -- Strings
string <- (
mlLiteralString
/ literalString
/ mlBasicString
/ basicString
)
basicString <- <'"' basicChar* '"'> { p.SetBasicString(p.buffer, begin, end) }
basicChar <- badControl / basicUnescaped / escaped
# This is basically the full printable range, excluding " and \
basicUnescaped <- [\t -!#-\[\]-\0x10FFFF]
mlBasicString <- '"""' mlBasicBody '"""' { p.SetMultilineBasicString() }
mlBasicBody <- (
mlBasicBodyChar*
mlBasicBodyEndQuotes? # needed for strings like """str"""""
)
mlBasicBodyChar <- (
!'"""' '"' { p.AddMultilineBasicQuote() }
/ <basicChar / newline> { p.AddMultilineBasicBody(p.buffer, begin, end) }
/ escape newline wsnl
)
mlBasicBodyEndQuotes <- (
('""' &'"""') { p.AddMultilineBasicQuote(); p.AddMultilineBasicQuote() }
/ ('"' &'"""') { p.AddMultilineBasicQuote() }
)
literalString <- "'" <literalChar*> "'" { p.SetLiteralString(p.buffer, begin, end) }
literalChar <- badControl / [\t -&(-\0x10FFFF]
mlLiteralString <- (
"'''"
<mlLiteralBody> { p.SetMultilineLiteralString(p.buffer, begin, end) }
"'''"
)
mlLiteralBody <- (
(!"'''" (mlLiteralChar / newline))*
mlLiteralBodyEndQuotes? # needed for '''str'''''
)
mlLiteralChar <- badControl / [\t -\0x10FFFF]
mlLiteralBodyEndQuotes <- ("''" &"'''") / ("'" &"'''")
# -------------------------------------------------------------------------
# -- Datetimes
datetime <- (fullDate ([[T ]] fullTime)?) / partialTime
partialTime <- timeHour ':' timeMinute ':' timeSecond timeSecfrac?
fullDate <- dateFullYear '-' dateMonth '-' dateMDay
fullTime <- partialTime timeOffset?
dateFullYear <- digitQuad
dateMonth <- digitDual
dateMDay <- digitDual
timeHour <- digitDual
timeMinute <- digitDual
timeSecond <- digitDual
timeSecfrac <- '.' decimalDigit+
timeNumoffset <- [\-+] timeHour ':' timeMinute
timeOffset <- [[Z]] / timeNumoffset
digitDual <- decimalDigit decimalDigit
digitQuad <- digitDual digitDual
# -------------------------------------------------------------------------
# -- Arrays
array <- (
'[' { p.StartArray() }
wsnl arrayValues? wsnl
']'
)
arrayValues <- (
(wsnl comment)* wsnl
val { p.AddArrayVal() }
(
(wsnl comment)* wsnl
arraySep
(wsnl comment)* wsnl
val { p.AddArrayVal() }
)*
(wsnl comment)* wsnl
arraySep?
(wsnl comment)*
)
arraySep <- ','