示例#1
0
 def assert_simple(self, stream, start, stop, algorithm, target):
     matcher = Repeat(RangeMatch(), start, stop, algorithm=algorithm)
     matcher.config.no_full_first_match()
     result = list(matcher.match_list(stream))
     #print(result)
     result = [''.join(map(str, l)) for (l, _s) in result]
     assert target == result, result
示例#2
0
 def assert_separator(self, stream, start, stop, algorithm, target):
     matcher = Repeat(Any('abc'), start, stop, 
                      algorithm=algorithm, separator=Any(','))
     matcher.config.no_full_first_match()
     result = [''.join(l) 
               for (l, _s) in matcher.match_string(stream)]
     assert target == result, result
示例#3
0
 def assert_simple(self, stream, start, stop, algorithm, target):
     matcher = Repeat(RangeMatch(), start, stop, algorithm=algorithm)
     matcher.config.no_full_first_match()
     result = list(matcher.match_list(stream))
     #print(result)
     result = [''.join(map(str, l)) for (l, _s) in result]
     assert target == result, result
示例#4
0
 def assert_separator(self, stream, start, stop, algorithm, target):
     matcher = Repeat(Any('abc'),
                      start,
                      stop,
                      algorithm=algorithm,
                      separator=Any(','))
     matcher.config.no_full_first_match()
     result = [''.join(l) for (l, _s) in matcher.match_string(stream)]
     assert target == result, result
示例#5
0
 def repeat(m, st=0, sp=None, d=0, s=None, a=False):
     '''
     Wrap `Repeat` to adapt the separator.
     '''
     if s is None:
         s = separator
     elif not a:
         s = And(separator, s, separator)
     return Repeat(m, st, sp, d, s, a)
示例#6
0
文件: operators.py 项目: msander/lepl
def RepeatWrapper(matcher, start, stop, step, separator, add, reduce):
    '''Parse `step` if it is a string.'''
    # Handle circular dependencies
    from lepl.matchers.derived import Repeat
    try:
        int(step)  # if this works, we may have a var, so keep the instance
        limit = step
        algorithm = DEPTH_FIRST
    except ValueError:
        if (isinstance(step, basestring)):
            limit = None
            algorithm = None
            while step:
                match = DIGITS.match(step)
                if match:
                    if limit is None:
                        limit = int(match.group(1))
                        step = match.group(2)
                    else:
                        raise TypeError(
                            fmt('Cannot parse limit/algorithm for []: {}',
                                step))
                else:
                    if algorithm is None:
                        algorithm = step[0]
                        step = step[1:]
        else:
            raise TypeError(
                'The step of [...] must be an integer limit, or a '
                'string to select the algorithm, or both as a string '
                'like "d1" for a single value, depth first')
    return Repeat(matcher,
                  start=start,
                  stop=stop,
                  limit=limit,
                  algorithm=algorithm,
                  separator=separator,
                  add_=add,
                  reduce=reduce)