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
|
/*
mxte -- A table driven tagging engine for Python (Version 0.9)
Copyright (c) 2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
Copyright (c) 2000-2001, eGenix.com Software GmbH; mailto:info@egenix.com
*/
/* Debugging switches */
/*#define MAL_DEBUG*/
/*#define MAL_REF_DEBUG*/
/* Logging file used by debugging facility */
#ifndef MAL_DEBUG_OUTPUTFILE
# define MAL_DEBUG_OUTPUTFILE "mxTagEngine.log"
#endif
#include "mx.h"
#include "mxstdlib.h"
#include "mxTextTools.h"
/* --- Internal definitions ----------------------------------------------- */
#if 0
/* The beginnings of a Tag Table compiler... */
typedef struct {
PyObject *tagobj; /* Tag object to assign or NULL */
int cmd; /* Command (encoded as integer) */
PyObject *match; /* Matching argument */
int jne, je; /* Jump offsets */
} TagTableEntry;
#endif
/* --- Tagging Enigne ----------------------------------------------------- */
/* Forwards */
static int match_append(int flags, PyObject *pytext, PyObject *taglist,
PyObject *tagobj, int l, int r, PyObject *subtags);
/* fast_tag: a table driven parser engine
- return codes: rc = 2: match ok; rc = 1: match failed; rc = 0: error
- doesn't check type of passed arguments !
- doesn't increment reference counts of passed objects !
*/
int fast_tag(PyObject *pytext, /* must be a Python string */
char *text, /* text of that string */
int len_text, /* "length" of the Python string */
PyObject *table, /* must be a tuple (this is
*not* checked) */
int start_text, /* start position in text */
PyObject *taglist, /* must be a Python list or None */
int *next) /* output: next position in text */
{
register int x; /* current (head) position in text */
int i; /* index of current table entry */
int start = start_text; /* start position for current tag */
int len_table = PyTuple_GET_SIZE(table); /* table length */
int rc = -1; /* return code: -1 not set, 0 error, 1
not ok, 2 ok */
int loopcount = -1; /* loop counter */
int loopstart = start; /* loop start position */
int flags; /* flags set in command */
int cmd; /* command */
int jne; /* rel. jump distance on 'not matched' */
int je; /* dito on 'matched' */
PyObject *tagobj, /* tag object */
*match; /* matching parameter */
/* Main loop */
for (x = start, i = 0, je = 0;;) {
next_entry:
/* Get next entry */
i += je;
if (i >= len_table || i < 0)
/* Out of bounds: we're finished */
goto finished;
je = 1;
jne = 0;
/* Parse arguments - inlined for speed */
{
register PyObject *entry;
register PyObject *w;
/* Parse entry */
entry = PyTuple_GET_ITEM(table,i);
Py_AssertWithArg(
PyTuple_Check(entry) && PyTuple_GET_SIZE(entry) >= 3,
PyExc_TypeError,
"tag table entry %i: expected a tuple of the form "
"(tagobj,command,arg[,jne[,je]])",i);
/* Get tagobj */
tagobj = PyTuple_GET_ITEM(entry,0);
/* Get cmd */
w = PyTuple_GET_ITEM(entry,1);
Py_AssertWithArg(PyInt_Check(w),
PyExc_TypeError,
"tag table entry %i: "
"command must be an integer",i);
cmd = PyInt_AS_LONG(w);
/* Get match object */
match = PyTuple_GET_ITEM(entry,2);
/* Optional parameters*/
if (PyTuple_GET_SIZE(entry) > 3) {
/* get jne */
w = PyTuple_GET_ITEM(entry,3);
Py_AssertWithArg(PyInt_Check(w),
PyExc_TypeError,
"tag table entry %i: "
"jne must be an integer",i);
jne = PyInt_AS_LONG(w);
if (PyTuple_GET_SIZE(entry) > 4) {
/* get je */
w = PyTuple_GET_ITEM(entry,4);
Py_AssertWithArg(PyInt_Check(w),
PyExc_TypeError,
"tag table entry %i: "
"je must be an integer",i);
je = PyInt_AS_LONG(w);
}
}
}
/* Decode flags and cmd */
flags = (cmd >> 8) << 8;
cmd = cmd & 0xff;
IF_DEBUGGING {
PyObject *v;
mxDebugPrintf("\n# Tag Table entry %i: ",i);
v = PyObject_Repr(tagobj);
if (!v) {
v = tagobj;
Py_INCREF(v);
}
if (PyString_Check(v))
mxDebugPrintf("tagobj=%s cmd=%i flags=%i position=%i\n",
PyString_AsString(v),cmd,flags,x);
else
mxDebugPrintf("tagobj at 0x%lx cmd=%i flags=%i position=%i\n",
(long)v,cmd,flags,x);
Py_DECREF(v);
}
/* Low-level matching commands */
if (cmd > MATCH_MAX_SPECIALS && cmd < MATCH_MAX_LOWLEVEL) {
char *m;
Py_AssertWithArg(PyString_Check(match),
PyExc_TypeError,
"Tag Table entry %i: arg must be a string",i);
m = PyString_AS_STRING(match);
/* save starting position */
start = x;
switch (cmd) {
case MATCH_ALLIN:
{
register int ml = PyString_GET_SIZE(match);
register char *tx = text + x;
DPRINTF("\nAllIn :\n"
" looking for = '%.40s'\n"
" in string = '%.40s'\n",m,tx);
if (ml > 1)
for (; x < len_text; tx++, x++) {
register int j;
register char *mj = m;
register char ctx = *tx;
for (j=0; j < ml && ctx != *mj; mj++, j++) ;
if (j == ml) break;
}
else if (ml == 1)
/* one char only: use faster variant: */
for (; x < len_text && *tx == *m; tx++, x++) ;
break;
}
case MATCH_ALLNOTIN:
{
register int ml = PyString_GET_SIZE(match);
register char *tx = text + x;
DPRINTF("\nAllNotIn :\n"
" looking for = '%.40s'\n"
" not in string = '%.40s'\n",m,tx);
if (ml != 1)
for (; x < len_text; tx++, x++) {
register int j;
register char *mj = m;
register char ctx = *tx;
for (j=0; j < ml && ctx != *mj; mj++, j++) ;
if (j != ml) break;
}
else
/* one char only: use faster variant: */
for (; x < len_text && *tx != *m; tx++, x++) ;
break;
}
case MATCH_IS:
{
DPRINTF("\nIs :\n"
" looking for = '%.40s'\n"
" in string = '%.40s'\n",m,text+x);
if (*(text + x) == *m && x < len_text)
x++;
break;
}
case MATCH_ISIN:
{
register int ml = PyString_GET_SIZE(match);
register char ctx = *(text + x);
DPRINTF("\nIsIn :\n"
" looking for = '%.40s'\n"
" in string = '%.40s'\n",m,text+x);
if (ml > 0 && x < len_text) {
register int j;
register char *mj = m;
for (j=0; j < ml && ctx != *mj; mj++, j++) ;
if (j != ml) x++;
}
break;
}
case MATCH_ISNOTIN:
{
register int ml = PyString_GET_SIZE(match);
register char ctx = *(text + x);
DPRINTF("\nIsNotIn :\n"
" looking for = '%.40s'\n"
" not in string = '%.40s'\n",m,text+x);
if (ml > 0 && x < len_text) {
register int j;
register char *mj = m;
for (j=0; j < ml && ctx != *mj; mj++, j++) ;
if (j == ml) x++;
}
else
x++;
break;
}
case MATCH_WORD:
{
int ml1 = PyString_GET_SIZE(match) - 1;
register char *tx = text + x + ml1;
register int j = ml1;
register char *mj = m + j;
DPRINTF("\nWord :\n"
" looking for = '%.40s'\n"
" in string = '%.40s'\n",m,tx);
if (x+ml1 >= len_text) break;
/* compare from right to left */
for (; j >= 0 && *tx == *mj;
tx--, mj--, j--) ;
if (j >= 0) /* not matched */
x = start; /* reset */
else
x += ml1 + 1;
break;
}
case MATCH_WORDSTART:
case MATCH_WORDEND:
{
int ml1 = PyString_GET_SIZE(match) - 1;
if (ml1 >= 0) {
register char *tx = text + x;
DPRINTF("\nWordStart/End :\n"
" looking for = '%.40s'\n"
" in string = '%.40s'\n",m,tx);
/* Brute-force method; from right to left */
for (;;) {
register int j = ml1;
register char *mj = m + j;
if (x+j >= len_text) {
/* reached eof: no match, rewind */
x = start;
break;
}
/* scan from right to left */
for (tx += j; j >= 0 && *tx == *mj;
tx--, mj--, j--) ;
/*
DPRINTF("match text[%i+%i]: %c == %c\n",
x,j,*tx,*mj);
*/
if (j < 0) {
/* found */
if (cmd == MATCH_WORDEND) x += ml1 + 1;
break;
}
/* not found: rewind and advance one char */
tx -= j - 1;
x++;
}
}
break;
}
case MATCH_ALLINSET:
{
register char *tx = text + x;
Py_AssertWithArg(PyString_GET_SIZE(match) == 32,
PyExc_TypeError,
"Tag Table entry %i: "
"sets must have 32 chars "
"(cmd=AllInSet)",i);
DPRINTF("\nAllInSet :\n"
" looking for = set at 0x%lx\n"
" in string = '%.40s'\n",(long)match,tx);
for (;x < len_text &&
((unsigned char) m[(unsigned char)*tx >> 3] &
(1 << (*tx & 7))) > 0;
tx++, x++) ;
break;
}
case MATCH_ISINSET:
{
register char *tx = text + x;
Py_AssertWithArg(PyString_GET_SIZE(match) == 32,
PyExc_TypeError,
"Tag Table entry %i: "
"sets must have 32 chars "
"(cmd=IsInSet)",i);
DPRINTF("\nIsInSet :\n"
" looking for = set at 0x%lx\n"
" in string = '%.40s'\n",(long)match,tx);
if (x < len_text &&
((unsigned char) m[(unsigned char)*tx >> 3] &
(1 << (*tx & 7))) > 0) x++;
break;
}
default:
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"unknown command",i);
} /* switch */
/* Not matched */
if (start == x) {
DPRINTF(" (no success)\n");
if (jne == 0) {
/* failed */
rc = 1;
goto finished;
}
else
je = jne;
goto next_entry;
}
/* Matched */
if (tagobj != Py_None) {
if (match_append(flags,pytext,taglist,tagobj,
start,x,NULL) < 0)
goto onError;
DPRINTF(" [%i:%i] (matched and remembered this slice)\n",
start,x);
}
else
DPRINTF(" [%i:%i] (matched but not saved)\n",start,x);
if (flags & MATCH_LOOKAHEAD) {
x = start;
DPRINTF(" LOOKAHEAD flag set: reseting position to %i\n",
x);
}
goto next_entry;
}
/* Jumps & special commands */
if (cmd < MATCH_MAX_SPECIALS) {
switch (cmd) {
case MATCH_FAIL: /* == MATCH_JUMP */
if (jne == 0) { /* match failed */
rc = 1;
goto finished;
}
else
je = jne;
goto next_entry;
case MATCH_SKIP:
if (PyInt_Check(match)) {
DPRINTF("\nSkip %li characters\n"
" in string = '%.40s'\n",
PyInt_AS_LONG(match),text+x);
start = x;
x += PyInt_AS_LONG(match);
break;
}
else
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"expected an integer (cmd=Skip)",i);
case MATCH_MOVE:
if (PyInt_Check(match)) {
start = x;
x = PyInt_AS_LONG(match);
if (x < 0)
/* Relative to end of the slice */
x += len_text + 1;
else
/* Relative to beginning of the slice */
x += start_text;
DPRINTF("\nMove to position %i \n"
" string = '%.40s'\n",
x,text+x);
break;
}
else
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"expected an integer (cmd=Move)",i);
case MATCH_EOF:
DPRINTF("\nEOF at position %i ? \n"
" string = '%.40s'\n",
x,text+x);
if (len_text > x) { /* not matched */
DPRINTF(" (no success)\n");
if (jne == 0) { /* match failed */
rc = 1;
goto finished;
}
else
je = jne;
goto next_entry;
}
/* Matched & finished */
if (tagobj != Py_None) {
x = len_text;
if (match_append(flags,pytext,taglist,tagobj,
x,x,NULL) < 0)
goto onError;
DPRINTF(" [%i:%i] (matched and remembered this slice)\n",
x,x);
}
else
DPRINTF(" [%i:%i] (matched but not saved)\n",
x,x);
rc = 2;
goto finished;
default:
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"unknown command",i);
}
/* Matched */
if (x < 0)
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"moved/skipped beyond start of text",i);
if (tagobj != Py_None) {
if (match_append(flags,pytext,taglist,tagobj,
start,x,NULL) < 0)
goto onError;
DPRINTF(" [%i:%i] (matched and remembered this slice)\n",
start,x);
}
else
DPRINTF(" [%i:%i] (matched but not saved)\n",start,x);
if (flags & MATCH_LOOKAHEAD) {
x = start;
DPRINTF(" LOOKAHEAD flag set: reseting position to %i\n",
x);
}
goto next_entry;
}
/* Higher level matching commands */
switch (cmd) {
case MATCH_SWORDSTART:
case MATCH_SWORDEND:
case MATCH_SFINDWORD:
{
int len_match;
DPRINTF("\nsWordStart/End/sFindWord :\n"
" in string = '%.40s'\n",text+x);
/* Boyer Moore search */
if (_mxBMS_Check(match)) {
mxBMSObject *so = (mxBMSObject *)match;
len_match = so->c->len_match;
start = x;
if (so->tr) {
/* search with translate table */
x = bm_tr_search(so->c,
text,
start,
len_text,
PyString_AS_STRING(so->tr));
}
else {
/* exact search */
x = bm_search(so->c,
text,
start,
len_text);
}
}
#ifdef MXFASTSEARCH
/* Fast Search */
else if (_mxFS_Check(match)) {
mxFSObject *so = (mxFSObject *)match;
len_match = so->c->len_match;
start = x;
if (so->tr) {
/* search with translate table */
x = fs_tr_search(so->c,
text,
start,
len_text,
PyString_AS_STRING(so->tr));
}
else {
/* exact search */
x = fs_search(so->c,
text,
start,
len_text);
}
}
#endif
else {
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"expected a search object "
"(cmd=WordStart/End)",i);
}
if (start == x) {
/* not matched */
DPRINTF(" (no success)\n");
if (jne == 0) {
/* match failed */
rc = 1;
goto finished;
}
else
je = jne;
}
else {
/* matched */
/* Apply correction of x for SWORDSTART and start
for SFINDWORD */
if (cmd == MATCH_SWORDSTART)
x -= len_match;
else if (cmd == MATCH_SFINDWORD)
start = x - len_match;
if (tagobj != Py_None) {
if (match_append(flags,pytext,taglist,tagobj,
start,x,NULL) < 0)
goto onError;
DPRINTF(" [%i:%i] (matched and remembered this slice)\n",
start,x);
}
else
DPRINTF(" [%i:%i] (matched but not saved)\n",start,x);
if (flags & MATCH_LOOKAHEAD) {
x = start;
DPRINTF(" LOOKAHEAD option set: reseting position to %i\n",
x);
}
}
goto next_entry;
}
case MATCH_TABLE:
case MATCH_SUBTABLE:
if (PyInt_Check(match) &&
PyInt_AS_LONG(match) == MATCH_THISTABLE)
match = table;
if (PyTuple_Check(match)) {
PyObject *subtags;
int y = x;
int newrc = 0;
int len_taglist;
if (taglist != Py_None && cmd != MATCH_SUBTABLE) {
/* Create a new list for use as subtaglist */
subtags = PyList_New(0);
if (subtags == NULL)
goto onError;
len_taglist = 0;
}
else {
/* Use taglist as subtaglist */
subtags = taglist;
Py_INCREF(subtags);
len_taglist = PyList_Size(taglist);
if (len_taglist < 0)
goto onError;
}
DPRINTF("\n[Sub]Table : using table at 0x%lx\n",(long)match);
start = x;
/* match other table */
newrc = fast_tag(pytext,text,len_text,match,start,subtags,&y);
if (newrc == 0) {
Py_DECREF(subtags);
goto onError;
}
if (newrc == 1) {
/* not matched */
DPRINTF(" (no success)\n");
/* Undo changes to taglist in case of SUBTABLE match */
if (cmd == MATCH_SUBTABLE && taglist != Py_None) {
DPRINTF(" undoing changes: del taglist[%i:%i]\n",
len_taglist, PyList_Size(taglist));
if (PyList_SetSlice(taglist,
len_taglist,
PyList_Size(taglist),
NULL))
goto onError;
}
if (jne == 0) {
/* match failed */
rc = 1;
Py_DECREF(subtags);
goto finished;
}
else
je = jne;
}
else {
/* matched */
/* move x to new position */
x = y;
if (tagobj != Py_None) {
if (match_append(flags,pytext,taglist,tagobj,
start,x,subtags) < 0) {
/* append failed */
Py_DECREF(subtags);
goto onError;
DPRINTF(" [%i:%i] (matched and remembered this slice)\n",
start,x);
}
}
else
DPRINTF(" [%i:%i] (matched but not saved)\n",start,x);
if (flags & MATCH_LOOKAHEAD) {
x = start;
DPRINTF(" LOOKAHEAD option set: reseting position to %i\n",
x);
}
}
Py_DECREF(subtags);
goto next_entry;
}
else
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"expected a table (cmd=[Sub]Table)",i);
goto next_entry;
case MATCH_TABLEINLIST:
case MATCH_SUBTABLEINLIST:
if (PyTuple_Check(match)) {
PyObject *subtags;
int y = x;
int newrc = 0;
int len = PyTuple_GET_SIZE(match);
int len_taglist;
if (len == 2) {
PyObject *w;
PyObject *tables;
int index;
/* get tables (a list of tuples) */
tables = PyTuple_GET_ITEM(match,0);
Py_AssertWithArg(tables != NULL && PyList_Check(tables),
PyExc_TypeError,
"Tag Table entry %i: "
"first entry in arg tuple "
"must be a list",i);
/* get index (integer) */
w = PyTuple_GET_ITEM(match,1);
Py_AssertWithArg(w != NULL && PyInt_Check(w),
PyExc_TypeError,
"Tag Table entry %i: "
"second entry in arg tuple "
"must be an integer",i);
index = PyInt_AS_LONG(w);
/* get matching table and incr ref count for it */
match = PyList_GetItem(tables,index);
if (match != NULL) {
Py_INCREF(match);
if (!PyTuple_Check(match)) {
Py_DECREF(match);
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"Tag Tables must be a tuples",i);
}
}
else /* not found */
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"matching table not found "
"in list of tables",i);
}
else /* incorrect length */
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"expected (list,index) as arg",i);
if (taglist != Py_None && cmd != MATCH_SUBTABLEINLIST) {
/* Create a new list for use as subtaglist */
subtags = PyList_New(0);
if (subtags == NULL) {
Py_DECREF(match);
goto onError;
}
len_taglist = 0;
}
else {
/* Use taglist as subtaglist */
subtags = taglist;
Py_INCREF(subtags);
len_taglist = PyList_Size(taglist);
if (len_taglist < 0)
goto onError;
}
DPRINTF("\n[Sub]TableInList : using table at 0x%lx\n",
(long)match);
start = x;
/* match other table */
newrc = fast_tag(pytext,text,len_text,match,start,subtags,&y);
if (newrc == 0) {
Py_DECREF(subtags);
Py_DECREF(match);
goto onError;
}
if (newrc == 1) {
/* not matched */
DPRINTF(" (no success)\n");
/* Undo changes to taglist in case of SUBTABLE match */
if (cmd == MATCH_SUBTABLE && taglist != Py_None) {
DPRINTF(" undoing changes: del taglist[%i:%i]\n",
len_taglist, PyList_Size(taglist));
if (PyList_SetSlice(taglist,
len_taglist,
PyList_Size(taglist),
NULL))
goto onError;
}
if (jne == 0) {
/* match failed */
rc = 1;
Py_DECREF(subtags);
Py_DECREF(match);
goto finished;
}
else
je = jne;
}
else {
/* matched */
/* move x to new position */
x = y;
if (tagobj != Py_None) {
if (match_append(flags,pytext,taglist,tagobj,
start,x,subtags) < 0) {
/* append failed */
Py_DECREF(subtags);
Py_DECREF(match);
goto onError;
}
DPRINTF(" [%i:%i] (matched and remembered this slice)\n",
start,x);
}
else {
DPRINTF(" [%i:%i] (matched but not saved)\n",start,x);
}
if (flags & MATCH_LOOKAHEAD) {
x = start;
DPRINTF(" LOOKAHEAD option set: reseting position to %i\n",
x);
}
}
Py_DECREF(subtags);
Py_DECREF(match);
goto next_entry;
}
else
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"expected a tuple (tables,index) "
"(cmd=TableInList)",i);
goto next_entry;
case MATCH_LOOP:
DPRINTF("\nLoop: pre loop counter = %i\n",loopcount);
if (loopcount > 0)
/* we are inside a loop */
loopcount--;
else if (loopcount < 0) {
/* starting a new loop */
if (PyInt_Check(match)) {
loopcount = PyInt_AS_LONG(match);
loopstart = x;
}
else
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"expected an integer (cmd=Loop)",i);
}
if (loopcount == 0) {
/* finished loop */
loopcount = -1;
if (loopstart == x) {
/* not matched */
DPRINTF(" (no success)\n");
}
else if (tagobj != Py_None) {
/* matched */
if (match_append(flags,pytext,taglist,tagobj,
loopstart,x,NULL) < 0)
goto onError;
DPRINTF(" [%i:%i] (matched and remembered this slice)\n",
loopstart,x);
}
else {
DPRINTF(" [%i:%i] (matched but not saved)\n",start,x);
}
if (flags & MATCH_LOOKAHEAD) {
x = start;
DPRINTF(" LOOKAHEAD option set: reseting position to %i\n",
x);
}
/* skip loop body */
je = jne;
}
DPRINTF("\nloop: post loop counter = %i\n",loopcount);
goto next_entry;
case MATCH_LOOPCONTROL:
if (PyInt_Check(match)) {
DPRINTF("\nLoopControl: loop counter = %i, "
"setting it to = %li\n",
loopcount,PyInt_AS_LONG(match));
loopcount = PyInt_AS_LONG(match);
goto next_entry;
}
else
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"expected an integer (cmd=LoopControl)",i);
goto next_entry;
case MATCH_CALL:
case MATCH_CALLARG:
{
PyObject *fct;
int argc;
if (!PyTuple_Check(match)) {
argc = 0;
fct = match;
}
else {
argc = PyTuple_GET_SIZE(match) - 1;
Py_AssertWithArg(argc >= 0,
PyExc_TypeError,
"Tag Table entry %i: "
"expected a tuple (fct,arg0,arg1,...)"
"(cmd=CallArg)",i);
fct = PyTuple_GET_ITEM(match,0);
}
if (PyCallable_Check(fct)) {
PyObject *args;
register PyObject *w;
register int i;
DPRINTF("\nCall[Arg] :\n");
start = x;
/* Build args = (pytext,start,len_text[,arg0,arg1,...]) */
args = PyTuple_New(3 + argc);
if (!args)
goto onError;
Py_INCREF(pytext);
PyTuple_SET_ITEM(args,0,pytext);
w = PyInt_FromLong(start);
if (!w)
goto onError;
PyTuple_SET_ITEM(args,1,w);
w = PyInt_FromLong(len_text);
if (!w)
goto onError;
PyTuple_SET_ITEM(args,2,w);
for (i = 0; i < argc; i++) {
w = PyTuple_GET_ITEM(match,i + 1);
Py_INCREF(w);
PyTuple_SET_ITEM(args,3 + i,w);
}
w = PyEval_CallObject(fct,args);
Py_DECREF(args);
if (w == NULL)
goto onError;
Py_AssertWithArg(PyInt_Check(w),
PyExc_TypeError,
"Tag Table entry %i: "
"matching fct has to return an integer",i);
x = PyInt_AS_LONG(w);
Py_DECREF(w);
if (start == x) {
/* not matched */
DPRINTF(" (no success)\n");
if (jne == 0) {
/* match failed */
rc = 1;
goto finished;
}
else
je = jne;
}
else if (tagobj != Py_None) {
/* matched */
if (match_append(flags,pytext,taglist,tagobj,
start,x,NULL) < 0)
goto onError;
DPRINTF(" [%i:%i] (matched and remembered this slice)\n",
start,x);
}
else {
DPRINTF(" [%i:%i] (matched but not saved)\n",
start,x);
}
if (flags & MATCH_LOOKAHEAD) {
x = start;
DPRINTF(" LOOKAHEAD option set: reseting position to %i\n",
x);
}
goto next_entry;
}
else
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: "
"expected a callable object "
"(cmd=Call[Arg])",i);
}
goto next_entry;
default:
Py_ErrorWithArg(PyExc_TypeError,
"Tag Table entry %i: unknown command",i);
} /* switch */
} /* for-loop */
finished:
/* In case no specific return code was set, check if we have
matched successfully (pointer beyond the end of the table) or
failed to match (pointer negative) */
if (rc < 0) {
if (i >= len_table)
rc = 2;
else if (i < 0)
rc = 1;
else
Py_ErrorWith2Args(PyExc_SystemError,
"Internal Error: "
"tagging engine finished with no proper result"
"at position %i in table 0x%0x",
i,(int)table);
}
DPRINTF("\nTag Engine finished: %s; Tag Table entry %i; position %i\n",
rc==1?"failed":"success",i,x);
/* Record the current head position */
*next = x;
return rc;
onError:
rc = 0;
return rc;
}
/*
What to do with a successful match depends on the value of flags:
flags mod x == 1:
-----------------
MATCH_CALLTAG:
call the tagobj with (taglist,pytext,l,r,subtags) and
let it decide what to do
MATCH_APPENDTAG:
do a tagobj.append((None,l,r,subtags))
default:
do a taglist.append((tagobj,l,r,subtags))
- subtags is made to reference Py_None, if subtags == NULL
- returns -1 if not successful, 0 on success
- refcounts: all reference counts are incremented upon success only
*/
static
int match_append(int flags,
PyObject *pytext,
PyObject *taglist,
PyObject *tagobj,
int l,
int r,
PyObject *subtags)
{
register PyObject *w;
if (subtags == NULL)
subtags = Py_None;
/* Default mechanism: */
if (flags == 0) {
/* append result to taglist */
if (taglist == Py_None)
return 0; /* nothing to be done */
/* Build w = (tagobj,l,r,subtags) */
w = PyTuple_New(4);
if (!w)
goto onError;
Py_INCREF(tagobj);
PyTuple_SET_ITEM(w,0,tagobj);
PyTuple_SET_ITEM(w,1,PyInt_FromLong(l));
PyTuple_SET_ITEM(w,2,PyInt_FromLong(r));
Py_INCREF(subtags);
PyTuple_SET_ITEM(w,3,subtags);
if (PyList_Append(taglist,w))
goto onError;
Py_DECREF(w);
return 0;
}
/* Flags are set: */
if (flags & MATCH_APPENDTAGOBJ) {
/* append the tagobj to the taglist */
if (taglist == Py_None)
return 0; /* nothing to be done */
return PyList_Append(taglist,tagobj);
}
if (flags & MATCH_APPENDMATCH) {
/* append the match obj to the taglist */
register PyObject *v;
if (taglist == Py_None)
return 0; /* nothing to be done */
v = PyString_FromStringAndSize(PyString_AS_STRING(pytext) + l, r-l);
if (!v)
goto onError;
if (PyList_Append(taglist,v))
goto onError;
Py_DECREF(v);
return 0;
}
if (flags & MATCH_CALLTAG) {
/* call tagobj */
register PyObject *args;
/* Build args = (taglist,pytext,l,r,subtags) */
args = PyTuple_New(5);
if (!args)
goto onError;
Py_INCREF(taglist);
PyTuple_SET_ITEM(args,0,taglist);
Py_INCREF(pytext);
PyTuple_SET_ITEM(args,1,pytext);
PyTuple_SET_ITEM(args,2,PyInt_FromLong(l));
PyTuple_SET_ITEM(args,3,PyInt_FromLong(r));
Py_INCREF(subtags);
PyTuple_SET_ITEM(args,4,subtags);
w = PyEval_CallObject(tagobj,args);
Py_DECREF(args);
if (w == NULL)
goto onError;
Py_DECREF(w);
return 0;
}
if (flags & MATCH_APPENDTAG) {
/* append to tagobj */
Py_Assert(PyList_Check(tagobj),
PyExc_TypeError,
"Tag Table: used AppendToTag, but tagobj is not a list");
/* Build w = (None,l,r,subtags) */
w = PyTuple_New(4);
if (!w)
goto onError;
Py_INCREF(Py_None);
PyTuple_SET_ITEM(w,0,Py_None);
PyTuple_SET_ITEM(w,1,PyInt_FromLong(l));
PyTuple_SET_ITEM(w,2,PyInt_FromLong(r));
Py_INCREF(subtags);
PyTuple_SET_ITEM(w,3,subtags);
if (PyList_Append(tagobj,w))
goto onError;
Py_DECREF(w);
return 0;
}
Py_Error(PyExc_TypeError,
"Tag Table: unknown flag in command");
onError:
return -1;
}
|