mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
bpo-34681: Rename class Pattern in sre_parse to State. (GH-9310)
Also rename corresponding attributes, parameters and variables.
This commit is contained in:
parent
9c53fa6ad9
commit
e0c19ddc66
3 changed files with 26 additions and 26 deletions
|
@ -334,7 +334,7 @@ class Scanner:
|
||||||
self.lexicon = lexicon
|
self.lexicon = lexicon
|
||||||
# combine phrases into a compound pattern
|
# combine phrases into a compound pattern
|
||||||
p = []
|
p = []
|
||||||
s = sre_parse.Pattern()
|
s = sre_parse.State()
|
||||||
s.flags = flags
|
s.flags = flags
|
||||||
for phrase, action in lexicon:
|
for phrase, action in lexicon:
|
||||||
gid = s.opengroup()
|
gid = s.opengroup()
|
||||||
|
|
|
@ -597,7 +597,7 @@ def isstring(obj):
|
||||||
|
|
||||||
def _code(p, flags):
|
def _code(p, flags):
|
||||||
|
|
||||||
flags = p.pattern.flags | flags
|
flags = p.state.flags | flags
|
||||||
code = []
|
code = []
|
||||||
|
|
||||||
# compile info block
|
# compile info block
|
||||||
|
@ -772,13 +772,13 @@ def compile(p, flags=0):
|
||||||
dis(code)
|
dis(code)
|
||||||
|
|
||||||
# map in either direction
|
# map in either direction
|
||||||
groupindex = p.pattern.groupdict
|
groupindex = p.state.groupdict
|
||||||
indexgroup = [None] * p.pattern.groups
|
indexgroup = [None] * p.state.groups
|
||||||
for k, i in groupindex.items():
|
for k, i in groupindex.items():
|
||||||
indexgroup[i] = k
|
indexgroup[i] = k
|
||||||
|
|
||||||
return _sre.compile(
|
return _sre.compile(
|
||||||
pattern, flags | p.pattern.flags, code,
|
pattern, flags | p.state.flags, code,
|
||||||
p.pattern.groups-1,
|
p.state.groups-1,
|
||||||
groupindex, tuple(indexgroup)
|
groupindex, tuple(indexgroup)
|
||||||
)
|
)
|
||||||
|
|
|
@ -71,8 +71,8 @@ GLOBAL_FLAGS = SRE_FLAG_DEBUG | SRE_FLAG_TEMPLATE
|
||||||
class Verbose(Exception):
|
class Verbose(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class Pattern:
|
class State:
|
||||||
# main pattern object. keeps track of global attributes
|
# keeps track of state for parsing
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.flags = 0
|
self.flags = 0
|
||||||
self.groupdict = {}
|
self.groupdict = {}
|
||||||
|
@ -108,8 +108,8 @@ class Pattern:
|
||||||
|
|
||||||
class SubPattern:
|
class SubPattern:
|
||||||
# a subpattern, in intermediate form
|
# a subpattern, in intermediate form
|
||||||
def __init__(self, pattern, data=None):
|
def __init__(self, state, data=None):
|
||||||
self.pattern = pattern
|
self.state = state
|
||||||
if data is None:
|
if data is None:
|
||||||
data = []
|
data = []
|
||||||
self.data = data
|
self.data = data
|
||||||
|
@ -163,7 +163,7 @@ class SubPattern:
|
||||||
del self.data[index]
|
del self.data[index]
|
||||||
def __getitem__(self, index):
|
def __getitem__(self, index):
|
||||||
if isinstance(index, slice):
|
if isinstance(index, slice):
|
||||||
return SubPattern(self.pattern, self.data[index])
|
return SubPattern(self.state, self.data[index])
|
||||||
return self.data[index]
|
return self.data[index]
|
||||||
def __setitem__(self, index, code):
|
def __setitem__(self, index, code):
|
||||||
self.data[index] = code
|
self.data[index] = code
|
||||||
|
@ -202,7 +202,7 @@ class SubPattern:
|
||||||
lo = lo + 1
|
lo = lo + 1
|
||||||
hi = hi + 1
|
hi = hi + 1
|
||||||
elif op is GROUPREF:
|
elif op is GROUPREF:
|
||||||
i, j = self.pattern.groupwidths[av]
|
i, j = self.state.groupwidths[av]
|
||||||
lo = lo + i
|
lo = lo + i
|
||||||
hi = hi + j
|
hi = hi + j
|
||||||
elif op is GROUPREF_EXISTS:
|
elif op is GROUPREF_EXISTS:
|
||||||
|
@ -940,28 +940,28 @@ def fix_flags(src, flags):
|
||||||
raise ValueError("ASCII and LOCALE flags are incompatible")
|
raise ValueError("ASCII and LOCALE flags are incompatible")
|
||||||
return flags
|
return flags
|
||||||
|
|
||||||
def parse(str, flags=0, pattern=None):
|
def parse(str, flags=0, state=None):
|
||||||
# parse 're' pattern into list of (opcode, argument) tuples
|
# parse 're' pattern into list of (opcode, argument) tuples
|
||||||
|
|
||||||
source = Tokenizer(str)
|
source = Tokenizer(str)
|
||||||
|
|
||||||
if pattern is None:
|
if state is None:
|
||||||
pattern = Pattern()
|
state = State()
|
||||||
pattern.flags = flags
|
state.flags = flags
|
||||||
pattern.str = str
|
state.str = str
|
||||||
|
|
||||||
try:
|
try:
|
||||||
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
|
p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
|
||||||
except Verbose:
|
except Verbose:
|
||||||
# the VERBOSE flag was switched on inside the pattern. to be
|
# the VERBOSE flag was switched on inside the pattern. to be
|
||||||
# on the safe side, we'll parse the whole thing again...
|
# on the safe side, we'll parse the whole thing again...
|
||||||
pattern = Pattern()
|
state = State()
|
||||||
pattern.flags = flags | SRE_FLAG_VERBOSE
|
state.flags = flags | SRE_FLAG_VERBOSE
|
||||||
pattern.str = str
|
state.str = str
|
||||||
source.seek(0)
|
source.seek(0)
|
||||||
p = _parse_sub(source, pattern, True, 0)
|
p = _parse_sub(source, state, True, 0)
|
||||||
|
|
||||||
p.pattern.flags = fix_flags(str, p.pattern.flags)
|
p.state.flags = fix_flags(str, p.state.flags)
|
||||||
|
|
||||||
if source.next is not None:
|
if source.next is not None:
|
||||||
assert source.next == ")"
|
assert source.next == ")"
|
||||||
|
@ -972,7 +972,7 @@ def parse(str, flags=0, pattern=None):
|
||||||
|
|
||||||
return p
|
return p
|
||||||
|
|
||||||
def parse_template(source, pattern):
|
def parse_template(source, state):
|
||||||
# parse 're' replacement string into list of literals and
|
# parse 're' replacement string into list of literals and
|
||||||
# group references
|
# group references
|
||||||
s = Tokenizer(source)
|
s = Tokenizer(source)
|
||||||
|
@ -982,14 +982,14 @@ def parse_template(source, pattern):
|
||||||
literal = []
|
literal = []
|
||||||
lappend = literal.append
|
lappend = literal.append
|
||||||
def addgroup(index, pos):
|
def addgroup(index, pos):
|
||||||
if index > pattern.groups:
|
if index > state.groups:
|
||||||
raise s.error("invalid group reference %d" % index, pos)
|
raise s.error("invalid group reference %d" % index, pos)
|
||||||
if literal:
|
if literal:
|
||||||
literals.append(''.join(literal))
|
literals.append(''.join(literal))
|
||||||
del literal[:]
|
del literal[:]
|
||||||
groups.append((len(literals), index))
|
groups.append((len(literals), index))
|
||||||
literals.append(None)
|
literals.append(None)
|
||||||
groupindex = pattern.groupindex
|
groupindex = state.groupindex
|
||||||
while True:
|
while True:
|
||||||
this = sget()
|
this = sget()
|
||||||
if this is None:
|
if this is None:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue