1377
1376
RETURN_FAILURE;
1379
1378
case SRE_OP_POSSESSIVE_REPEAT:
1380
/* create repeat context. all the hard work is done
1381
by the UNTIL operator (POSSESSIVE_UNTIL) */
1382
/* <POSSESSIVE_REPEAT> <skip> <1=min> <2=max> item
1383
<POSSESSIVE_UNTIL> tail */
1379
/* create possessive repeat contexts. */
1380
/* <POSSESSIVE_REPEAT> <skip> <1=min> <2=max> <pattern>
1384
1382
TRACE(("|%p|%p|POSSESSIVE_REPEAT %d %d\n", ctx->pattern,
1385
1383
ctx->ptr, ctx->pattern[1], ctx->pattern[2]));
1387
/* install new repeat context */
1388
ctx->u.rep = (SRE_REPEAT*) PyObject_MALLOC(sizeof(*ctx->u.rep));
1385
/* Set the global Input pointer to this context's Input
1387
state->ptr = ctx->ptr;
1389
/* Initialize Count to 0 */
1392
/* Keep trying to parse the <pattern> sub-pattern until the
1393
end is reached, creating a new context each time. */
1394
while (ctx->count <= (int)ctx->pattern[2] ||
1395
(int)ctx->pattern[2] == 65535) {
1397
/* We have not reached the maximin matches, so try to
1399
DO_JUMP(JUMP_POSS_REPEAT, jump_poss_repeat,
1401
RETURN_ON_ERROR(ret);
1403
/* Check to see if the last attempted match
1406
/* Success, increment the count. */
1409
/* Last attempted match failed. */
1411
/* Check for minimum required matches. */
1412
if (ctx->count < (int)ctx->pattern[1]) {
1416
/* We have sufficient matches, so exit loop. */
1393
ctx->u.rep->count = -1;
1394
ctx->u.rep->pattern = ctx->pattern;
1395
ctx->u.rep->prev = state->repeat;
1396
ctx->u.rep->last_ptr = NULL;
1397
state->repeat = ctx->u.rep;
1399
state->ptr = ctx->ptr;
1401
/* Go to the Until Clause; we evaluate that first because
1402
it is possible we may have a match count of 0 and so we
1403
want to skip the evaluation of pattern until UNTIL has
1404
set up a stack point where it can fail and just continue
1406
ctx->pattern += ctx->pattern[0];
1409
/* TODO move code to UNTIL handler */
1410
printf("\nResult: %lu; Last Count: %lu @ %s with next op "
1411
"code: %d\n", ret, ctx->u.rep->count,
1412
(char *)state->ptr, ctx->pattern[ctx->pattern[0]+1]);
1413
state->repeat = ctx->u.rep->prev;
1414
PyObject_FREE(ctx->u.rep);
1416
1422
/* Evaluate Tail */
1417
1423
ctx->pattern += ctx->pattern[0] + 1;
1420
case SRE_OP_POSSESSIVE_UNTIL:
1421
/* maximizing repeat */
1422
/* <REPEAT> <skip> <1=min> <2=max> item <POSSESSIVE_UNTIL>
1425
/* FIXME: we probably need to deal with zero-width
1426
matches in here... */
1428
ctx->u.rep = state->repeat;
1430
RETURN_ERROR(SRE_ERROR_STATE);
1433
state->ptr = ctx->ptr;
1435
ctx->count = ctx->u.rep->count+1;
1437
TRACE(("|%p|%p|POSSESSIVE_UNTIL %d\n", ctx->pattern,
1438
ctx->ptr, ctx->count));
1439
printf("\nCount: %lu @ %s; %d needed, %d capacity\n",
1440
ctx->count, ctx->ptr, ctx->u.rep->pattern[1],
1441
ctx->u.rep->pattern[2]);
1442
printf("Offset: %p; End: %p; Context: %p\n", state->ptr,
1443
ctx->u.rep->last_ptr, ctx->ptr);
1445
if (ctx->count < ctx->u.rep->pattern[1]) {
1446
/* not enough matches */
1447
ctx->u.rep->count = ctx->count;
1448
DO_JUMP(JUMP_POSS_UNTIL_1, jump_poss_until_1,
1449
ctx->u.rep->pattern+3);
1450
printf("Return: %lu @ count = %lu\n", ret, ctx->count);
1452
RETURN_ON_ERROR(ret);
1455
ctx->u.rep->count = ctx->count-1;
1456
state->ptr = ctx->ptr;
1460
if ((ctx->count < ctx->u.rep->pattern[2] ||
1461
ctx->u.rep->pattern[2] == 65535) &&
1462
state->ptr != ctx->u.rep->last_ptr) {
1463
/* we may have enough matches, but if we can
1464
match another item, do so */
1465
ctx->u.rep->count = ctx->count;
1467
MARK_PUSH(ctx->lastmark);
1468
/* zero-width match protection */
1469
DATA_PUSH(&ctx->u.rep->last_ptr);
1470
ctx->u.rep->last_ptr = state->ptr;
1471
DO_JUMP(JUMP_POSS_UNTIL_2, jump_poss_until_2,
1472
ctx->u.rep->pattern+3);
1473
DATA_POP(&ctx->u.rep->last_ptr);
1474
printf("Return: %lu @ count = %lu w/ %s\n", ret,
1475
ctx->count, ctx->ptr);
1477
MARK_POP_DISCARD(ctx->lastmark);
1478
RETURN_ON_ERROR(ret);
1481
MARK_POP(ctx->lastmark);
1483
ctx->u.rep->count = ctx->count-1;
1484
state->ptr = ctx->ptr;
1487
/* Maximum matches reached or No more matches to match, so
1489
state->repeat = ctx->u.rep->prev;
1492
1426
case SRE_OP_GROUPREF:
1493
1427
/* match backreference */
1494
1428
TRACE(("|%p|%p|GROUPREF %d\n", ctx->pattern,