-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTokens.lua
359 lines (288 loc) · 6.63 KB
/
Tokens.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
local class = require("class")
local AnyType = require("typecheck.AnyType")
local ArrayType = require("typecheck.ArrayType")
local ClassType = require("typecheck.ClassType")
local CType = require("typecheck.CType")
local Type = require("typecheck.Type")
local UnionType = require("typecheck.UnionType")
local parse_error = "unexpected token '%s' at position %s"
local function get_token_error(token)
if not token then
return "token expected"
end
return parse_error:format(token.value, token.pos)
end
local function get_type(name)
if name == "any" then
return AnyType()
end
if name:find("^ffi%.") or name:find("^love%.") then
return CType(name)
end
if name:find("%.") then
return ClassType(name)
end
return Type(name)
end
--------------------------------------------------------------------------------
---@class typecheck.Tokens
---@field pos integer
---@field stack number[]
---@field [number] typecheck.Token
---@operator call: typecheck.Tokens
local Tokens = class()
function Tokens:new()
self.stack = {}
self.pos = 1
end
function Tokens:get_token_error()
return get_token_error(self.token)
end
function Tokens:_push()
table.insert(self.stack, self.pos)
end
function Tokens:_pop(save)
if save then
table.remove(self.stack)
return
end
local token = self.token
self.pos = table.remove(self.stack)
self.token = self[self.pos]
return token
end
function Tokens:step()
self.pos = self.pos + 1
self.token = self[self.pos]
end
function Tokens:parse_func_name()
if not self.token or self.token.type ~= "id" then
return nil, get_token_error(self.token)
end
self:_push()
local name = self.token.value
self:step()
if not self.token or (self.token.type ~= "colon" and self.token.type ~= "point") then
self:_pop(true)
return name
end
local out = {name, self.token.value}
local is_method = false
if self.token.type == "colon" then
is_method = true
end
self:step()
if self.token.type ~= "id" then
return nil, get_token_error(self:_pop())
end
out[3] = self.token.value
self:step()
self:_pop(true)
return table.concat(out), is_method
end
function Tokens:parse_name_novararg()
if not self.token or self.token.type ~= "id" then
return nil, get_token_error(self.token)
end
self:_push()
local name = ""
local next_type = "id"
while self.token and self.token.type == next_type do
name = name .. self.token.value
self:step()
next_type = next_type ~= "point" and "point" or "id"
end
if next_type == "id" then
return nil, get_token_error(self:_pop())
end
self:_pop(true)
return name
end
function Tokens:parse_name()
if not self.token then
return nil, get_token_error()
end
local token = self.token
if token.type == "vararg" then
self:step()
return token.value
end
return self:parse_name_novararg()
end
function Tokens:parse_type()
if not self.token then
return nil, get_token_error()
end
self:_push()
local array_depth = 0
while self.token and self.token.type == "array" do
array_depth = array_depth + 1
self:step()
end
local _type
if self.token.type == "leftparan" then
self:step()
_type = self:parse_type_union()
if not _type or not self.token or self.token.type ~= "rightparan" then
return nil, get_token_error(self:_pop())
end
self:step()
else
local t, err = self:parse_name_novararg()
if not t then
self:_pop()
return nil, err
end
while self.token and self.token.type == "asterisk" do
t = t .. self.token.value
self:step()
end
_type = get_type(t)
end
while self.token and self.token.type == "array" do
array_depth = array_depth + 1
self:step()
end
for _ = 1, array_depth do
_type = ArrayType(_type)
end
self:_pop(true)
return _type
end
function Tokens:parse_type_union()
if not self.token then
return nil, get_token_error()
end
self:_push()
local union = UnionType()
local final_err
local step = "token"
while self.pos <= #self do
if step == "pipe" and self.token.type == "pipe" then
self:step()
step = "token"
elseif step == "token" then
local _type, err = self:parse_type()
if not _type then
final_err = err
break
end
table.insert(union, _type)
step = "pipe"
else
break
end
end
if #union == 0 then
local t = self:_pop()
return nil, final_err or get_token_error(t)
end
if self.token and self.token.type == "question" then
union.is_optional = true
self:step()
end
self:_pop(true)
if #union == 1 and not union.is_optional then
return union[1]
end
return union
end
function Tokens:parse_types()
if not self.token then
return nil, get_token_error()
end
self:_push()
local types = {}
local step = "type"
while self.pos <= #self do
local token = self.token
if step == "comma" and token.type == "comma" then
self:step()
step = "type"
elseif step == "comma" and token.type == "vararg" then
types.is_vararg = true
self:step()
break
elseif step == "type" then
local _type, err = self:parse_type_union()
if not _type then
self:_pop()
return nil, err
end
table.insert(types, _type)
step = "comma"
else
break
end
end
self:_pop(true)
return types
end
function Tokens:parse_param()
if not self.token then
return nil, get_token_error()
end
self:_push()
local param_name, err = self:parse_name()
if not param_name then
self:_pop()
return nil, err
end
if self.token.type ~= "colon" then
return nil, get_token_error(self:_pop())
end
self:step()
local param_type, err = self:parse_type_union()
if not param_type then
self:_pop()
return nil, err
end
self:_pop(true)
return param_name, param_type, param_name == "..."
end
function Tokens:parse_params()
if not self.token then
return nil, get_token_error()
end
self:_push()
if self.token.type ~= "leftparan" then
return nil, get_token_error(self:_pop())
end
self:step()
local param_names = {}
local param_types = {}
local expect_rightparan = false
local step = "param"
while self.pos <= #self do
local token = self.token
if expect_rightparan and token.type ~= "rightparan" then
return nil, get_token_error(self:_pop())
end
if token.type == "rightparan" then
self:step()
break
end
if step == "comma" and token.type == "comma" then
self:step()
step = "param"
elseif step == "param" then
local param_name, param_type, is_vararg = self:parse_param()
if not param_name then
self:_pop()
return nil, param_type
end
table.insert(param_names, param_name)
table.insert(param_types, param_type)
if is_vararg then
param_types.is_vararg = is_vararg
expect_rightparan = true
end
step = "comma"
else
return nil, get_token_error(self:_pop())
end
end
self:_pop(true)
return param_names, param_types
end
return Tokens