-
Notifications
You must be signed in to change notification settings - Fork 2
/
regex.lua
367 lines (328 loc) · 8.94 KB
/
regex.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
--
-- Copyright (C) Masatoshi Fukunaga
--
-- 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.
--
-- regex.lua
-- lua-regex
-- Created by Masatoshi Teruya on 17/06/01.
--
--- file scope variables
local unpack = unpack or table.unpack
local sub = string.sub
local type = type
local pcre2 = require('pcre2')
--- constants
local CFLG2OPT_LUT = {
i = pcre2.CASELESS, -- : Do caseless matching.
s = pcre2.DOTALL, -- : `.` matches anything including NL.
m = pcre2.MULTILINE, -- : `^` and `$` match newlines within data.
u = pcre2.UTF, -- : Treat pattern and subjects as UTF strings.
x = pcre2.EXTENDED, -- : Ignore white space and `#` comments
}
--- static variables
local RECACHE = setmetatable({}, {
__mode = 'v',
})
--- flags2opts
--- @param flags string
--- @return table opts
--- @return boolean global
--- @return boolean cache
--- @return boolean jit
local function flags2opts(flags)
local opts = {}
local global = false
local cache = false
local jit = false
local nopt = 0
for i = 1, #flags do
local flg = sub(flags, i, i)
if flg == 'j' then
-- jit compile flag
jit = true
elseif flg == 'g' then
-- global flag
global = true
elseif flg == 'o' then
-- compile-once mode flag
cache = true
elseif flg == 'U' then
-- do not check the pattern for UTF valid.
-- only relevant if UTF option is set.
opts[nopt + 1] = pcre2.UTF
opts[nopt + 2] = pcre2.NO_UTF_CHECK
nopt = nopt + 2
else
local opt = CFLG2OPT_LUT[flg]
if not opt then
-- invalid flag
error(('unknown flag %q'):format(flg))
end
-- add option
nopt = nopt + 1
opts[nopt] = opt
end
end
return opts, global, cache, jit
end
--- @class regex
--- @field p pcre2
--- @field global boolean
--- @field lastidx integer
local Regex = {}
--- init
--- @param pattern string
--- @param flags string?
--- @return regex? regex
--- @return any err
function Regex:init(pattern, flags)
assert(type(pattern) == 'string', 'pattern must be string')
assert(flags == nil or type(flags) == 'string',
'flags must be string or nil')
if not flags then
flags = ''
end
-- parse flags
local cache_key = pattern .. '@' .. flags
local opts, global, cache, jit = flags2opts(flags)
-- check the cache table
if cache then
-- return the cached object if exists
local re = RECACHE[cache_key]
if re then
return re
end
end
-- compile pattern
local p, err = pcre2.new(pattern, unpack(opts))
if not p then
return nil, err
elseif jit then
-- jit compile
local ok
ok, err = p:jit_compile()
if not ok then
return nil, err
end
end
self.p = p
self.global = global
self.lastidx = 0
-- save into cache table
if cache then
RECACHE[cache_key] = self
end
return self
end
--- matches
--- @param sbj string
--- @param offset integer
--- @return string[]? arr
--- @return any err
function Regex:matches(sbj, offset)
local head, tail, err = self.p:match_nocap(sbj, offset)
if head then
local arr = {}
local idx = 1
while head do
arr[idx] = sub(sbj, head, tail)
idx = idx + 1
head, tail, err = self.p:match_nocap(sbj, tail)
end
if err then
return nil, err
end
return arr
end
return nil, err
end
--- match
--- @param sbj string
--- @param offset integer?
--- @return string[]? arr
--- @return any err
function Regex:match(sbj, offset)
local heads, tails, err = self.p:match(sbj, offset or self.lastidx)
if heads then
-- found
local arr = {}
for i = 1, #heads do
arr[i] = sub(sbj, heads[i], tails[i])
end
-- updaet a last-index if global option is enabled
if self.global == true then
self.lastidx = tails[1]
end
return arr
elseif err then
return nil, err
elseif self.global then
-- reset a last-index to 0 if global option is enabled
self.lastidx = 0
end
end
--- indexesof
--- @param sbj string
--- @param offset? integer
--- @return integer[]? arr
--- @return any err
function Regex:indexesof(sbj, offset)
local head, tail, err = self.p:match_nocap(sbj, offset)
if head then
local arr = {}
local idx = 1
while head do
arr[idx], arr[idx + 1] = head, tail
idx = idx + 2
head, tail, err = self.p:match_nocap(sbj, tail)
end
if err then
return nil, err
end
return arr
elseif err then
return nil, err
end
end
--- indexof
--- @param sbj string
--- @param offset? integer
--- @return integer[]? arr
--- @return any err
function Regex:indexof(sbj, offset)
local heads, tails, err = self.p:match(sbj, offset or self.lastidx)
if heads then
local arr = {}
local idx = 1
for i = 1, #heads do
arr[idx], arr[idx + 1] = heads[i], tails[i]
idx = idx + 2
end
-- updaet a last-index if global option is enabled
if self.global == true then
self.lastidx = tails[1]
end
return arr
elseif err then
return nil, err
elseif self.global then
-- reset a last-index to 0 if global option is enabled
self.lastidx = 0
end
end
--- test
--- @param sbj string
--- @param offset integer?
--- @return boolean ok
--- @return any err
function Regex:test(sbj, offset)
local head, tail, err = self.p:match_nocap(sbj, offset or self.lastidx)
-- found
if head then
-- updaet a last-index if global option is enabled
if self.global == true then
self.lastidx = tail
end
return true
elseif self.global then
-- reset a last-index to 0 if global option is enabled
self.lastidx = 0
end
return false, err
end
Regex = require('metamodule').new(Regex)
--- matches
--- @param sbj string
--- @param pattern string
--- @param flags? string
--- @param offset? integer
--- @return string[]? arr
--- @return any err
local function matches(sbj, pattern, flags, offset)
local re, err = Regex(pattern, flags)
if re then
return re:matches(sbj, offset)
end
return nil, err
end
--- match
--- @param sbj string
--- @param pattern string
--- @param flags? string
--- @param offset? integer
--- @return string[]? arr
--- @return any err
local function match(sbj, pattern, flags, offset)
local re, err = Regex(pattern, flags)
if re then
return re:match(sbj, offset)
end
return nil, err
end
--- indexesof
--- @param sbj string
--- @param pattern string
--- @param flags? string
--- @param offset? integer
--- @return integer[]? arr
--- @return any err
local function indexesof(sbj, pattern, flags, offset)
local re, err = Regex(pattern, flags)
if re then
return re:indexesof(sbj, offset)
end
return nil, err
end
--- indexof
--- @param sbj string
--- @param pattern string
--- @param flags? string
--- @param offset? integer
--- @return integer[]? arr
--- @return any err
local function indexof(sbj, pattern, flags, offset)
local re, err = Regex(pattern, flags)
if re then
return re:indexof(sbj, offset)
end
return nil, err
end
--- test
--- @param sbj string
--- @param pattern string
--- @param flags? string
--- @param offset? integer
--- @return boolean ok
--- @return any err
local function test(sbj, pattern, flags, offset)
local re, err = Regex(pattern, flags)
if re then
return re:test(sbj, offset)
end
return false, err
end
return {
new = Regex,
matches = matches,
match = match,
indexesof = indexesof,
indexof = indexof,
test = test,
}