Beispiel #1
0
    def __get_config(self):
        config = {
            "search": None,
            "replace_fn": None,
        }

        def replace_printf(m):
            return r'printf("%d\n", (int){})'.format(
                m.group("list").split(",")[0])

        def replace_empty(m):
            return ""

        if self.arg == "a":
            config["search"] = r"transparent_crc\s*\((?P<list>[^)]*)\)"
            config["replace_fn"] = replace_printf
        elif self.arg == "b":
            config["search"] = r'extern "C"'
            config["replace_fn"] = replace_empty
        elif self.arg == "c":
            config["search"] = r'extern "C\+\+"'
            config["replace_fn"] = replace_empty
        else:
            raise UnknownArgumentError()

        return config
Beispiel #2
0
    def transform(self, test_case, state, process_event_notifier):
        with open(test_case, "r") as in_file:
            old = in_file.read()

        if state != 0:
            return (PassResult.STOP, state)

        cmd = [self.external_programs["clang-format"], "-i"]

        if self.arg == "regular":
            cmd.extend(["-style", "{SpacesInAngles: true}", test_case])
        elif self.arg == "final":
            cmd.append(test_case)
        else:
            raise UnknownArgumentError(self.__class__.__name__, self.arg)

        _, _, returncode = process_event_notifier.run_process(cmd)
        if returncode != 0:
            return (PassResult.ERROR, state)

        with open(test_case, "r") as in_file:
            new = in_file.read()

        if old == new:
            return (PassResult.STOP, state)
        else:
            return (PassResult.OK, state)
Beispiel #3
0
    def __get_config(self):
        config = {
            'search': None,
            'replace_fn': None,
        }

        def replace_printf(m):
            return r"printf('%d\n', (int){})".format(
                m.group('list').split(',')[0])

        def replace_empty(m):
            return ''

        if self.arg == 'a':
            config['search'] = r'transparent_crc\s*\((?P<list>[^)]*)\)'
            config['replace_fn'] = replace_printf
        elif self.arg == 'b':
            config['search'] = r"extern 'C'"
            config['replace_fn'] = replace_empty
        elif self.arg == 'c':
            config['search'] = r"extern 'C\+\+'"
            config['replace_fn'] = replace_empty
        else:
            raise UnknownArgumentError(self.__class__.__name__, self.arg)

        return config
Beispiel #4
0
    def __get_config(self):
        config = {"search": None,
                  "replace_fn": None,
                 }

        if self.arg == "a":
            # Delete first digit
            def replace_fn(m):
                return m.group("pref") + m.group("numpart") + m.group("suf")

            config["search"] = r"(?P<pref>" + self.border_or_space + r"[+-]?(?:0|(?:0[xX]))?)[0-9a-fA-F](?P<numpart>[0-9a-fA-F]+)(?P<suf>[ULul]*" + self.border_or_space + r")"
        elif self.arg == "b":
            # Delete prefix
            def replace_fn(m):
                return m.group("del") + m.group("numpart") + m.group("suf")

            config["search"] = r"(?P<del>" + self.border_or_space + r")(?P<pref>[+-]?(?:0|(?:0[xX])))(?P<numpart>[0-9a-fA-F]+)(?P<suf>[ULul]*" + self.border_or_space + r")"
        elif self.arg == "c":
            # Delete suffix
            def replace_fn(m):
                return m.group("pref") + m.group("numpart") + m.group("del")

            config["search"] = r"(?P<pref>" + self.border_or_space + r"[+-]?(?:0|(?:0[xX]))?)(?P<numpart>[0-9a-fA-F]+)[ULul]+(?P<del>" + self.border_or_space + r")"
        elif self.arg == "d":
            # Hex to dec
            def replace_fn(m):
                return m.group("pref") + str(int(m.group("numpart"), 16)) + m.group("suf")

            config["search"] = r"(?P<pref>" + self.border_or_space + r")(?P<numpart>0[Xx][0-9a-fA-F]+)(?P<suf>[ULul]*" + self.border_or_space + r")"
        else:
            raise UnknownArgumentError(self.__class__.__name__, self.arg)

        config["replace_fn"] = replace_fn
        return config
Beispiel #5
0
    def __get_config(self):
        config = {
            'search': None,
            'replace_fn': None,
        }

        if self.arg == 'a':
            # Delete first digit
            def replace_fn(m):
                return m.group('pref') + m.group('numpart') + m.group('suf')

            config[
                'search'] = r'(?P<pref>' + self.border_or_space + r'[+-]?(?:0|(?:0[xX]))?)[0-9a-fA-F](?P<numpart>[0-9a-fA-F]+)(?P<suf>[ULul]*' + self.border_or_space + r')'
        elif self.arg == 'b':
            # Delete prefix
            def replace_fn(m):
                return m.group('del') + m.group('numpart') + m.group('suf')

            config[
                'search'] = r'(?P<del>' + self.border_or_space + r')(?P<pref>[+-]?(?:0|(?:0[xX])))(?P<numpart>[0-9a-fA-F]+)(?P<suf>[ULul]*' + self.border_or_space + r')'
        elif self.arg == 'c':
            # Delete suffix
            def replace_fn(m):
                return m.group('pref') + m.group('numpart') + m.group('del')

            config[
                'search'] = r'(?P<pref>' + self.border_or_space + r'[+-]?(?:0|(?:0[xX]))?)(?P<numpart>[0-9a-fA-F]+)[ULul]+(?P<del>' + self.border_or_space + r')'
        elif self.arg == 'd':
            # Hex to dec
            def replace_fn(m):
                return m.group('pref') + str(int(m.group('numpart'),
                                                 16)) + m.group('suf')

            config[
                'search'] = r'(?P<pref>' + self.border_or_space + r')(?P<numpart>0[Xx][0-9a-fA-F]+)(?P<suf>[ULul]*' + self.border_or_space + r')'
        else:
            raise UnknownArgumentError(self.__class__.__name__, self.arg)

        config['replace_fn'] = replace_fn
        return config
Beispiel #6
0
    def transform(self, test_case, state, process_event_notifier):
        with open(test_case, "r") as in_file:
            prog = in_file.read()
            prog2 = prog

        while True:
            if state is None:
                return (PassResult.STOP, state)
            else:
                if self.arg not in ["b", "c"]:
                    raise UnknownArgumentError(self.__class__.__name__,
                                               self.arg)

                prog2 = prog2[0:state["del1"][1]] + prog2[state[
                    self.arg][0]:state[self.arg][1]] + prog2[state["del2"][0]:]

                if prog != prog2:
                    with open(test_case, "w") as out_file:
                        out_file.write(prog2)

                    return (PassResult.OK, state)
                else:
                    state = self.advance(test_case, state)
Beispiel #7
0
    def advance(self, test_case, state):
        new_state = state.copy()

        if self.arg == 'a':
            lim = len(self.regexes_to_replace)
        elif self.arg == 'b':
            lim = len(self.delimited_regexes_to_replace)
        elif self.arg == 'c':
            lim = 1
        else:
            raise UnknownArgumentError(self.__class__.__name__, self.arg)

        new_state['regex'] += 1

        if new_state['regex'] >= lim:
            new_state['regex'] = 0
            new_state['pos'] += 1

        with open(test_case, 'r') as in_file:
            length = len(in_file.read())
            if new_state['pos'] >= length:
                return None

        return new_state
Beispiel #8
0
    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)
Beispiel #9
0
    def __get_config(self):
        config = {"search": None,
                  "replace_fn": None,
                  "prefix": "",
                 }

        def replace_all(string, match):
            return string[0:match[0]] + string[match[1]:]

        def replace_only(string, match):
            return string[0:match[0]] + string[(match[0] + 1):(match[1] - 1)] + string[match[1]:]

        def replace_inside(string, match):
            return string[0:(match[0] + 1)] + string[(match[1] - 1):]

        if self.arg == "square-inside":
            config["search"] = nestedmatcher.BalancedExpr.squares
            config["replace_fn"] = replace_inside
        elif self.arg == "angles-inside":
            config["search"] = nestedmatcher.BalancedExpr.angles
            config["replace_fn"] = replace_inside
        elif self.arg == "parens-inside":
            config["search"] = nestedmatcher.BalancedExpr.parens
            config["replace_fn"] = replace_inside
        elif self.arg == "curly-inside":
            config["search"] = nestedmatcher.BalancedExpr.curlies
            config["replace_fn"] = replace_inside
        elif self.arg == "square":
            config["search"] = nestedmatcher.BalancedExpr.squares
            config["replace_fn"] = replace_all
        elif self.arg == "angles":
            config["search"] = nestedmatcher.BalancedExpr.angles
            config["replace_fn"] = replace_all
        elif self.arg == "parens-to-zero":
            config["search"] = nestedmatcher.BalancedExpr.parens
            config["replace_fn"] = lambda string, match: string[0:match[0]] + "0" + string[match[1]:]
        elif self.arg == "parens":
            config["search"] = nestedmatcher.BalancedExpr.parens
            config["replace_fn"] = replace_all
        elif self.arg == "curly":
            config["search"] = nestedmatcher.BalancedExpr.curlies
            config["replace_fn"] = replace_all
        elif self.arg == "curly2":
            config["search"] = nestedmatcher.BalancedExpr.curlies
            config["replace_fn"] = lambda string, match: string[0:match[0]] + ";" + string[match[1]:]
        elif self.arg == "curly3":
            config["search"] = nestedmatcher.BalancedExpr.curlies
            config["replace_fn"] = replace_all
            config["prefix"] = "=\\s*"
        elif self.arg == "parens-only":
            config["search"] = nestedmatcher.BalancedExpr.parens
            config["replace_fn"] = replace_only
        elif self.arg == "curly-only":
            config["search"] = nestedmatcher.BalancedExpr.curlies
            config["replace_fn"] = replace_only
        elif self.arg == "angles-only":
            config["search"] = nestedmatcher.BalancedExpr.angles
            config["replace_fn"] = replace_only
        elif self.arg == "square-only":
            config["search"] = nestedmatcher.BalancedExpr.squares
            config["replace_fn"] = replace_only
        else:
            raise UnknownArgumentError(self.__class__.__name__, self.arg)

        return config
Beispiel #10
0
    def __get_config(self):
        config = {
            'search': None,
            'replace_fn': None,
            'prefix': '',
        }

        def replace_all(string, match):
            return string[0:match[0]] + string[match[1]:]

        def replace_only(string, match):
            return string[0:match[0]] + string[
                (match[0] + 1):(match[1] - 1)] + string[match[1]:]

        def replace_inside(string, match):
            return string[0:(match[0] + 1)] + string[(match[1] - 1):]

        if self.arg == 'square-inside':
            config['search'] = nestedmatcher.BalancedExpr.squares
            config['replace_fn'] = replace_inside
        elif self.arg == 'angles-inside':
            config['search'] = nestedmatcher.BalancedExpr.angles
            config['replace_fn'] = replace_inside
        elif self.arg == 'parens-inside':
            config['search'] = nestedmatcher.BalancedExpr.parens
            config['replace_fn'] = replace_inside
        elif self.arg == 'curly-inside':
            config['search'] = nestedmatcher.BalancedExpr.curlies
            config['replace_fn'] = replace_inside
        elif self.arg == 'square':
            config['search'] = nestedmatcher.BalancedExpr.squares
            config['replace_fn'] = replace_all
        elif self.arg == 'angles':
            config['search'] = nestedmatcher.BalancedExpr.angles
            config['replace_fn'] = replace_all
        elif self.arg == 'parens-to-zero':
            config['search'] = nestedmatcher.BalancedExpr.parens
            config['replace_fn'] = lambda string, match: string[0:match[
                0]] + '0' + string[match[1]:]
        elif self.arg == 'parens':
            config['search'] = nestedmatcher.BalancedExpr.parens
            config['replace_fn'] = replace_all
        elif self.arg == 'curly':
            config['search'] = nestedmatcher.BalancedExpr.curlies
            config['replace_fn'] = replace_all
        elif self.arg == 'curly2':
            config['search'] = nestedmatcher.BalancedExpr.curlies
            config['replace_fn'] = lambda string, match: string[0:match[
                0]] + ';' + string[match[1]:]
        elif self.arg == 'curly3':
            config['search'] = nestedmatcher.BalancedExpr.curlies
            config['replace_fn'] = replace_all
            config['prefix'] = '=\\s*'
        elif self.arg == 'parens-only':
            config['search'] = nestedmatcher.BalancedExpr.parens
            config['replace_fn'] = replace_only
        elif self.arg == 'curly-only':
            config['search'] = nestedmatcher.BalancedExpr.curlies
            config['replace_fn'] = replace_only
        elif self.arg == 'angles-only':
            config['search'] = nestedmatcher.BalancedExpr.angles
            config['replace_fn'] = replace_only
        elif self.arg == 'square-only':
            config['search'] = nestedmatcher.BalancedExpr.squares
            config['replace_fn'] = replace_only
        else:
            raise UnknownArgumentError(self.__class__.__name__, self.arg)

        return config