Esempio n. 1
0
                try:
                    while True:
                        (value, stream2) = yield generator
                        acc2 = acc1 + value
                        if stop is None or count2 <= stop:
                            queue.append((count2, acc2, stream2, 
                                          rest._match(stream2)))
                except StopIteration:
                    pass
        finally:
            _cleanup(queue)
            
    return match


@trampoline_matcher_factory(False, matcher=to(Literal))
def OrderByResultCount(matcher, ascending=True):
    '''
    Modify a matcher to return results in length order.
    '''

    def match(support, stream):
        '''
        Attempt to match the stream.
        '''
        generator = matcher._match(stream)
        results = []
        try:
            while True:
                # syntax error if this on one line?!
                result = yield generator
Esempio n. 2
0
            return self


#noinspection PyUnusedLocal
@function_matcher
def Eof(support, stream):
    '''
    Match the end of a stream.  Returns nothing.  

    This is also aliased to Eos in lepl.derived.
    '''
    if s_empty(stream):
        return [], stream


@trampoline_matcher_factory(matcher=to(Literal))
def Consumer(matcher, consume=True):
    '''
    Only accept the match if it consumes data from the input
    '''
    #noinspection PyUnusedLocal
    def match(support, stream_in):
        '''
        Do the match and test whether the stream has progressed.
        '''
        try:
            generator = matcher._match(stream_in)
            while True:
                (result, stream_out) = yield generator
                if consume != s_eq(stream_in, stream_out):
                    yield (result, stream_out)
Esempio n. 3
0
            self.matcher = coerce_(matcher)
            return self


@function_matcher
def Eof(support, stream):
    '''
    Match the end of a stream.  Returns nothing.  

    This is also aliased to Eos in lepl.derived.
    '''
    if not stream:
        return ([], stream)


@trampoline_matcher_factory(matcher=to(Literal))
def Consumer(matcher, consume=True):
    '''
    Only accept the match if it consumes data from the input
    '''
    def match(support, stream_in):
        '''
        Do the match and test whether the stream has progressed.
        '''
        try:
            generator = matcher._match(stream_in)
            while True:
                (result, stream_out) = yield generator
                if consume == (stream_in != stream_out):
                    yield (result, stream_out)
        except StopIteration:
Esempio n. 4
0
            self._match = matcher._match
            return self


@function_matcher
def Eof(support, stream):
    '''
    Match the end of a stream.  Returns nothing.  

    This is also aliased to Eos in lepl.derived.
    '''
    if s_empty(stream):
        return ([], stream)


@trampoline_matcher_factory(matcher=to(Literal))
def Consumer(matcher, consume=True):
    '''
    Only accept the match if it consumes data from the input
    '''
    def match(support, stream_in):
        '''
        Do the match and test whether the stream has progressed.
        '''
        try:
            generator = matcher._match(stream_in)
            while True:
                (result, stream_out) = yield generator
                if consume != s_eq(stream_in, stream_out):
                    yield (result, stream_out)
        except StopIteration:
Esempio n. 5
0
    '''
    for (_count, _acc, _stream, generator) in queue:
        generator.generator.close()
        

def search_factory(factory):
    '''
    Add the arg processing common to all searching.
    '''
    def new_factory(first, start, stop, rest=None):
        rest = first if rest is None else rest
        return factory(first, start, stop, rest)
    return document(new_factory, factory)


@trampoline_matcher_factory(False, first=to(Literal), rest=to(Literal))
@search_factory
def DepthFirst(first, start, stop, rest):
    '''
    (Post order) Depth first repetition (typically used via `Repeat`).
    '''
    def match(support, stream):
        stack = deque()
        try:
            stack.append((0, [], stream, first._match(stream)))
            while stack:
                (count1, acc1, stream1, generator) = stack[-1]
                extended = False
                if stop is None or count1 < stop:
                    count2 = count1 + 1
                    try:
Esempio n. 6
0
            self.matcher = coerce_(matcher)
            return self
        

@function_matcher
def Eof(support, stream):
    '''
    Match the end of a stream.  Returns nothing.  

    This is also aliased to Eos in lepl.derived.
    '''
    if not stream:
        return ([], stream)


@trampoline_matcher_factory(matcher=to(Literal))
def Consumer(matcher, consume=True):
    '''
    Only accept the match if it consumes data from the input
    '''
    def match(support, stream_in):
        '''
        Do the match and test whether the stream has progressed.
        '''
        try:
            generator = matcher._match(stream_in)
            while True:
                (result, stream_out) = yield generator
                if consume == (stream_in != stream_out):
                    yield (result, stream_out)
        except StopIteration:
Esempio n. 7
0
def search_factory(factory):
    '''
    Add the arg processing common to all searching.
    '''
    def new_factory(first,
                    start,
                    stop,
                    rest=None,
                    generator_manager_queue_len=None):
        rest = first if rest is None else rest
        return factory(first, start, stop, rest, generator_manager_queue_len)

    return document(new_factory, factory)


@trampoline_matcher_factory(first=to(Literal), rest=to(Literal))
@search_factory
def DepthFirst(first, start, stop, rest, generator_manager_queue_len):
    '''
    (Post order) Depth first repetition (typically used via `Repeat`).
    '''
    def match(support, stream):
        stack = deque()
        try:
            stack.append((0, [], stream, first._match(stream)))
            stream = None
            while stack:
                (count1, acc1, stream1, generator) = stack[-1]
                extended = False
                if stop == None or count1 < stop:
                    count2 = count1 + 1
Esempio n. 8
0
                try:
                    while True:
                        (value, stream2) = yield generator
                        acc2 = acc1 + value
                        if stop is None or count2 <= stop:
                            queue.append(
                                (count2, acc2, stream2, rest._match(stream2)))
                except StopIteration:
                    pass
        finally:
            _cleanup(queue)

    return match


@trampoline_matcher_factory(False, matcher=to(Literal))
def OrderByResultCount(matcher, ascending=True):
    '''
    Modify a matcher to return results in length order.
    '''
    def match(support, stream):
        '''
        Attempt to match the stream.
        '''
        generator = matcher._match(stream)
        results = []
        try:
            while True:
                # syntax error if this on one line?!
                result = yield generator
                results.append(result)