Skip to content

Commit

Permalink
Start making Pattern.either to expand any pattern to Either.
Browse files Browse the repository at this point in the history
  • Loading branch information
keleshev committed May 12, 2012
1 parent fd37b68 commit d6850ae
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docopt.py
Expand Up @@ -37,6 +37,29 @@ def flat(self):
return [self]
return sum([c.flat for c in self.children], [])

@property
def either(self):
if not hasattr(self, 'children'):
return Either(self)
else:
ret = []
groups = [list(deepcopy(self.children))]
#groups = [self]
while groups:
children = groups.pop(0)
eithers = [c for c in children if type(c) == Either]
if len(eithers):
either = eithers[0]
children.pop(children.index(either))
for c in either.children:
groups.append([c] + children)
print groups
else:
ret.append(children)
return Either(*[Required(*e) for e in ret])




class Argument(Pattern):

Expand Down
14 changes: 14 additions & 0 deletions test_docopt.py
Expand Up @@ -248,3 +248,17 @@ def test_basic_pattern_matching():
assert pattern.match([Option('x'),
Argument(None, 9), Argument(None, 5)]) == (
False, [], [])


def test_pattern_either():
assert Option('a').either == Either(Option('a'))
assert Argument('a').either == Either(Argument('a'))
assert Required(Either(Option('a'), Option('b')), Option('c')).either == \
Either(Required(Option('a'), Option('c')),
Required(Option('b'), Option('c')))
assert Required(Option('a'), Either(Option('b'), Option('c'))).either == \
Either(Required(Option('b'), Option('a')),
Required(Option('c'), Option('a')))
# print Either(Option('x'), Either(Option('y'), Option('z'))).either
# assert Either(Option('x'), Either(Option('y'), Option('z'))).either == \
# Either(Option('z'), Option('y'), Option('z'))

0 comments on commit d6850ae

Please sign in to comment.