-
Notifications
You must be signed in to change notification settings - Fork 0
/
pat.lua
484 lines (444 loc) · 14.4 KB
/
pat.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
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
--[========================================================================[--
Lua pattern matching library (minus string.gsub) ported to Lua.
Copyright © 1994–2018 Lua.org, PUC-Rio.
Copyright © 2019 Pedro Gimeno Fortea.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
--]========================================================================]
--
local LUA_MAXCAPTURES = 32
local CAP_UNFINISHED = -1
local CAP_POSITION = -2
local L_ESC = 37 -- 37='%'
-- only used for string.find(x, y, z, true)
-- local SPECIALS = '^$*+?.([%-'
local function LUA_QL(x)
return "'" .. x .. "'"
end
local function check_capture(ms, l)
l = l - 48 -- 48='0'
if l < 1 or l > ms.level or ms.capture[l].len == CAP_UNFINISHED then
return error("invalid capture index")
end
return l
end
local function capture_to_close(ms)
local level = ms.level
while level > 0 do
if ms.capture[level].len == CAP_UNFINISHED then
return level
end
level = level - 1
end
return error("invalid pattern capture")
end
local function classend(pat, p)
local cc = pat:byte(p)
p = p + 1
if cc == L_ESC then
if (pat:byte(p) or 0) == 0 then
return error("malformed pattern (ends with " .. LUA_QL('%') .. ")")
end
return p + 1
end
if cc == 91 then -- 91='['
if pat:byte(p) == 94 then -- 94='^'
p = p + 1
end
repeat -- look for a `]'
cc = pat:byte(p) or 0
p = p + 1
if cc == 0 then
return error("malformed pattern (missing " .. LUA_QL(']') .. ")")
end
if cc == L_ESC then
if (pat:byte(p) or 0) ~= 0 then
p = p + 1
end
end
until pat:byte(p) == 93 -- 93=']'
return p + 1
end
return p
end
local function match_class(c, cl)
local u = cl - cl % 64 + cl % 32 -- upper case
local negate = cl == u -- true if uppercase
local res
if u == 65 then -- 65='A'
res = c >= 65 and c <= 90 or c >= 97 and c <= 122 -- 65='A', 90='Z', 97='a', 122='z'
elseif u == 67 then -- 67='C'
res = c < 32 or c == 127
elseif u == 68 then -- 68='D'
res = c >= 48 and c <= 57 -- 48='0', 57='9'
elseif u == 76 then -- 76='L'
res = c >= 97 and c <= 122 -- 97='a', 122='z'
elseif u == 80 then -- 80='P'
res = c >= 33 and c <= 47 or c >= 58 and c <= 64
or c >= 91 and c <= 96 or c >= 123 and c <= 126
elseif u == 83 then -- 83='S'
res = c == 9 or c >= 10 and c <= 13 or c == 32 -- 9=HT, 10=LF, 13=CR, 32=' '
elseif u == 85 then -- 85='U'
res = c >= 65 and c <= 90
elseif u == 87 then -- 87='W'
res = c >= 65 and c <= 90 or c >= 97 and c <= 122 -- 65='A', 90='Z', 97='a', 122='z'
or c >= 48 and c <= 57 -- 48='0', 57='9'
elseif u == 88 then -- 88='X'
res = c >= 65 and c <= 70 or c >= 97 and c <= 102 -- 65='A', 70='F', 97='a', 102='f'
or c >= 48 and c <= 57 -- 48='0', 57='9'
elseif u == 90 then
res = c == 0
else
return c == cl
end
return negate ~= res
end
local function matchbracketclass(c, pat, p, ec)
local sig = true
p = p + 1
if pat:byte(p) == 94 then -- 94='^'
sig = false
p = p + 1
end
while p < ec do
local cc = pat:byte(p)
if cc == L_ESC then
p = p + 1
if match_class(c, pat:byte(p)) then
return sig
end
elseif pat:byte(p + 1) == 45 and p + 2 < ec then -- 45='-'
p = p + 2
if cc <= c and c <= pat:byte(p) then
return sig
end
elseif cc == c then
return sig
end
p = p + 1
end
return not sig
end
local function singlematch(c, pat, p, ep)
local cc = pat:byte(p)
if cc == 46 then -- 46='.'
return true
end
if cc == L_ESC then
return match_class(c, pat:byte(p + 1))
end
if cc == 91 then -- 91='['
return matchbracketclass(c, pat, p, ep - 1)
end
return cc == c
end
local match
local function matchbalance(ms, str, s, pat, p)
local b = pat:byte(p)
local e = pat:byte(p + 1)
if (b or 0) == 0 or (e or 0) == 0 then
return error("unbalanced pattern")
end
if str:byte(s) ~= b then
return false
end
local cont = 1
s = s + 1
while s < ms.src_end do
local cc = str:byte(s)
if cc == e then
cont = cont - 1
if cont == 0 then
return s + 1
end
elseif cc == b then
cont = cont + 1
end
s = s + 1
end
return false
end
local function max_expand(ms, str, s, pat, p, ep)
local i = 0 -- counts maximum expand for item
while s + i < ms.src_end and singlematch(str:byte(s + i), pat, p, ep) do
i = i + 1
end
while i >= 0 do
local res = match(ms, str, s + i, pat, ep + 1)
if res then return res end
i = i - 1 -- else it didn't match; reduce 1 repetition to try again
end
return false
end
local function min_expand(ms, str, s, pat, p, ep)
repeat
local res = match(ms, str, s, pat, ep + 1)
if res then
return res
end
if s >= ms.src_end or not singlematch(str:byte(s), pat, p, ep) then
return false
end
s = s + 1
until false
end
local function start_capture(ms, str, s, pat, p, what)
local level = ms.level + 1
if level > LUA_MAXCAPTURES then
return error("too many captures")
end
if not ms.capture[level] then
ms.capture[level] = {}
end
ms.capture[level].init = s
ms.capture[level].len = what
ms.level = level
local res = match(ms, str, s, pat, p)
if not res then
ms.level = ms.level - 1
end
return res
end
local function end_capture(ms, str, s, pat, p)
local l = capture_to_close(ms)
ms.capture[l].len = s - ms.capture[l].init
local res = match(ms, str, s, pat, p)
if not res then
ms.capture[l].len = CAP_UNFINISHED
end
return res
end
local function substrcomp(str, start1, start2, len)
for i = start1, start1 + len - 1 do
if str:byte(i) ~= str:byte(i + (start2 - start1)) then
return false
end
end
return true
end
local function match_capture(ms, str, s, l)
l = check_capture(ms, l)
local len = ms.capture[l].len
if ms.src_end - s >= len and substrcomp(str, ms.capture[l].init, s, len) then
return s + len
end
return false
end
-- this is local already, don't add 'local' or it will fail
match = function(ms, str, s, pat, p)
local cc = pat:byte(p) or 0
if cc == 40 then -- 40='('
-- start capture
if pat:byte(p + 1) == 41 then -- 41=')'
-- position capture
return start_capture(ms, str, s, pat, p + 2, CAP_POSITION)
end
return start_capture(ms, str, s, pat, p + 1, CAP_UNFINISHED)
end
if cc == 41 then -- 41=')'
-- end capture
return end_capture(ms, str, s, pat, p + 1)
end
if cc == L_ESC then
cc = pat:byte(p + 1)
if cc == 98 then -- 98='b'
-- balanced string
s = matchbalance(ms, str, s, pat, p + 2)
if not s then
return false
end
return match(ms, str, s, pat, p + 4)
end
if cc == 102 then -- 102='f'
-- frontier
p = p + 2
if pat:byte(p) ~= 91 then -- 91='['
return error("missing " .. LUA_QL('[') .. " after "
.. LUA_QL('%f') .. " in pattern")
end
local ep = classend(pat, p)
local previous = str:byte(s - 1) or 0
if matchbracketclass(previous, pat, p, ep - 1)
or not matchbracketclass(str:byte(s) or 0, pat, p, ep - 1)
then
return false
end
return match(ms, str, s, pat, ep)
end
if cc >= 48 and cc <= 57 then -- 48='0', 57='9'
-- capture results (%0-%9)?
s = match_capture(ms, str, s, cc)
if not s then
return false
end
return match(ms, str, s, pat, p + 2)
end
cc = -1 -- don't match anything else until default
end
if cc == 0 then -- end of pattern
return s
end
if cc == 36 then -- 36='$'
if (pat:byte(p + 1) or 0) == 0 then -- is the `$' the last char in pattern?
return s == ms.src_end and s -- check end of string
end
-- else fall through to default
end
-- default
local ep = classend(pat, p)
local m = s < ms.src_end and singlematch(str:byte(s), pat, p, ep)
cc = pat:byte(ep) or 0
if cc == 63 then
if m then
local res = match(ms, str, s + 1, pat, ep + 1)
if res then
return res
end
end
return match(ms, str, s, pat, ep + 1)
end
if cc == 42 then -- 42='*'
-- 0 or more repetitions
return max_expand(ms, str, s, pat, p, ep)
end
if cc == 43 then -- 43='+'
-- 1 or more repetitions
return m and max_expand(ms, str, s + 1, pat, p, ep)
end
if cc == 45 then -- 45='-'
-- 0 or more repetitions (minimum)
return min_expand(ms, str, s, pat, p, ep)
end
return m and match(ms, str, s + 1, pat, ep)
end
-- lmemfind not implemented - only used for string.find(x, y, z, true)
local function push_onecapture(ms, i, str, s, e)
if i > ms.level then
if i ~= 1 then
return error("invalid capture index")
end
ms.captures[#ms.captures + 1] = str:sub(s, e - 1)
else
local l = ms.capture[i].len
if l == CAP_UNFINISHED then
return error("unfinished capture")
end
if l == CAP_POSITION then
ms.captures[#ms.captures + 1] = ms.capture[i].init
else
ms.captures[#ms.captures + 1] = str:sub(ms.capture[i].init, ms.capture[i].init + l - 1)
end
end
end
local function push_captures(ms, str, s, e)
local nlevels = ms.level
if nlevels == 0 and s then
nlevels = 1
end
for i = 1, nlevels do
push_onecapture(ms, i, str, s, e)
end
end
local function posrelat(pos, len)
if pos < 0 then pos = pos + len + 1 end
return pos >= 1 and pos or 1
end
local string_find = string.find
local SPECIALS = {
[("^"):byte(1)] = true,
[("$"):byte(1)] = true,
[("*"):byte(1)] = true,
[("+"):byte(1)] = true,
[("?"):byte(1)] = true,
[("."):byte(1)] = true,
[("("):byte(1)] = true,
[("["):byte(1)] = true,
[("%"):byte(1)] = true,
[("-"):byte(1)] = true,
[0] = true
}
local function specials_free(s)
for i = 1, #s do
if SPECIALS[s:byte(i)] then
if s:byte(i) == 0 then
return true -- stop at first NUL
end
return false
end
end
return true
end
local function str_find_aux(find, str, pat, init, explicit)
local l1 = #str
init = posrelat(init or 1, l1)
if init < 1 then
init = 1
elseif init > l1 + 1 then
init = l1 + 1
end
if find and (explicit or specials_free(pat)) then -- explicit request?
-- do a plain search
return string_find(str, pat, init, true)
end
local p = 1
local anchor = false
if pat:byte(1) == 94 then -- 94='^'
p = p + 1
anchor = true
end
local s1 = init
local ms = { captures = {}, capture = {}, src_end = l1 + 1 }
repeat
ms.level = 0
local res = match(ms, str, s1, pat, p)
if res then
if find then
push_captures(ms, str, false, 0)
return s1, res - 1, unpack(ms.captures)
end
push_captures(ms, str, s1, res)
return unpack(ms.captures)
end
s1 = s1 + 1
until anchor or s1 > ms.src_end
return nil
end
local function str_find(str, pat, init, explicit)
return str_find_aux(true, str, pat, init, explicit)
end
local function str_match(str, pat, init)
return str_find_aux(false, str, pat, init)
end
local function gmatch(str, pat)
local init = 1
local ms = { captures = {}, capture = {}, src_init = str, src_end = #str + 1 }
local function gmatch_aux()
for i = 1, #ms.captures do ms.captures[i] = nil end
for src = init, ms.src_end do
ms.level = 0
local e = match(ms, str, src, pat, 1)
if e then
local newstart = e
if e == src then newstart = newstart + 1 end -- empty match? go at least one position
init = newstart
push_captures(ms, str, src, e)
return unpack(ms.captures)
end
end
end
return gmatch_aux
end
return { find = str_find, match = str_match, gmatch = gmatch }