-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.lua
More file actions
524 lines (371 loc) · 13.7 KB
/
lexer.lua
File metadata and controls
524 lines (371 loc) · 13.7 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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
local common = require("common")
local lexer = { PlainTextToken = {}, NewlineToken = {}, TagToken = {}, TagSegment = {} }
function lexer.PlainTextToken.new(position, content)
local self = setmetatable({}, { __index = lexer.PlainTextToken })
self.kind = "PlainTextToken"
self.position = position
self.content = content
return self
end
function lexer.NewlineToken.new(position)
local self = setmetatable({}, { __index = lexer.NewlineToken })
self.kind = "NewlineToken"
self.position = position
return self
end
function lexer.TagToken.new(position, name, arguments, isEndTag, originalString)
local self = setmetatable({}, { __index = lexer.TagToken })
self.kind = "TagToken"
self.position = position
self.name = name
self.arguments = arguments
self.isEndTag = isEndTag
self.originalString = originalString
return self
end
function lexer.TagSegment.new(position, content, originalString)
local self = setmetatable({}, { __index = lexer.TagSegment })
self.position = position
self.content = content
self.originalString = originalString
return self
end
local function acceptForLexer(result, nextIndex, warnings)
local emptyStrictProblems = {}
return common.ParseAccept.new(result, nextIndex, warnings, emptyStrictProblems)
end
local StringReference = {}
function StringReference.new(text)
local function precomputePositionMap(input, length)
local positionMap = {}
local line = 1
local col = 0
for i = 1, length do
col = col + 1
positionMap[i] = common.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] = common.Position.new(length + 1, line, col + 1)
return positionMap
end
local self = setmetatable({}, { __index = StringReference })
self.text = text
self.length = #text
self.positionMap = precomputePositionMap(text, #text)
return self
end
function StringReference:getPosition(index)
local position = self.positionMap[index]
assert(position ~= nil)
return position
end
local function tokenizeNonEmptyStringWithValidator(
textref,
startIndex,
validEscapes,
readUntilChars,
validateChar,
validateFailMessage)
local pos = startIndex
local content = ""
local warnings = {}
while pos <= textref.length do
local ch = textref.text:sub(pos, pos)
local reachedTheEnd = readUntilChars[ch]
if reachedTheEnd then
break
end
if not validateChar(ch) then
return common.ParseFail.new(common.Problem.new(validateFailMessage:format(ch), textref:getPosition(pos)))
end
local isEscape = ch == "\\"
if isEscape then
local escapeSequence = textref.text:sub(pos, pos + 1)
local escapeResult = validEscapes[escapeSequence]
if escapeResult then
assert(#escapeResult == 1)
content = content .. escapeResult
pos = pos + (1 + 1)
else
warnings[#warnings + 1] = common.Problem.new("invalid escape " .. escapeSequence, textref:getPosition(pos))
content = content .. "\\"
pos = pos + 1
end
else
content = content .. ch
pos = pos + 1
end
end
if content == "" then
return common.ParseReject.new()
end
return acceptForLexer(content, pos, warnings)
end
local function tokenizeNonEmptyString(
textref,
startIndex,
validEscapes,
readUntilChars)
local function anyCharValidator(_)
return true
end
return tokenizeNonEmptyStringWithValidator(
textref,
startIndex,
validEscapes,
readUntilChars,
anyCharValidator,
"unused")
end
local function seekExpectedStrings(
textref,
startIndex,
expectedStrings)
local matches = false
local matchedString = ""
for _, expectedString in ipairs(expectedStrings) do
matches = textref.text:sub(startIndex, startIndex + #expectedString - 1) == expectedString
if matches then
matchedString = expectedString
break
end
end
if not matches then
return common.ParseReject.new()
end
local noWarning = {}
return acceptForLexer(matchedString, startIndex + #matchedString, noWarning)
end
local function tokenizeTagName(textref, startIndex)
local validEscapes = {}
local readUntilChars = {
[':'] = true,
['>'] = true,
}
local function alnumUnderscoreValidator(ch)
return ch:match("^[0-9a-zA-Z_]*$") ~= nil
end
local function tagNameValidator(char)
return alnumUnderscoreValidator(char) or char == "#"
end
local result = tokenizeNonEmptyStringWithValidator(
textref,
startIndex,
validEscapes,
readUntilChars,
tagNameValidator,
"invalid character in tag name: %s")
if result.kind == "ParseReject" then
return common.ParseFail.new(common.Problem.new("tag name cannot be empty", textref:getPosition(startIndex)))
end
return result
end
local function tokenizeUnquotedTagArg(textref, startIndex)
local validEscapes = {}
local readUntilChars = {
[':'] = true,
['>'] = true,
}
local function alnumUnderscoreValidator(ch)
return ch:match("^[0-9a-zA-Z_]*$") ~= nil
end
local result = tokenizeNonEmptyStringWithValidator(
textref,
startIndex,
validEscapes,
readUntilChars,
alnumUnderscoreValidator,
"invalid character in unquoted tag argument: %s")
local isEmptyString = result.kind == "ParseReject"
local acceptPosition = textref:getPosition(startIndex)
if isEmptyString then
local noWarning = {}
return acceptForLexer(lexer.TagSegment.new(acceptPosition, "", ""), startIndex, noWarning)
end
if result.kind == "ParseFail" then
return result
end
assert(result.kind == "ParseAccept")
local originalString = textref.text:sub(startIndex, result.nextIndex - 1)
return acceptForLexer(lexer.TagSegment.new(acceptPosition, result.result, originalString), result.nextIndex, result.warnings)
end
local function tokenizeSingleQuotedTagArg(textref, startIndex)
local seekResult = seekExpectedStrings(textref, startIndex, { "'" })
if seekResult.kind == "ParseReject" or seekResult.kind == "ParseFail" then
return common.ParseReject.new()
end
local validEscapes = {
["\\'"] = "'",
['\\\\'] = '\\',
}
local readUntilChars = {
["'"] = true,
}
local result = tokenizeNonEmptyString(
textref,
seekResult.nextIndex,
validEscapes,
readUntilChars)
if result.kind == "ParseFail" then
return result
end
local isEmptyString = result.kind == "ParseReject"
local endQuotePosition = result.kind == "ParseAccept" and result.nextIndex or startIndex + 1
local seekResultEnd = seekExpectedStrings(textref, endQuotePosition, { "'" })
if seekResultEnd.kind == "ParseReject" or seekResultEnd.kind == "ParseFail" then
return common.ParseFail.new(common.Problem.new("unclosed single-quoted tag argument", textref:getPosition(endQuotePosition)))
end
local acceptPosition = textref:getPosition(startIndex)
local originalString = textref.text:sub(startIndex, seekResultEnd.nextIndex - 1)
if isEmptyString then
local noWarning = {}
return acceptForLexer(lexer.TagSegment.new(acceptPosition, "", originalString), seekResultEnd.nextIndex, noWarning)
end
assert(result.kind == "ParseAccept")
return acceptForLexer(lexer.TagSegment.new(acceptPosition, result.result, originalString), seekResultEnd.nextIndex, result.warnings)
end
local function tokenizeDoubleQuotedTagArg(textref, startIndex)
local seekResult = seekExpectedStrings(textref, startIndex, { '"' })
if seekResult.kind == "ParseReject" or seekResult.kind == "ParseFail" then
return common.ParseReject.new()
end
local validEscapes = {
['\\"'] = '"',
['\\\\'] = '\\',
}
local readUntilChars = {
['"'] = true,
}
local result = tokenizeNonEmptyString(
textref,
seekResult.nextIndex,
validEscapes,
readUntilChars)
if result.kind == "ParseFail" then
return result
end
local isEmptyString = result.kind == "ParseReject"
local endQuotePosition = result.kind == "ParseAccept" and result.nextIndex or startIndex + 1
local seekResultEnd = seekExpectedStrings(textref, endQuotePosition, { '"' })
if seekResultEnd.kind == "ParseReject" or seekResultEnd.kind == "ParseFail" then
return common.ParseFail.new(common.Problem.new("unclosed double-quoted tag argument", textref:getPosition(endQuotePosition)))
end
local acceptPosition = textref:getPosition(startIndex)
local originalString = textref.text:sub(startIndex, seekResultEnd.nextIndex - 1)
if isEmptyString then
local noWarning = {}
return acceptForLexer(lexer.TagSegment.new(acceptPosition, "", originalString), seekResultEnd.nextIndex, noWarning)
end
assert(result.kind == "ParseAccept")
return acceptForLexer(lexer.TagSegment.new(acceptPosition, result.result, originalString), seekResultEnd.nextIndex, result.warnings)
end
local function tokenizeTag(textref, startIndex)
local startTagResult = seekExpectedStrings(textref, startIndex, { "</", "<" })
if startTagResult.kind == "ParseReject" or startTagResult.kind == "ParseFail" then
return common.ParseReject.new()
end
local warnings = {}
local isEndTag = startTagResult.result == "</"
local tagNameResult = tokenizeTagName(textref, startTagResult.nextIndex)
if tagNameResult.kind == "ParseFail" then
warnings[#warnings + 1] = tagNameResult.failure
return acceptForLexer(lexer.PlainTextToken.new(textref:getPosition(startIndex), textref.text:sub(startIndex, tagNameResult.failure.position.index - 1)), tagNameResult.failure.position.index, warnings)
end
assert(not (tagNameResult.kind == "ParseReject"))
for _, warning in ipairs(tagNameResult.warnings) do
warnings[#warnings + 1] = warning
end
local pos = tagNameResult.nextIndex
local tokenizersToTry = { tokenizeDoubleQuotedTagArg, tokenizeSingleQuotedTagArg, tokenizeUnquotedTagArg }
local arguments = {}
while pos <= textref.length do
local seekResult = seekExpectedStrings(textref, pos, { ">", ":" })
if seekResult.kind == "ParseReject" or seekResult.kind == "ParseFail" then
local ch = textref.text:sub(pos, pos)
warnings[#warnings + 1] = common.Problem.new("invalid character in tag: " .. ch, textref:getPosition(pos))
return acceptForLexer(lexer.PlainTextToken.new(textref:getPosition(startIndex), textref.text:sub(startIndex, pos - 1)), pos, warnings)
end
pos = seekResult.nextIndex
local reachedEndOfTag = seekResult.result == ">"
if reachedEndOfTag then
local originalString = textref.text:sub(startIndex, pos - 1)
return acceptForLexer(lexer.TagToken.new(textref:getPosition(startIndex), tagNameResult.result, arguments, isEndTag, originalString), pos, warnings)
end
local foundResult = nil
for _, tokenizer in ipairs(tokenizersToTry) do
local result = tokenizer(textref, pos)
if result.kind == "ParseFail" then
warnings[#warnings + 1] = result.failure
return acceptForLexer(lexer.PlainTextToken.new(textref:getPosition(startIndex), textref.text:sub(startIndex, result.failure.position.index - 1)), result.failure.position.index, warnings)
end
if result.kind == "ParseAccept" then
foundResult = result
break
end
end
assert(foundResult.kind == "ParseAccept")
arguments[#arguments + 1] = foundResult.result
pos = foundResult.nextIndex
for _, warning in ipairs(foundResult.warnings) do
warnings[#warnings + 1] = warning
end
end
warnings[#warnings + 1] = common.Problem.new("unclosed tag", textref:getPosition(pos))
return acceptForLexer(lexer.PlainTextToken.new(textref:getPosition(startIndex), textref.text:sub(startIndex, pos - 1)), pos, warnings)
end
local function tokenizeNewline(textref, startIndex)
local result = seekExpectedStrings(textref, startIndex, { "\n" })
if result.kind == "ParseReject" or result.kind == "ParseFail" then
return common.ParseReject.new()
end
return acceptForLexer(lexer.NewlineToken.new(textref:getPosition(startIndex)), result.nextIndex, {})
end
local function tokenizePlainText(textref, startIndex)
local validEscapes = {
['\\<'] = '<',
['\\\\'] = '\\',
}
local readUntilChars = {
['<'] = true,
['\n'] = true,
}
local result = tokenizeNonEmptyString(
textref,
startIndex,
validEscapes,
readUntilChars)
assert(not (result.kind == "ParseFail"))
if result.kind == "ParseAccept" then
return acceptForLexer(lexer.PlainTextToken.new(textref:getPosition(startIndex), result.result), result.nextIndex, result.warnings)
end
return result
end
function lexer.tokenize(text)
local tokens = {}
local warnings = {}
local textref = StringReference.new(text)
local pos = 1
local tokenizersInCycle = { tokenizePlainText, tokenizeNewline, tokenizeTag }
while pos <= textref.length do
for _, tokenizer in ipairs(tokenizersInCycle) do
if pos > textref.length then
break
end
local result = tokenizer(textref, pos)
if result.kind == "ParseAccept" then
tokens[#tokens + 1] = result.result
pos = result.nextIndex
for _, warning in ipairs(result.warnings) do
warnings[#warnings + 1] = warning
end
end
assert(not (result.kind == "ParseFail"))
end
end
return acceptForLexer(tokens, pos, warnings)
end
return lexer