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

Function parse_template

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 11

matchMethod · 0.95
errorMethod · 0.95
getuntilMethod · 0.95
checkgroupnameMethod · 0.95
TokenizerClass · 0.85
lenFunction · 0.85
addgroupFunction · 0.85
chrFunction · 0.85
addliteralFunction · 0.85
isdecimalMethod · 0.45
isasciiMethod · 0.45

Tested by

no test coverage detected