blob: da422a9da57953a7a0b0b3219e538a8ecd756c7c [file] [log] [blame]
Guido van Rossum7627c0d2000-03-31 14:58:54 +00001#
2# Secret Labs' Regular Expression Engine
Guido van Rossum7627c0d2000-03-31 14:58:54 +00003#
4# convert template to internal format
5#
Fredrik Lundh770617b2001-01-14 15:06:11 +00006# Copyright (c) 1997-2001 by Secret Labs AB. All rights reserved.
Guido van Rossum7627c0d2000-03-31 14:58:54 +00007#
Fredrik Lundh29c4ba92000-08-01 18:20:07 +00008# See the sre.py file for information on usage and redistribution.
Guido van Rossum7627c0d2000-03-31 14:58:54 +00009#
10
Fred Drakeb8f22742001-09-04 19:10:20 +000011"""Internal support module for sre"""
12
Victor Stinner7fa767e2014-03-20 09:16:38 +010013import _sre
Christian Heimes5e696852008-04-09 08:37:03 +000014import sre_parse
Guido van Rossum7627c0d2000-03-31 14:58:54 +000015from sre_constants import *
16
Fredrik Lundhb35ffc02001-01-15 12:46:09 +000017assert _sre.MAGIC == MAGIC, "SRE module mismatch"
18
Martin v. Löwis78e2f062003-04-19 12:56:08 +000019if _sre.CODESIZE == 2:
20 MAXCODE = 65535
21else:
Guido van Rossume2a383d2007-01-15 16:59:06 +000022 MAXCODE = 0xFFFFFFFF
Fredrik Lundh3562f112000-07-02 12:00:07 +000023
Raymond Hettinger049ade22005-02-28 19:27:52 +000024_LITERAL_CODES = set([LITERAL, NOT_LITERAL])
25_REPEATING_CODES = set([REPEAT, MIN_REPEAT, MAX_REPEAT])
26_SUCCESS_CODES = set([SUCCESS, FAILURE])
27_ASSERT_CODES = set([ASSERT, ASSERT_NOT])
28
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000029def _compile(code, pattern, flags):
30 # internal: compile a (sub)pattern
31 emit = code.append
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000032 _len = len
Raymond Hettinger049ade22005-02-28 19:27:52 +000033 LITERAL_CODES = _LITERAL_CODES
34 REPEATING_CODES = _REPEATING_CODES
35 SUCCESS_CODES = _SUCCESS_CODES
36 ASSERT_CODES = _ASSERT_CODES
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000037 for op, av in pattern:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000038 if op in LITERAL_CODES:
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000039 if flags & SRE_FLAG_IGNORECASE:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020040 emit(OP_IGNORE[op])
Fredrik Lundh2e240442001-01-15 18:28:14 +000041 emit(_sre.getlower(av, flags))
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000042 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020043 emit(op)
Fredrik Lundh2e240442001-01-15 18:28:14 +000044 emit(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000045 elif op is IN:
46 if flags & SRE_FLAG_IGNORECASE:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020047 emit(OP_IGNORE[op])
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000048 def fixup(literal, flags=flags):
49 return _sre.getlower(literal, flags)
50 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020051 emit(op)
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +020052 fixup = None
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000053 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000054 _compile_charset(av, flags, code, fixup)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000055 code[skip] = _len(code) - skip
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000056 elif op is ANY:
57 if flags & SRE_FLAG_DOTALL:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020058 emit(ANY_ALL)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000059 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020060 emit(ANY)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000061 elif op in REPEATING_CODES:
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000062 if flags & SRE_FLAG_TEMPLATE:
Collin Winterce36ad82007-08-30 01:19:48 +000063 raise error("internal: unsupported template operator")
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000064 elif _simple(av) and op is not REPEAT:
65 if op is MAX_REPEAT:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020066 emit(REPEAT_ONE)
Guido van Rossum41c99e72003-04-14 17:59:34 +000067 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020068 emit(MIN_REPEAT_ONE)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000069 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000070 emit(av[0])
71 emit(av[1])
72 _compile(code, av[2], flags)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020073 emit(SUCCESS)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000074 code[skip] = _len(code) - skip
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000075 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020076 emit(REPEAT)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000077 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000078 emit(av[0])
79 emit(av[1])
80 _compile(code, av[2], flags)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000081 code[skip] = _len(code) - skip
82 if op is MAX_REPEAT:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020083 emit(MAX_UNTIL)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000084 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020085 emit(MIN_UNTIL)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000086 elif op is SUBPATTERN:
87 if av[0]:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020088 emit(MARK)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000089 emit((av[0]-1)*2)
90 # _compile_info(code, av[1], flags)
91 _compile(code, av[1], flags)
92 if av[0]:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020093 emit(MARK)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000094 emit((av[0]-1)*2+1)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000095 elif op in SUCCESS_CODES:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020096 emit(op)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000097 elif op in ASSERT_CODES:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +020098 emit(op)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000099 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000100 if av[0] >= 0:
101 emit(0) # look ahead
102 else:
103 lo, hi = av[1].getwidth()
104 if lo != hi:
Collin Winterce36ad82007-08-30 01:19:48 +0000105 raise error("look-behind requires fixed-width pattern")
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000106 emit(lo) # look behind
107 _compile(code, av[1], flags)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200108 emit(SUCCESS)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000109 code[skip] = _len(code) - skip
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000110 elif op is CALL:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200111 emit(op)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000112 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000113 _compile(code, av, flags)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200114 emit(SUCCESS)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000115 code[skip] = _len(code) - skip
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000116 elif op is AT:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200117 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000118 if flags & SRE_FLAG_MULTILINE:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000119 av = AT_MULTILINE.get(av, av)
120 if flags & SRE_FLAG_LOCALE:
121 av = AT_LOCALE.get(av, av)
122 elif flags & SRE_FLAG_UNICODE:
123 av = AT_UNICODE.get(av, av)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200124 emit(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000125 elif op is BRANCH:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200126 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000127 tail = []
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000128 tailappend = tail.append
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000129 for av in av[1]:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000130 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000131 # _compile_info(code, av, flags)
132 _compile(code, av, flags)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200133 emit(JUMP)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000134 tailappend(_len(code)); emit(0)
135 code[skip] = _len(code) - skip
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000136 emit(0) # end of branch
137 for tail in tail:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000138 code[tail] = _len(code) - tail
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000139 elif op is CATEGORY:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200140 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000141 if flags & SRE_FLAG_LOCALE:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000142 av = CH_LOCALE[av]
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000143 elif flags & SRE_FLAG_UNICODE:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000144 av = CH_UNICODE[av]
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200145 emit(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000146 elif op is GROUPREF:
147 if flags & SRE_FLAG_IGNORECASE:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200148 emit(OP_IGNORE[op])
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000149 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200150 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000151 emit(av-1)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000152 elif op is GROUPREF_EXISTS:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200153 emit(op)
Andrew M. Kuchlingc30faa82005-06-02 13:35:52 +0000154 emit(av[0]-1)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000155 skipyes = _len(code); emit(0)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000156 _compile(code, av[1], flags)
157 if av[2]:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200158 emit(JUMP)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000159 skipno = _len(code); emit(0)
160 code[skipyes] = _len(code) - skipyes + 1
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000161 _compile(code, av[2], flags)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000162 code[skipno] = _len(code) - skipno
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000163 else:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000164 code[skipyes] = _len(code) - skipyes + 1
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000165 else:
Collin Winterce36ad82007-08-30 01:19:48 +0000166 raise ValueError("unsupported operand type", op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000167
168def _compile_charset(charset, flags, code, fixup=None):
169 # compile charset subprogram
170 emit = code.append
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000171 for op, av in _optimize_charset(charset, fixup):
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200172 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000173 if op is NEGATE:
174 pass
175 elif op is LITERAL:
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200176 emit(av)
177 elif op is RANGE or op is RANGE_IGNORE:
178 emit(av[0])
179 emit(av[1])
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000180 elif op is CHARSET:
181 code.extend(av)
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000182 elif op is BIGCHARSET:
183 code.extend(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000184 elif op is CATEGORY:
185 if flags & SRE_FLAG_LOCALE:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200186 emit(CH_LOCALE[av])
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000187 elif flags & SRE_FLAG_UNICODE:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200188 emit(CH_UNICODE[av])
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000189 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200190 emit(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000191 else:
Collin Winterce36ad82007-08-30 01:19:48 +0000192 raise error("internal: unsupported set operator")
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200193 emit(FAILURE)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000194
195def _optimize_charset(charset, fixup):
196 # internal: optimize character set
Fredrik Lundh3562f112000-07-02 12:00:07 +0000197 out = []
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200198 tail = []
199 charmap = bytearray(256)
200 for op, av in charset:
201 while True:
202 try:
203 if op is LITERAL:
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200204 if fixup:
205 av = fixup(av)
206 charmap[av] = 1
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200207 elif op is RANGE:
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200208 r = range(av[0], av[1]+1)
209 if fixup:
210 r = map(fixup, r)
211 for i in r:
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200212 charmap[i] = 1
213 elif op is NEGATE:
214 out.append((op, av))
215 else:
216 tail.append((op, av))
217 except IndexError:
218 if len(charmap) == 256:
219 # character set contains non-UCS1 character codes
220 charmap += b'\0' * 0xff00
221 continue
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200222 # Character set contains non-BMP character codes.
223 # There are only two ranges of cased non-BMP characters:
224 # 10400-1044F (Deseret) and 118A0-118DF (Warang Citi),
225 # and for both ranges RANGE_IGNORE works.
226 if fixup and op is RANGE:
227 op = RANGE_IGNORE
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200228 tail.append((op, av))
229 break
230
Fredrik Lundh3562f112000-07-02 12:00:07 +0000231 # compress character map
Fredrik Lundh3562f112000-07-02 12:00:07 +0000232 runs = []
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200233 q = 0
234 while True:
235 p = charmap.find(1, q)
236 if p < 0:
237 break
238 if len(runs) >= 2:
239 runs = None
240 break
241 q = charmap.find(0, p)
242 if q < 0:
243 runs.append((p, len(charmap)))
244 break
245 runs.append((p, q))
246 if runs is not None:
Fredrik Lundh3562f112000-07-02 12:00:07 +0000247 # use literal/range
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200248 for p, q in runs:
249 if q - p == 1:
250 out.append((LITERAL, p))
Fredrik Lundh3562f112000-07-02 12:00:07 +0000251 else:
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200252 out.append((RANGE, (p, q - 1)))
253 out += tail
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200254 # if the case was changed or new representation is more compact
255 if fixup or len(out) < len(charset):
Fredrik Lundh3562f112000-07-02 12:00:07 +0000256 return out
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200257 # else original character set is good enough
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200258 return charset
259
260 # use bitmap
261 if len(charmap) == 256:
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000262 data = _mk_bitmap(charmap)
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200263 out.append((CHARSET, data))
264 out += tail
Fredrik Lundh3562f112000-07-02 12:00:07 +0000265 return out
Fredrik Lundh3562f112000-07-02 12:00:07 +0000266
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200267 # To represent a big charset, first a bitmap of all characters in the
268 # set is constructed. Then, this bitmap is sliced into chunks of 256
269 # characters, duplicate chunks are eliminated, and each chunk is
270 # given a number. In the compiled expression, the charset is
271 # represented by a 32-bit word sequence, consisting of one word for
272 # the number of different chunks, a sequence of 256 bytes (64 words)
273 # of chunk numbers indexed by their original chunk position, and a
274 # sequence of 256-bit chunks (8 words each).
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000275
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200276 # Compression is normally good: in a typical charset, large ranges of
277 # Unicode will be either completely excluded (e.g. if only cyrillic
278 # letters are to be matched), or completely included (e.g. if large
279 # subranges of Kanji match). These ranges will be represented by
280 # chunks of all one-bits or all zero-bits.
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000281
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200282 # Matching can be also done efficiently: the more significant byte of
283 # the Unicode character is an index into the chunk number, and the
284 # less significant byte is a bit index in the chunk (just like the
285 # CHARSET matching).
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000286
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200287 charmap = bytes(charmap) # should be hashable
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000288 comps = {}
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200289 mapping = bytearray(256)
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000290 block = 0
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200291 data = bytearray()
292 for i in range(0, 65536, 256):
293 chunk = charmap[i: i + 256]
294 if chunk in comps:
295 mapping[i // 256] = comps[chunk]
296 else:
297 mapping[i // 256] = comps[chunk] = block
298 block += 1
299 data += chunk
300 data = _mk_bitmap(data)
301 data[0:0] = [block] + _bytes_to_codes(mapping)
302 out.append((BIGCHARSET, data))
303 out += tail
304 return out
305
306_CODEBITS = _sre.CODESIZE * 8
307_BITS_TRANS = b'0' + b'1' * 255
308def _mk_bitmap(bits, _CODEBITS=_CODEBITS, _int=int):
309 s = bits.translate(_BITS_TRANS)[::-1]
310 return [_int(s[i - _CODEBITS: i], 2)
311 for i in range(len(s), 0, -_CODEBITS)]
312
313def _bytes_to_codes(b):
314 # Convert block indices to word array
315 import array
316 a = array.array('I', b)
317 assert a.itemsize == _sre.CODESIZE
318 assert len(a) * a.itemsize == len(b)
319 return a.tolist()
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000320
Fredrik Lundhe1869832000-08-01 22:47:49 +0000321def _simple(av):
322 # check if av is a "simple" operator
323 lo, hi = av[2].getwidth()
Fredrik Lundhe1869832000-08-01 22:47:49 +0000324 return lo == hi == 1 and av[2][0][0] != SUBPATTERN
325
Antoine Pitrou79aa68d2013-10-25 21:36:10 +0200326def _generate_overlap_table(prefix):
327 """
328 Generate an overlap table for the following prefix.
329 An overlap table is a table of the same size as the prefix which
330 informs about the potential self-overlap for each index in the prefix:
331 - if overlap[i] == 0, prefix[i:] can't overlap prefix[0:...]
332 - if overlap[i] == k with 0 < k <= i, prefix[i-k+1:i+1] overlaps with
333 prefix[0:k]
334 """
335 table = [0] * len(prefix)
336 for i in range(1, len(prefix)):
337 idx = table[i - 1]
338 while prefix[i] != prefix[idx]:
339 if idx == 0:
340 table[i] = 0
341 break
342 idx = table[idx - 1]
343 else:
344 table[i] = idx + 1
345 return table
346
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000347def _compile_info(code, pattern, flags):
348 # internal: compile an info block. in the current version,
Fredrik Lundh3562f112000-07-02 12:00:07 +0000349 # this contains min/max pattern width, and an optional literal
350 # prefix or a character map
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000351 lo, hi = pattern.getwidth()
352 if lo == 0:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000353 return # not worth it
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000354 # look for a literal prefix
355 prefix = []
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000356 prefixappend = prefix.append
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000357 prefix_skip = 0
Fredrik Lundh3562f112000-07-02 12:00:07 +0000358 charset = [] # not used
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000359 charsetappend = charset.append
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000360 if not (flags & SRE_FLAG_IGNORECASE):
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000361 # look for literal prefix
Fredrik Lundh90a07912000-06-30 07:50:59 +0000362 for op, av in pattern.data:
363 if op is LITERAL:
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000364 if len(prefix) == prefix_skip:
365 prefix_skip = prefix_skip + 1
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000366 prefixappend(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000367 elif op is SUBPATTERN and len(av[1]) == 1:
368 op, av = av[1][0]
369 if op is LITERAL:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000370 prefixappend(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000371 else:
372 break
Fredrik Lundh90a07912000-06-30 07:50:59 +0000373 else:
374 break
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000375 # if no prefix, look for charset prefix
376 if not prefix and pattern.data:
377 op, av = pattern.data[0]
378 if op is SUBPATTERN and av[1]:
379 op, av = av[1][0]
380 if op is LITERAL:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000381 charsetappend((op, av))
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000382 elif op is BRANCH:
383 c = []
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000384 cappend = c.append
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000385 for p in av[1]:
386 if not p:
387 break
388 op, av = p[0]
389 if op is LITERAL:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000390 cappend((op, av))
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000391 else:
392 break
393 else:
394 charset = c
395 elif op is BRANCH:
396 c = []
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000397 cappend = c.append
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000398 for p in av[1]:
399 if not p:
400 break
401 op, av = p[0]
402 if op is LITERAL:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000403 cappend((op, av))
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000404 else:
405 break
406 else:
407 charset = c
408 elif op is IN:
409 charset = av
410## if prefix:
411## print "*** PREFIX", prefix, prefix_skip
412## if charset:
413## print "*** CHARSET", charset
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000414 # add an info block
415 emit = code.append
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200416 emit(INFO)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000417 skip = len(code); emit(0)
418 # literal flag
419 mask = 0
Fredrik Lundh3562f112000-07-02 12:00:07 +0000420 if prefix:
421 mask = SRE_INFO_PREFIX
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000422 if len(prefix) == prefix_skip == len(pattern.data):
Fredrik Lundh3562f112000-07-02 12:00:07 +0000423 mask = mask + SRE_INFO_LITERAL
424 elif charset:
425 mask = mask + SRE_INFO_CHARSET
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000426 emit(mask)
427 # pattern length
Fredrik Lundh3562f112000-07-02 12:00:07 +0000428 if lo < MAXCODE:
429 emit(lo)
430 else:
431 emit(MAXCODE)
432 prefix = prefix[:MAXCODE]
433 if hi < MAXCODE:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000434 emit(hi)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000435 else:
Fredrik Lundh90a07912000-06-30 07:50:59 +0000436 emit(0)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000437 # add literal prefix
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000438 if prefix:
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000439 emit(len(prefix)) # length
440 emit(prefix_skip) # skip
441 code.extend(prefix)
442 # generate overlap table
Antoine Pitrou79aa68d2013-10-25 21:36:10 +0200443 code.extend(_generate_overlap_table(prefix))
Fredrik Lundh3562f112000-07-02 12:00:07 +0000444 elif charset:
Guido van Rossum577fb5a2003-02-24 01:18:35 +0000445 _compile_charset(charset, flags, code)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000446 code[skip] = len(code) - skip
447
Just van Rossum74902502003-07-02 21:37:16 +0000448def isstring(obj):
Thomas Wouters40a088d2008-03-18 20:19:54 +0000449 return isinstance(obj, (str, bytes))
Just van Rossum74902502003-07-02 21:37:16 +0000450
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +0000451def _code(p, flags):
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000452
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000453 flags = p.pattern.flags | flags
Fredrik Lundhbe2211e2000-06-29 16:57:40 +0000454 code = []
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000455
456 # compile info block
457 _compile_info(code, p, flags)
458
459 # compile the pattern
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000460 _compile(code, p.data, flags)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000461
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200462 code.append(SUCCESS)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000463
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000464 return code
465
466def compile(p, flags=0):
467 # internal: convert pattern list to internal format
468
Just van Rossum74902502003-07-02 21:37:16 +0000469 if isstring(p):
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000470 pattern = p
471 p = sre_parse.parse(p, flags)
472 else:
473 pattern = None
474
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +0000475 code = _code(p, flags)
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000476
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200477 # print(code)
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000478
Fredrik Lundhc2301732000-07-02 22:25:39 +0000479 # map in either direction
480 groupindex = p.pattern.groupdict
481 indexgroup = [None] * p.pattern.groups
482 for k, i in groupindex.items():
483 indexgroup[i] = k
484
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000485 return _sre.compile(
Christian Heimes072c0f12008-01-03 23:01:04 +0000486 pattern, flags | p.pattern.flags, code,
Fredrik Lundh6f013982000-07-03 18:44:21 +0000487 p.pattern.groups-1,
488 groupindex, indexgroup
Fredrik Lundh90a07912000-06-30 07:50:59 +0000489 )