/
parse.lua
318 lines (268 loc) · 7.7 KB
/
parse.lua
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
local lpeg = require 'lpeg'
local P, R, S, V = lpeg.P, lpeg.R, lpeg.S, lpeg.V
local C, Ct, Cmt, Cg, Cc, Cf, Cmt = lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg, lpeg.Cc, lpeg.Cf, lpeg.Cmt
local line
local lastLinePos
local function pack(...)
return { n = select('#', ...), ... }
end
-- Utility
local ws = Cmt(S(' \t\r\n') ^ 0, function(str, pos)
str = str:sub(lastLinePos, pos)
while str:find('\n') do
line = line + 1
lastLinePos = pos
str = str:sub(str:find('\n') + 1)
end
return true
end)
local comma = P(',') ^ 0
local _ = V
local function maybe(pattern)
if type(pattern) == 'string' then pattern = V(pattern) end
return pattern ^ -1
end
local function list(pattern, min)
if type(pattern) == 'string' then pattern = V(pattern) end
min = min or 0
return Ct((pattern * ws * comma * ws) ^ min)
end
-- Formatters
local function simpleValue(key)
return function(value)
return {
kind = key,
value = value
}
end
end
local cName = simpleValue('name')
local cInt = simpleValue('int')
local cFloat = simpleValue('float')
local cBoolean = simpleValue('boolean')
local cEnum = simpleValue('enum')
local cString = function(value)
return {
kind = 'string',
value = value:gsub('\\"', '"')
}
end
local function cList(value)
return {
kind = 'list',
values = value
}
end
local function cObjectField(name, value)
return {
name = name,
value = value
}
end
local function cObject(fields)
return {
kind = 'inputObject',
values = fields
}
end
local function cAlias(name)
return {
kind = 'alias',
name = name
}
end
local function cArgument(name, value)
return {
kind = 'argument',
name = name,
value = value
}
end
local function cField(...)
local tokens = pack(...)
local field = { kind = 'field' }
for i = 1, #tokens do
local key = tokens[i].kind
if not key then
if tokens[i][1].kind == 'argument' then
key = 'arguments'
elseif tokens[i][1].kind == 'directive' then
key = 'directives'
end
end
field[key] = tokens[i]
end
return field
end
local function cSelectionSet(selections)
return {
kind = 'selectionSet',
selections = selections
}
end
local function cFragmentSpread(name, directives)
return {
kind = 'fragmentSpread',
name = name,
directives = directives
}
end
local function cOperation(...)
local args = pack(...)
if args[1].kind == 'selectionSet' then
return {
kind = 'operation',
operation = 'query',
selectionSet = args[1]
}
else
local result = {
kind = 'operation',
operation = args[1]
}
for i = 2, #args do
local key = args[i].kind
if not key then
if args[i][1].kind == 'variableDefinition' then
key = 'variableDefinitions'
elseif args[i][1].kind == 'directive' then
key = 'directives'
end
end
result[key] = args[i]
end
return result
end
end
local function cDocument(definitions)
return {
kind = 'document',
definitions = definitions
}
end
local function cFragmentDefinition(name, typeCondition, selectionSet)
return {
kind = 'fragmentDefinition',
name = name,
typeCondition = typeCondition,
selectionSet = selectionSet
}
end
local function cNamedType(name)
return {
kind = 'namedType',
name = name
}
end
local function cListType(type)
return {
kind = 'listType',
type = type
}
end
local function cNonNullType(type)
return {
kind = 'nonNullType',
type = type
}
end
local function cInlineFragment(...)
local args = pack(...)
local result = { kind = 'inlineFragment' }
result.selectionSet = args[#args]
for i = 1, #args - 1 do
if args[i].kind == 'namedType' or args[i].kind == 'listType' or args[i].kind == 'nonNullType' then
result.typeCondition = args[i]
elseif args[i][1] and args[i][1].kind == 'directive' then
result.directives = args[i]
end
end
return result
end
local function cVariable(name)
return {
kind = 'variable',
name = name
}
end
local function cVariableDefinition(variable, type, defaultValue)
return {
kind = 'variableDefinition',
variable = variable,
type = type,
defaultValue = defaultValue
}
end
local function cDirective(name, arguments)
return {
kind = 'directive',
name = name,
arguments = arguments
}
end
-- Simple types
local rawName = (P'_' + R('az', 'AZ')) * (P'_' + R'09' + R('az', 'AZ')) ^ 0
local name = rawName / cName
local fragmentName = (rawName - ('on' * -rawName)) / cName
local alias = ws * name * P':' * ws / cAlias
local integerPart = P'-' ^ -1 * ('0' + R'19' * R'09' ^ 0)
local intValue = integerPart / cInt
local fractionalPart = '.' * R'09' ^ 1
local exponentialPart = S'Ee' * S'+-' ^ -1 * R'09' ^ 1
local floatValue = integerPart * ((fractionalPart * exponentialPart) + fractionalPart + exponentialPart) / cFloat
local booleanValue = (P'true' + P'false') / cBoolean
local stringValue = P'"' * C((P'\\"' + 1 - S'"\n') ^ 0) * P'"' / cString
local enumValue = (rawName - 'true' - 'false' - 'null') / cEnum
local variable = ws * '$' * name / cVariable
-- Grammar
local graphQL = P {
'document',
document = ws * list('definition') / cDocument * -1,
definition = _'operation' + _'fragmentDefinition',
operationType = C(P'query' + P'mutation'),
operation = (_'operationType' * ws * maybe(name) * maybe('variableDefinitions') * maybe('directives') * _'selectionSet' + _'selectionSet') / cOperation,
fragmentDefinition = 'fragment' * ws * fragmentName * ws * _'typeCondition' * ws * _'selectionSet' / cFragmentDefinition,
selectionSet = ws * '{' * ws * list('selection') * ws * '}' / cSelectionSet,
selection = ws * (_'field' + _'fragmentSpread' + _'inlineFragment'),
field = ws * maybe(alias) * name * maybe('arguments') * maybe('directives') * maybe('selectionSet') / cField,
fragmentSpread = ws * '...' * ws * fragmentName * maybe('directives') / cFragmentSpread,
inlineFragment = ws * '...' * ws * maybe('typeCondition') * maybe('directives') * _'selectionSet' / cInlineFragment,
typeCondition = 'on' * ws * _'namedType',
argument = ws * name * ':' * _'value' / cArgument,
arguments = '(' * list('argument', 1) * ')',
directive = '@' * name * maybe('arguments') / cDirective,
directives = ws * list('directive', 1) * ws,
variableDefinition = ws * variable * ws * ':' * ws * _'type' * (ws * '=' * _'value') ^ -1 * comma * ws / cVariableDefinition,
variableDefinitions = ws * '(' * list('variableDefinition', 1) * ')',
value = ws * (variable + _'objectValue' + _'listValue' + enumValue + stringValue + booleanValue + floatValue + intValue),
listValue = '[' * list('value') * ']' / cList,
objectFieldValue = ws * C(rawName) * ws * ':' * ws * _'value' * comma / cObjectField,
objectValue = '{' * ws * list('objectFieldValue') * ws * '}' / cObject,
type = _'nonNullType' + _'listType' + _'namedType',
namedType = name / cNamedType,
listType = '[' * ws * _'type' * ws * ']' / cListType,
nonNullType = (_'namedType' + _'listType') * '!' / cNonNullType
}
-- TODO doesn't handle quotes that immediately follow escaped backslashes.
local function stripComments(str)
return (str .. '\n'):gsub('(.-\n)', function(line)
local index = 1
while line:find('#', index) do
local pos = line:find('#', index) - 1
local chunk = line:sub(1, pos)
local _, quotes = chunk:gsub('([^\\]")', '')
if quotes % 2 == 0 then
return chunk .. '\n'
else
index = pos + 2
end
end
return line
end):sub(1, -2)
end
return function(str)
assert(type(str) == 'string', 'parser expects a string')
str = stripComments(str)
line, lastLinePos = 1, 1
return graphQL:match(str) or error('Syntax error near line ' .. line, 2)
end