Beispiel #1
0
 def append(child):
     if isinstance(child, Matcher):
         if isinstance(child, Delayed):
             child.assert_matcher()
             pending.append(child.matcher)
             stack.append((child, empty()))
         else:
             stack.append((child, iter(child)))
Beispiel #2
0
 def append(child):
     if isinstance(child, Matcher):
         if isinstance(child, Delayed):
             child.assert_matcher()
             pending.append(child.matcher)
             stack.append((child, empty()))
         else:
             stack.append((child, iter(child)))
Beispiel #3
0
 def _match(self, stream):
     '''
     Attempt to match the stream.
     '''
     if not self.__first:
         self.__counter += 1
         if self.__curtail(self.__counter, stream):
             return empty()
         else:
             cache = PerCallCache(self.__matcher._match(stream))
             if self.__first is None:
                 self.__first = cache
             return cache.generator()
     else:
         return self.__first.view()
Beispiel #4
0
 def _match(self, stream):
     '''
     Attempt to match the stream.
     '''
     if not self.__first:
         self.__counter += 1
         if self.__curtail(self.__counter, stream):
             return empty()
         else:
             cache = PerCallCache(self.__matcher._match(stream))
             if self.__first is None:
                 self.__first = cache
             return cache.generator()
     else:
         return self.__first.view()
Beispiel #5
0
def dfs_edges(node, type_):
    '''
    Iterative DFS, based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
    
    Returns forward and reverse edges.  Also returns root node in correct 
    order for pre- (FORWARD) and post- (BACKWARD) ordering.

    ``type_`` selects which values are iterated over.  Children which are
    not of this type are flagged with LEAF.
    '''
    while isinstance(node, type_):
        try:
            stack = [(node, iter(node), ROOT)]
            yield node, node, FORWARD | ROOT
            visited = set()
            visited = fallback_add(visited, node)
            while stack:
                parent, children, p_type = stack[-1]
                try:
                    child = next(children)
                    if isinstance(child, type_):
                        if safe_in(child, visited, False):
                            yield parent, child, NON_TREE
                        else:
                            stack.append((child, iter(child), NODE))
                            yield parent, child, FORWARD | NODE
                            visited = fallback_add(visited, child)
                    else:
                        stack.append((child, empty(), LEAF))
                        yield parent, child, FORWARD | LEAF
                except StopIteration:
                    stack.pop()
                    if stack:
                        yield stack[-1][0], parent, BACKWARD | p_type
            yield node, node, BACKWARD | ROOT
            return
        except Reset:
            yield # in response to the throw (ignored by caller)
Beispiel #6
0
def dfs_edges(node, type_):
    '''
    Iterative DFS, based on http://www.ics.uci.edu/~eppstein/PADS/DFS.py
    
    Returns forward and reverse edges.  Also returns root node in correct 
    order for pre- (FORWARD) and post- (BACKWARD) ordering.

    ``type_`` selects which values are iterated over.  Children which are
    not of this type are flagged with LEAF.
    '''
    while isinstance(node, type_):
        try:
            stack = [(node, iter(node), ROOT)]
            yield node, node, FORWARD | ROOT
            visited = set()
            safe_add(visited, node)  # cannot track loops in unhashable objects
            while stack:
                parent, children, ptype = stack[-1]
                try:
                    child = next(children)
                    if isinstance(child, type_):
                        if safe_in(child, visited, False):
                            yield parent, child, NONTREE
                        else:
                            stack.append((child, iter(child), NODE))
                            yield parent, child, FORWARD | NODE
                            safe_add(visited, child)
                    else:
                        stack.append((child, empty(), LEAF))
                        yield parent, child, FORWARD | LEAF
                except StopIteration:
                    stack.pop()
                    if stack:
                        yield stack[-1][0], parent, BACKWARD | ptype
            yield node, node, BACKWARD | ROOT
            return
        except Reset:
            yield  # in response to the throw (ignored by caller)