MCPcopy Index your code
hub / github.com/RustPython/RustPython / _parse_sub

Function _parse_sub

Lib/re/_parser.py:452–510  ·  view source on GitHub ↗
(source, state, verbose, nested)

Source from the content-addressed store, hash-verified

450 return list(dict.fromkeys(items))
451
452def _parse_sub(source, state, verbose, nested):
453 # parse an alternation: a|b|c
454
455 items = []
456 itemsappend = items.append
457 sourcematch = source.match
458 start = source.tell()
459 while True:
460 itemsappend(_parse(source, state, verbose, nested + 1,
461 not nested and not items))
462 if not sourcematch("|"):
463 break
464 if not nested:
465 verbose = state.flags & SRE_FLAG_VERBOSE
466
467 if len(items) == 1:
468 return items[0]
469
470 subpattern = SubPattern(state)
471
472 # check if all items share a common prefix
473 while True:
474 prefix = None
475 for item in items:
476 if not item:
477 break
478 if prefix is None:
479 prefix = item[0]
480 elif item[0] != prefix:
481 break
482 else:
483 # all subitems start with a common "prefix".
484 # move it out of the branch
485 for item in items:
486 del item[0]
487 subpattern.append(prefix)
488 continue # check next one
489 break
490
491 # check if the branch can be replaced by a character set
492 set = []
493 for item in items:
494 if len(item) != 1:
495 break
496 op, av = item[0]
497 if op is LITERAL:
498 set.append((op, av))
499 elif op is IN and av[0][0] is not NEGATE:
500 set.extend(av)
501 else:
502 break
503 else:
504 # we can store this as a character set instead of a
505 # branch (the compiler may optimize this even more)
506 subpattern.append((IN, _uniq(set)))
507 return subpattern
508
509 subpattern.append((BRANCH, (None, items)))

Callers 2

_parseFunction · 0.85
parseFunction · 0.85

Calls 7

appendMethod · 0.95
lenFunction · 0.85
SubPatternClass · 0.85
_uniqFunction · 0.85
_parseFunction · 0.70
tellMethod · 0.45
extendMethod · 0.45

Tested by

no test coverage detected