Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #61 from Yelp/adds-sequence-helper-func
Browse files Browse the repository at this point in the history
Adds `sequence` helper function; closes #59
  • Loading branch information
ajm188 committed Sep 22, 2016
2 parents 1b031dc + 0eb7923 commit 7fa6d4e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/source/util.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ Modifies a grammar element to print the tokens that it matches.

Modifies a grammar element to parse to the result of calling `action` on the tokens produced by that grammar element.

**sequence(grammar, n)**

Creates a grammar element that matches exactly `n` of the input grammar.

`undebt.pattern.common`
-----------------------

Expand Down
13 changes: 13 additions & 0 deletions tests/pattern/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

import mock
from pyparsing import Keyword
from pyparsing import Literal

from undebt.pattern.testing import assert_parse
from undebt.pattern.util import debug
from undebt.pattern.util import in_string
from undebt.pattern.util import leading_whitespace
from undebt.pattern.util import quoted
from undebt.pattern.util import sequence
from undebt.pattern.util import trailing_whitespace


Expand Down Expand Up @@ -73,3 +75,14 @@ def test_in_string():
assert in_string(7, test)
assert in_string(8, test)
assert not in_string(9, test)


def test_sequence():
a = Literal('a')
double_a = sequence(a, n=2)
triple_a = sequence(a, n=3)

assert double_a.matches('aa')
assert triple_a.matches('aaa')
assert not double_a.matches('aaa')
assert not triple_a.matches('aa')
2 changes: 1 addition & 1 deletion undebt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

# this should be the only place where the version is kept, if you
# need to know the version somewhere else just import it from here
__version__ = "0.6.1"
__version__ = "0.6.2"
9 changes: 9 additions & 0 deletions undebt/pattern/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import print_function

import functools
import operator

from pyparsing import _trim_arity
from pyparsing import col
Expand Down Expand Up @@ -107,3 +108,11 @@ def new_replace(s, l, tokens):
return _trim_arity(old_replace)(s, l, tokendict)
return new_replace
return decorator


def sequence(grammar, n):
"""
Creates a grammar element that matches exactly N of the input
grammar.
"""
return functools.reduce(operator.add, [grammar] * n)

0 comments on commit 7fa6d4e

Please sign in to comment.