summaryrefslogtreecommitdiffstats
path: root/Lib/sre_parse.py
diff options
context:
space:
mode:
authorGustavo Niemeyer <gustavo@niemeyer.net>2003-10-17 22:13:16 (GMT)
committerGustavo Niemeyer <gustavo@niemeyer.net>2003-10-17 22:13:16 (GMT)
commitad3fc44ccb40f2ad33c0d09f5a2dfbd4feb442eb (patch)
tree929ea71dea18a5ee0c5c862bbb39d37b693209ad /Lib/sre_parse.py
parent41e2809febd6e09a34adf21beb6d2ae2360fdc46 (diff)
downloadcpython-ad3fc44ccb40f2ad33c0d09f5a2dfbd4feb442eb.zip
cpython-ad3fc44ccb40f2ad33c0d09f5a2dfbd4feb442eb.tar.gz
cpython-ad3fc44ccb40f2ad33c0d09f5a2dfbd4feb442eb.tar.bz2
Implemented non-recursive SRE matching.
Diffstat (limited to 'Lib/sre_parse.py')
-rw-r--r--Lib/sre_parse.py40
1 files changed, 39 insertions, 1 deletions
diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py
index b85aea7..fe5d143 100644
--- a/Lib/sre_parse.py
+++ b/Lib/sre_parse.py
@@ -364,6 +364,20 @@ def _parse_sub(source, state, nested=1):
subpattern.append((BRANCH, (None, items)))
return subpattern
+def _parse_sub_cond(source, state, condgroup):
+ item_yes = _parse(source, state)
+ if source.match("|"):
+ item_no = _parse(source, state)
+ if source.match("|"):
+ raise error, "conditional backref with more than two branches"
+ else:
+ item_no = None
+ if source.next and not source.match(")", 0):
+ raise error, "pattern not properly closed"
+ subpattern = SubPattern(state)
+ subpattern.append((GROUPREF_EXISTS, (condgroup, item_yes, item_no)))
+ return subpattern
+
def _parse(source, state):
# parse a simple pattern
@@ -499,6 +513,7 @@ def _parse(source, state):
elif this == "(":
group = 1
name = None
+ condgroup = None
if source.match("?"):
group = 0
# options
@@ -568,6 +583,26 @@ def _parse(source, state):
else:
subpattern.append((ASSERT_NOT, (dir, p)))
continue
+ elif source.match("("):
+ # conditional backreference group
+ condname = ""
+ while 1:
+ char = source.get()
+ if char is None:
+ raise error, "unterminated name"
+ if char == ")":
+ break
+ condname = condname + char
+ group = 2
+ if isname(condname):
+ condgroup = state.groupdict.get(condname)
+ if condgroup is None:
+ raise error, "unknown group name"
+ else:
+ try:
+ condgroup = atoi(condname)
+ except ValueError:
+ raise error, "bad character in group name"
else:
# flags
if not source.next in FLAGS:
@@ -581,7 +616,10 @@ def _parse(source, state):
group = None
else:
group = state.opengroup(name)
- p = _parse_sub(source, state)
+ if condgroup:
+ p = _parse_sub_cond(source, state, condgroup)
+ else:
+ p = _parse_sub(source, state)
if not source.match(")"):
raise error, "unbalanced parenthesis"
if group is not None: