Example #1
0
 def _assert_clone(self, matcher, copy):
     original = preorder(matcher, Matcher)
     duplicate = preorder(copy, Matcher)
     try:
         while True:
             o = next(original)
             d = next(duplicate)
             assert type(o) == type(d), (str(o), str(d), o, d)
             if isinstance(o, Matcher):
                 assert o is not d, (str(o), str(d), o, d)
             else:
                 assert o is d, (str(o), str(d), o, d)
     except StopIteration:
         self.assert_empty(original, 'original')
         self.assert_empty(duplicate, 'duplicate')
Example #2
0
 def test_reset(self):
     nodes = preorder(graph(), SimpleNode, exclude=LEAF)
     assert next(nodes).label == 1
     assert next(nodes).label == 11
     reset(nodes)
     assert next(nodes).label == 1
     assert next(nodes).label == 11
Example #3
0
 def test_reset(self):
     nodes = preorder(graph(), SimpleNode, exclude=LEAF)
     assert next(nodes).label == 1
     assert next(nodes).label == 11
     reset(nodes)
     assert next(nodes).label == 1
     assert next(nodes).label == 11
Example #4
0
 def __call__(self, graph):
     from lepl.matchers.core import Delayed
     from lepl.matchers.combine import Or
     for delayed in [x for x in preorder(graph, Matcher) 
                     if isinstance(x, Delayed)]:
         for loop in either_loops(delayed, self.conservative):
             for i in range(len(loop)):
                 if is_child(loop[i], Or, fail=False):
                     # we cannot be at the end of the list here, since that
                     # is a Delayed instance
                     # copy from tuple to list
                     loop[i].matchers = list(loop[i].matchers)
                     matchers = loop[i].matchers
                     target = loop[i+1]
                     # move target to end of list
                     index = matchers.index(target)
                     del matchers[index]
                     matchers.append(target)
     return graph
Example #5
0
 def __call__(self, graph):
     from lepl.matchers.core import Delayed
     from lepl.matchers.combine import Or
     for delayed in [
             x for x in preorder(graph, Matcher) if isinstance(x, Delayed)
     ]:
         for loop in either_loops(delayed, self.conservative):
             for i in range(len(loop)):
                 if is_child(loop[i], Or, fail=False):
                     # we cannot be at the end of the list here, since that
                     # is a Delayed instance
                     # copy from tuple to list
                     loop[i].matchers = list(loop[i].matchers)
                     matchers = loop[i].matchers
                     target = loop[i + 1]
                     # move target to end of list
                     index = matchers.index(target)
                     del matchers[index]
                     matchers.append(target)
     return graph
Example #6
0
 def __call__(self, graph):
     self._warn('Alternatives are being re-ordered to improve stability with left-recursion.\n'
                'This will change the ordering of results.')
     #raise Exception('wtf')
     for delayed in [x for x in preorder(graph, Matcher) 
                     if isinstance(x, Delayed)]:
         for loop in either_loops(delayed, self.conservative):
             for i in range(len(loop)):
                 if is_child(loop[i], Or, fail=False):
                     # we cannot be at the end of the list here, since that
                     # is a Delayed instance
                     # copy from tuple to list
                     loop[i].matchers = list(loop[i].matchers)
                     matchers = loop[i].matchers
                     target = loop[i+1]
                     # move target to end of list
                     index = matchers.index(target)
                     del matchers[index]
                     matchers.append(target)
     return graph
Example #7
0
 def __call__(self, graph):
     self._warn('Alternatives are being re-ordered to improve stability with left-recursion.\n'
                'This will change the ordering of results.')
     #raise Exception('wtf')
     for delayed in [x for x in preorder(graph, Matcher) 
                     if isinstance(x, Delayed)]:
         for loop in either_loops(delayed, self.conservative):
             for i in range(len(loop)):
                 if is_child(loop[i], Or, fail=False):
                     # we cannot be at the end of the list here, since that
                     # is a Delayed instance
                     # copy from tuple to list
                     loop[i].matchers = list(loop[i].matchers)
                     matchers = loop[i].matchers
                     target = loop[i+1]
                     # move target to end of list
                     index = matchers.index(target)
                     del matchers[index]
                     matchers.append(target)
     return graph
Example #8
0
 def test_preorder(self):
     result = [
         node.label for node in preorder(graph(), SimpleNode, exclude=LEAF)
     ]
     assert result == [1, 11, 111, 112, 12], result
Example #9
0
 def test_preorder(self):
     g = [1, [11, [111, 112], 12]]
     result = [node for node in preorder(g, list) if isinstance(node, int)]
     assert result == [1, 11, 111, 112, 12], result
Example #10
0
 def test_preorder(self):
     result = [node.label for node in preorder(graph(), SimpleNode, exclude=LEAF)]
     assert result == [1, 11, 111, 112, 12], result
Example #11
0
 def test_preorder(self):
     g = [1, [11, [111, 112], 12]]
     result = [node for node in preorder(g, list) if isinstance(node, int)]
     assert result == [1, 11, 111, 112, 12], result
Example #12
0
 def pairs(matcher):
     for a in preorder(matcher, Matcher):
         for b in preorder(matcher, Matcher):
             yield (a, b)