blob: d7ee4e8cb6e4ce99e241a3a3a5950741decd6dfa [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 Storchaka0c938f62014-11-10 12:37:16 +020072 if (flags & SRE_FLAG_IGNORECASE and
73 not (flags & SRE_FLAG_LOCALE) and
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +030074 flags & SRE_FLAG_UNICODE and
75 not (flags & SRE_FLAG_ASCII)):
Serhiy Storchaka0c938f62014-11-10 12:37:16 +020076 fixes = _ignorecase_fixes
77 else:
78 fixes = None
Fredrik Lundh7898c3e2000-08-07 20:59:04 +000079 for op, av in pattern:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +000080 if op in LITERAL_CODES:
Serhiy Storchaka898ff032017-05-05 08:53:40 +030081 if not flags & SRE_FLAG_IGNORECASE:
82 emit(op)
83 emit(av)
84 elif flags & SRE_FLAG_LOCALE:
85 emit(OP_LOC_IGNORE[op])
86 emit(av)
87 else:
Serhiy Storchaka0c938f62014-11-10 12:37:16 +020088 lo = _sre.getlower(av, flags)
89 if fixes and lo in fixes:
Serhiy Storchaka5619ab92014-11-10 12:43:14 +020090 emit(IN_IGNORE)
Serhiy Storchaka0c938f62014-11-10 12:37:16 +020091 skip = _len(code); emit(0)
92 if op is NOT_LITERAL:
Serhiy Storchaka5619ab92014-11-10 12:43:14 +020093 emit(NEGATE)
Serhiy Storchaka0c938f62014-11-10 12:37:16 +020094 for k in (lo,) + fixes[lo]:
Serhiy Storchaka5619ab92014-11-10 12:43:14 +020095 emit(LITERAL)
Serhiy Storchaka0c938f62014-11-10 12:37:16 +020096 emit(k)
Serhiy Storchaka5619ab92014-11-10 12:43:14 +020097 emit(FAILURE)
Serhiy Storchaka0c938f62014-11-10 12:37:16 +020098 code[skip] = _len(code) - skip
99 else:
Serhiy Storchaka5619ab92014-11-10 12:43:14 +0200100 emit(OP_IGNORE[op])
Serhiy Storchaka0c938f62014-11-10 12:37:16 +0200101 emit(lo)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000102 elif op is IN:
Serhiy Storchaka898ff032017-05-05 08:53:40 +0300103 if not flags & SRE_FLAG_IGNORECASE:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200104 emit(op)
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200105 fixup = None
Serhiy Storchaka898ff032017-05-05 08:53:40 +0300106 elif flags & SRE_FLAG_LOCALE:
107 emit(IN_LOC_IGNORE)
108 fixup = None
109 else:
110 emit(IN_IGNORE)
111 def fixup(literal, flags=flags):
112 return _sre.getlower(literal, flags)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000113 skip = _len(code); emit(0)
Serhiy Storchaka0c938f62014-11-10 12:37:16 +0200114 _compile_charset(av, flags, code, fixup, fixes)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000115 code[skip] = _len(code) - skip
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000116 elif op is ANY:
117 if flags & SRE_FLAG_DOTALL:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200118 emit(ANY_ALL)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000119 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200120 emit(ANY)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000121 elif op in REPEATING_CODES:
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000122 if flags & SRE_FLAG_TEMPLATE:
Serhiy Storchaka632a77e2015-03-25 21:03:47 +0200123 raise error("internal: unsupported template operator %r" % (op,))
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000124 elif _simple(av) and op is not REPEAT:
125 if op is MAX_REPEAT:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200126 emit(REPEAT_ONE)
Guido van Rossum41c99e72003-04-14 17:59:34 +0000127 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200128 emit(MIN_REPEAT_ONE)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000129 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000130 emit(av[0])
131 emit(av[1])
132 _compile(code, av[2], flags)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200133 emit(SUCCESS)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000134 code[skip] = _len(code) - skip
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000135 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200136 emit(REPEAT)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000137 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000138 emit(av[0])
139 emit(av[1])
140 _compile(code, av[2], flags)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000141 code[skip] = _len(code) - skip
142 if op is MAX_REPEAT:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200143 emit(MAX_UNTIL)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000144 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200145 emit(MIN_UNTIL)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000146 elif op is SUBPATTERN:
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300147 group, add_flags, del_flags, p = av
148 if group:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200149 emit(MARK)
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300150 emit((group-1)*2)
151 # _compile_info(code, p, (flags | add_flags) & ~del_flags)
152 _compile(code, p, (flags | add_flags) & ~del_flags)
153 if group:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200154 emit(MARK)
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300155 emit((group-1)*2+1)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000156 elif op in SUCCESS_CODES:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200157 emit(op)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000158 elif op in ASSERT_CODES:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200159 emit(op)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000160 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000161 if av[0] >= 0:
162 emit(0) # look ahead
163 else:
164 lo, hi = av[1].getwidth()
165 if lo != hi:
Collin Winterce36ad82007-08-30 01:19:48 +0000166 raise error("look-behind requires fixed-width pattern")
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000167 emit(lo) # look behind
168 _compile(code, av[1], flags)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200169 emit(SUCCESS)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000170 code[skip] = _len(code) - skip
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000171 elif op is CALL:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200172 emit(op)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000173 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000174 _compile(code, av, flags)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200175 emit(SUCCESS)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000176 code[skip] = _len(code) - skip
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000177 elif op is AT:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200178 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000179 if flags & SRE_FLAG_MULTILINE:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000180 av = AT_MULTILINE.get(av, av)
181 if flags & SRE_FLAG_LOCALE:
182 av = AT_LOCALE.get(av, av)
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300183 elif (flags & SRE_FLAG_UNICODE) and not (flags & SRE_FLAG_ASCII):
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000184 av = AT_UNICODE.get(av, av)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200185 emit(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000186 elif op is BRANCH:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200187 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000188 tail = []
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000189 tailappend = tail.append
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000190 for av in av[1]:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000191 skip = _len(code); emit(0)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000192 # _compile_info(code, av, flags)
193 _compile(code, av, flags)
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200194 emit(JUMP)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000195 tailappend(_len(code)); emit(0)
196 code[skip] = _len(code) - skip
Serhiy Storchakaab140882014-11-11 21:13:28 +0200197 emit(FAILURE) # end of branch
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000198 for tail in tail:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000199 code[tail] = _len(code) - tail
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000200 elif op is CATEGORY:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200201 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000202 if flags & SRE_FLAG_LOCALE:
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000203 av = CH_LOCALE[av]
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300204 elif (flags & SRE_FLAG_UNICODE) and not (flags & SRE_FLAG_ASCII):
Fredrik Lundhb25e1ad2001-03-22 15:50:10 +0000205 av = CH_UNICODE[av]
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200206 emit(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000207 elif op is GROUPREF:
208 if flags & SRE_FLAG_IGNORECASE:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200209 emit(OP_IGNORE[op])
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000210 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200211 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000212 emit(av-1)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000213 elif op is GROUPREF_EXISTS:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200214 emit(op)
Andrew M. Kuchlingc30faa82005-06-02 13:35:52 +0000215 emit(av[0]-1)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000216 skipyes = _len(code); emit(0)
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000217 _compile(code, av[1], flags)
218 if av[2]:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200219 emit(JUMP)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000220 skipno = _len(code); emit(0)
221 code[skipyes] = _len(code) - skipyes + 1
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000222 _compile(code, av[2], flags)
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000223 code[skipno] = _len(code) - skipno
Gustavo Niemeyerad3fc442003-10-17 22:13:16 +0000224 else:
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000225 code[skipyes] = _len(code) - skipyes + 1
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000226 else:
Serhiy Storchaka632a77e2015-03-25 21:03:47 +0200227 raise error("internal: unsupported operand type %r" % (op,))
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000228
Serhiy Storchaka0c938f62014-11-10 12:37:16 +0200229def _compile_charset(charset, flags, code, fixup=None, fixes=None):
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000230 # compile charset subprogram
231 emit = code.append
Serhiy Storchaka5619ab92014-11-10 12:43:14 +0200232 for op, av in _optimize_charset(charset, fixup, fixes):
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200233 emit(op)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000234 if op is NEGATE:
235 pass
236 elif op is LITERAL:
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200237 emit(av)
238 elif op is RANGE or op is RANGE_IGNORE:
239 emit(av[0])
240 emit(av[1])
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000241 elif op is CHARSET:
242 code.extend(av)
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000243 elif op is BIGCHARSET:
244 code.extend(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000245 elif op is CATEGORY:
246 if flags & SRE_FLAG_LOCALE:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200247 emit(CH_LOCALE[av])
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300248 elif (flags & SRE_FLAG_UNICODE) and not (flags & SRE_FLAG_ASCII):
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200249 emit(CH_UNICODE[av])
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000250 else:
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200251 emit(av)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000252 else:
Serhiy Storchaka632a77e2015-03-25 21:03:47 +0200253 raise error("internal: unsupported set operator %r" % (op,))
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200254 emit(FAILURE)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000255
Serhiy Storchaka5619ab92014-11-10 12:43:14 +0200256def _optimize_charset(charset, fixup, fixes):
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000257 # internal: optimize character set
Fredrik Lundh3562f112000-07-02 12:00:07 +0000258 out = []
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200259 tail = []
260 charmap = bytearray(256)
261 for op, av in charset:
262 while True:
263 try:
264 if op is LITERAL:
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200265 if fixup:
Serhiy Storchaka5619ab92014-11-10 12:43:14 +0200266 lo = fixup(av)
267 charmap[lo] = 1
268 if fixes and lo in fixes:
269 for k in fixes[lo]:
Serhiy Storchaka0c938f62014-11-10 12:37:16 +0200270 charmap[k] = 1
271 else:
272 charmap[av] = 1
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200273 elif op is RANGE:
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200274 r = range(av[0], av[1]+1)
275 if fixup:
276 r = map(fixup, r)
Serhiy Storchaka0c938f62014-11-10 12:37:16 +0200277 if fixup and fixes:
278 for i in r:
279 charmap[i] = 1
280 if i in fixes:
281 for k in fixes[i]:
282 charmap[k] = 1
283 else:
284 for i in r:
285 charmap[i] = 1
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200286 elif op is NEGATE:
287 out.append((op, av))
288 else:
289 tail.append((op, av))
290 except IndexError:
291 if len(charmap) == 256:
292 # character set contains non-UCS1 character codes
293 charmap += b'\0' * 0xff00
294 continue
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200295 # Character set contains non-BMP character codes.
296 # There are only two ranges of cased non-BMP characters:
297 # 10400-1044F (Deseret) and 118A0-118DF (Warang Citi),
298 # and for both ranges RANGE_IGNORE works.
299 if fixup and op is RANGE:
300 op = RANGE_IGNORE
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200301 tail.append((op, av))
302 break
303
Fredrik Lundh3562f112000-07-02 12:00:07 +0000304 # compress character map
Fredrik Lundh3562f112000-07-02 12:00:07 +0000305 runs = []
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200306 q = 0
307 while True:
308 p = charmap.find(1, q)
309 if p < 0:
310 break
311 if len(runs) >= 2:
312 runs = None
313 break
314 q = charmap.find(0, p)
315 if q < 0:
316 runs.append((p, len(charmap)))
317 break
318 runs.append((p, q))
319 if runs is not None:
Fredrik Lundh3562f112000-07-02 12:00:07 +0000320 # use literal/range
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200321 for p, q in runs:
322 if q - p == 1:
323 out.append((LITERAL, p))
Fredrik Lundh3562f112000-07-02 12:00:07 +0000324 else:
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200325 out.append((RANGE, (p, q - 1)))
326 out += tail
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200327 # if the case was changed or new representation is more compact
328 if fixup or len(out) < len(charset):
Fredrik Lundh3562f112000-07-02 12:00:07 +0000329 return out
Serhiy Storchaka4b8f8942014-10-31 12:36:56 +0200330 # else original character set is good enough
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200331 return charset
332
333 # use bitmap
334 if len(charmap) == 256:
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000335 data = _mk_bitmap(charmap)
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200336 out.append((CHARSET, data))
337 out += tail
Fredrik Lundh3562f112000-07-02 12:00:07 +0000338 return out
Fredrik Lundh3562f112000-07-02 12:00:07 +0000339
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200340 # To represent a big charset, first a bitmap of all characters in the
341 # set is constructed. Then, this bitmap is sliced into chunks of 256
342 # characters, duplicate chunks are eliminated, and each chunk is
343 # given a number. In the compiled expression, the charset is
344 # represented by a 32-bit word sequence, consisting of one word for
345 # the number of different chunks, a sequence of 256 bytes (64 words)
346 # of chunk numbers indexed by their original chunk position, and a
347 # sequence of 256-bit chunks (8 words each).
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000348
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200349 # Compression is normally good: in a typical charset, large ranges of
350 # Unicode will be either completely excluded (e.g. if only cyrillic
351 # letters are to be matched), or completely included (e.g. if large
352 # subranges of Kanji match). These ranges will be represented by
353 # chunks of all one-bits or all zero-bits.
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000354
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200355 # Matching can be also done efficiently: the more significant byte of
356 # the Unicode character is an index into the chunk number, and the
357 # less significant byte is a bit index in the chunk (just like the
358 # CHARSET matching).
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000359
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200360 charmap = bytes(charmap) # should be hashable
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000361 comps = {}
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200362 mapping = bytearray(256)
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000363 block = 0
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200364 data = bytearray()
365 for i in range(0, 65536, 256):
366 chunk = charmap[i: i + 256]
367 if chunk in comps:
368 mapping[i // 256] = comps[chunk]
369 else:
370 mapping[i // 256] = comps[chunk] = block
371 block += 1
372 data += chunk
373 data = _mk_bitmap(data)
374 data[0:0] = [block] + _bytes_to_codes(mapping)
375 out.append((BIGCHARSET, data))
376 out += tail
377 return out
378
379_CODEBITS = _sre.CODESIZE * 8
Serhiy Storchakaab140882014-11-11 21:13:28 +0200380MAXCODE = (1 << _CODEBITS) - 1
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200381_BITS_TRANS = b'0' + b'1' * 255
382def _mk_bitmap(bits, _CODEBITS=_CODEBITS, _int=int):
383 s = bits.translate(_BITS_TRANS)[::-1]
384 return [_int(s[i - _CODEBITS: i], 2)
385 for i in range(len(s), 0, -_CODEBITS)]
386
387def _bytes_to_codes(b):
388 # Convert block indices to word array
Serhiy Storchaka19e91582014-11-10 13:24:47 +0200389 a = memoryview(b).cast('I')
Serhiy Storchaka68457be2013-10-27 08:20:29 +0200390 assert a.itemsize == _sre.CODESIZE
391 assert len(a) * a.itemsize == len(b)
392 return a.tolist()
Fredrik Lundh19af43d2001-07-02 16:58:38 +0000393
Fredrik Lundhe1869832000-08-01 22:47:49 +0000394def _simple(av):
395 # check if av is a "simple" operator
396 lo, hi = av[2].getwidth()
Fredrik Lundhe1869832000-08-01 22:47:49 +0000397 return lo == hi == 1 and av[2][0][0] != SUBPATTERN
398
Antoine Pitrou79aa68d2013-10-25 21:36:10 +0200399def _generate_overlap_table(prefix):
400 """
401 Generate an overlap table for the following prefix.
402 An overlap table is a table of the same size as the prefix which
403 informs about the potential self-overlap for each index in the prefix:
404 - if overlap[i] == 0, prefix[i:] can't overlap prefix[0:...]
405 - if overlap[i] == k with 0 < k <= i, prefix[i-k+1:i+1] overlaps with
406 prefix[0:k]
407 """
408 table = [0] * len(prefix)
409 for i in range(1, len(prefix)):
410 idx = table[i - 1]
411 while prefix[i] != prefix[idx]:
412 if idx == 0:
413 table[i] = 0
414 break
415 idx = table[idx - 1]
416 else:
417 table[i] = idx + 1
418 return table
419
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300420def _get_literal_prefix(pattern):
421 # look for literal prefix
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000422 prefix = []
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000423 prefixappend = prefix.append
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300424 prefix_skip = None
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300425 for op, av in pattern.data:
426 if op is LITERAL:
427 prefixappend(av)
428 elif op is SUBPATTERN:
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300429 group, add_flags, del_flags, p = av
430 if add_flags & SRE_FLAG_IGNORECASE:
431 break
432 prefix1, prefix_skip1, got_all = _get_literal_prefix(p)
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300433 if prefix_skip is None:
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300434 if group is not None:
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300435 prefix_skip = len(prefix)
436 elif prefix_skip1 is not None:
437 prefix_skip = len(prefix) + prefix_skip1
438 prefix.extend(prefix1)
439 if not got_all:
440 break
441 else:
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300442 break
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300443 else:
444 return prefix, prefix_skip, True
445 return prefix, prefix_skip, False
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300446
447def _get_charset_prefix(pattern):
Fredrik Lundh3562f112000-07-02 12:00:07 +0000448 charset = [] # not used
Raymond Hettinger01c9f8c2004-03-26 11:16:55 +0000449 charsetappend = charset.append
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300450 if pattern.data:
451 op, av = pattern.data[0]
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300452 if op is SUBPATTERN:
453 group, add_flags, del_flags, p = av
454 if p and not (add_flags & SRE_FLAG_IGNORECASE):
455 op, av = p[0]
456 if op is LITERAL:
457 charsetappend((op, av))
458 elif op is BRANCH:
459 c = []
460 cappend = c.append
461 for p in av[1]:
462 if not p:
463 break
464 op, av = p[0]
465 if op is LITERAL:
466 cappend((op, av))
467 else:
468 break
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000469 else:
Serhiy Storchakabe9a4e52016-09-10 00:57:55 +0300470 charset = c
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300471 elif op is BRANCH:
472 c = []
473 cappend = c.append
474 for p in av[1]:
475 if not p:
476 break
477 op, av = p[0]
478 if op is LITERAL:
479 cappend((op, av))
480 else:
481 break
482 else:
483 charset = c
484 elif op is IN:
485 charset = av
486 return charset
487
488def _compile_info(code, pattern, flags):
489 # internal: compile an info block. in the current version,
490 # this contains min/max pattern width, and an optional literal
491 # prefix or a character map
492 lo, hi = pattern.getwidth()
493 if hi > MAXCODE:
494 hi = MAXCODE
495 if lo == 0:
496 code.extend([INFO, 4, 0, lo, hi])
497 return
498 # look for a literal prefix
499 prefix = []
500 prefix_skip = 0
501 charset = [] # not used
502 if not (flags & SRE_FLAG_IGNORECASE):
503 # look for literal prefix
504 prefix, prefix_skip, got_all = _get_literal_prefix(pattern)
505 # if no prefix, look for charset prefix
506 if not prefix:
507 charset = _get_charset_prefix(pattern)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000508## if prefix:
Serhiy Storchakaab140882014-11-11 21:13:28 +0200509## print("*** PREFIX", prefix, prefix_skip)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000510## if charset:
Serhiy Storchakaab140882014-11-11 21:13:28 +0200511## print("*** CHARSET", charset)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000512 # add an info block
513 emit = code.append
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200514 emit(INFO)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000515 skip = len(code); emit(0)
516 # literal flag
517 mask = 0
Fredrik Lundh3562f112000-07-02 12:00:07 +0000518 if prefix:
519 mask = SRE_INFO_PREFIX
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300520 if prefix_skip is None and got_all:
Serhiy Storchakaab140882014-11-11 21:13:28 +0200521 mask = mask | SRE_INFO_LITERAL
Fredrik Lundh3562f112000-07-02 12:00:07 +0000522 elif charset:
Serhiy Storchakaab140882014-11-11 21:13:28 +0200523 mask = mask | SRE_INFO_CHARSET
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000524 emit(mask)
525 # pattern length
Fredrik Lundh3562f112000-07-02 12:00:07 +0000526 if lo < MAXCODE:
527 emit(lo)
528 else:
529 emit(MAXCODE)
530 prefix = prefix[:MAXCODE]
Serhiy Storchaka83e80272015-02-03 11:04:19 +0200531 emit(min(hi, MAXCODE))
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000532 # add literal prefix
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000533 if prefix:
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000534 emit(len(prefix)) # length
Serhiy Storchaka66dc4642015-06-21 14:06:55 +0300535 if prefix_skip is None:
536 prefix_skip = len(prefix)
Fredrik Lundh7898c3e2000-08-07 20:59:04 +0000537 emit(prefix_skip) # skip
538 code.extend(prefix)
539 # generate overlap table
Antoine Pitrou79aa68d2013-10-25 21:36:10 +0200540 code.extend(_generate_overlap_table(prefix))
Fredrik Lundh3562f112000-07-02 12:00:07 +0000541 elif charset:
Guido van Rossum577fb5a2003-02-24 01:18:35 +0000542 _compile_charset(charset, flags, code)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000543 code[skip] = len(code) - skip
544
Just van Rossum74902502003-07-02 21:37:16 +0000545def isstring(obj):
Thomas Wouters40a088d2008-03-18 20:19:54 +0000546 return isinstance(obj, (str, bytes))
Just van Rossum74902502003-07-02 21:37:16 +0000547
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +0000548def _code(p, flags):
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000549
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000550 flags = p.pattern.flags | flags
Fredrik Lundhbe2211e2000-06-29 16:57:40 +0000551 code = []
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000552
553 # compile info block
554 _compile_info(code, p, flags)
555
556 # compile the pattern
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000557 _compile(code, p.data, flags)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000558
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200559 code.append(SUCCESS)
Fredrik Lundh29c08be2000-06-29 23:33:12 +0000560
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000561 return code
562
563def compile(p, flags=0):
564 # internal: convert pattern list to internal format
565
Just van Rossum74902502003-07-02 21:37:16 +0000566 if isstring(p):
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000567 pattern = p
568 p = sre_parse.parse(p, flags)
569 else:
570 pattern = None
571
Fredrik Lundh2f2c67d2000-08-01 21:05:41 +0000572 code = _code(p, flags)
Fredrik Lundh29c4ba92000-08-01 18:20:07 +0000573
Serhiy Storchakac7f7d382014-11-09 20:48:36 +0200574 # print(code)
Fredrik Lundh8a3ebf82000-07-23 21:46:17 +0000575
Fredrik Lundhc2301732000-07-02 22:25:39 +0000576 # map in either direction
577 groupindex = p.pattern.groupdict
578 indexgroup = [None] * p.pattern.groups
579 for k, i in groupindex.items():
580 indexgroup[i] = k
581
Jeremy Hyltonb1aa1952000-06-01 17:39:12 +0000582 return _sre.compile(
Christian Heimes072c0f12008-01-03 23:01:04 +0000583 pattern, flags | p.pattern.flags, code,
Fredrik Lundh6f013982000-07-03 18:44:21 +0000584 p.pattern.groups-1,
Victor Stinner726a57d2016-11-22 23:04:39 +0100585 groupindex, tuple(indexgroup)
Fredrik Lundh90a07912000-06-30 07:50:59 +0000586 )