-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.lua
212 lines (163 loc) · 4.87 KB
/
Parser.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
type Module = {
Variables: table,
Banner: string
}
local Module: Module = {}
Module.__index = Module
function GetDictSize(Dict: table): number
local Count = 0
for _ in next, Dict do
Count += 1
end
return Count
end
function Module.new(Values): Module
local Class = {}
return setmetatable(Class, Module)
end
function Module:FormatTableKey(Index): string?
local Formatter = self.Formatter
local NeedsBrackets = Formatter:NeedsBrackets(Index)
--// Check if the data type is allowed
if NeedsBrackets then return end
if typeof(Index) ~= "string" then return end
return `{Index} = `
end
type ParseTableIntoStringData = {
Table: table,
Indent: number?,
NoBrackets: boolean?
}
function Module:ParseTableIntoString(Data: ParseTableIntoStringData): (string, number)
local Formatter = self.Formatter
--// Unpack configuration
local Indent = Data.Indent or 0
local NoBrackets = Data.NoBrackets
local Table = Data.Table
local ItemsCount = GetDictSize(Table)
--// Empty table
if ItemsCount == 0 then
return NoBrackets and "" or "{}", ItemsCount
end
local IndentString = string.rep(" ", Indent)
local TableString = `{not NoBrackets and "{" or ""}\n`
--// Generate string
local Position = 0
for Index, Value in next, Table do
local Ending = ""
local KeyString = ""
local ValueString = Formatter:Format(Value, Data)
Position += 1
--// Format the index value
if typeof(Index) ~= "number" then
KeyString = self:FormatTableKey(Index)
if not KeyString then
local IndexString = Formatter:Format(Value, Data)
KeyString = `[{IndexString}] = `
end
end
--// Check if , should be added to the line
if Position < ItemsCount then
Ending = ","
end
TableString ..= `{IndentString} {KeyString}{ValueString}{Ending}\n`
end
--// Close the table
TableString ..= `{IndentString}{not NoBrackets and "}" or ""}`
return TableString, ItemsCount
end
function Module:MakeVariableCodeLine(Data: table): string
local Name = Data.Name
local Value = Data.Value
local Comment = Data.Comment
--print("Variable-data:", Data)
local Line = `local {Name} = {Value}`
local End = Comment and ` -- {Comment}` or ""
return `{Line}{End}`
end
function Module:MakeVariableCodeLines(ClassDict: table): string
local Variables = self.Variables
--// Order variables into array
local VariablesDict = ClassDict.Variables
local Ordered = Variables:OrderVariables(VariablesDict)
--// Compile string
local Lines = ""
for Index, Data in Ordered do
local Line = self:MakeVariableCodeLine(Data)
Lines ..= `{Line}\n`
end
return Lines
end
function Module:MakeVariableCode(Order: table): string
local Variables = self.Variables
local ClassedVariables = Variables.VariablesDict
local Code = ""
local Index = 0
for _, Class in next, Order do
local ClassDict = ClassedVariables[Class]
if not ClassDict then continue end
Index += 1
local NewLine = Index > 1 and "\n" or ""
Code ..= `{NewLine}-- {Class}\n`
Code ..= self:MakeVariableCodeLines(ClassDict)
end
return Code
end
type MakePathStringData = {
Object: Instance,
Parents: table?,
NoVariables: boolean?
}
function Module:MakePathString(Data: table): (string, number)
local Variables = self.Variables
local Formatter = self.Formatter
local Base = Data.Object
local Parents = Data.Parents
local NoVariables = Data.NoVariables
local PathString = ""
local ParentsCount = 0
--// Get object parents
Parents = Parents or Variables:MakeParentsTable(Base)
local function ServiceCheck(Object: Instance, String: string)
local ServiceName = Variables:IsService(Object)
if not ServiceName then return end
local ServiceString = `game:GetService("{ServiceName}")`
--// NoVariables flag
if NoVariables then
PathString = ServiceString
return true
end
--// Make service into a variable
local Name = Variables:MakeVariable({
Name = ServiceName,
Class = "Services",
Value = ServiceString
})
PathString = Name
return true
end
--// Make the path string
for Index, Object in next, Parents do
local String = Formatter:ObjectToString(Object)
--// Check for an existing variable
local Variable = Variables:GetVariable(Object)
if Variable and not NoVariables then
String = Variable.Name
end
--// Check if the object is a service
if Index == 2 and Parents[1] == game then
if ServiceCheck(Object, String) then continue end
end
local Brackets = Formatter:NeedsBrackets(String)
local Separator = Index > 1 and "." or ""
ParentsCount += 1
PathString ..= Brackets and `["{String}"]` or `{Separator}{String}`
end
--// Cache path
-- PathCache[Object] = {
-- Variables = Variables,
-- Args = {PathString, #Parents}
-- }
return PathString, ParentsCount
end
return Module