예제 #1
0
def Repeat(matcher, start=0, stop=None, algorithm=DEPTH_FIRST, 
            separator=None, add_=False):
    '''
    This is called by the [] operator.  It repeats the given matcher between
    start and stop number of times (inclusive).  If ``add`` is true then the
    results are joined with `Add`. If ``separator`` is given then each
    repetition is separated by that matcher.
    '''
    first = coerce_(matcher)
    if separator is None:
        rest = first
    else:
        rest = And(coerce_(separator, Regexp), first)
    if start is None:
        start = 0
    assert_type('The start index for Repeat or [...]', start, int)
    assert_type('The stop index for Repeat or [...]', stop, int, none_ok=True)
    assert_type('The algorithm/increment for Repeat or [...]', algorithm, str)
    if start < 0:
        raise ValueError('Repeat or [...] cannot have a negative start.')
    if stop is not None and stop < start:
        raise ValueError('Repeat or [...] must have a stop '
                         'value greater than or equal to the start.')
    if 'dbgn'.find(algorithm) == -1:
        raise ValueError('Repeat or [...] must have a step (algorithm) '
                         'of d, b, g or n.')
    add_ = Add if add_ else Identity
    return {DEPTH_FIRST:
                add_(DepthFirst(first=first, start=start, 
                                stop=stop, rest=rest)),
            BREADTH_FIRST: 
                add_(BreadthFirst(first=first, start=start, 
                                  stop=stop, rest=rest)),
            GREEDY:        
                add_(OrderByResultCount(BreadthFirst(first=first, start=start, 
                                                     stop=stop, rest=rest))),
            NON_GREEDY:
                add_(OrderByResultCount(BreadthFirst(first=first, start=start, 
                                                     stop=stop, rest=rest),
                                       False))
            }[algorithm]
예제 #2
0
파일: lib.py 프로젝트: sunaaron/lepl
 def assert_bad(self, name, value, type_, none_ok, msg):
     try:
         assert_type(name, value, type_, none_ok=none_ok)
         assert False, "Expected failure"
     except TypeError as e:
         assert e.args[0] == msg, e.args[0]
예제 #3
0
파일: lib.py 프로젝트: sunaaron/lepl
 def test_ok(self):
     assert_type("", 1, int)
     assert_type("", "", str)
     assert_type("", None, int, none_ok=True)
예제 #4
0
 def assert_bad(self, name, value, type_, none_ok, msg):
     try:
         assert_type(name, value, type_, none_ok=none_ok)
         assert False, 'Expected failure'
     except TypeError as e:
         assert e.args[0] == msg, e.args[0]
예제 #5
0
 def test_ok(self):
     assert_type('', 1, int)
     assert_type('', '', str)
     assert_type('', None, int, none_ok=True)