-
Notifications
You must be signed in to change notification settings - Fork 0
/
tre.py
419 lines (353 loc) · 13.3 KB
/
tre.py
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
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
A module which creates a binding for the TRE regular expression engine.
You can find the home page of TRE at http://laurikari.net/tre/
Don't be surprised about many very easy and obvious documentation comments
as this module is also intended as a documentation on how to wrap C
libraries with ctypes. ctypes was put into the stdlib for Python 2.5 so it
does not count as external dependency anymore.
"""
# import important ctypes functions
from ctypes import cdll, Structure, POINTER, byref
# inport the built-in C datatypes
from ctypes import c_int, c_size_t, c_void_p, c_char, c_char_p, c_wchar
# get the constants that are defined in sre_constants
import sre_constants
# version of the binding
__version__ = '0.0.1'
# the constants defined in TRE's regex.h
#
# unfortunately, it is also possible that tre will use some of the
# constants in the system's regex.h - this has to be handled in some smart
# way. Further analyzing the system's regex.h is neccessary
#
# not all of them are needed, it doesn't hurt to be as complete as possible
# POSIX regcomp() flags
# enable extended regular expressions - that's the default in tre.py
# as opposed to TRE itself, which uses basic regular expressions
REG_EXTENDED = 1
# ignore case
REG_ICASE = REG_EXTENDED << 1
# special handling of newline character
REG_NEWLINE = REG_ICASE << 1
# do not report submatches. The submatch array won't be filled
REG_NOSUB = REG_NEWLINE << 1
# extra, nonstandard flags
# basic regular expression - this is the default, anyway
REG_BASIC = 0
# all characters of the input string are considered ordinary
REG_NOSPEC = REG_LITERAL = REG_NOSUB << 1
# by default the concatenation is left associative in TRE
REG_RIGHT_ASSOC = REG_LITERAL << 1
REG_UNGREEDY = REG_RIGHT_ASSOC << 1
# POSIX regexec() flags
REG_NOTBOL = 1
REG_NOTEOL = REG_NOTBOL << 1
# extra regexec() flags
REG_APPROX_MATCHER = 0x1000
REG_BACKTRACKING_MATCHER = REG_APPROX_MATCHER << 1
# error constants
reg_errcode_t = [
# no error
'REG_OK',
# no match
'REG_NOMATCH',
# invalid regular expression
'REG_BADPAT',
# unknown collating element
'REG_ECOLLATE',
# unknown charater class name
'REG_ECTYPE',
# trailing backslash
'REG_EESCAPE',
# invalid back reference
'REG_ESUBREG',
# [ and ] parenthesis imbalance
'REG_EBRACK',
# ( and ) parenthesis imbalance
'REG_EPAREN',
# { and } parenthesis imbalance
'REG_EBRACE',
# invalid content of {}
'REG_BADBR',
# invalid use of range operator
'REG_ERANGE',
# out of memory
# when this occurs it's almost certain a bug in this file
'REG_ESPACE',
# invalid use of repetition operator
# according to the documentation TRE never returns this error code
'REG_BADRPT',
]
try:
# first try to import the library by it's unixish soname
libtre = cdll.LoadLibrary('libtre.so.4')
except (WindowsError, OSError):
# the unix lib is not available, try the windows one
libtre = cdll.LoadLibrary('tre4.dll')
# create the custom types needed for TRE
# not all types are really custom, TRE uses a lot of standard C types
# but gives them only new names
# a regoff_t is just an ordinary c_int
regoff_t = c_int
class regex_t(Structure):
"""This is the regex_t structure as defined by TRE.
The exact field information was taken from the header files
of TRE - it was found in regex.h"""
_fields_ = [
('re_nsub', c_size_t),
('value', c_void_p),
]
# define a pointer type to regex_t structure
regex_p = POINTER(regex_t)
class regmatch_t(Structure):
"""A regmatch_t structure"""
_fields_ = [
('rm_so', regoff_t),
('rm_eo', regoff_t),
]
# a pointer type to the regmatch_t structure. This ist just the
# same thing as needed for the regex_p type
regmatch_p = POINTER(regmatch_t)
class regaparams_t(Structure):
"""A regaparams_t structure used for approximate matching
functions.
This class is internal and not part of the API and should
therefore not be used.
"""
_fields_ = [
('cost_ins', c_int),
('cost_del', c_int),
('cost_subst', c_int),
('max_cost', c_int),
('max_ins', c_int),
('max_del', c_int),
('max_subst', c_int),
('max_err', c_int)
]
regaparams_p = POINTER(regaparams_t)
class regamatch_t(Structure):
"""A regamatch_t structure used for approximate matching
functions
"""
_fields_ = [
('nmatch',c_size_t),
('pmatch', regmatch_p),
('cost', c_int),
('num_ins', c_int),
('num_del', c_int),
('num_subst', c_int)
]
regamatch_p = POINTER(regamatch_t)
# function definitions
# the regcomp() functions
libtre.regncomp.argtypes = [regex_p, POINTER(c_char), c_size_t, c_int]
libtre.regwncomp.argtypes = [regex_p, POINTER(c_wchar), c_size_t, c_int]
libtre.regfree.restype = None
libtre.regfree.argtypes = [regex_p]
# regexec() functions
libtre.regnexec.argtypes = [regex_p, POINTER(c_char), c_size_t, c_size_t, POINTER(regmatch_t), c_int]
libtre.regwnexec.argtypes = [regex_p, POINTER(c_wchar), c_size_t, c_size_t, POINTER(regmatch_t), c_int]
# tre_version()
libtre.tre_version.argtypes = []
libtre.tre_version.restype = c_char_p
# approximate matching functions
libtre.reganexec.argtypes = [regex_p, POINTER(c_char), c_size_t,
regamatch_p, regaparams_t, c_int]
libtre.regawnexec.argtypes = [regex_p, POINTER(c_wchar), c_size_t,
regamatch_p, regaparams_t, c_int]
def _get_specialized(action, string):
"""Returns tuple of (string_type, reg_function) that is used
to match the strings.
This is because TRE can match both char and wchar and
we need to decide which to use.
Action can be 'comp' for the compile functions or 'exec'
for the matching functions."""
if isinstance(string, unicode):
return c_wchar, getattr(libtre, 'regwn' + action)
else:
return c_char, getattr(libtre, 'regn' + action)
def _slice_string(string, pos=None, endpos=None):
if endpos:
string = string[:endpos]
if pos:
string = string[pos:]
return string
class Match(object):
"""A Match object that is returned as a result of a search"""
def __init__(self, match, cost=None, num_ins=None, num_del=None,
num_subst=None):
self.match = match
if cost is not None:
self.cost = cost
self.num = (num_ins, num_del, num_subst)
def groups(self):
return self.match[1:]
def group(self, index=None):
if index is None:
return ''.join(self.match)
return self.match[index]
class TREPattern(object):
"""This class represents a compiled regular expression"""
def __init__(self, pattern, flags=0):
"""
Constructor - see, the signature is the same as of re.compile
that can be very useful to retain API compatibility.
Note, the flags aren't yet implemented - REG_EXTENDED is used
for everything instead.
"""
if isinstance(pattern, basestring):
string_type, reg_function = _get_specialized('comp', pattern)
else:
raise TypeError("first argument must be string or unicode")
# the real compiled regex - a regex_t instance
self.preg = byref(regex_t())
pattern_buffer = (string_type * len(pattern))()
pattern_buffer.value = pattern
result = reg_function(self.preg, pattern_buffer, len(pattern),
REG_EXTENDED)
if reg_errcode_t[result] != 'REG_OK':
if reg_errcode_t[result] in ('REG_EBRACK', 'REG_EPAREN',
'REG_EBRACE'):
raise sre_constants.error("unbalanced parenthesis")
else:
raise sre_constants.error('Parse error, symbol %s code %d' %
(reg_errcode_t[result], result))
# how much memory to reserve
# refer to the re_nsub field of the regex_t
self.match_buffers = self.preg._obj.re_nsub + 1
def _exec(self, string):
pmatch = (regmatch_t * self.match_buffers)()
nmatch = c_size_t(self.match_buffers)
string_type, reg_function = _get_specialized('exec', string)
string_buffer = (string_type*len(string))()
string_buffer.value = string
result = reg_function(self.preg, string_buffer, len(string),
nmatch, pmatch, 0)
if reg_errcode_t[result] != 'REG_OK':
if reg_errcode_t[result] == 'REG_NOMATCH':
return None
else:
raise Exception('Exec error, status %s' % result)
return pmatch
def search(self, string, pos=None, endpos=None):
"""
Finds the first match of the pattern
"""
string = _slice_string(string, pos, endpos)
pmatch = self._exec(string)
if pmatch is None:
return None
matches = list()
for match in pmatch:
chunk = string[match.rm_so:match.rm_eo]
matches.append(chunk)
return Match(tuple(matches))
def match(self, string, pos=None, endpos=None):
string = _slice_string(string, pos, endpos)
pmatch = self._exec(string)
if pmatch is None:
return None
matches = list()
for match in pmatch:
if match.rm_so != 0:
break
chunk = string[match.rm_so:match.rm_eo]
matches.append(chunk)
if matches:
return Match(tuple(matches))
def approx(self, string, pos=None, endpos=None, cost_ins=0,
cost_del=0, cost_subst=0, max_costs=0,
max_ins=0, max_del=0, max_subst=0, max=0):
"""Like search but returns an approximate match"""
if endpos:
string = string[:endpos]
if pos:
string = string[pos:]
params = regaparams_t()
params.cost_ins = cost_ins
params.cost_del = cost_del
params.cost_subst = cost_subst
params.max_cost = max_costs
params.max_ins = max_ins
params.max_del = max_del
params.max_subst = max_subst
params.max_err = max
pmatch = (regmatch_t * self.match_buffers)()
amatch = regamatch_t()
amatch.nmatch = c_size_t(self.match_buffers)
amatch.pmatch = pmatch
string_type = c_char
reg_function = libtre.reganexec
if type(string) == unicode:
string_type = c_wchar
reg_function = libtre.regawnexec
string_buffer = (string_type*len(string))()
string_buffer.value = string
result = reg_function(self.preg, string_buffer, len(string),
byref(amatch), params, 0)
if reg_errcode_t[result] != 'REG_OK':
if result == REG_NOMATCH:
return None
else:
raise Exception('Exec error, status %s' % result)
matches = list()
for match in pmatch:
chunk = string[match.rm_so:match.rm_eo]
matches.append(chunk)
return Match(tuple(matches), amatch.cost, amatch.num_ins,
amatch.num_del, amatch.num_subst)
def finditer(self, string):
"""Returns an iterator with all matches"""
pmatch = (regmatch_t * self.match_buffers)()
nmatch = c_size_t(self.match_buffers)
# get the proper types and functions for the string
string_type, reg_function = _get_specialized('exec', string)
string_buffer = (string_type * len(string))()
string_buffer.value = string
# loop until no matches are found (REG_NOMATCH)
while True:
result = reg_function(self.preg, string_buffer, len(string),
nmatch, pmatch, 0)
if reg_errcode_t[result] == 'REG_NOMATCH':
raise StopIteration
elif reg_errcode_t[result] != 'REG_OK':
raise sre_constants.error('Exec error')
for match in pmatch:
yield string[match.rm_so:match.rm_eo]
# move string offset
string = string[match.rm_eo:]
string_buffer = (string_type * len(string))()
string_buffer.value = string
def findall(self, string):
"""Returns all matches in a list"""
return list(self.finditer(string))
def __del__(self):
"""Free any allocated preg structures"""
if hasattr(self, 'preg'):
libtre.regfree(self.preg)
def compile(*args, **kwargs):
"""Returns a compiled pattern.
Compiled patterns passed to this function are
returned without changes"""
pattern = args[0]
if isinstance(pattern, TREPattern):
return pattern
return TREPattern(*args, **kwargs)
def search(pattern, string):
"""Searches ``pattern`` in the ``string``.
Flags are currently not supported"""
pattern = compile(pattern)
return pattern.search(string)
def match(pattern, string):
"""Match ``pattern`` in the ``string``.
Flags are currently not supported"""
pattern = compile(pattern)
return pattern.match(string)
if __name__ == '__main__':
# this module is not meant to run stand-alone, so just display
# the version of TRE it uses.
# This can also be seen as a small self-test which shows whether
# TRE can be called at all
print libtre.tre_version()