def __get_next_match(self, test_case, pos): with open(test_case, "r") as in_file: prog = in_file.read() m = nestedmatcher.search(self.parts, prog, pos=pos) return m
def test_1(self): parts = [ RegExPattern(r'This\s'), (BalancedPattern(BalancedExpr.parens), 'nested') ] m = search(parts, '(This (string) contains) two nested matches!') self.assertEqual(m, {'all': (1, 14), 'nested': (6, 14)})
def test_2(self): parts = [ RegExPattern(r'This\s'), (BalancedPattern(BalancedExpr.parens), 'nested'), RegExPattern(r'[^(]*'), (BalancedPattern(BalancedExpr.parens), 'nested2') ] m = search(parts, '(This (string) contains) two (nested) matches!') self.assertEqual(m, { 'all': (1, 37), 'nested': (6, 14), 'nested2': (29, 37) })
def test_3(self): parts = [ RegExPattern(r'[Tt]his\s'), (BalancedPattern(BalancedExpr.parens), 'nested'), RegExPattern(r'[^(]*'), (BalancedPattern(BalancedExpr.parens), 'nested2') ] m = search(parts, '(This string this (contains)) two (nested) matches!') self.assertEqual(m, { 'all': (13, 42), 'nested': (18, 28), 'nested2': (34, 42) })
def transform(self, test_case, state, process_event_notifier): with open(test_case, "r") as in_file: prog = in_file.read() prog2 = prog if state["pos"] > len(prog): return (PassResult.STOP, state) if self.arg == "a": l = self.regexes_to_replace[state["regex"]] search = l[0] replace = l[1] m = nestedmatcher.search(search, prog2, pos=state["pos"], search=False) if m is not None: prog2 = prog2[0:m["all"][0]] + replace + prog2[m["all"][1]:] if prog != prog2: with open(test_case, "w") as out_file: out_file.write(prog2) return (PassResult.OK, state) elif self.arg == "b": l = self.delimited_regexes_to_replace[state["regex"]] search = l[0] replace = l[1] if prog2.startswith(","): front = (self.border_or_space_optional_pattern, "delim1") else: front = (self.border_or_space_pattern, "delim1") if prog2.endswith(","): back = (self.border_or_space_optional_pattern, "delim2") else: back = (self.border_or_space_pattern, "delim2") search = [front] + search + [back] m = nestedmatcher.search(search, prog2, pos=state["pos"], search=False) if m is not None: prog2 = prog2[0:m["delim1"][1]] + replace + prog2[ m["delim2"][0]:] if prog != prog2: with open(test_case, "w") as out_file: out_file.write(prog2) return (PassResult.OK, state) elif self.arg == "c": search = [ nestedmatcher.RegExPattern(r"^while\s*"), nestedmatcher.BalancedPattern( nestedmatcher.BalancedExpr.parens), nestedmatcher.RegExPattern(r"\s*"), (nestedmatcher.BalancedPattern( nestedmatcher.BalancedExpr.curlies), "body") ] m = nestedmatcher.search(search, prog2, pos=state["pos"], search=False) if m is not None: body = prog2[m["body"][0]:m["body"][1]] body = re.sub(r"break\s*;", "", body) prog2 = prog2[0:m["all"][0]] + body + prog2[m["all"][1]:] if prog != prog2: with open(test_case, "w") as out_file: out_file.write(prog2) return (PassResult.OK, state) else: raise UnknownArgumentError() return (PassResult.INVALID, state)
def transform(self, test_case, state, process_event_notifier): with open(test_case, 'r') as in_file: prog = in_file.read() prog2 = prog if state['pos'] > len(prog): return (PassResult.STOP, state) if self.arg == 'a': item = self.regexes_to_replace[state['regex']] search = item[0] replace = item[1] m = nestedmatcher.search(search, prog2, pos=state['pos'], search=False) if m is not None: prog2 = prog2[0:m['all'][0]] + replace + prog2[m['all'][1]:] if prog != prog2: with open(test_case, 'w') as out_file: out_file.write(prog2) return (PassResult.OK, state) elif self.arg == 'b': item = self.delimited_regexes_to_replace[state['regex']] search = item[0] replace = item[1] if prog2.startswith(','): front = (self.border_or_space_optional_pattern, 'delim1') else: front = (self.border_or_space_pattern, 'delim1') if prog2.endswith(','): back = (self.border_or_space_optional_pattern, 'delim2') else: back = (self.border_or_space_pattern, 'delim2') search = [front] + search + [back] m = nestedmatcher.search(search, prog2, pos=state['pos'], search=False) if m is not None: prog2 = prog2[0:m['delim1'][1]] + replace + prog2[ m['delim2'][0]:] if prog != prog2: with open(test_case, 'w') as out_file: out_file.write(prog2) return (PassResult.OK, state) elif self.arg == 'c': search = [ nestedmatcher.RegExPattern(r'^while\s*'), nestedmatcher.BalancedPattern( nestedmatcher.BalancedExpr.parens), nestedmatcher.RegExPattern(r'\s*'), (nestedmatcher.BalancedPattern( nestedmatcher.BalancedExpr.curlies), 'body') ] m = nestedmatcher.search(search, prog2, pos=state['pos'], search=False) if m is not None: body = prog2[m['body'][0]:m['body'][1]] body = re.sub(r'break\s*;', '', body) prog2 = prog2[0:m['all'][0]] + body + prog2[m['all'][1]:] if prog != prog2: with open(test_case, 'w') as out_file: out_file.write(prog2) return (PassResult.OK, state) else: raise UnknownArgumentError(self.__class__.__name__, self.arg) return (PassResult.INVALID, state)