Modified:
branches/groups/src/esmre.py
branches/groups/test/test_esmre.py
Log:
Get hints from named groups.
Modified: branches/groups/src/esmre.py
==============================================================================
--- branches/groups/src/esmre.py (original)
+++ branches/groups/src/esmre.py Wed Oct 1 12:20:23 2008
@@ -161,7 +161,40 @@
return self
-class StartOfExtensionGroupState(InGroupState):
+class StartOfExtensionGroupState(object):
+ def __init__(self, parent_state):
+ self.parent_state = parent_state
+
+ def process_byte(self, ch):
+ if ch == "P":
+ return MaybeStartOfNamedGroupState(self.parent_state)
+ else:
+ return IgnoredGroupState(self.parent_state).process_byte(ch)
+
+
+class MaybeStartOfNamedGroupState(object):
+ def __init__(self, parent_state):
+ self.parent_state = parent_state
+
+ def process_byte(self, ch):
+ if ch == "<":
+ return InNamedGroupNameState(self.parent_state)
+ else:
+ return IgnoredGroupState(self.parent_state)
+
+
+class InNamedGroupNameState(object):
+ def __init__(self, parent_state):
+ self.parent_state = parent_state
+
+ def process_byte(self, ch):
+ if ch == ">":
+ return InGroupState(self.parent_state)
+ else:
+ return self
+
+
+class IgnoredGroupState(InGroupState):
def update_hints(self, ch):
pass
Modified: branches/groups/test/test_esmre.py
==============================================================================
--- branches/groups/test/test_esmre.py (original)
+++ branches/groups/test/test_esmre.py Wed Oct 1 12:20:23 2008
@@ -114,6 +114,9 @@
# non-grouping paren
r"(?:foo)",
+ # previous named group
+ r"(?P=foo)",
+
# comment
r"(?#foo)",
@@ -133,6 +136,11 @@
r"(?(1)foo|bar)"]:
self.checkHints([], regex)
+
+ def testGetsHintsFromNamedGroup(self):
+ self.checkHints(
+ ["/"], r"(?P<date>[0-3][0-9]/[0-1][0-9]/[1-2][0-9]{3})")
+
class ShortlistTests(unittest.TestCase):
def checkShortlist(self, expected_shortlist, hints):