-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodegen.lua
More file actions
376 lines (270 loc) · 9.03 KB
/
codegen.lua
File metadata and controls
376 lines (270 loc) · 9.03 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
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
local lexer = require("lexer")
local parser = require("parser")
local codegen = { CompileAccept = {}, CompileFail = {}, CompileOptions = {} }
function codegen.CompileAccept.new(result, warnings)
local self = setmetatable({}, { __index = codegen.CompileAccept })
self.kind = "CompileAccept"
self.result = result
self.warnings = warnings
return self
end
function codegen.CompileFail.new(errors)
local self = setmetatable({}, { __index = codegen.CompileFail })
self.kind = "CompileFail"
self.errors = errors
return self
end
function codegen.CompileOptions.new(useStrictMode)
local self = setmetatable({}, { __index = codegen.CompileOptions })
self.useStrictMode = useStrictMode
return self
end
local function precomputePositionMap(input, length)
local positionMap = {}
local line = 1
local col = 0
for i = 1, length do
col = col + 1
positionMap[i] = lexer.Position.new(i, line, col)
local ch = input:sub(i, i)
if ch == "\n" then
line = line + 1
col = 0
end
end
positionMap[length + 1] = lexer.Position.new(length + 1, line, col + 1)
return positionMap
end
local function computeReverseMap(
text,
startIndex,
validEscapes,
readUntilChars)
local positionMap = precomputePositionMap(text, #text)
local reverseMap = {}
local pos = startIndex
local content = ""
while pos <= #text do
local ch = text:sub(pos, pos)
local reachedTheEnd = readUntilChars[ch]
if reachedTheEnd then
break
end
local isEscape = ch == "\\"
if isEscape then
local escapeSequence = text:sub(pos, pos + 1)
local escapeResult = validEscapes[escapeSequence]
if escapeResult then
reverseMap[#content + 1] = positionMap[pos]
content = content .. escapeResult
pos = pos + 2
else
reverseMap[#content + 1] = positionMap[pos]
content = content .. "\\"
pos = pos + 1
end
else
reverseMap[#content + 1] = positionMap[pos]
content = content .. ch
pos = pos + 1
end
end
return reverseMap
end
local function reverseUnquotedTagArg(text)
local validEscapes = {}
local readUntilChars = {
[':'] = true,
['>'] = true,
}
local result = computeReverseMap(
text,
1,
validEscapes,
readUntilChars)
return result
end
local function reverseSingleQuotedTagArg(text)
assert(text:sub(1, 1) == "'")
assert(text:sub(#text, #text) == "'")
local validEscapes = {
["\\'"] = "'",
['\\\\'] = '\\',
}
local readUntilChars = {
["'"] = true,
}
local result = computeReverseMap(
text,
2,
validEscapes,
readUntilChars)
return result
end
local function reverseDoubleQuotedTagArg(text)
assert(text:sub(1, 1) == '"')
assert(text:sub(#text, #text) == '"')
local validEscapes = {
['\\"'] = '"',
['\\\\'] = '\\',
}
local readUntilChars = {
['"'] = true,
}
local result = computeReverseMap(
text,
2,
validEscapes,
readUntilChars)
return result
end
local function reverseTagArg(text)
local firstChar = text:sub(1, 1)
if firstChar == '"' then
return reverseDoubleQuotedTagArg(text)
end
if firstChar == "'" then
return reverseSingleQuotedTagArg(text)
end
return reverseUnquotedTagArg(text)
end
local function localToGlobalPosition(localPosition, parentPosition)
local index = parentPosition.index + localPosition.index - 1
local line = parentPosition.line + localPosition.line - 1
local col = localPosition.line == 1 and (parentPosition.col + localPosition.col - 1) or localPosition.col
return lexer.Position.new(index, line, col)
end
local function translateProblemsToGlobalPositions(problems, parentPosition, originalMinecraftTextString)
local newProblems = {}
local reverseMap = reverseTagArg(originalMinecraftTextString)
for _, problem in ipairs(problems) do
newProblems[#newProblems + 1] = lexer.Problem.new(problem.message, localToGlobalPosition(reverseMap[problem.position.index], parentPosition))
end
return newProblems
end
local function span(content, opt)
local classNames = {}
if opt.format then
classNames[#classNames + 1] = "mcformat-" .. opt.format
end
if opt.class then
classNames[#classNames + 1] = opt.class
end
local class = #classNames > 0 and ' class="' .. table.concat(classNames, " ") .. '"' or ""
local style = opt.style and ' style="' .. opt.style .. '"' or ""
return "<span" .. class .. style .. ">" .. content .. "</span>"
end
local function genComponents(components, options)
local result = ""
local warnings = {}
local function addWarnings(newWarnings)
for _, warning in ipairs(newWarnings) do
warnings[#warnings + 1] = warning
end
end
for _, component in ipairs(components) do
local generateResult = codegen.generate(component, options)
if generateResult.kind == "CompileFail" then
return generateResult
end
addWarnings(generateResult.warnings)
result = result .. generateResult.result
end
return codegen.CompileAccept.new(result, warnings)
end
function codegen.generate(node, options)
local result = ""
local warnings = {}
local function addWarnings(newWarnings)
for _, warning in ipairs(newWarnings) do
warnings[#warnings + 1] = warning
end
end
if node.kind == "MinecraftTextNode" then
local componentResult = genComponents(node.components, options)
if componentResult.kind == "CompileFail" then
return componentResult
end
addWarnings(componentResult.warnings)
result = result .. componentResult.result
elseif node.kind == "PlainTextNode" then
result = result .. node.content
elseif node.kind == "NewlineNode" then
result = result .. "<br>"
elseif node.kind == "NamedColorNode" then
local componentResult = genComponents(node.components, options)
if componentResult.kind == "CompileFail" then
return componentResult
end
addWarnings(componentResult.warnings)
result = result .. span(componentResult.result, { format = node.color })
elseif node.kind == "HexColorNode" then
local componentResult = genComponents(node.components, options)
if componentResult.kind == "CompileFail" then
return componentResult
end
addWarnings(componentResult.warnings)
result = result .. span(componentResult.result, { style = "color: #" .. node.color .. ";" })
elseif node.kind == "DecorationNode" then
local componentResult = genComponents(node.components, options)
if componentResult.kind == "CompileFail" then
return componentResult
end
addWarnings(componentResult.warnings)
result = result .. span(componentResult.result, { format = node.decoration })
elseif node.kind == "ShowTextNode" then
local showTextSecondaryParse = codegen.compile(node.text, options)
if showTextSecondaryParse.kind == "CompileFail" then
return codegen.CompileFail.new(translateProblemsToGlobalPositions(showTextSecondaryParse.errors, node.textPosition, node.originalString))
end
addWarnings(translateProblemsToGlobalPositions(showTextSecondaryParse.warnings, node.textPosition, node.originalString))
local mctooltip = span(showTextSecondaryParse.result, { class = "mctooltip" })
local componentResult = genComponents(node.components, options)
if componentResult.kind == "CompileFail" then
return componentResult
end
addWarnings(componentResult.warnings)
result = result .. span(componentResult.result .. mctooltip, {})
else
error("This should not be reached")
end
return codegen.CompileAccept.new(result, warnings)
end
function codegen.compile(text, options)
local warnings = {}
local function addWarnings(newWarnings)
for _, warning in ipairs(newWarnings) do
warnings[#warnings + 1] = warning
end
end
local function addWarningsAsErrors(newErrors, newWarnings)
for _, warning in ipairs(newWarnings) do
newErrors[#newErrors + 1] = warning
end
end
local tokens = lexer.tokenize(text)
if options.useStrictMode then
local hasWarning = #tokens.warnings > 0
if hasWarning then
return codegen.CompileFail.new(tokens.warnings)
end
end
addWarnings(tokens.warnings)
local ast = parser.parse(tokens.result)
if options.useStrictMode then
local hasErrorOrWarning = #ast.strictProblems > 0 or #ast.warnings > 0
if hasErrorOrWarning then
local errors = ast.strictProblems
addWarningsAsErrors(errors, ast.warnings)
return codegen.CompileFail.new(errors)
end
end
addWarnings(ast.warnings)
local result = codegen.generate(ast.result, options)
if result.kind == "CompileFail" then
return result
end
addWarnings(result.warnings)
return codegen.CompileAccept.new(result.result, warnings)
end
return codegen