-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTyper.lua
448 lines (356 loc) · 10.9 KB
/
Typer.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
-- Powerful, light-weight type checker
-- @documentation https://rostrap.github.io/Libraries/Debugging/Typer/
-- @author Validark
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Resources = require(ReplicatedStorage:WaitForChild("Resources"))
local Table = Resources:LoadLibrary("Table")
local BuiltInTypes = {
Nil = "nil";
Bool = "boolean";
Boolean = "boolean";
Number = "number";
String = "string";
Userdata = "userdata";
Function = "function";
Thread = "thread";
Table = "table";
}
local CustomTypes = {
Any = function()
return true
end;
Array = function(Array, Type, Callback, Castable)
if Type ~= "table" then
return false
end
local Size = #Array
local Bool = false
for Key, Value in next, Array do
Bool = true
if type(Key) ~= "number" or Key % 1 ~= 0 or Key < 1 or Key > Size then
return false
elseif Callback then
local Success = Callback(Value)
if Success then
if Castable then
Array[Key] = Success
end
else
return false
end
end
end
return Bool
end;
Dictionary = function(Dictionary, Type, Callback, Castable)
if Type ~= "table" then
return false
end
local Bool = false
for Key, Value in next, Dictionary do
Bool = true
if type(Key) == "number" then
return false
elseif Callback then
local Success = Callback(Value)
if Success then
if Castable then
Dictionary[Key] = Success
end
else
return false
end
end
end
return Bool
end;
Table = function(Table, Type, Callback, Castable)
if Type ~= "table" then
return false
end
if Callback then
local Bool = false
for Key, Value in next, Table do
Bool = true
local Success = Callback(Value)
if Success then
if Castable then
Table[Key] = Success
end
else
return false
end
end
return Bool
else
return true
end
end;
EmptyTable = function(Value, Type)
return Type ~= "table" or next(Value) == nil
end;
NonNil = function(Value)
return Value ~= nil
end;
Integer = function(Value, Type)
return Type == "number" and Value % 1 == 0
end;
PositiveInteger = function(Value, Type)
return Type == "number" and Value > 0 and Value % 1 == 0
end;
NegativeInteger = function(Value, Type)
return Type == "number" and Value < 0 and Value % 1 == 0
end;
NonPositiveInteger = function(Value, Type)
return Type == "number" and Value <= 0 and Value % 1 == 0
end;
NonNegativeInteger = function(Value, Type)
return Type == "number" and Value >= 0 and Value % 1 == 0
end;
PositiveNumber = function(Value, Type)
return Type == "number" and Value > 0
end;
NegativeNumber = function(Value, Type)
return Type == "number" and Value < 0
end;
NonPositiveNumber = function(Value, Type)
return Type == "number" and Value <= 0
end;
NonNegativeNumber = function(Value, Type)
return Type == "number" and Value >= 0
end;
Truthy = function(Value)
return Value and true or false
end;
Falsy = function(Value)
return not Value
end;
Enum = function(_, Type)
return Type == "Enum" or Type == "EnumItem"
end;
EnumType = function(_, Type) -- For whatever reason, typeof() returns "Enum" for EnumItems
return Type == "Enum"
end;
True = function(Value)
return Value == true
end;
False = function(Value)
return Value == false
end;
}
local function TransformTableCheckerData(PotentialTypes)
-- [0] is the Expectation string
-- Array in the form {"number", "string", "nil"} where each value is a string matchable by typeof()
-- Key-Value pairs in the form {[string Name] = function}
if not PotentialTypes[0] then -- It was already transformed if written to 0, no haxing pls
local Expectation = ": expected "
PotentialTypes[0] = Expectation
for Name in next, PotentialTypes do
local NameType = type(Name)
if NameType == "string" then
Expectation = Expectation .. Name .. " or "
elseif NameType ~= "number" then
Resources:LoadLibrary("Debug").Error("Key-Value pairs should be in the form [string Name] = function, got %s", Name)
end
end
local i = 0
local AmountPotentialTypes = #PotentialTypes
while i < AmountPotentialTypes do
i = i + 1
local PotentialType = PotentialTypes[i]
if type(PotentialType) ~= "string" then
Resources:LoadLibrary("Debug").Error("PotentialTypes in the array section must be strings in the form {\"number\", \"string\", \"nil\"}")
end
Expectation = Expectation .. PotentialType .. " or "
local TypeCheck = CustomTypes[PotentialType]
if TypeCheck then
table.remove(PotentialTypes, i)
i = i - 1
AmountPotentialTypes = AmountPotentialTypes - 1
PotentialTypes[PotentialType] = TypeCheck
end
end
PotentialTypes[0] = Expectation:sub(1, -5)
end
return PotentialTypes
end
local function Check(PotentialTypes, Parameter, ArgumentNumOrName)
local TypeOf = typeof(Parameter)
for i = 1, #PotentialTypes do
if PotentialTypes[i] == TypeOf then
return Parameter or true
end
end
for Key, CheckFunction in next, PotentialTypes do
if type(Key) == "string" then
local Success = CheckFunction(Parameter, TypeOf)
if Success then
return Key:find("^Enum") and Success or Parameter or true
end
end
end
local ArgumentNumberType = type(ArgumentNumOrName)
return false, "bad argument"
.. (ArgumentNumOrName and (ArgumentNumberType == "number" and " #" .. ArgumentNumOrName or ArgumentNumberType == "string" and " to " .. ArgumentNumOrName) or "")
.. PotentialTypes[0] .. ", got " .. Resources:LoadLibrary("Debug").Inspect(Parameter)
end
local Typer = {}
local CallToCheck = {__call = Check}
setmetatable(Typer, {
__index = function(self, index)
local t = {}
self[index] = t
for i in (index .. "Or"):gmatch("(%w-)Or") do -- Not the prettiest, but hey, we got parsing baby!
if i:find("^Optional") then
i = i:sub(9)
t[1] = "nil"
end
if i:find("^InstanceOfClass") then
local ClassName = i:sub(16)
t["Instance of class " .. ClassName] = function(Value, Type)
return Type == "Instance" and Value.ClassName == ClassName
end
elseif i:find("^InstanceWhichIsA") then
local ClassName = i:sub(17)
t["Instance which is a " .. ClassName] = function(Value, Type)
return Type == "Instance" and Value:IsA(ClassName)
end
elseif i:find("^EnumOfType") then
i = i:sub(11)
local Castables = {}
local Enumerators = Enum[i]:GetEnumItems()
for a = 1, #Enumerators do
local Enumerator = Enumerators[a]
Castables[Enumerator] = Enumerator
Castables[Enumerator.Name] = Enumerator
Castables[Enumerator.Value] = Enumerator
end
t["Enum of type " .. i] = function(Value)
return Castables[Value] or false
end
elseif i:find("^EnumerationOfType") then
i = i:sub(18)
local EnumerationType = Resources:LoadLibrary("Enumeration")[i]
t["Enumeration of type " .. i] = function(Value)
return EnumerationType:Cast(Value)
end
elseif i:find("^ArrayOf%a+s$") then
i = i:match("^ArrayOf(%a+)s$")
local ArrayType = Typer[i]
local Function = CustomTypes.Array
local Castable = i:find("^Enum") and true or false
t["Array of " .. ArrayType[0]:sub(12):gsub("%S+", "%1s", 1)] = function(Value, Type)
return Function(Value, Type, ArrayType, Castable)
end
elseif i:find("^DictionaryOf%a+s$") then
i = i:match("^DictionaryOf(%a+)s$")
local DictionaryType = Typer[i]
local Function = CustomTypes.Dictionary
local Castable = i:find("^Enum") and true or false
t["Dictionary of " .. DictionaryType[0]:sub(12):gsub("%S+", "%1s", 1)] = function(Value, Type)
return Function(Value, Type, DictionaryType, Castable)
end
elseif i:find("^TableOf%a+s$") then
i = i:match("^TableOf(%a+)s$")
local TableType = Typer[i]
local Function = CustomTypes.Table
local Castable = i:find("^Enum") and true or false
t["Table of " .. TableType[0]:sub(12):gsub("%S+", "%1s", 1)] = function(Value, Type)
return Function(Value, Type, TableType, Castable)
end
else
t[#t + 1] = BuiltInTypes[i] or i
end
end
return setmetatable(TransformTableCheckerData(t), CallToCheck)
end;
})
function Typer.AssignSignature(...)
local FirstValueToCheckOffset = 0
local StackSignature
if CustomTypes.PositiveInteger(..., type((...))) then
FirstValueToCheckOffset = ... - 1
StackSignature = {select(2, ...)}
else
StackSignature = {...}
end
local Function = table.remove(StackSignature)
local NumTypes = #StackSignature
local Castable
for a = 1, NumTypes do
local ParameterSignature = StackSignature[a]
if type(ParameterSignature) == "table" then
for i in next, TransformTableCheckerData(ParameterSignature) do
if type(i) == "string" and i:find("^Enum") then
if not Castable then
Castable = {}
for b = 1, a - 1 do
Castable[b] = false
end
end
Castable[a] = true
end
end
if Castable and not Castable[a] then
Castable[a] = false
end
else
Resources:LoadLibrary("Debug").Error("Definition for parameter #" .. a .. " must be a table")
end
end
if Castable then
return function(...)
local NumParameters = select("#", ...) -- This preserves nil's on the stack
local Stack = {...}
for a = 1, NumParameters < NumTypes and NumTypes or NumParameters do
local Success, Error = Check(StackSignature[a] or Typer.Nil, Stack[a + FirstValueToCheckOffset], a + FirstValueToCheckOffset)
if Success then
if Castable[a] and Success ~= true then
Stack[a + FirstValueToCheckOffset] = Success
end
elseif not Success then
return Resources:LoadLibrary("Debug").Error(Error)
end
end
return Function(unpack(Stack, 1, NumParameters))
end
else -- Don't penalize cases which don't need to cast an Enum
return function(...)
local NumParameters = select("#", ...)
for a = 1, NumParameters < NumTypes and NumTypes or NumParameters do
local Success, Error = Check(StackSignature[a] or Typer.Nil, select(a + FirstValueToCheckOffset, ...), a + FirstValueToCheckOffset)
if not Success then
return Resources:LoadLibrary("Debug").Error(Error)
end
end
return Function(...)
end
end
end
local ExternalTransformTable = Typer.AssignSignature(Typer.Table, TransformTableCheckerData)
Typer.Check = function(PotentialTypes, Parameter, ArgumentNumOrName)
return Check(ExternalTransformTable(PotentialTypes), Parameter, ArgumentNumOrName)
end
Typer.MapDefinition = Typer.AssignSignature(Typer.Table, function(self)
for _, Type in next, self do
ExternalTransformTable(Type)
end
return function(Tab)
if type(Tab) ~= "table" then
return false, "|Map.__call| Must be called with a Table"
end
for Index in next, Tab do
if not self[Index] then
return false, "|Map.__call| " .. Resources:LoadLibrary("Debug").Inspect(Index) .. " is not a valid Key"
end
end
for Index, Type in next, self do
local Success, Error = Typer.Check(Type, Tab[Index], Index)
if not Success then
return false, "|Map.__call| " .. Error
end
end
return Tab
end;
end)
return Table.Lock(Typer)