-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvgui.lua
More file actions
339 lines (289 loc) · 8.4 KB
/
vgui.lua
File metadata and controls
339 lines (289 loc) · 8.4 KB
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
-- Token types enumeration
local TokenType = {
LeftBrace = 1,
RightBrace = 2,
Flag = 3,
String = 4,
Name = 5
}
-- Token class
local Token = {}
Token.__index = Token
function Token.new(type, value)
return setmetatable({
type = type,
value = value
}, Token)
end
-- VguiObject class
local VguiObject = {}
VguiObject.__index = VguiObject
function VguiObject.new(name, value)
local self = setmetatable({
name = name,
value = value,
properties = {},
_properties = {},
flags = {}
}, VguiObject)
return self
end
function VguiObject:isValue()
return self.value ~= nil
end
function VguiObject:get(name)
return self.properties[name]
end
function VguiObject:getNameFlagKey()
local flags = {}
for k, v in pairs(self.flags) do
flags[k] = v
end
return {name = self.name, flags = flags}
end
function VguiObject:compareFlags(other)
for k, v in pairs(self.flags) do
if other.flags[k] ~= v then
return false
end
end
return true
end
function VguiObject:mergeOrAddProperty(other)
local key = other:getNameFlagKey()
local existing = self._properties[key.name]
if existing and not existing:isValue() and not other:isValue() then
existing:tryMerge(other)
else
self._properties[key.name] = other
self.properties[other.name] = other
end
end
function VguiObject:tryMerge(other)
if other.name ~= self.name or not self:compareFlags(other) or self:isValue() or other:isValue() then
return false
end
for _, prop in pairs(other.properties) do
self:mergeOrAddProperty(prop)
end
return true
end
-- Lexer class
local Lexer = {}
Lexer.__index = Lexer
function Lexer.new(input)
return setmetatable({
input = input,
index = 1,
length = #input
}, Lexer)
end
function Lexer:peek(n)
n = n or 1
local pos = self.index + n - 1
if pos <= self.length then
return self.input:sub(pos, pos)
end
return nil
end
function Lexer:advance(n)
n = n or 1
self.index = self.index + n
end
function Lexer:skipWhitespace()
while self:peek() and string.match(self:peek(), "%s") do
self:advance()
end
end
function Lexer:skipLine()
while self:peek() and self:peek() ~= "\n" and self:peek() ~= "\r" do
self:advance()
end
while self:peek() and (self:peek() == "\n" or self:peek() == "\r") do
self:advance()
end
end
function Lexer:getStringToken()
local str = ""
while self:peek() and self:peek() ~= '"' do
str = str .. self:peek()
self:advance()
end
self:advance() -- consume closing quote
return Token.new(TokenType.String, str)
end
function Lexer:getNameToken()
local name = ""
while self:peek() and string.match(self:peek(), "[%w]") do
name = name .. self:peek()
self:advance()
end
return Token.new(TokenType.Name, name)
end
function Lexer:getFlagToken()
local flag = ""
while self:peek() and self:peek() ~= "]" do
flag = flag .. self:peek()
self:advance()
end
self:advance() -- consume closing bracket
return Token.new(TokenType.Flag, flag)
end
function Lexer:getTokens()
local tokens = {}
while self:peek() do
self:skipWhitespace()
-- Skip comments
if self:peek() == "/" and self:peek(2) == "/" then
self:skipLine()
goto continue
end
-- Skip preprocessor directives
if self:peek() == "#" then
self:skipLine()
goto continue
end
if self:peek() == '"' then
self:advance()
table.insert(tokens, self:getStringToken())
elseif string.match(self:peek(), "[%w]") then
table.insert(tokens, self:getNameToken())
elseif self:peek() == "[" then
self:advance()
table.insert(tokens, self:getFlagToken())
elseif self:peek() == "{" then
table.insert(tokens, Token.new(TokenType.LeftBrace, "{"))
self:advance()
elseif self:peek() == "}" then
table.insert(tokens, Token.new(TokenType.RightBrace, "}"))
self:advance()
else
self:advance()
end
::continue::
end
return tokens
end
-- Parser class
local Parser = {}
Parser.__index = Parser
function Parser.new(tokens)
return setmetatable({
tokens = tokens,
index = 1
}, Parser)
end
function Parser:peek(n)
n = n or 1
return self.tokens[self.index + n - 1]
end
function Parser:eat(expectedType)
local token = self:peek()
if not token or token.type ~= expectedType then
error(string.format("Expected %s but got %s", expectedType, token and token.type or "nil"))
end
self.index = self.index + 1
return token
end
function Parser:parseValue()
local name = self:eat(TokenType.String)
if self:peek() and self:peek().type == TokenType.LeftBrace then
self:eat(TokenType.LeftBrace)
local values = self:parseValueList()
self:eat(TokenType.RightBrace)
return {
name = name,
string = nil,
body = { values = values }
}
end
return {
name = name,
string = self:eat(TokenType.String),
body = nil
}
end
function Parser:parseValueList()
local values = {}
while self:peek() and (self:peek().type == TokenType.String or self:peek().type == TokenType.Name) do
table.insert(values, self:parseValue())
end
return values
end
function Parser:parseRoot()
return {
name = Token.new(TokenType.Name, "Root"),
string = nil,
body = { values = self:parseValueList() }
}
end
-- Preprocessor class
local Preprocessor = {}
Preprocessor.__index = Preprocessor
function Preprocessor.new(sourceProvider)
return setmetatable({
sourceProvider = sourceProvider
}, Preprocessor)
end
function Preprocessor:process(file)
local lines = self.sourceProvider:readAllLines(file)
local result = {}
for _, line in ipairs(lines) do
local trimmed = line:match("^%s*(.-)%s*$")
if trimmed:sub(1, 6) == "#base " then
local baseFile = trimmed:sub(7):match('"(.+)"')
local preprocessor = Preprocessor.new(self.sourceProvider)
local processed = preprocessor:process(baseFile)
for _, processedLine in ipairs(processed) do
table.insert(result, processedLine)
end
else
table.insert(result, line)
end
end
return result
end
-- VguiSerializer
local VguiSerializer = {}
function VguiSerializer.fromFile(rootPath, file)
-- Create a source provider that uses Lmaobox's local filesystem API
local sourceProvider = {
readAllLines = function(self, filepath)
local f = io.open(rootPath .. "/" .. filepath, "r")
if not f then error("Could not open file: " .. filepath) end
local lines = {}
for line in f:lines() do
table.insert(lines, line)
end
f:close()
return lines
end
}
local preprocessor = Preprocessor.new(sourceProvider)
local processed = table.concat(preprocessor:process(file), "\n")
local lexer = Lexer.new(processed)
local tokens = lexer:getTokens()
local parser = Parser.new(tokens)
local root = parser:parseRoot()
return VguiSerializer.fromValue(root)
end
function VguiSerializer.fromValue(value)
if value.string then
return VguiObject.new(value.name.value, value.string.value)
end
local obj = VguiObject.new(value.name.value)
for _, subValue in ipairs(value.body.values) do
obj:mergeOrAddProperty(VguiSerializer.fromValue(subValue))
end
return obj
end
-- Export the module
return {
TokenType = TokenType,
Token = Token,
VguiObject = VguiObject,
Lexer = Lexer,
Parser = Parser,
Preprocessor = Preprocessor,
VguiSerializer = VguiSerializer
}