MCPcopy Index your code
hub / github.com/python/cpython / parse_template

Function parse_template

Lib/re/_parser.py:989–1065  ·  view source on GitHub ↗
(source, pattern)

Source from the content-addressed store, hash-verified

987 return p
988
989def parse_template(source, pattern):
990 # parse 're' replacement string into list of literals and
991 # group references
992 s = Tokenizer(source)
993 sget = s.get
994 result = []
995 literal = []
996 lappend = literal.append
997 def addliteral():
998 if s.istext:
999 result.append(''.join(literal))
1000 else:
1001 # The tokenizer implicitly decodes bytes objects as latin-1, we must
1002 # therefore re-encode the final representation.
1003 result.append(''.join(literal).encode('latin-1'))
1004 del literal[:]
1005 def addgroup(index, pos):
1006 if index > pattern.groups:
1007 raise s.error("invalid group reference %d" % index, pos)
1008 addliteral()
1009 result.append(index)
1010 groupindex = pattern.groupindex
1011 while True:
1012 this = sget()
1013 if this is None:
1014 break # end of replacement string
1015 if this[0] == "\\":
1016 # group
1017 c = this[1]
1018 if c == "g":
1019 if not s.match("<"):
1020 raise s.error("missing <")
1021 name = s.getuntil(">", "group name")
1022 if not (name.isdecimal() and name.isascii()):
1023 s.checkgroupname(name, 1)
1024 try:
1025 index = groupindex[name]
1026 except KeyError:
1027 raise IndexError("unknown group name %r" % name) from None
1028 else:
1029 index = int(name)
1030 if index >= MAXGROUPS:
1031 raise s.error("invalid group reference %d" % index,
1032 len(name) + 1)
1033 addgroup(index, len(name) + 1)
1034 elif c == "0":
1035 if s.next in OCTDIGITS:
1036 this += sget()
1037 if s.next in OCTDIGITS:
1038 this += sget()
1039 lappend(chr(int(this[1:], 8) & 0xff))
1040 elif c in DIGITS:
1041 isoctal = False
1042 if s.next in DIGITS:
1043 this += sget()
1044 if (c in OCTDIGITS and this[2] in OCTDIGITS and
1045 s.next in OCTDIGITS):
1046 this += sget()

Callers

nothing calls this directly

Calls 9

matchMethod · 0.95
errorMethod · 0.95
getuntilMethod · 0.95
checkgroupnameMethod · 0.95
addgroupFunction · 0.85
addliteralFunction · 0.85
isdecimalMethod · 0.80
isasciiMethod · 0.80
TokenizerClass · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…