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 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517
|
import sys
from rpython.rlib.debug import check_nonneg
from rpython.rlib.unroll import unrolling_iterable
from rpython.rlib.rsre import rsre_char, rsre_constants as consts
from rpython.tool.sourcetools import func_with_new_name
from rpython.rlib.objectmodel import we_are_translated, not_rpython
from rpython.rlib import jit
from rpython.rlib.rsre.rsre_jit import install_jitdriver, install_jitdriver_spec
_seen_specname = {}
def specializectx(func):
"""A decorator that specializes 'func(ctx,...)' for each concrete subclass
of AbstractMatchContext. During annotation, if 'ctx' is known to be a
specific subclass, calling 'func' is a direct call; if 'ctx' is only known
to be of class AbstractMatchContext, calling 'func' is an indirect call.
"""
from rpython.rlib.rsre.rsre_utf8 import Utf8MatchContext
assert func.__code__.co_varnames[0] == 'ctx'
specname = '_spec_' + func.__name__
while specname in _seen_specname:
specname += '_'
_seen_specname[specname] = True
# Install a copy of the function under the name '_spec_funcname' in each
# concrete subclass
specialized_methods = []
for prefix, concreteclass in [('buf', BufMatchContext),
('str', StrMatchContext),
('uni', UnicodeMatchContext),
('utf8', Utf8MatchContext),
]:
newfunc = func_with_new_name(func, prefix + specname)
assert not hasattr(concreteclass, specname)
setattr(concreteclass, specname, newfunc)
specialized_methods.append(newfunc)
# Return a dispatcher function, specialized on the exact type of 'ctx'
def dispatch(ctx, *args):
return getattr(ctx, specname)(*args)
dispatch._annspecialcase_ = 'specialize:argtype(0)'
dispatch._specialized_methods_ = specialized_methods
return func_with_new_name(dispatch, specname)
# ____________________________________________________________
class Error(Exception):
def __init__(self, msg):
self.msg = msg
class EndOfString(Exception):
pass
class CompiledPattern(object):
_immutable_fields_ = ['pattern[*]', 'flags']
def __init__(self, pattern, flags):
self.pattern = pattern
if not consts.V37: # 'flags' is ignored in >=3.7 mode
self.flags = flags
# check we don't get the old value of MAXREPEAT
# during the untranslated tests.
# On python3, MAXCODE can appear in patterns. It will be 65535
# when CODESIZE is 2
if not we_are_translated() and rsre_char.CODESIZE != 2:
assert 65535 not in pattern
def lowa(self, char_ord):
"""Pre-3.7: uses getlower(flags).
Post-3.7: this is always getlower_ascii().
"""
if not consts.V37:
return rsre_char.getlower(char_ord, self.flags)
else:
return rsre_char.getlower_ascii(char_ord)
def char_loc_ignore(self, index, char_ord):
assert consts.V37
pattern = self.pat(index)
return (char_ord == pattern or
rsre_char.getlower_locale(char_ord) == pattern or
rsre_char.getupper_locale(char_ord) == pattern)
def charset_loc_ignore(self, ctx, ppos, char_ord):
lo = rsre_char.getlower_locale(char_ord)
if rsre_char.check_charset(ctx, self, ppos, lo):
return True
up = rsre_char.getupper_locale(char_ord)
return up != lo and rsre_char.check_charset(ctx, self, ppos, up)
def pat(self, index):
jit.promote(self)
check_nonneg(index)
result = self.pattern[index]
# Check that we only return non-negative integers from this helper.
# It is possible that self.pattern contains negative integers
# (see set_charset() and set_bigcharset() in rsre_char.py)
# but they should not be fetched via this helper here.
assert result >= 0
return result
MODE_ANY = '\x00' # an empty match is fine
MODE_NONEMPTY = '\x01' # must have a non-empty match
MODE_FULL = '\x02' # must match the whole string
class AbstractMatchContext(object):
"""Abstract base class"""
_immutable_fields_ = ['end']
match_start = 0
match_end = 0
match_marks = None
match_marks_flat = None
match_mode = MODE_ANY
def __init__(self, match_start, end):
# 'match_start' and 'end' must be known to be non-negative
# and they must not be more than len(string).
check_nonneg(match_start)
check_nonneg(end)
self.match_start = match_start
self.end = end
def reset(self, start, must_advance=False):
self.match_start = start
self.match_marks = None
self.match_marks_flat = None
#
assert MODE_ANY == chr(False)
assert MODE_NONEMPTY == chr(True)
self.match_mode = chr(must_advance)
@not_rpython
def _fullmatch_only(self, x=None):
raise Exception("'ctx.fullmatch_only' was replaced with"
" 'ctx.match_mode'")
fullmatch_only = property(_fullmatch_only, _fullmatch_only)
@not_rpython
def str(self, index):
"""Must be overridden in a concrete subclass.
The @not_rpython is used to generate a translation-time crash
if there is a call to str() that is indirect. All calls must
be direct for performance reasons; you need to specialize the
caller with @specializectx."""
raise NotImplementedError
# The following methods are provided to be overriden in
# Utf8MatchContext. The non-utf8 implementation is provided
# by the FixedMatchContext abstract subclass, in order to use
# the same @not_rpython safety trick as above. If you get a
# "not_rpython" error during translation, either consider
# calling the methods xxx_indirect() instead of xxx(), or if
# applicable add the @specializectx decorator.
ZERO = 0
@not_rpython
def next(self, position):
raise NotImplementedError
@not_rpython
def prev(self, position):
raise NotImplementedError
@not_rpython
def next_n(self, position, n):
raise NotImplementedError
@not_rpython
def prev_n(self, position, n, start_position):
raise NotImplementedError
@not_rpython
def debug_check_pos(self, position):
raise NotImplementedError
@not_rpython
def maximum_distance(self, position_low, position_high):
raise NotImplementedError
@not_rpython
def get_single_byte(self, base_position, index):
raise NotImplementedError
def bytes_difference(self, position1, position2):
return position1 - position2
def go_forward_by_bytes(self, base_position, index):
return base_position + index
def next_indirect(self, position):
assert position < self.end
return position + 1 # like next(), but can be called indirectly
def prev_indirect(self, position):
position -= 1 # like prev(), but can be called indirectly
if position < 0:
raise EndOfString
return position
def get_mark(self, gid):
return find_mark(self.match_marks, gid)
def flatten_marks(self):
# for testing
if self.match_marks_flat is None:
self._compute_flattened_marks()
return self.match_marks_flat
def _compute_flattened_marks(self):
self.match_marks_flat = [self.match_start, self.match_end]
mark = self.match_marks
if mark is not None:
self.match_lastindex = mark.gid
else:
self.match_lastindex = -1
while mark is not None:
index = mark.gid + 2
while index >= len(self.match_marks_flat):
self.match_marks_flat.append(-1)
if self.match_marks_flat[index] == -1:
self.match_marks_flat[index] = mark.position
mark = mark.prev
self.match_marks = None # clear
def span(self, groupnum=0):
# compatibility
fmarks = self.flatten_marks()
groupnum *= 2
if groupnum >= len(fmarks):
return (-1, -1)
return (fmarks[groupnum], fmarks[groupnum+1])
def group(self, groupnum=0):
frm, to = self.span(groupnum)
if 0 <= frm <= to:
return self._string[frm:to]
else:
return None
def fresh_copy(self, start):
raise NotImplementedError
class FixedMatchContext(AbstractMatchContext):
"""Abstract subclass to introduce the default implementation for
these position methods. The Utf8MatchContext subclass doesn't
inherit from here."""
next = AbstractMatchContext.next_indirect
prev = AbstractMatchContext.prev_indirect
def next_n(self, position, n, end_position):
position += n
if position > end_position:
raise EndOfString
return position
def prev_n(self, position, n, start_position):
position -= n
if position < start_position:
raise EndOfString
return position
def debug_check_pos(self, position):
pass
def maximum_distance(self, position_low, position_high):
return position_high - position_low
class BufMatchContext(FixedMatchContext):
"""Concrete subclass for matching in a buffer."""
_immutable_fields_ = ["_buffer"]
def __init__(self, buf, match_start, end):
FixedMatchContext.__init__(self, match_start, end)
self._buffer = buf
def str(self, index):
check_nonneg(index)
return ord(self._buffer.getitem(index))
def fresh_copy(self, start):
return BufMatchContext(self._buffer, start,
self.end)
def get_single_byte(self, base_position, index):
return self.str(base_position + index)
class StrMatchContext(FixedMatchContext):
"""Concrete subclass for matching in a plain string."""
_immutable_fields_ = ["_string"]
def __init__(self, string, match_start, end):
FixedMatchContext.__init__(self, match_start, end)
self._string = string
def str(self, index):
check_nonneg(index)
return ord(self._string[index])
def fresh_copy(self, start):
return StrMatchContext(self._string, start,
self.end)
def get_single_byte(self, base_position, index):
return self.str(base_position + index)
def _real_pos(self, index):
return index # overridden by tests
class UnicodeMatchContext(FixedMatchContext):
"""Concrete subclass for matching in a unicode string."""
_immutable_fields_ = ["_unicodestr"]
def __init__(self, unicodestr, match_start, end):
FixedMatchContext.__init__(self, match_start, end)
self._unicodestr = unicodestr
def str(self, index):
check_nonneg(index)
return ord(self._unicodestr[index])
def fresh_copy(self, start):
return UnicodeMatchContext(self._unicodestr, start,
self.end)
def get_single_byte(self, base_position, index):
return self.str(base_position + index)
# ____________________________________________________________
class Mark(object):
_immutable_ = True
def __init__(self, gid, position, prev):
self.gid = gid
self.position = position
self.prev = prev # chained list
def __repr__(self):
return "Mark(%r, %r, %r)" % (self.gid, self.position, self.prev)
def find_mark(mark, gid):
while mark is not None:
if mark.gid == gid:
return mark.position
mark = mark.prev
return -1
# ____________________________________________________________
class MatchResult(object):
subresult = None
def move_to_next_result(self, ctx, pattern):
# returns either 'self' or None
result = self.subresult
if result is None:
return
if result.move_to_next_result(ctx, pattern):
return self
return self.find_next_result(ctx, pattern)
def find_next_result(self, ctx, pattern):
raise NotImplementedError
MATCHED_OK = MatchResult()
class BranchMatchResult(MatchResult):
def __init__(self, ppos, ptr, marks):
self.ppos = ppos
self.start_ptr = ptr
self.start_marks = marks
@jit.unroll_safe
def find_first_result(self, ctx, pattern):
ppos = jit.hint(self.ppos, promote=True)
while pattern.pat(ppos):
result = sre_match(ctx, pattern, ppos + 1, self.start_ptr, self.start_marks)
ppos += pattern.pat(ppos)
if result is not None:
self.subresult = result
self.ppos = ppos
return self
find_next_result = find_first_result
class RepeatOneMatchResult(MatchResult):
install_jitdriver('RepeatOne',
greens=['nextppos', 'pattern'],
reds=['ptr', 'self', 'ctx'],
debugprint=(1, 0)) # indices in 'greens'
def __init__(self, nextppos, minptr, ptr, marks):
self.nextppos = nextppos
self.minptr = minptr
self.start_ptr = ptr
self.start_marks = marks
def find_first_result(self, ctx, pattern):
ptr = self.start_ptr
nextppos = self.nextppos
while ptr >= self.minptr:
ctx.jitdriver_RepeatOne.jit_merge_point(
self=self, ptr=ptr, ctx=ctx, nextppos=nextppos,
pattern=pattern)
result = sre_match(ctx, pattern, nextppos, ptr, self.start_marks)
try:
ptr = ctx.prev_indirect(ptr)
except EndOfString:
ptr = -1
if result is not None:
self.subresult = result
self.start_ptr = ptr
return self
find_next_result = find_first_result
class MinRepeatOneMatchResult(MatchResult):
install_jitdriver('MinRepeatOne',
greens=['nextppos', 'ppos3', 'pattern'],
reds=['max_count', 'ptr', 'self', 'ctx'],
debugprint=(2, 0)) # indices in 'greens'
def __init__(self, nextppos, ppos3, max_count, ptr, marks):
self.nextppos = nextppos
self.ppos3 = ppos3
self.max_count = max_count
self.start_ptr = ptr
self.start_marks = marks
def find_first_result(self, ctx, pattern):
ptr = self.start_ptr
nextppos = self.nextppos
max_count = self.max_count
ppos3 = self.ppos3
while max_count >= 0:
ctx.jitdriver_MinRepeatOne.jit_merge_point(
self=self, ptr=ptr, ctx=ctx, nextppos=nextppos, ppos3=ppos3,
max_count=max_count, pattern=pattern)
result = sre_match(ctx, pattern, nextppos, ptr, self.start_marks)
if result is not None:
self.subresult = result
self.start_ptr = ptr
self.max_count = max_count
return self
if not self.next_char_ok(ctx, pattern, ptr, ppos3):
break
ptr = ctx.next_indirect(ptr)
max_count -= 1
def find_next_result(self, ctx, pattern):
ptr = self.start_ptr
if not self.next_char_ok(ctx, pattern, ptr, self.ppos3):
return
self.start_ptr = ctx.next_indirect(ptr)
return self.find_first_result(ctx, pattern)
def next_char_ok(self, ctx, pattern, ptr, ppos):
if ptr == ctx.end:
return False
op = pattern.pat(ppos)
for op1, checkerfn in unroll_char_checker:
if op1 == op:
return checkerfn(ctx, pattern, ptr, ppos)
# obscure case: it should be a single char pattern, but isn't
# one of the opcodes in unroll_char_checker (see test_ext_opcode)
return sre_match(ctx, pattern, ppos, ptr, self.start_marks) is not None
class AbstractUntilMatchResult(MatchResult):
def __init__(self, ppos, tailppos, ptr, marks):
self.ppos = ppos
self.tailppos = tailppos
self.cur_ptr = ptr
self.cur_marks = marks
self.pending = None
self.num_pending = 0
class Pending(object):
def __init__(self, ptr, marks, enum, next):
self.ptr = ptr
self.marks = marks
self.enum = enum
self.next = next # chained list
class MaxUntilMatchResult(AbstractUntilMatchResult):
install_jitdriver('MaxUntil',
greens=['ppos', 'tailppos', 'match_more', 'pattern'],
reds=['ptr', 'marks', 'self', 'ctx'],
debugprint=(3, 0, 2))
def find_first_result(self, ctx, pattern):
return self.search_next(ctx, pattern, match_more=True)
def find_next_result(self, ctx, pattern):
return self.search_next(ctx, pattern, match_more=False)
def search_next(self, ctx, pattern, match_more):
ppos = self.ppos
tailppos = self.tailppos
ptr = self.cur_ptr
marks = self.cur_marks
while True:
ctx.jitdriver_MaxUntil.jit_merge_point(
ppos=ppos, tailppos=tailppos, match_more=match_more,
ptr=ptr, marks=marks, self=self, ctx=ctx,
pattern=pattern)
if match_more:
max = pattern.pat(ppos+2)
if max == rsre_char.MAXREPEAT or self.num_pending < max:
# try to match one more 'item'
enum = sre_match(ctx, pattern, ppos + 3, ptr, marks)
else:
enum = None # 'max' reached, no more matches
else:
p = self.pending
if p is None:
return
self.pending = p.next
self.num_pending -= 1
ptr = p.ptr
marks = p.marks
enum = p.enum.move_to_next_result(ctx, pattern)
#
min = pattern.pat(ppos+1)
if enum is not None:
# matched one more 'item'. record it and continue.
last_match_zero_length = (ctx.match_end == ptr)
self.pending = Pending(ptr, marks, enum, self.pending)
self.num_pending += 1
ptr = ctx.match_end
marks = ctx.match_marks
if last_match_zero_length and self.num_pending >= min:
# zero-width protection: after an empty match, if there
# are enough matches, don't try to match more. Instead,
# fall through to trying to match 'tail'.
pass
else:
match_more = True
continue
# 'item' no longer matches.
if self.num_pending >= min:
# try to match 'tail' if we have enough 'item'
result = sre_match(ctx, pattern, tailppos, ptr, marks)
if result is not None:
self.subresult = result
self.cur_ptr = ptr
self.cur_marks = marks
return self
match_more = False
class MinUntilMatchResult(AbstractUntilMatchResult):
install_jitdriver('MinUntil',
greens=['ppos', 'tailppos', 'resume', 'pattern'],
reds=['ptr', 'marks', 'self', 'ctx'],
debugprint=(3, 0, 2))
def find_first_result(self, ctx, pattern):
return self.search_next(ctx, pattern, resume=False)
def find_next_result(self, ctx, pattern):
return self.search_next(ctx, pattern, resume=True)
def search_next(self, ctx, pattern, resume):
ppos = self.ppos
ptr = self.cur_ptr
marks = self.cur_marks
tailppos = self.tailppos
while True:
ctx.jitdriver_MinUntil.jit_merge_point(
ppos=ppos, tailppos=tailppos, resume=resume,
ptr=ptr, marks=marks, self=self, ctx=ctx,
pattern=pattern)
# try to match 'tail' if we have enough 'item'
min = pattern.pat(ppos+1)
if not resume and self.num_pending >= min:
result = sre_match(ctx, pattern, tailppos, ptr, marks)
if result is not None:
self.subresult = result
self.cur_ptr = ptr
self.cur_marks = marks
return self
resume = False
max = pattern.pat(ppos+2)
if max == rsre_char.MAXREPEAT or self.num_pending < max:
# try to match one more 'item'
enum = sre_match(ctx, pattern, ppos + 3, ptr, marks)
#
# zero-width match protection
if self.num_pending >= min:
while enum is not None and ptr == ctx.match_end:
enum = enum.move_to_next_result(ctx, pattern)
else:
enum = None # 'max' reached, no more matches
while enum is None:
# 'item' does not match; try to get further results from
# the 'pending' list.
p = self.pending
if p is None:
return
self.pending = p.next
self.num_pending -= 1
ptr = p.ptr
marks = p.marks
enum = p.enum.move_to_next_result(ctx, pattern)
# matched one more 'item'. record it and continue
self.pending = Pending(ptr, marks, enum, self.pending)
self.num_pending += 1
ptr = ctx.match_end
marks = ctx.match_marks
install_jitdriver_spec('MaxUntilPossessive',
greens=['ppos', 'pattern'],
reds=['ptr', 'matches_done', 'marks', 'ctx'],
debugprint=(1, 0))
@specializectx
def find_repetition_end_possessive(ctx, pattern, ppos, ptr, marks):
matches_done = 0
enum = None
while True:
ctx.jitdriver_MaxUntilPossessive.jit_merge_point(
ppos=ppos,
ptr=ptr, marks=marks, ctx=ctx, matches_done=matches_done,
pattern=pattern)
maxmatch = pattern.pat(ppos+2)
if maxmatch == rsre_char.MAXREPEAT or matches_done < maxmatch:
# try to match one more 'item'
enum = sre_match(ctx, pattern, ppos + 3, ptr, marks)
else:
enum = None # 'max' reached, no more matches
minmatch = pattern.pat(ppos+1)
if enum is not None:
matches_done += 1
# matched one more 'item'.
last_match_zero_length = (ctx.match_end == ptr)
ptr = ctx.match_end
marks = ctx.match_marks
if last_match_zero_length and matches_done >= minmatch:
# zero-width protection: after an empty match, if there
# are enough matches, don't try to match more. Instead,
# fall through to trying to match 'tail'.
pass
else:
continue
if matches_done >= minmatch:
ctx.match_marks = marks
return ptr
return -1
# ____________________________________________________________
@specializectx
@jit.unroll_safe
def sre_match(ctx, pattern, ppos, ptr, marks):
"""Returns either None or a MatchResult object. Usually we only need
the first result, but there is the case of REPEAT...UNTIL where we
need all results; in that case we use the method move_to_next_result()
of the MatchResult."""
while True:
op = pattern.pat(ppos)
ppos += 1
#jit.jit_debug("sre_match", op, ppos, ptr)
#
# When using the JIT, calls to sre_match() must always have a constant
# (green) argument for 'ppos'. If not, the following assert fails.
jit.assert_green(op)
if op == consts.OPCODE_FAILURE:
return
elif op == consts.OPCODE_SUCCESS:
mode = ctx.match_mode
if mode == MODE_FULL:
if ptr != ctx.end:
return # not a full match
elif mode == MODE_NONEMPTY:
if ptr == ctx.match_start:
return # empty match
ctx.match_end = ptr
ctx.match_marks = marks
return MATCHED_OK
elif (op == consts.OPCODE_MAX_UNTIL or
op == consts.OPCODE_MIN_UNTIL):
ctx.match_end = ptr
ctx.match_marks = marks
return MATCHED_OK
elif op == consts.OPCODE_ANY:
# match anything (except a newline)
# <ANY>
if ptr >= ctx.end or rsre_char.is_linebreak(ctx.str(ptr)):
return
ptr = ctx.next(ptr)
elif op == consts.OPCODE_ANY_ALL:
# match anything
# <ANY_ALL>
if ptr >= ctx.end:
return
ptr = ctx.next(ptr)
elif op == consts.OPCODE_ASSERT:
# assert subpattern
# <ASSERT> <0=skip> <1=back> <pattern>
try:
ptr1 = ctx.prev_n(ptr, pattern.pat(ppos+1), ctx.ZERO)
except EndOfString:
return
saved = ctx.match_mode
ctx.match_mode = MODE_ANY
stop = sre_match(ctx, pattern, ppos + 2, ptr1, marks) is None
ctx.match_mode = saved
if stop:
return
marks = ctx.match_marks
ppos += pattern.pat(ppos)
elif op == consts.OPCODE_ASSERT_NOT:
# assert not subpattern
# <ASSERT_NOT> <0=skip> <1=back> <pattern>
try:
ptr1 = ctx.prev_n(ptr, pattern.pat(ppos+1), ctx.ZERO)
except EndOfString:
pass
else:
saved = ctx.match_mode
ctx.match_mode = MODE_ANY
stop = sre_match(ctx, pattern, ppos + 2, ptr1, marks) is not None
ctx.match_mode = saved
if stop:
return
ppos += pattern.pat(ppos)
elif op == consts.OPCODE_AT:
# match at given position (e.g. at beginning, at boundary, etc.)
# <AT> <code>
if not sre_at(ctx, pattern.pat(ppos), ptr):
return
ppos += 1
elif op == consts.OPCODE_BRANCH:
# alternation
# <BRANCH> <0=skip> code <JUMP> ... <NULL>
result = BranchMatchResult(ppos, ptr, marks)
return result.find_first_result(ctx, pattern)
elif op == consts.OPCODE_CATEGORY:
# seems to be never produced, but used by some tests from
# pypy/module/_sre/test
# <CATEGORY> <category>
if (ptr == ctx.end or
not rsre_char.category_dispatch(pattern.pat(ppos), ctx.str(ptr))):
return
ptr = ctx.next(ptr)
ppos += 1
elif op == consts.OPCODE_GROUPREF:
# match backreference
# <GROUPREF> <groupnum>
startptr, length_bytes = get_group_ref(ctx, marks, pattern.pat(ppos))
if length_bytes < 0:
return # group was not previously defined
if not match_repeated(ctx, ptr, startptr, length_bytes):
return # no match
ptr = ctx.go_forward_by_bytes(ptr, length_bytes)
ppos += 1
elif op == consts.OPCODE_GROUPREF_IGNORE:
# match backreference
# <GROUPREF> <groupnum>
startptr, length_bytes = get_group_ref(ctx, marks, pattern.pat(ppos))
if length_bytes < 0:
return # group was not previously defined
ptr = match_repeated_ignore(ctx, ptr, startptr, length_bytes, pattern)
if ptr < ctx.ZERO:
return # no match
ppos += 1
elif consts.eq(op, consts.OPCODE37_GROUPREF_UNI_IGNORE):
# unicode version of OPCODE_GROUPREF_IGNORE
# <GROUPREF> <groupnum>
startptr, length_bytes = get_group_ref(ctx, marks, pattern.pat(ppos))
if length_bytes < 0:
return # group was not previously defined
ptr = match_repeated_uni_ignore(ctx, ptr, startptr, length_bytes)
if ptr < ctx.ZERO:
return # no match
ppos += 1
elif consts.eq(op, consts.OPCODE37_GROUPREF_LOC_IGNORE):
# locale version of OPCODE_GROUPREF_IGNORE
# <GROUPREF> <groupnum>
startptr, length_bytes = get_group_ref(ctx, marks, pattern.pat(ppos))
if length_bytes < 0:
return # group was not previously defined
ptr = match_repeated_loc_ignore(ctx, ptr, startptr, length_bytes)
if ptr < ctx.ZERO:
return # no match
ppos += 1
elif op == consts.OPCODE_GROUPREF_EXISTS:
# conditional match depending on the existence of a group
# <GROUPREF_EXISTS> <group> <skip> codeyes <JUMP> codeno ...
_, length_bytes = get_group_ref(ctx, marks, pattern.pat(ppos))
if length_bytes >= 0:
ppos += 2 # jump to 'codeyes'
else:
ppos += pattern.pat(ppos+1) # jump to 'codeno'
elif op == consts.OPCODE_IN:
# match set member (or non_member)
# <IN> <skip> <set>
if ptr >= ctx.end or not rsre_char.check_charset(ctx, pattern, ppos+1,
ctx.str(ptr)):
return
ppos += pattern.pat(ppos)
ptr = ctx.next(ptr)
elif op == consts.OPCODE_IN_IGNORE:
# match set member (or non_member), ignoring case
# <IN> <skip> <set>
if ptr >= ctx.end or not rsre_char.check_charset(ctx, pattern, ppos+1,
pattern.lowa(ctx.str(ptr))):
return
ppos += pattern.pat(ppos)
ptr = ctx.next(ptr)
elif consts.eq(op, consts.OPCODE37_IN_UNI_IGNORE):
# match set member (or non_member), ignoring case, unicode mode
# <IN> <skip> <set>
if ptr >= ctx.end or not rsre_char.check_charset(ctx, pattern, ppos+1,
rsre_char.getlower_unicode(ctx.str(ptr))):
return
ppos += pattern.pat(ppos)
ptr = ctx.next(ptr)
elif consts.eq(op, consts.OPCODE37_IN_LOC_IGNORE):
# match set member (or non_member), ignoring case, locale mode
# <IN> <skip> <set>
if ptr >= ctx.end or not pattern.charset_loc_ignore(ctx, ppos+1,
ctx.str(ptr)):
return
ppos += pattern.pat(ppos)
ptr = ctx.next(ptr)
elif op == consts.OPCODE_INFO:
# optimization info block
# <INFO> <0=skip> <1=flags> <2=min> ...
if ctx.maximum_distance(ptr, ctx.end) < pattern.pat(ppos+2):
return
ppos += pattern.pat(ppos)
elif op == consts.OPCODE_JUMP:
ppos += pattern.pat(ppos)
elif op == consts.OPCODE_LITERAL:
# match literal string
# <LITERAL> <code>
if ptr >= ctx.end or ctx.str(ptr) != pattern.pat(ppos):
return
ppos += 1
ptr = ctx.next(ptr)
elif op == consts.OPCODE_LITERAL_IGNORE:
# match literal string, ignoring case
# <LITERAL_IGNORE> <code>
if ptr >= ctx.end or pattern.lowa(ctx.str(ptr)) != pattern.pat(ppos):
return
ppos += 1
ptr = ctx.next(ptr)
elif consts.eq(op, consts.OPCODE37_LITERAL_UNI_IGNORE):
# match literal string, ignoring case, unicode mode
# <LITERAL_IGNORE> <code>
if ptr >= ctx.end or rsre_char.getlower_unicode(ctx.str(ptr)) != pattern.pat(ppos):
return
ppos += 1
ptr = ctx.next(ptr)
elif consts.eq(op, consts.OPCODE37_LITERAL_LOC_IGNORE):
# match literal string, ignoring case, locale mode
# <LITERAL_IGNORE> <code>
if ptr >= ctx.end or not pattern.char_loc_ignore(ppos, ctx.str(ptr)):
return
ppos += 1
ptr = ctx.next(ptr)
elif op == consts.OPCODE_MARK:
# set mark
# <MARK> <gid>
gid = pattern.pat(ppos)
marks = Mark(gid, ptr, marks)
ppos += 1
elif op == consts.OPCODE_NOT_LITERAL:
# match if it's not a literal string
# <NOT_LITERAL> <code>
if ptr >= ctx.end or ctx.str(ptr) == pattern.pat(ppos):
return
ppos += 1
ptr = ctx.next(ptr)
elif op == consts.OPCODE_NOT_LITERAL_IGNORE:
# match if it's not a literal string, ignoring case
# <NOT_LITERAL> <code>
if ptr >= ctx.end or pattern.lowa(ctx.str(ptr)) == pattern.pat(ppos):
return
ppos += 1
ptr = ctx.next(ptr)
elif consts.eq(op, consts.OPCODE37_NOT_LITERAL_UNI_IGNORE):
# match if it's not a literal string, ignoring case, unicode mode
# <NOT_LITERAL> <code>
if ptr >= ctx.end or rsre_char.getlower_unicode(ctx.str(ptr)) == pattern.pat(ppos):
return
ppos += 1
ptr = ctx.next(ptr)
elif consts.eq(op, consts.OPCODE37_NOT_LITERAL_LOC_IGNORE):
# match if it's not a literal string, ignoring case, locale mode
# <NOT_LITERAL> <code>
if ptr >= ctx.end or pattern.char_loc_ignore(ppos, ctx.str(ptr)):
return
ppos += 1
ptr = ctx.next(ptr)
elif op == consts.OPCODE_REPEAT:
# general repeat. in this version of the re module, all the work
# is done here, and not on the later UNTIL operator.
# <REPEAT> <skip> <1=min> <2=max> item <UNTIL> tail
# FIXME: we probably need to deal with zero-width matches in here..
# decode the later UNTIL operator to see if it is actually
# a MAX_UNTIL or MIN_UNTIL
untilppos = ppos + pattern.pat(ppos)
tailppos = untilppos + 1
op = pattern.pat(untilppos)
if op == consts.OPCODE_MAX_UNTIL:
# the hard case: we have to match as many repetitions as
# possible, followed by the 'tail'. we do this by
# remembering each state for each possible number of
# 'item' matching.
result = MaxUntilMatchResult(ppos, tailppos, ptr, marks)
return result.find_first_result(ctx, pattern)
elif op == consts.OPCODE_MIN_UNTIL:
# first try to match the 'tail', and if it fails, try
# to match one more 'item' and try again
result = MinUntilMatchResult(ppos, tailppos, ptr, marks)
return result.find_first_result(ctx, pattern)
else:
raise Error("missing UNTIL after REPEAT")
elif op == consts.OPCODE_REPEAT_ONE:
# match repeated sequence (maximizing regexp).
# this operator only works if the repeated item is
# exactly one character wide, and we're not already
# collecting backtracking points. for other cases,
# use the MAX_REPEAT operator.
# <REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail
start = ptr
try:
minptr = ctx.next_n(start, pattern.pat(ppos+1), ctx.end)
except EndOfString:
return # cannot match
ptr = find_repetition_end(ctx, pattern, ppos+3, start,
pattern.pat(ppos+2),
marks)
# when we arrive here, ptr points to the tail of the target
# string. check if the rest of the pattern matches,
# and backtrack if not.
nextppos = ppos + pattern.pat(ppos)
result = RepeatOneMatchResult(nextppos, minptr, ptr, marks)
return result.find_first_result(ctx, pattern)
elif op == consts.OPCODE_MIN_REPEAT_ONE:
# match repeated sequence (minimizing regexp).
# this operator only works if the repeated item is
# exactly one character wide, and we're not already
# collecting backtracking points. for other cases,
# use the MIN_REPEAT operator.
# <MIN_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS> tail
start = ptr
min = pattern.pat(ppos+1)
if min > 0:
try:
minptr = ctx.next_n(ptr, min, ctx.end)
except EndOfString:
return # cannot match
# count using pattern min as the maximum
ptr = find_repetition_end(ctx, pattern, ppos+3, ptr, min, marks)
if ptr < minptr:
return # did not match minimum number of times
max_count = sys.maxint
max = pattern.pat(ppos+2)
if max != rsre_char.MAXREPEAT:
max_count = max - min
assert max_count >= 0
nextppos = ppos + pattern.pat(ppos)
result = MinRepeatOneMatchResult(nextppos, ppos+3, max_count,
ptr, marks)
return result.find_first_result(ctx, pattern)
elif consts.eq(op, consts.OPCODE_POSSESSIVE_REPEAT_ONE):
# match repeated sequence (maximizing regexp) without
# backtracking
# this operator only works if the repeated item is
# exactly one character wide, and we're not already
# collecting backtracking points. for other cases,
# use the MAX_REPEAT operator
# <POSSESSIVE_REPEAT_ONE> <skip> <1=min> <2=max> item <SUCCESS>
# tail
start = ptr
try:
minptr = ctx.next_n(start, pattern.pat(ppos+1), ctx.end)
except EndOfString:
return # cannot match
ptr = find_repetition_end(ctx, pattern, ppos+3, start,
pattern.pat(ppos+2),
marks)
# when we arrive here, ptr points to the tail of the target
# string. match the rest of the pattern.
ppos += pattern.pat(ppos)
elif consts.eq(op, consts.OPCODE_POSSESSIVE_REPEAT):
# create possessive repeat contexts.
# <POSSESSIVE_REPEAT> <skip> <1=min> <2=max> pattern
# <SUCCESS> tail
start = ptr
ptr = find_repetition_end_possessive(
ctx, pattern,
ppos, start,
marks)
if ptr < 0:
return None
marks = ctx.match_marks
ppos += pattern.pat(ppos) + 1 # match tail now
elif consts.eq(op, consts.OPCODE_ATOMIC_GROUP):
# Atomic Group Sub Pattern
# <ATOMIC_GROUP> <skip> pattern <SUCCESS> tail
match = sre_match(ctx, pattern, ppos + 1, ptr, marks)
if match is None:
return None
ptr = ctx.match_end
ppos += pattern.pat(ppos) # match tail now
else:
raise Error("bad pattern code %d" % op)
def get_group_ref(ctx, marks, groupnum):
gid = groupnum * 2
startptr = find_mark(marks, gid)
if startptr < ctx.ZERO:
return 0, -1
endptr = find_mark(marks, gid + 1)
length_bytes = ctx.bytes_difference(endptr, startptr)
return startptr, length_bytes
@specializectx
def match_repeated(ctx, ptr, oldptr, length_bytes):
if ctx.bytes_difference(ctx.end, ptr) < length_bytes:
return False
for i in range(length_bytes):
if ctx.get_single_byte(ptr, i) != ctx.get_single_byte(oldptr, i):
return False
return True
@specializectx
def match_repeated_ignore(ctx, ptr, oldptr, length_bytes, pattern):
oldend = ctx.go_forward_by_bytes(oldptr, length_bytes)
while oldptr < oldend:
if ptr >= ctx.end:
return -1
if pattern.lowa(ctx.str(ptr)) != pattern.lowa(ctx.str(oldptr)):
return -1
ptr = ctx.next(ptr)
oldptr = ctx.next(oldptr)
return ptr
@specializectx
def match_repeated_uni_ignore(ctx, ptr, oldptr, length_bytes):
oldend = ctx.go_forward_by_bytes(oldptr, length_bytes)
while oldptr < oldend:
if ptr >= ctx.end:
return -1
if rsre_char.getlower_unicode(ctx.str(ptr)) != rsre_char.getlower_unicode(ctx.str(oldptr)):
return -1
ptr = ctx.next(ptr)
oldptr = ctx.next(oldptr)
return ptr
@specializectx
def match_repeated_loc_ignore(ctx, ptr, oldptr, length_bytes):
oldend = ctx.go_forward_by_bytes(oldptr, length_bytes)
while oldptr < oldend:
if ptr >= ctx.end:
return -1
if rsre_char.getlower_locale(ctx.str(ptr)) != rsre_char.getlower_locale(ctx.str(oldptr)):
return -1
ptr = ctx.next(ptr)
oldptr = ctx.next(oldptr)
return ptr
@specializectx
def find_repetition_end(ctx, pattern, ppos, ptr, maxcount, marks):
end = ctx.end
# First get rid of the cases where we don't have room for any match.
if maxcount <= 0 or ptr >= end:
return ptr
ptrp1 = ctx.next(ptr)
# Check the first character directly. If it doesn't match, we are done.
# The idea is to be fast for cases like re.search("b+"), where we expect
# the common case to be a non-match. It's much faster with the JIT to
# have the non-match inlined here rather than detect it in the fre() call.
op = pattern.pat(ppos)
for op1, checkerfn in unroll_char_checker:
if op1 == op:
if checkerfn(ctx, pattern, ptr, ppos):
break
return ptr
else:
# obscure case: it should be a single char pattern, but isn't
# one of the opcodes in unroll_char_checker (see test_ext_opcode)
return general_find_repetition_end(ctx, pattern, ppos, ptr, maxcount, marks)
# It matches at least once. If maxcount == 1 (relatively common),
# then we are done.
if maxcount == 1:
return ptrp1
# Else we really need to count how many times it matches.
if maxcount != rsre_char.MAXREPEAT:
# adjust end
try:
end = ctx.next_n(ptr, maxcount, end)
except EndOfString:
pass
op = pattern.pat(ppos)
for op1, fre in unroll_fre_checker:
if op1 == op:
return fre(ctx, pattern, ptrp1, end, ppos)
raise Error("rsre.find_repetition_end[%d]" % op)
@specializectx
def general_find_repetition_end(ctx, pattern, ppos, ptr, maxcount, marks):
# moved into its own JIT-opaque function
end = ctx.end
if maxcount != rsre_char.MAXREPEAT:
# adjust end
end1 = ptr + maxcount
if end1 <= end:
end = end1
while ptr < end and sre_match(ctx, pattern, ppos, ptr, marks) is not None:
ptr = ctx.next(ptr)
return ptr
@specializectx
def match_ANY(ctx, pattern, ptr, ppos): # dot wildcard.
return not rsre_char.is_linebreak(ctx.str(ptr))
def match_ANY_ALL(ctx, pattern, ptr, ppos):
return True # match anything (including a newline)
@specializectx
def match_IN(ctx, pattern, ptr, ppos):
return rsre_char.check_charset(ctx, pattern, ppos+2, ctx.str(ptr))
@specializectx
def match_IN_IGNORE(ctx, pattern, ptr, ppos):
return rsre_char.check_charset(ctx, pattern, ppos+2, pattern.lowa(ctx.str(ptr)))
@specializectx
def match_IN_UNI_IGNORE(ctx, pattern, ptr, ppos):
return rsre_char.check_charset(ctx, pattern, ppos+2, rsre_char.getlower_unicode(ctx.str(ptr)))
@specializectx
def match_IN_LOC_IGNORE(ctx, pattern, ptr, ppos):
return pattern.charset_loc_ignore(ctx, ppos+2, ctx.str(ptr))
@specializectx
def match_LITERAL(ctx, pattern, ptr, ppos):
return ctx.str(ptr) == pattern.pat(ppos+1)
@specializectx
def match_LITERAL_IGNORE(ctx, pattern, ptr, ppos):
return pattern.lowa(ctx.str(ptr)) == pattern.pat(ppos+1)
@specializectx
def match_LITERAL_UNI_IGNORE(ctx, pattern, ptr, ppos):
return rsre_char.getlower_unicode(ctx.str(ptr)) == pattern.pat(ppos+1)
@specializectx
def match_LITERAL_LOC_IGNORE(ctx, pattern, ptr, ppos):
return pattern.char_loc_ignore(ppos+1, ctx.str(ptr))
@specializectx
def match_NOT_LITERAL(ctx, pattern, ptr, ppos):
return ctx.str(ptr) != pattern.pat(ppos+1)
@specializectx
def match_NOT_LITERAL_IGNORE(ctx, pattern, ptr, ppos):
return pattern.lowa(ctx.str(ptr)) != pattern.pat(ppos+1)
@specializectx
def match_NOT_LITERAL_UNI_IGNORE(ctx, pattern, ptr, ppos):
return rsre_char.getlower_unicode(ctx.str(ptr)) != pattern.pat(ppos+1)
@specializectx
def match_NOT_LITERAL_LOC_IGNORE(ctx, pattern, ptr, ppos):
return not pattern.char_loc_ignore(ppos+1, ctx.str(ptr))
def _make_fre(checkerfn):
if checkerfn == match_ANY_ALL:
def fre(ctx, pattern, ptr, end, ppos):
return end
elif checkerfn in (match_IN, match_IN_IGNORE, match_IN_UNI_IGNORE):
# produces three jitdrivers:
# MatchIn
# MatchInIgnore
# MatchInUniIgnore
name = checkerfn.__name__.title().replace('_', '')
method_name = "jitdriver_" + name
install_jitdriver_spec(name,
greens=['ppos', 'pattern'],
reds=['ptr', 'end', 'ctx'],
debugprint=(1, 0))
@specializectx
def fre(ctx, pattern, ptr, end, ppos):
while True:
getattr(ctx, method_name).jit_merge_point(ctx=ctx, ptr=ptr,
end=end, ppos=ppos,
pattern=pattern)
if ptr < end and checkerfn(ctx, pattern, ptr, ppos):
ptr = ctx.next(ptr)
else:
return ptr
else:
# in the other cases, the fre() function is not JITted at all
# and is present as a residual call.
@specializectx
def fre(ctx, pattern, ptr, end, ppos):
while ptr < end and checkerfn(ctx, pattern, ptr, ppos):
ptr = ctx.next(ptr)
return ptr
fre = func_with_new_name(fre, 'fre_' + checkerfn.__name__)
return fre
unroll_char_checker = [
(consts.OPCODE_ANY, match_ANY),
(consts.OPCODE_ANY_ALL, match_ANY_ALL),
(consts.OPCODE_IN, match_IN),
(consts.OPCODE_IN_IGNORE, match_IN_IGNORE),
(consts.OPCODE37_IN_UNI_IGNORE, match_IN_UNI_IGNORE),
(consts.OPCODE37_IN_LOC_IGNORE, match_IN_LOC_IGNORE),
(consts.OPCODE_LITERAL, match_LITERAL),
(consts.OPCODE_LITERAL_IGNORE, match_LITERAL_IGNORE),
(consts.OPCODE37_LITERAL_UNI_IGNORE, match_LITERAL_UNI_IGNORE),
(consts.OPCODE37_LITERAL_LOC_IGNORE, match_LITERAL_LOC_IGNORE),
(consts.OPCODE_NOT_LITERAL, match_NOT_LITERAL),
(consts.OPCODE_NOT_LITERAL_IGNORE, match_NOT_LITERAL_IGNORE),
(consts.OPCODE37_NOT_LITERAL_UNI_IGNORE, match_NOT_LITERAL_UNI_IGNORE),
(consts.OPCODE37_NOT_LITERAL_LOC_IGNORE, match_NOT_LITERAL_LOC_IGNORE),
]
unroll_char_checker = [(_op, _fn) for (_op, _fn) in unroll_char_checker
if _op is not None] # possibly removes the OPCODE37_*
unroll_fre_checker = [(_op, _make_fre(_fn))
for (_op, _fn) in unroll_char_checker]
unroll_char_checker = unrolling_iterable(unroll_char_checker)
unroll_fre_checker = unrolling_iterable(unroll_fre_checker)
##### At dispatch
@specializectx
def sre_at(ctx, atcode, ptr):
if (atcode == consts.AT_BEGINNING or
atcode == consts.AT_BEGINNING_STRING):
return ptr == ctx.ZERO
elif atcode == consts.AT_BEGINNING_LINE:
try:
prevptr = ctx.prev(ptr)
except EndOfString:
return True
return rsre_char.is_linebreak(ctx.str(prevptr))
elif atcode == consts.AT_BOUNDARY:
return at_boundary(ctx, ptr)
elif atcode == consts.AT_NON_BOUNDARY:
return at_non_boundary(ctx, ptr)
elif atcode == consts.AT_END:
return (ptr == ctx.end or
(ctx.next(ptr) == ctx.end and rsre_char.is_linebreak(ctx.str(ptr))))
elif atcode == consts.AT_END_LINE:
return ptr == ctx.end or rsre_char.is_linebreak(ctx.str(ptr))
elif atcode == consts.AT_END_STRING:
return ptr == ctx.end
elif atcode == consts.AT_LOC_BOUNDARY:
return at_loc_boundary(ctx, ptr)
elif atcode == consts.AT_LOC_NON_BOUNDARY:
return at_loc_non_boundary(ctx, ptr)
elif atcode == consts.AT_UNI_BOUNDARY:
return at_uni_boundary(ctx, ptr)
elif atcode == consts.AT_UNI_NON_BOUNDARY:
return at_uni_non_boundary(ctx, ptr)
return False
def _make_boundary(word_checker):
@specializectx
def at_boundary(ctx, ptr):
if ctx.end == ctx.ZERO:
return False
try:
prevptr = ctx.prev(ptr)
except EndOfString:
that = False
else:
that = word_checker(ctx.str(prevptr))
this = ptr < ctx.end and word_checker(ctx.str(ptr))
return this != that
@specializectx
def at_non_boundary(ctx, ptr):
if ctx.end == ctx.ZERO:
return False
try:
prevptr = ctx.prev(ptr)
except EndOfString:
that = False
else:
that = word_checker(ctx.str(prevptr))
this = ptr < ctx.end and word_checker(ctx.str(ptr))
return this == that
return at_boundary, at_non_boundary
at_boundary, at_non_boundary = _make_boundary(rsre_char.is_word)
at_loc_boundary, at_loc_non_boundary = _make_boundary(rsre_char.is_loc_word)
at_uni_boundary, at_uni_non_boundary = _make_boundary(rsre_char.is_uni_word)
# ____________________________________________________________
def _adjust(start, end, length):
if start < 0: start = 0
elif start > length: start = length
if end < 0: end = 0
elif end > length: end = length
return start, end
def match(pattern, string, start=0, end=sys.maxint, fullmatch=False):
assert isinstance(pattern, CompiledPattern)
start, end = _adjust(start, end, len(string))
ctx = StrMatchContext(string, start, end)
if fullmatch:
ctx.match_mode = MODE_FULL
if match_context(ctx, pattern):
return ctx
else:
return None
def fullmatch(pattern, string, start=0, end=sys.maxint):
return match(pattern, string, start, end, fullmatch=True)
def search(pattern, string, start=0, end=sys.maxint):
assert isinstance(pattern, CompiledPattern)
start, end = _adjust(start, end, len(string))
ctx = StrMatchContext(string, start, end)
if search_context(ctx, pattern):
return ctx
else:
return None
install_jitdriver('Match',
greens=['pattern'], reds=['ctx'],
debugprint=(0,))
def match_context(ctx, pattern):
ctx.original_pos = ctx.match_start
if ctx.end < ctx.match_start:
return False
ctx.jitdriver_Match.jit_merge_point(ctx=ctx, pattern=pattern)
return sre_match(ctx, pattern, 0, ctx.match_start, None) is not None
def search_context(ctx, pattern):
ctx.original_pos = ctx.match_start
if ctx.end < ctx.match_start:
return False
base = 0
charset = False
if pattern.pat(base) == consts.OPCODE_INFO:
flags = pattern.pat(2)
if flags & consts.SRE_INFO_PREFIX:
if pattern.pat(5) > 1:
return fast_search(ctx, pattern)
else:
charset = (flags & consts.SRE_INFO_CHARSET)
base += 1 + pattern.pat(1)
if pattern.pat(base) == consts.OPCODE_LITERAL:
return literal_search(ctx, pattern, base)
if charset:
return charset_search(ctx, pattern, base)
return regular_search(ctx, pattern, base)
install_jitdriver('RegularSearch',
greens=['base', 'pattern'],
reds=['start', 'ctx'],
debugprint=(1, 0))
def regular_search(ctx, pattern, base):
start = ctx.match_start
while True:
ctx.jitdriver_RegularSearch.jit_merge_point(ctx=ctx, pattern=pattern,
start=start, base=base)
if sre_match(ctx, pattern, base, start, None) is not None:
ctx.match_start = start
return True
if start >= ctx.end:
break
start = ctx.next_indirect(start)
return False
install_jitdriver_spec("LiteralSearch",
greens=['base', 'character', 'pattern'],
reds=['start', 'ctx'],
debugprint=(2, 0, 1))
@specializectx
def literal_search(ctx, pattern, base):
# pattern starts with a literal character. this is used
# for short prefixes, and if fast search is disabled
character = pattern.pat(base + 1)
base += 2
start = ctx.match_start
while start < ctx.end:
ctx.jitdriver_LiteralSearch.jit_merge_point(ctx=ctx, start=start,
base=base, character=character, pattern=pattern)
start1 = ctx.next(start)
if ctx.str(start) == character:
if sre_match(ctx, pattern, base, start1, None) is not None:
ctx.match_start = start
return True
start = start1
return False
install_jitdriver_spec("CharsetSearch",
greens=['base', 'pattern'],
reds=['start', 'ctx'],
debugprint=(1, 0))
@specializectx
def charset_search(ctx, pattern, base):
# pattern starts with a character from a known set
start = ctx.match_start
while start < ctx.end:
ctx.jitdriver_CharsetSearch.jit_merge_point(ctx=ctx, start=start,
base=base, pattern=pattern)
if rsre_char.check_charset(ctx, pattern, 5, ctx.str(start)):
if sre_match(ctx, pattern, base, start, None) is not None:
ctx.match_start = start
return True
start = ctx.next(start)
return False
install_jitdriver_spec('FastSearch',
greens=['i', 'prefix_len', 'pattern'],
reds=['string_position', 'ctx'],
debugprint=(2, 0))
@specializectx
def fast_search(ctx, pattern):
# skips forward in a string as fast as possible using information from
# an optimization info block
# <INFO> <1=skip> <2=flags> <3=min> <4=...>
# <5=length> <6=skip> <7=prefix data> <overlap data>
string_position = ctx.match_start
if string_position >= ctx.end:
return False
prefix_len = pattern.pat(5)
assert prefix_len >= 0
i = 0
while True:
ctx.jitdriver_FastSearch.jit_merge_point(ctx=ctx,
string_position=string_position, i=i, prefix_len=prefix_len,
pattern=pattern)
char_ord = ctx.str(string_position)
if char_ord != pattern.pat(7 + i):
if i > 0:
overlap_offset = prefix_len + (7 - 1)
i = pattern.pat(overlap_offset + i)
continue
else:
i += 1
if i == prefix_len:
# start = string_position + 1 - prefix_len: computed later
ptr = string_position
prefix_skip = pattern.pat(6)
if prefix_skip == prefix_len:
ptr = ctx.next(ptr)
else:
assert prefix_skip < prefix_len
ptr = ctx.prev_n(ptr, prefix_len-1 - prefix_skip, ctx.ZERO)
#flags = pattern.pat(2)
#if flags & rsre_char.SRE_INFO_LITERAL:
# # matched all of pure literal pattern
# ctx.match_start = start
# ctx.match_end = ptr
# ctx.match_marks = None
# return True
pattern_offset = pattern.pat(1) + 1
ppos_start = pattern_offset + 2 * prefix_skip
if sre_match(ctx, pattern, ppos_start, ptr, None) is not None:
start = ctx.prev_n(ptr, prefix_skip, ctx.ZERO)
ctx.match_start = start
return True
overlap_offset = prefix_len + (7 - 1)
i = pattern.pat(overlap_offset + i)
string_position = ctx.next(string_position)
if string_position >= ctx.end:
return False
|