blob: db8b8a2778f582535675a666d7bb94612b325236 [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
Raymond Hettingerdf1b6992014-11-09 15:56:33 -080019_LITERAL_CODES = {LITERAL, NOT_LITERAL}
20_REPEATING_CODES = {REPEAT, MIN_REPEAT, MAX_REPEAT}
21_SUCCESS_CODES = {SUCCESS, FAILURE}
22_ASSERT_CODES = {ASSERT, ASSERT_NOT}
Raymond Hettinger049ade22005-02-28 19:27:52 +000023
Serhiy Storchaka0c938f62014-11-10 12:37:16 +020024# Sets of lowercase characters which have the same uppercase.
25_equivalences = (
26 # LATIN SMALL LETTER I, LATIN SMALL LETTER DOTLESS I
27 (0x69, 0x131), # iı
28 # LATIN SMALL LETTER S, LATIN SMALL LETTER LONG S
29 (0x73, 0x17f), # sſ
30 # MICRO SIGN, GREEK SMALL LETTER MU
31 (0xb5, 0x3bc), # µμ
32 # COMBINING GREEK YPOGEGRAMMENI, GREEK SMALL LETTER IOTA, GREEK PROSGEGRAMMENI
33 (0x345, 0x3b9, 0x1fbe), # \u0345ιι
34 # GREEK SMALL LETTER IOTA WITH DIALYTIKA AND TONOS, GREEK SMALL LETTER IOTA WITH DIALYTIKA AND OXIA
35 (0x390, 0x1fd3), # ΐΐ
36 # GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND TONOS, GREEK SMALL LETTER UPSILON WITH DIALYTIKA AND OXIA
37 (0x3b0, 0x1fe3), # ΰΰ
38 # GREEK SMALL LETTER BETA, GREEK BETA SYMBOL
39 (0x3b2, 0x3d0), # βϐ
40 # GREEK SMALL LETTER EPSILON, GREEK LUNATE EPSILON SYMBOL
41 (0x3b5, 0x3f5), # εϵ
42 # GREEK SMALL LETTER THETA, GREEK THETA SYMBOL
43 (0x3b8, 0x3d1), # θϑ
44 # GREEK SMALL LETTER KAPPA, GREEK KAPPA SYMBOL
45 (0x3ba, 0x3f0), # κϰ
46 # GREEK SMALL LETTER PI, GREEK PI SYMBOL
47 (0x3c0, 0x3d6), # πϖ
48 # GREEK SMALL LETTER RHO, GREEK RHO SYMBOL
49 (0x3c1, 0x3f1), # ρϱ
50 # GREEK SMALL LETTER FINAL SIGMA, GREEK SMALL LETTER SIGMA
51 (0x3c2, 0x3c3), # ςσ
52 # GREEK SMALL LETTER PHI, GREEK PHI SYMBOL
53 (0x3c6, 0x3d5), # φϕ
54 # LATIN SMALL LETTER S WITH DOT ABOVE, LATIN SMALL LETTER LONG S WITH DOT ABOVE
55 (0x1e61, 0x1e9b), # ṡẛ
56 # LATIN SMALL LIGATURE LONG S T, LATIN SMALL LIGATURE ST
57 (0xfb05, 0xfb06), # ſtst
58)
59
60# Maps the lowercase code to lowercase codes which have the same uppercase.
61_ignorecase_fixes = {i: tuple(j for j in t if i != j)
62 for t in _equivalences for i in t}
63
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000064def _compile(code, pattern, flags):
65 # internal: compile a (sub)pattern
66 emit = code.append
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000067 _len = len
Raymond Hettinger049ade22005-02-28 19:27:52 +000068 LITERAL_CODES = _LITERAL_CODES
69 REPEATING_CODES = _REPEATING_CODES
70 SUCCESS_CODES = _SUCCESS_CODES
71 ASSERT_CODES = _ASSERT_CODES
Serhiy Storchaka7186cc22017-05-05 10:42:46 +030072 tolower = None
73 fixes = None
74 if flags & SRE_FLAG_IGNORECASE and not flags & SRE_FLAG_LOCALE:
75 if flags & SRE_FLAG_UNICODE and not flags & SRE_FLAG_ASCII:
76 tolower = _sre.unicode_tolower
77 fixes = _ignorecase_fixes
78 else:
79 tolower = _sre.ascii_tolower
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000080 for op, av in pattern:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000081 if op in LITERAL_CODES:
Serhiy Storchaka898ff032017-05-05 08:53:40 +030082 if not flags & SRE_FLAG_IGNORECASE:
83 emit(op)
84 emit(av)
85 elif flags & SRE_FLAG_LOCALE:
86 emit(OP_LOC_IGNORE[op])
87 emit(av)
88 else:
Serhiy Storchaka7186cc22017-05-05 10:42:46 +030089 lo = tolower(av)
Serhiy Storchaka0c938f62014-11-10 12:37:16 +020090 if fixes and lo in fixes:
Serhiy Storchaka5619ab92014-11-10 12:43:14 +020091 emit(IN_IGNORE)
Serhiy Storchaka0c938f62014-11-10 12:37:16 +020092 skip = _len(code); emit(0)
93 if op is NOT_LITERAL:
Serhiy Storchaka5619ab92014-11-10 12:43:14 +020094 emit(NEGATE)
Serhiy Storchaka0c938f62014-11-10 12:37:16 +020095 for k in (lo,) + fixes[lo]:
Serhiy Storchaka5619ab92014-11-10 12:43:14 +020096 emit(LITERAL)
Serhiy Storchaka0c938f62014-11-10 12:37:16 +020097 emit(k)
Serhiy Storchaka5619ab92014-11-10 12:43:14 +020098 emit(FAILURE)
Serhiy Storchaka0c938f62014-11-10 12:37:16 +020099 code[skip] = _len(code) - skip
100 else:
Serhiy Storchaka5619ab92014-11-10 12:43:14 +0200101 emit(OP_IGNORE[op])
Serhiy Storchaka0c938f62014-11-10 12:37:16 +0200102 emit(lo)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000103 elif op is IN:
Serhiy Storchaka898ff032017-05-05 08:53:40 +0300104 if not flags & SRE_FLAG_IGNORECASE:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200105 emit(op)
Serhiy Storchaka898ff032017-05-05 08:53:40 +0300106 elif flags & SRE_FLAG_LOCALE:
107 emit(IN_LOC_IGNORE)
Serhiy Storchaka898ff032017-05-05 08:53:40 +0300108 else:
109 emit(IN_IGNORE)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000110 skip = _len(code); emit(0)
Serhiy Storchaka7186cc22017-05-05 10:42:46 +0300111 _compile_charset(av, flags, code, tolower, fixes)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000112 code[skip] = _len(code) - skip
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000113 elif op is ANY:
114 if flags & SRE_FLAG_DOTALL:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200115 emit(ANY_ALL)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000116 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200117 emit(ANY)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000118 elif op in REPEATING_CODES:
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000119 if flags & SRE_FLAG_TEMPLATE:
Serhiy Storchaka632a77e2015-03-25 21:03:47 +0200120 raise error("internal: unsupported template operator %r" % (op,))
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000121 elif _simple(av) and op is not REPEAT:
122 if op is MAX_REPEAT:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200123 emit(REPEAT_ONE)
Guido van Rossum41c99e72003-04-14 17:59:34 +0000124 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200125 emit(MIN_REPEAT_ONE)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000126 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000127 emit(av[0])
128 emit(av[1])
129 _compile(code, av[2], flags)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200130 emit(SUCCESS)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000131 code[skip] = _len(code) - skip
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000132 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200133 emit(REPEAT)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000134 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000135 emit(av[0])
136 emit(av[1])
137 _compile(code, av[2], flags)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000138 code[skip] = _len(code) - skip
139 if op is MAX_REPEAT:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200140 emit(MAX_UNTIL)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000141 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200142 emit(MIN_UNTIL)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000143 elif op is SUBPATTERN:
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300144 group, add_flags, del_flags, p = av
145 if group:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200146 emit(MARK)
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300147 emit((group-1)*2)
148 # _compile_info(code, p, (flags | add_flags) & ~del_flags)
149 _compile(code, p, (flags | add_flags) & ~del_flags)
150 if group:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200151 emit(MARK)
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300152 emit((group-1)*2+1)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000153 elif op in SUCCESS_CODES:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200154 emit(op)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000155 elif op in ASSERT_CODES:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200156 emit(op)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000157 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000158 if av[0] >= 0:
159 emit(0) # look ahead
160 else:
161 lo, hi = av[1].getwidth()
162 if lo != hi:
Collin Winterce36ad82007-08-30 01:19:48 +0000163 raise error("look-behind requires fixed-width pattern")
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000164 emit(lo) # look behind
165 _compile(code, av[1], flags)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200166 emit(SUCCESS)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000167 code[skip] = _len(code) - skip
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000168 elif op is CALL:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200169 emit(op)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000170 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000171 _compile(code, av, flags)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200172 emit(SUCCESS)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000173 code[skip] = _len(code) - skip
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000174 elif op is AT:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200175 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000176 if flags & SRE_FLAG_MULTILINE:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000177 av = AT_MULTILINE.get(av, av)
178 if flags & SRE_FLAG_LOCALE:
179 av = AT_LOCALE.get(av, av)
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300180 elif (flags & SRE_FLAG_UNICODE) and not (flags & SRE_FLAG_ASCII):
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000181 av = AT_UNICODE.get(av, av)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200182 emit(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000183 elif op is BRANCH:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200184 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000185 tail = []
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000186 tailappend = tail.append
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000187 for av in av[1]:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000188 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000189 # _compile_info(code, av, flags)
190 _compile(code, av, flags)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200191 emit(JUMP)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000192 tailappend(_len(code)); emit(0)
193 code[skip] = _len(code) - skip
Serhiy Storchakaab140882014-11-11 21:13:28 +0200194 emit(FAILURE) # end of branch
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000195 for tail in tail:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000196 code[tail] = _len(code) - tail
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000197 elif op is CATEGORY:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200198 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000199 if flags & SRE_FLAG_LOCALE:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000200 av = CH_LOCALE[av]
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300201 elif (flags & SRE_FLAG_UNICODE) and not (flags & SRE_FLAG_ASCII):
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000202 av = CH_UNICODE[av]
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200203 emit(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000204 elif op is GROUPREF:
205 if flags & SRE_FLAG_IGNORECASE:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200206 emit(OP_IGNORE[op])
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000207 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200208 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000209 emit(av-1)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000210 elif op is GROUPREF_EXISTS:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200211 emit(op)
Andrew M. Kuchlingc30faa82005-06-02 13:35:52 +0000212 emit(av[0]-1)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000213 skipyes = _len(code); emit(0)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000214 _compile(code, av[1], flags)
215 if av[2]:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200216 emit(JUMP)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000217 skipno = _len(code); emit(0)
218 code[skipyes] = _len(code) - skipyes + 1
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000219 _compile(code, av[2], flags)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000220 code[skipno] = _len(code) - skipno
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000221 else:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000222 code[skipyes] = _len(code) - skipyes + 1
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000223 else:
Serhiy Storchaka632a77e2015-03-25 21:03:47 +0200224 raise error("internal: unsupported operand type %r" % (op,))
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000225
Serhiy Storchaka0c938f62014-11-10 12:37:16 +0200226def _compile_charset(charset, flags, code, fixup=None, fixes=None):
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000227 # compile charset subprogram
228 emit = code.append
Serhiy Storchaka5619ab92014-11-10 12:43:14 +0200229 for op, av in _optimize_charset(charset, fixup, fixes):
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200230 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000231 if op is NEGATE:
232 pass
233 elif op is LITERAL:
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200234 emit(av)
235 elif op is RANGE or op is RANGE_IGNORE:
236 emit(av[0])
237 emit(av[1])
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000238 elif op is CHARSET:
239 code.extend(av)
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000240 elif op is BIGCHARSET:
241 code.extend(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000242 elif op is CATEGORY:
243 if flags & SRE_FLAG_LOCALE:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200244 emit(CH_LOCALE[av])
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300245 elif (flags & SRE_FLAG_UNICODE) and not (flags & SRE_FLAG_ASCII):
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200246 emit(CH_UNICODE[av])
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000247 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200248 emit(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000249 else:
Serhiy Storchaka632a77e2015-03-25 21:03:47 +0200250 raise error("internal: unsupported set operator %r" % (op,))
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200251 emit(FAILURE)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000252
Serhiy Storchaka5619ab92014-11-10 12:43:14 +0200253def _optimize_charset(charset, fixup, fixes):
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000254 # internal: optimize character set
Fredrik Lundh3562f112000-07-02 12:00:07 +0000255 out = []
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200256 tail = []
257 charmap = bytearray(256)
258 for op, av in charset:
259 while True:
260 try:
261 if op is LITERAL:
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200262 if fixup:
Serhiy Storchaka5619ab92014-11-10 12:43:14 +0200263 lo = fixup(av)
264 charmap[lo] = 1
265 if fixes and lo in fixes:
266 for k in fixes[lo]:
Serhiy Storchaka0c938f62014-11-10 12:37:16 +0200267 charmap[k] = 1
268 else:
269 charmap[av] = 1
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200270 elif op is RANGE:
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200271 r = range(av[0], av[1]+1)
272 if fixup:
273 r = map(fixup, r)
Serhiy Storchaka0c938f62014-11-10 12:37:16 +0200274 if fixup and fixes:
275 for i in r:
276 charmap[i] = 1
277 if i in fixes:
278 for k in fixes[i]:
279 charmap[k] = 1
280 else:
281 for i in r:
282 charmap[i] = 1
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200283 elif op is NEGATE:
284 out.append((op, av))
285 else:
286 tail.append((op, av))
287 except IndexError:
288 if len(charmap) == 256:
289 # character set contains non-UCS1 character codes
290 charmap += b'\0' * 0xff00
291 continue
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200292 # Character set contains non-BMP character codes.
293 # There are only two ranges of cased non-BMP characters:
294 # 10400-1044F (Deseret) and 118A0-118DF (Warang Citi),
295 # and for both ranges RANGE_IGNORE works.
296 if fixup and op is RANGE:
297 op = RANGE_IGNORE
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200298 tail.append((op, av))
299 break
300
Fredrik Lundh3562f112000-07-02 12:00:07 +0000301 # compress character map
Fredrik Lundh3562f112000-07-02 12:00:07 +0000302 runs = []
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200303 q = 0
304 while True:
305 p = charmap.find(1, q)
306 if p < 0:
307 break
308 if len(runs) >= 2:
309 runs = None
310 break
311 q = charmap.find(0, p)
312 if q < 0:
313 runs.append((p, len(charmap)))
314 break
315 runs.append((p, q))
316 if runs is not None:
Fredrik Lundh3562f112000-07-02 12:00:07 +0000317 # use literal/range
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200318 for p, q in runs:
319 if q - p == 1:
320 out.append((LITERAL, p))
Fredrik Lundh3562f112000-07-02 12:00:07 +0000321 else:
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200322 out.append((RANGE, (p, q - 1)))
323 out += tail
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200324 # if the case was changed or new representation is more compact
325 if fixup or len(out) < len(charset):
Fredrik Lundh3562f112000-07-02 12:00:07 +0000326 return out
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200327 # else original character set is good enough
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200328 return charset
329
330 # use bitmap
331 if len(charmap) == 256:
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000332 data = _mk_bitmap(charmap)
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200333 out.append((CHARSET, data))
334 out += tail
Fredrik Lundh3562f112000-07-02 12:00:07 +0000335 return out
Fredrik Lundh3562f112000-07-02 12:00:07 +0000336
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200337 # To represent a big charset, first a bitmap of all characters in the
338 # set is constructed. Then, this bitmap is sliced into chunks of 256
339 # characters, duplicate chunks are eliminated, and each chunk is
340 # given a number. In the compiled expression, the charset is
341 # represented by a 32-bit word sequence, consisting of one word for
342 # the number of different chunks, a sequence of 256 bytes (64 words)
343 # of chunk numbers indexed by their original chunk position, and a
344 # sequence of 256-bit chunks (8 words each).
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000345
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200346 # Compression is normally good: in a typical charset, large ranges of
347 # Unicode will be either completely excluded (e.g. if only cyrillic
348 # letters are to be matched), or completely included (e.g. if large
349 # subranges of Kanji match). These ranges will be represented by
350 # chunks of all one-bits or all zero-bits.
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000351
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200352 # Matching can be also done efficiently: the more significant byte of
353 # the Unicode character is an index into the chunk number, and the
354 # less significant byte is a bit index in the chunk (just like the
355 # CHARSET matching).
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000356
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200357 charmap = bytes(charmap) # should be hashable
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000358 comps = {}
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200359 mapping = bytearray(256)
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000360 block = 0
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200361 data = bytearray()
362 for i in range(0, 65536, 256):
363 chunk = charmap[i: i + 256]
364 if chunk in comps:
365 mapping[i // 256] = comps[chunk]
366 else:
367 mapping[i // 256] = comps[chunk] = block
368 block += 1
369 data += chunk
370 data = _mk_bitmap(data)
371 data[0:0] = [block] + _bytes_to_codes(mapping)
372 out.append((BIGCHARSET, data))
373 out += tail
374 return out
375
376_CODEBITS = _sre.CODESIZE * 8
Serhiy Storchakaab140882014-11-11 21:13:28 +0200377MAXCODE = (1 << _CODEBITS) - 1
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200378_BITS_TRANS = b'0' + b'1' * 255
379def _mk_bitmap(bits, _CODEBITS=_CODEBITS, _int=int):
380 s = bits.translate(_BITS_TRANS)[::-1]
381 return [_int(s[i - _CODEBITS: i], 2)
382 for i in range(len(s), 0, -_CODEBITS)]
383
384def _bytes_to_codes(b):
385 # Convert block indices to word array
Serhiy Storchaka19e91582014-11-10 13:24:47 +0200386 a = memoryview(b).cast('I')
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200387 assert a.itemsize == _sre.CODESIZE
388 assert len(a) * a.itemsize == len(b)
389 return a.tolist()
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000390
Fredrik Lundhe1869832000-08-01 22:47:49 +0000391def _simple(av):
392 # check if av is a "simple" operator
393 lo, hi = av[2].getwidth()
Fredrik Lundhe1869832000-08-01 22:47:49 +0000394 return lo == hi == 1 and av[2][0][0] != SUBPATTERN
395
Antoine Pitrou79aa68d2013-10-25 21:36:10 +0200396def _generate_overlap_table(prefix):
397 """
398 Generate an overlap table for the following prefix.
399 An overlap table is a table of the same size as the prefix which
400 informs about the potential self-overlap for each index in the prefix:
401 - if overlap[i] == 0, prefix[i:] can't overlap prefix[0:...]
402 - if overlap[i] == k with 0 < k <= i, prefix[i-k+1:i+1] overlaps with
403 prefix[0:k]
404 """
405 table = [0] * len(prefix)
406 for i in range(1, len(prefix)):
407 idx = table[i - 1]
408 while prefix[i] != prefix[idx]:
409 if idx == 0:
410 table[i] = 0
411 break
412 idx = table[idx - 1]
413 else:
414 table[i] = idx + 1
415 return table
416
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300417def _get_literal_prefix(pattern):
418 # look for literal prefix
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000419 prefix = []
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000420 prefixappend = prefix.append
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300421 prefix_skip = None
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300422 for op, av in pattern.data:
423 if op is LITERAL:
424 prefixappend(av)
425 elif op is SUBPATTERN:
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300426 group, add_flags, del_flags, p = av
427 if add_flags & SRE_FLAG_IGNORECASE:
428 break
429 prefix1, prefix_skip1, got_all = _get_literal_prefix(p)
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300430 if prefix_skip is None:
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300431 if group is not None:
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300432 prefix_skip = len(prefix)
433 elif prefix_skip1 is not None:
434 prefix_skip = len(prefix) + prefix_skip1
435 prefix.extend(prefix1)
436 if not got_all:
437 break
438 else:
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300439 break
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300440 else:
441 return prefix, prefix_skip, True
442 return prefix, prefix_skip, False
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300443
444def _get_charset_prefix(pattern):
Fredrik Lundh3562f112000-07-02 12:00:07 +0000445 charset = [] # not used
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000446 charsetappend = charset.append
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300447 if pattern.data:
448 op, av = pattern.data[0]
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300449 if op is SUBPATTERN:
450 group, add_flags, del_flags, p = av
451 if p and not (add_flags & SRE_FLAG_IGNORECASE):
452 op, av = p[0]
453 if op is LITERAL:
454 charsetappend((op, av))
455 elif op is BRANCH:
456 c = []
457 cappend = c.append
458 for p in av[1]:
459 if not p:
460 break
461 op, av = p[0]
462 if op is LITERAL:
463 cappend((op, av))
464 else:
465 break
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000466 else:
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300467 charset = c
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300468 elif op is BRANCH:
469 c = []
470 cappend = c.append
471 for p in av[1]:
472 if not p:
473 break
474 op, av = p[0]
475 if op is LITERAL:
476 cappend((op, av))
477 else:
478 break
479 else:
480 charset = c
481 elif op is IN:
482 charset = av
483 return charset
484
485def _compile_info(code, pattern, flags):
486 # internal: compile an info block. in the current version,
487 # this contains min/max pattern width, and an optional literal
488 # prefix or a character map
489 lo, hi = pattern.getwidth()
490 if hi > MAXCODE:
491 hi = MAXCODE
492 if lo == 0:
493 code.extend([INFO, 4, 0, lo, hi])
494 return
495 # look for a literal prefix
496 prefix = []
497 prefix_skip = 0
498 charset = [] # not used
499 if not (flags & SRE_FLAG_IGNORECASE):
500 # look for literal prefix
501 prefix, prefix_skip, got_all = _get_literal_prefix(pattern)
502 # if no prefix, look for charset prefix
503 if not prefix:
504 charset = _get_charset_prefix(pattern)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000505## if prefix:
Serhiy Storchakaab140882014-11-11 21:13:28 +0200506## print("*** PREFIX", prefix, prefix_skip)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000507## if charset:
Serhiy Storchakaab140882014-11-11 21:13:28 +0200508## print("*** CHARSET", charset)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000509 # add an info block
510 emit = code.append
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200511 emit(INFO)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000512 skip = len(code); emit(0)
513 # literal flag
514 mask = 0
Fredrik Lundh3562f112000-07-02 12:00:07 +0000515 if prefix:
516 mask = SRE_INFO_PREFIX
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300517 if prefix_skip is None and got_all:
Serhiy Storchakaab140882014-11-11 21:13:28 +0200518 mask = mask | SRE_INFO_LITERAL
Fredrik Lundh3562f112000-07-02 12:00:07 +0000519 elif charset:
Serhiy Storchakaab140882014-11-11 21:13:28 +0200520 mask = mask | SRE_INFO_CHARSET
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000521 emit(mask)
522 # pattern length
Fredrik Lundh3562f112000-07-02 12:00:07 +0000523 if lo < MAXCODE:
524 emit(lo)
525 else:
526 emit(MAXCODE)
527 prefix = prefix[:MAXCODE]
Serhiy Storchaka83e80272015-02-03 11:04:19 +0200528 emit(min(hi, MAXCODE))
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000529 # add literal prefix
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000530 if prefix:
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000531 emit(len(prefix)) # length
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300532 if prefix_skip is None:
533 prefix_skip = len(prefix)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000534 emit(prefix_skip) # skip
535 code.extend(prefix)
536 # generate overlap table
Antoine Pitrou79aa68d2013-10-25 21:36:10 +0200537 code.extend(_generate_overlap_table(prefix))
Fredrik Lundh3562f112000-07-02 12:00:07 +0000538 elif charset:
Guido van Rossum577fb5a2003-02-24 01:18:35 +0000539 _compile_charset(charset, flags, code)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000540 code[skip] = len(code) - skip
541
Just van Rossum74902502003-07-02 21:37:16 +0000542def isstring(obj):
Thomas Wouters40a088d2008-03-18 20:19:54 +0000543 return isinstance(obj, (str, bytes))
Just van Rossum74902502003-07-02 21:37:16 +0000544
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +0000545def _code(p, flags):
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000546
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000547 flags = p.pattern.flags | flags
Fredrik Lundhbe2211e2000-06-29 16:57:40 +0000548 code = []
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000549
550 # compile info block
551 _compile_info(code, p, flags)
552
553 # compile the pattern
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000554 _compile(code, p.data, flags)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000555
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200556 code.append(SUCCESS)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000557
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000558 return code
559
560def compile(p, flags=0):
561 # internal: convert pattern list to internal format
562
Just van Rossum74902502003-07-02 21:37:16 +0000563 if isstring(p):
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000564 pattern = p
565 p = sre_parse.parse(p, flags)
566 else:
567 pattern = None
568
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +0000569 code = _code(p, flags)
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000570
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200571 # print(code)
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000572
Fredrik Lundhc2301732000-07-02 22:25:39 +0000573 # map in either direction
574 groupindex = p.pattern.groupdict
575 indexgroup = [None] * p.pattern.groups
576 for k, i in groupindex.items():
577 indexgroup[i] = k
578
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000579 return _sre.compile(
Christian Heimes072c0f12008-01-03 23:01:04 +0000580 pattern, flags | p.pattern.flags, code,
Fredrik Lundh6f013982000-07-03 18:44:21 +0000581 p.pattern.groups-1,
Victor Stinner726a57d2016-11-22 23:04:39 +0100582 groupindex, tuple(indexgroup)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000583 )