Exemplo n.º 1
0
    def processOne(self, match):
        """ NB don't replace "raise" with "throw" """
        tryex_indent = self.calcIndent(match.node) + "    "

        match.try_colon.remove()
        try_suite = match.try_suite
        try_suite.insert_child(0, makeLeaf("LBRACE", "{", ' '))
        last_dedent_idx = self.findNodeReverseIndex(try_suite, "DEDENT")
        indent = self.calcIndent(match.exc_word)
        try_suite.insert_child(last_dedent_idx,
                               makeLeaf("RBRACE", "}", indent))

        name_parent = match.exc_word.parent
        name_idx = getNodeIndex(match.exc_word)
        match.exc_word.replace(makeLeaf("PYJS", "catch", " "))

        if "exc_as" in match:
            match.exc_as.remove()
        if "exc_as_name" in match:
            as_name = match.exc_as_name.value
            match.exc_as_name.remove()
        else:
            as_name = "e"

        name_parent.insert_child(name_idx + 1, makeLeaf("LPAR", "(", ""))
        name_parent.insert_child(name_idx + 2, makeLeaf("PYJS", as_name, " "))
        name_parent.insert_child(name_idx + 3, makeLeaf("RPAR", ")", " "))

        if "exc_what" in match:
            exc_what = match.exc_what
            what_idx = getNodeIndex(exc_what)
            exc_what.parent.insert_child(what_idx, makeLeaf("PYJS", "/*", ' '))
            exc_what.parent.insert_child(what_idx + 2,
                                         makeLeaf("PYJS", "*/", ' '))

        match.exc_colon.remove()
        exc_suite = match.exc_suite
        exc_suite.insert_child(0, makeLeaf("LBRACE", "{", ' '))
        last_dedent_idx = self.findNodeReverseIndex(exc_suite, "DEDENT")
        exc_suite.insert_child(last_dedent_idx,
                               makeLeaf("RBRACE", "}", tryex_indent[:-4]))
        if "fin_word" not in match:
            exc_suite.insert_child(last_dedent_idx + 1,
                                   makeLeaf("NEWLINE", "\n", ""))

        if "fin_word" in match:
            match.fin_word.prefix += " "
            match.fin_colon.remove()
            fin_suite = match.fin_suite
            fin_suite.insert_child(0, makeLeaf("LBRACE", "{", ' '))
            last_dedent_idx = self.findNodeReverseIndex(fin_suite, "DEDENT")
            fin_suite.insert_child(last_dedent_idx,
                                   makeLeaf("RBRACE", "}", tryex_indent[:-4]))
            fin_suite.insert_child(last_dedent_idx + 1,
                                   makeLeaf("NEWLINE", "\n", ""))
Exemplo n.º 2
0
    def gather(self, node):
        """ return list of AnonObjs containing the info we need """
        tv = Treeverser(node)
        matches = tv.gatherMatches(self.TRYEX_PATTERN)

        infos = []
        for match in matches:
            info = AnonObj()
            info.node = match.node
            info.try_word = match.try_word
            info.try_colon = match.try_colon
            info.try_suite = match.try_suite[0]
            if "exc_clause" in match:
                info.exc_clause = match.exc_clause
                for node in gatherSubNodesD(info.exc_clause):
                    if getNodeKind(node) == "NAME" and node.value == "as":
                        info.exc_as = node
                        as_idx = getNodeIndex(node)
                        info.exc_as_name = node.parent.children[as_idx + 1]

            info.exc_word = match.exc_word
            if "exc_what" in match:
                info.exc_what = match.exc_what[0]
            info.exc_colon = match.exc_colon
            info.exc_suite = match.exc_suite[0]

            if "fin_word" in match:
                info.fin_word = match.fin_word
                info.fin_colon = match.fin_colon
                info.fin_suite = match.fin_suite[0]

            infos.append(info)
        return infos
def fixPassStatements(nodes):
    for node in gatherSubNodesD(nodes):
        if getNodeKind(node) == "NAME" and node.value == "pass":
            pass_idx = getNodeIndex(node)
            after_node = node.parent.children[pass_idx + 1]
            if getNodeKind(after_node) == "SEMI":
                after_node.remove()
            node.parent.insert_child(pass_idx,
                                     makeLeaf("PYJS", "/*", node.prefix))
            node.prefix = " "
            node.parent.insert_child(pass_idx + 2, makeLeaf("PYJS", "*/", ' '))
Exemplo n.º 4
0
    def processOne(self, match):
        whileloop_indent = self.calcIndent(match.node)

        par_node = match.node
        while_idx = getNodeIndex(match.name)
        par_node.insert_child(while_idx + 1, makeLeaf("LPAR", "(", ''))
        match.colon.replace(makeLeaf("RPAR", ")", " "))

        suite = match.suite
        suite.insert_child(0, makeLeaf("LBRACE", "{", ' '))
        last_dedent_idx = self.findNodeReverseIndex(suite, "DEDENT")
        suite.insert_child(last_dedent_idx,
                           makeLeaf("RBRACE", "}", whileloop_indent))
        suite.insert_child(last_dedent_idx + 1, makeLeaf("NEWLINE", "\n", ""))
Exemplo n.º 5
0
    def processOne(self, match):
        forloop_indent = self.calcIndent(match.node)

        # replace ( x, y ) with [ x, y ]
        loop_vars_lpar_node = self.findNodeForward(match.loop_vars, "LPAR",
                                                   None)
        if loop_vars_lpar_node:
            loop_vars_lpar_node.replace(
                makeLeaf("LSQB", "[", loop_vars_lpar_node.prefix))
            loop_vars_rpar_node = self.findNodeReverse(match.loop_vars, "RPAR")
            loop_vars_rpar_node.replace(
                makeLeaf("RSQB", "]", loop_vars_rpar_node.prefix))
        else:  # replace x, y with [ x, y ]
            loop_vars_comma_node = self.findNodeForward(
                match.loop_vars, "COMMA", None)
            if loop_vars_comma_node:
                match.loop_vars.insert_child(0, makeLeaf("LSQB", "[", " "))
                match.loop_vars.append_child(makeLeaf("RSQB", "]", " "))

            # in => of
        match.in_word.replace(makeLeaf("PYJS", "of", " "))

        # parens round loop head
        par_node = match.node
        for_idx = getNodeIndex(match.for_word)
        par_node.insert_child(for_idx + 1, makeLeaf("LPAR", "(", ''))
        par_node.insert_child(for_idx + 2, makeLeaf("PYJS", "let", ' '))
        match.for_colon.replace(makeLeaf("RPAR", ")", " "))

        # braces round loop body
        for_suite = match.for_suite
        for_suite.insert_child(0, makeLeaf("LBRACE", "{", ' '))
        last_dedent_idx = self.findNodeReverseIndex(for_suite, "DEDENT")
        for_suite.insert_child(last_dedent_idx,
                               makeLeaf("RBRACE", "}", forloop_indent))
        for_suite.insert_child(last_dedent_idx + 1,
                               makeLeaf("NEWLINE", "\n", ""))
Exemplo n.º 6
0
    def processAll(self, matches):
        all_if_suites = []
        for match in matches:

            if_indent = self.calcIndent(match.if_clause.if_word)

            # if part
            par_node = match.if_clause.if_suite.parent
            if_idx = par_node.children.index(match.if_clause.if_word)
            par_node.insert_child(if_idx + 1, makeLeaf("LPAR", "(", ''))
            match.if_clause.if_colon.replace(makeLeaf("RPAR", ")", " "))
            if_suite = match.if_clause.if_suite
            if_suite.insert_child(0, makeLeaf("LBRACE", "{", ' '))
            last_dedent = self.findNodeReverse(if_suite, "DEDENT")
            last_dedent.prefix = ""
            last_dedent_idx = getNodeIndex(last_dedent)
            last_dedent.parent.insert_child(last_dedent_idx + 0,
                                            makeLeaf("RBRACE", "}", if_indent))
            if not match.elif_clauses and not match.else_clause:
                last_dedent.parent.insert_child(last_dedent_idx + 1,
                                                makeLeaf("NEWLINE", "\n", ""))

            all_if_suites.append(if_suite)

            # elif parts
            for elif_match in match.elif_clauses:
                par_node = elif_match.elif_test.parent
                if_idx = par_node.children.index(elif_match.elif_word)
                elif_match.elif_word.replace(makeLeaf("PYJS", "else if", " "))
                par_node.insert_child(if_idx + 1, makeLeaf("LPAR", "(", ''))
                elif_match.elif_colon.replace(makeLeaf("RPAR", ")", " "))
                elif_suite = elif_match.elif_suite
                elif_suite.insert_child(0, makeLeaf("LBRACE", "{", ' '))
                last_dedent_idx = self.findNodeReverseIndex(
                    elif_suite, "DEDENT")
                elif_suite.insert_child(last_dedent_idx,
                                        makeLeaf("RBRACE", "}", ''))
                if not match.else_clause:
                    elif_suite.insert_child(last_dedent_idx + 1,
                                            makeLeaf("NEWLINE", "\n", ""))

                all_if_suites.append(elif_suite)

            # else part
            if match.else_clause:
                match.else_clause.else_colon.remove()
                prefix = match.else_clause.else_word.prefix
                match.else_clause.else_word.prefix = " "
                else_suite = match.else_clause.else_suite
                last_dedent = self.findNodeReverse(else_suite, "DEDENT")

                else_suite.insert_child(0, makeLeaf("LBRACE", "{", " "))
                last_dedent_idx = getNodeIndex(last_dedent)
                else_suite.insert_child(last_dedent_idx,
                                        makeLeaf("RBRACE", "}", if_indent))

                # sometimes we get a blank line due to this but its better than
                #    missing the '\n' most of the time
                else_suite.insert_child(last_dedent_idx + 1,
                                        makeLeaf("NEWLINE", "\n", ""))

                all_if_suites.append(else_suite)

        for sub_if_suite in all_if_suites:
            sub_if_matches = self.gather(sub_if_suite)
            self.processAll(sub_if_matches)