MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / _parse_sub

Function _parse_sub

tools/python-3.11.9-amd64/Lib/re/_parser.py:456–514  ·  view source on GitHub ↗
(source, state, verbose, nested)

Source from the content-addressed store, hash-verified

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

Callers 2

_parseFunction · 0.85
parseFunction · 0.85

Calls 6

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

Tested by

no test coverage detected