def clone(self): ''' Clone this matcher (and associated graph). This is currently only used when we want to test different configurations. ''' from lepl.core.rewriters import clone_matcher return clone_matcher(self)
def test_describe(self): ''' Use a description of the graph to check against changes. ''' #basicConfig(level=DEBUG) name = Word(Letter()) > 'name' expression = Delayed() variable = Delayed() function = (expression / '()') > 'function' expression += (variable | function) > 'expression' variable += (name | expression / '.' / name) dotted_name = function & Eos() base = dotted_name.tree() # print(base) desc0 = NodeStats(dotted_name) print(desc0) assert desc0.total == 18, desc0 self.assert_count(desc0, And, 5) self.assert_count(desc0, Or, 2) self.assert_count(desc0, Delayed, 2) clone0 = clone_matcher(dotted_name) # print(clone0.tree()) diff = Differ() diff_text = '\n'.join( diff.compare(base.split('\n'), clone0.tree().split('\n'))) #print(diff_text) descx = NodeStats(clone0) print(descx) assert descx == desc0 clone1 = Flatten()(dotted_name) print(clone1.tree()) desc1 = NodeStats(clone1) print(desc1) # flattened And (Or no longer flattened as Delayed intervenes) assert desc1.total == 17, desc1 self.assert_count(desc1, And, 4) self.assert_count(desc1, Or, 2) self.assert_count(desc1, Delayed, 2) self.assert_count(desc1, Transform, 7) self.assert_count(desc1, TransformationWrapper, 7) clone2 = ComposeTransforms()(clone1) desc2 = NodeStats(clone2) #print(desc2) # compressed a transform assert desc2.total == 17, desc2 self.assert_count(desc2, And, 4) self.assert_count(desc2, Or, 2) self.assert_count(desc2, Delayed, 2) self.assert_count(desc2, Transform, 6) self.assert_count(desc2, TransformationWrapper, 6) clone3 = RightMemoize()(clone2) desc3 = NodeStats(clone3) #print(desc3) assert desc3.total == 17, desc3 self.assert_count(desc3, _RMemo, 17) self.assert_count(desc3, Delayed, 2) clone4 = LeftMemoize()(clone2) desc4 = NodeStats(clone4) #print(desc4) assert desc4.total == 17, desc4 self.assert_count(desc4, _LMemo, 20) # left memo duplicates delayed self.assert_count(desc4, Delayed, 3) clone5 = AutoMemoize(left=LMemo, right=RMemo)(clone2) desc5 = NodeStats(clone5) #print(desc5) assert desc5.total == 17, desc5 self.assert_count(desc5, _RMemo, 5) self.assert_count(desc5, _LMemo, 15) # left memo duplicates delayed self.assert_count(desc5, Delayed, 3) try: clone3.config.clear() clone3.parse_string('1join()') assert False, 'Expected error' except MemoException as error: assert 'Left recursion was detected' in str(error), str(error) clone4.config.clear() clone4.parse_string('1join()') clone5.config.clear() clone5.parse_string('1join()')
def test_describe(self): ''' Use a description of the graph to check against changes. ''' #basicConfig(level=DEBUG) name = Word(Letter()) > 'name' expression = Delayed() variable = Delayed() function = (expression / '()') > 'function' expression += (variable | function) > 'expression' variable += (name | expression / '.' / name) dotted_name = function & Eos() base = dotted_name.tree() # print(base) desc0 = NodeStats(dotted_name) print(desc0) assert desc0.total == 18, desc0 self.assert_count(desc0, And, 5) self.assert_count(desc0, Or, 2) self.assert_count(desc0, Delayed, 2) clone0 = clone_matcher(dotted_name) # print(clone0.tree()) diff = Differ() diff_text = '\n'.join(diff.compare(base.split('\n'), clone0.tree().split('\n'))) #print(diff_text) descx = NodeStats(clone0) print(descx) assert descx == desc0 clone1 = Flatten()(dotted_name) print(clone1.tree()) desc1 = NodeStats(clone1) print(desc1) # flattened And (Or no longer flattened as Delayed intervenes) assert desc1.total == 17, desc1 self.assert_count(desc1, And, 4) self.assert_count(desc1, Or, 2) self.assert_count(desc1, Delayed, 2) self.assert_count(desc1, Transform, 7) self.assert_count(desc1, TransformationWrapper, 7) clone2 = ComposeTransforms()(clone1) desc2 = NodeStats(clone2) #print(desc2) # compressed a transform assert desc2.total == 17, desc2 self.assert_count(desc2, And, 4) self.assert_count(desc2, Or, 2) self.assert_count(desc2, Delayed, 2) self.assert_count(desc2, Transform, 6) self.assert_count(desc2, TransformationWrapper, 6) clone3 = RightMemoize()(clone2) desc3 = NodeStats(clone3) #print(desc3) assert desc3.total == 17, desc3 self.assert_count(desc3, _RMemo, 17) self.assert_count(desc3, Delayed, 2) clone4 = LeftMemoize()(clone2) desc4 = NodeStats(clone4) #print(desc4) assert desc4.total == 17, desc4 self.assert_count(desc4, _LMemo, 20) # left memo duplicates delayed self.assert_count(desc4, Delayed, 3) clone5 = AutoMemoize(left=LMemo, right=RMemo)(clone2) desc5 = NodeStats(clone5) #print(desc5) assert desc5.total == 17, desc5 self.assert_count(desc5, _RMemo, 5) self.assert_count(desc5, _LMemo, 15) # left memo duplicates delayed self.assert_count(desc5, Delayed, 3) try: clone3.config.clear() clone3.parse_string('1join()') assert False, 'Expected error' except MemoException as error: assert 'Left recursion was detected' in str(error), str(error) clone4.config.clear() clone4.parse_string('1join()') clone5.config.clear() clone5.parse_string('1join()')
def __call__(self, graph): new_clone = make_clone(self.alphabet, clone, self.matcher, self.use) graph = clone_matcher(graph, new_clone) graph = RegexpContainer.to_matcher(graph) return graph