- make sure group names are valid identifiers

(closes the "SRE: symbolic reference" bug)
This commit is contained in:
Fredrik Lundh 2000-06-29 12:38:45 +00:00
parent 75f2d675ed
commit 4781b07201

View File

@ -168,6 +168,24 @@ class Tokenizer:
self.next = self.__next() self.next = self.__next()
return this return this
def isident(char):
return "a" <= char <= "z" or "A" <= char <= "Z" or char == "_"
def isdigit(char):
return "0" <= char <= "9"
def isname(name):
# check that group name is a valid string
# FIXME: <fl> this code is really lame. should use a regular
# expression instead, but I seem to have certain bootstrapping
# problems here ;-)
if not isident(name[0]):
return 0
for char in name:
if not isident(char) and not isdigit(char):
return 0
return 1
def _group(escape, state): def _group(escape, state):
# check if the escape string represents a valid group # check if the escape string represents a valid group
try: try:
@ -418,9 +436,10 @@ def _parse(source, state, flags=0):
raise error, "unterminated name" raise error, "unterminated name"
if char == ">": if char == ">":
break break
# FIXME: check for valid character
name = name + char name = name + char
group = 1 group = 1
if not isname(name):
raise error, "illegal character in group name"
elif source.match("="): elif source.match("="):
# named backreference # named backreference
raise error, "not yet implemented" raise error, "not yet implemented"
@ -522,20 +541,21 @@ def parse_template(source, pattern):
while 1: while 1:
char = s.get() char = s.get()
if char is None: if char is None:
raise error, "unterminated index" raise error, "unterminated group name"
if char == ">": if char == ">":
break break
# FIXME: check for valid character
name = name + char name = name + char
if not name: if not name:
raise error, "bad index" raise error, "bad group name"
try: try:
index = int(name) index = int(name)
except ValueError: except ValueError:
if not isname(name):
raise error, "illegal character in group name"
try: try:
index = pattern.groupindex[name] index = pattern.groupindex[name]
except KeyError: except KeyError:
raise IndexError, "unknown index" raise IndexError, "unknown group name"
a((MARK, index)) a((MARK, index))
elif len(this) > 1 and this[1] in DIGITS: elif len(this) > 1 and this[1] in DIGITS:
while s.next in DIGITS: while s.next in DIGITS: