Python symbol.comp_for() Examples

The following are 25 code examples of symbol.comp_for(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module symbol , or try the search function .
Example #1
Source File: transformer.py    From PokemonGo-DesktopMap with MIT License 6 votes vote down vote up
def com_argument(self, nodelist, kw, star_node):
        if len(nodelist) == 3 and nodelist[2][0] == symbol.comp_for:
            test = self.com_node(nodelist[1])
            return 0, self.com_generator_expression(test, nodelist[2])
        if len(nodelist) == 2:
            if kw:
                raise SyntaxError, "non-keyword arg after keyword arg"
            if star_node:
                raise SyntaxError, "only named arguments may follow *expression"
            return 0, self.com_node(nodelist[1])
        result = self.com_node(nodelist[3])
        n = nodelist[1]
        while len(n) == 2 and n[0] != token.NAME:
            n = n[1]
        if n[0] != token.NAME:
            raise SyntaxError, "keyword can't be an expression (%s)"%n[0]
        node = Keyword(n[1], result, lineno=n[2])
        return 1, node 
Example #2
Source File: transformer.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def com_argument(self, nodelist, kw, star_node):
        if len(nodelist) == 3 and nodelist[2][0] == symbol.comp_for:
            test = self.com_node(nodelist[1])
            return 0, self.com_generator_expression(test, nodelist[2])
        if len(nodelist) == 2:
            if kw:
                raise SyntaxError, "non-keyword arg after keyword arg"
            if star_node:
                raise SyntaxError, "only named arguments may follow *expression"
            return 0, self.com_node(nodelist[1])
        result = self.com_node(nodelist[3])
        n = nodelist[1]
        while len(n) == 2 and n[0] != token.NAME:
            n = n[1]
        if n[0] != token.NAME:
            raise SyntaxError, "keyword can't be an expression (%s)"%n[0]
        node = Keyword(n[1], result, lineno=n[2])
        return 1, node 
Example #3
Source File: transformer.py    From Splunking-Crime with GNU Affero General Public License v3.0 6 votes vote down vote up
def com_argument(self, nodelist, kw, star_node):
        if len(nodelist) == 3 and nodelist[2][0] == symbol.comp_for:
            test = self.com_node(nodelist[1])
            return 0, self.com_generator_expression(test, nodelist[2])
        if len(nodelist) == 2:
            if kw:
                raise SyntaxError, "non-keyword arg after keyword arg"
            if star_node:
                raise SyntaxError, "only named arguments may follow *expression"
            return 0, self.com_node(nodelist[1])
        result = self.com_node(nodelist[3])
        n = nodelist[1]
        while len(n) == 2 and n[0] != token.NAME:
            n = n[1]
        if n[0] != token.NAME:
            raise SyntaxError, "keyword can't be an expression (%s)"%n[0]
        node = Keyword(n[1], result, lineno=n[2])
        return 1, node 
Example #4
Source File: transformer.py    From BinderFilter with MIT License 6 votes vote down vote up
def com_argument(self, nodelist, kw, star_node):
        if len(nodelist) == 3 and nodelist[2][0] == symbol.comp_for:
            test = self.com_node(nodelist[1])
            return 0, self.com_generator_expression(test, nodelist[2])
        if len(nodelist) == 2:
            if kw:
                raise SyntaxError, "non-keyword arg after keyword arg"
            if star_node:
                raise SyntaxError, "only named arguments may follow *expression"
            return 0, self.com_node(nodelist[1])
        result = self.com_node(nodelist[3])
        n = nodelist[1]
        while len(n) == 2 and n[0] != token.NAME:
            n = n[1]
        if n[0] != token.NAME:
            raise SyntaxError, "keyword can't be an expression (%s)"%n[0]
        node = Keyword(n[1], result, lineno=n[2])
        return 1, node 
Example #5
Source File: transformer.py    From oss-ftp with MIT License 6 votes vote down vote up
def com_argument(self, nodelist, kw, star_node):
        if len(nodelist) == 3 and nodelist[2][0] == symbol.comp_for:
            test = self.com_node(nodelist[1])
            return 0, self.com_generator_expression(test, nodelist[2])
        if len(nodelist) == 2:
            if kw:
                raise SyntaxError, "non-keyword arg after keyword arg"
            if star_node:
                raise SyntaxError, "only named arguments may follow *expression"
            return 0, self.com_node(nodelist[1])
        result = self.com_node(nodelist[3])
        n = nodelist[1]
        while len(n) == 2 and n[0] != token.NAME:
            n = n[1]
        if n[0] != token.NAME:
            raise SyntaxError, "keyword can't be an expression (%s)"%n[0]
        node = Keyword(n[1], result, lineno=n[2])
        return 1, node 
Example #6
Source File: transformer.py    From oss-ftp with MIT License 5 votes vote down vote up
def com_dictorsetmaker(self, nodelist):
        # dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
        #                   (test (comp_for | (',' test)* [','])) )
        assert nodelist[0] == symbol.dictorsetmaker
        nodelist = nodelist[1:]
        if len(nodelist) == 1 or nodelist[1][0] == token.COMMA:
            # set literal
            items = []
            for i in range(0, len(nodelist), 2):
                items.append(self.com_node(nodelist[i]))
            return Set(items, lineno=items[0].lineno)
        elif nodelist[1][0] == symbol.comp_for:
            # set comprehension
            expr = self.com_node(nodelist[0])
            return self.com_comprehension(expr, None, nodelist[1], 'set')
        elif len(nodelist) > 3 and nodelist[3][0] == symbol.comp_for:
            # dict comprehension
            assert nodelist[1][0] == token.COLON
            key = self.com_node(nodelist[0])
            value = self.com_node(nodelist[2])
            return self.com_comprehension(key, value, nodelist[3], 'dict')
        else:
            # dict literal
            items = []
            for i in range(0, len(nodelist), 4):
                items.append((self.com_node(nodelist[i]),
                              self.com_node(nodelist[i+2])))
            return Dict(items, lineno=items[0][0].lineno) 
Example #7
Source File: transformer.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def com_call_function(self, primaryNode, nodelist):
        if nodelist[0] == token.RPAR:
            return CallFunc(primaryNode, [], lineno=extractLineNo(nodelist))
        args = []
        kw = 0
        star_node = dstar_node = None
        len_nodelist = len(nodelist)
        i = 1
        while i < len_nodelist:
            node = nodelist[i]

            if node[0]==token.STAR:
                if star_node is not None:
                    raise SyntaxError, 'already have the varargs indentifier'
                star_node = self.com_node(nodelist[i+1])
                i = i + 3
                continue
            elif node[0]==token.DOUBLESTAR:
                if dstar_node is not None:
                    raise SyntaxError, 'already have the kwargs indentifier'
                dstar_node = self.com_node(nodelist[i+1])
                i = i + 3
                continue

            # positional or named parameters
            kw, result = self.com_argument(node, kw, star_node)

            if len_nodelist != 2 and isinstance(result, GenExpr) \
               and len(node) == 3 and node[2][0] == symbol.comp_for:
                # allow f(x for x in y), but reject f(x for x in y, 1)
                # should use f((x for x in y), 1) instead of f(x for x in y, 1)
                raise SyntaxError, 'generator expression needs parenthesis'

            args.append(result)
            i = i + 2

        return CallFunc(primaryNode, args, star_node, dstar_node,
                        lineno=extractLineNo(nodelist)) 
Example #8
Source File: transformer.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def com_dictorsetmaker(self, nodelist):
        # dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
        #                   (test (comp_for | (',' test)* [','])) )
        assert nodelist[0] == symbol.dictorsetmaker
        nodelist = nodelist[1:]
        if len(nodelist) == 1 or nodelist[1][0] == token.COMMA:
            # set literal
            items = []
            for i in range(0, len(nodelist), 2):
                items.append(self.com_node(nodelist[i]))
            return Set(items, lineno=items[0].lineno)
        elif nodelist[1][0] == symbol.comp_for:
            # set comprehension
            expr = self.com_node(nodelist[0])
            return self.com_comprehension(expr, None, nodelist[1], 'set')
        elif len(nodelist) > 3 and nodelist[3][0] == symbol.comp_for:
            # dict comprehension
            assert nodelist[1][0] == token.COLON
            key = self.com_node(nodelist[0])
            value = self.com_node(nodelist[2])
            return self.com_comprehension(key, value, nodelist[3], 'dict')
        else:
            # dict literal
            items = []
            for i in range(0, len(nodelist), 4):
                items.append((self.com_node(nodelist[i]),
                              self.com_node(nodelist[i+2])))
            return Dict(items, lineno=items[0][0].lineno) 
Example #9
Source File: transformer.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def com_generator_expression(self, expr, node):
        # comp_iter: comp_for | comp_if
        # comp_for: 'for' exprlist 'in' test [comp_iter]
        # comp_if: 'if' test [comp_iter]

        lineno = node[1][2]
        fors = []
        while node:
            t = node[1][1]
            if t == 'for':
                assignNode = self.com_assign(node[2], OP_ASSIGN)
                genNode = self.com_node(node[4])
                newfor = GenExprFor(assignNode, genNode, [],
                                    lineno=node[1][2])
                fors.append(newfor)
                if (len(node)) == 5:
                    node = None
                else:
                    node = self.com_comp_iter(node[5])
            elif t == 'if':
                test = self.com_node(node[2])
                newif = GenExprIf(test, lineno=node[1][2])
                newfor.ifs.append(newif)
                if len(node) == 3:
                    node = None
                else:
                    node = self.com_comp_iter(node[3])
            else:
                raise SyntaxError, \
                        ("unexpected generator expression element: %s %d"
                         % (node, lineno))
        fors[0].is_outmost = True
        return GenExpr(GenExprInner(expr, fors), lineno=lineno) 
Example #10
Source File: transformer.py    From PokemonGo-DesktopMap with MIT License 5 votes vote down vote up
def testlist_comp(self, nodelist):
        # test ( comp_for | (',' test)* [','] )
        assert nodelist[0][0] == symbol.test
        if len(nodelist) == 2 and nodelist[1][0] == symbol.comp_for:
            test = self.com_node(nodelist[0])
            return self.com_generator_expression(test, nodelist[1])
        return self.testlist(nodelist) 
Example #11
Source File: transformer.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def com_call_function(self, primaryNode, nodelist):
        if nodelist[0] == token.RPAR:
            return CallFunc(primaryNode, [], lineno=extractLineNo(nodelist))
        args = []
        kw = 0
        star_node = dstar_node = None
        len_nodelist = len(nodelist)
        i = 1
        while i < len_nodelist:
            node = nodelist[i]

            if node[0]==token.STAR:
                if star_node is not None:
                    raise SyntaxError, 'already have the varargs indentifier'
                star_node = self.com_node(nodelist[i+1])
                i = i + 3
                continue
            elif node[0]==token.DOUBLESTAR:
                if dstar_node is not None:
                    raise SyntaxError, 'already have the kwargs indentifier'
                dstar_node = self.com_node(nodelist[i+1])
                i = i + 3
                continue

            # positional or named parameters
            kw, result = self.com_argument(node, kw, star_node)

            if len_nodelist != 2 and isinstance(result, GenExpr) \
               and len(node) == 3 and node[2][0] == symbol.comp_for:
                # allow f(x for x in y), but reject f(x for x in y, 1)
                # should use f((x for x in y), 1) instead of f(x for x in y, 1)
                raise SyntaxError, 'generator expression needs parenthesis'

            args.append(result)
            i = i + 2

        return CallFunc(primaryNode, args, star_node, dstar_node,
                        lineno=extractLineNo(nodelist)) 
Example #12
Source File: transformer.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def com_dictorsetmaker(self, nodelist):
        # dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
        #                   (test (comp_for | (',' test)* [','])) )
        assert nodelist[0] == symbol.dictorsetmaker
        nodelist = nodelist[1:]
        if len(nodelist) == 1 or nodelist[1][0] == token.COMMA:
            # set literal
            items = []
            for i in range(0, len(nodelist), 2):
                items.append(self.com_node(nodelist[i]))
            return Set(items, lineno=items[0].lineno)
        elif nodelist[1][0] == symbol.comp_for:
            # set comprehension
            expr = self.com_node(nodelist[0])
            return self.com_comprehension(expr, None, nodelist[1], 'set')
        elif len(nodelist) > 3 and nodelist[3][0] == symbol.comp_for:
            # dict comprehension
            assert nodelist[1][0] == token.COLON
            key = self.com_node(nodelist[0])
            value = self.com_node(nodelist[2])
            return self.com_comprehension(key, value, nodelist[3], 'dict')
        else:
            # dict literal
            items = []
            for i in range(0, len(nodelist), 4):
                items.append((self.com_node(nodelist[i]),
                              self.com_node(nodelist[i+2])))
            return Dict(items, lineno=items[0][0].lineno) 
Example #13
Source File: transformer.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def com_generator_expression(self, expr, node):
        # comp_iter: comp_for | comp_if
        # comp_for: 'for' exprlist 'in' test [comp_iter]
        # comp_if: 'if' test [comp_iter]

        lineno = node[1][2]
        fors = []
        while node:
            t = node[1][1]
            if t == 'for':
                assignNode = self.com_assign(node[2], OP_ASSIGN)
                genNode = self.com_node(node[4])
                newfor = GenExprFor(assignNode, genNode, [],
                                    lineno=node[1][2])
                fors.append(newfor)
                if (len(node)) == 5:
                    node = None
                else:
                    node = self.com_comp_iter(node[5])
            elif t == 'if':
                test = self.com_node(node[2])
                newif = GenExprIf(test, lineno=node[1][2])
                newfor.ifs.append(newif)
                if len(node) == 3:
                    node = None
                else:
                    node = self.com_comp_iter(node[3])
            else:
                raise SyntaxError, \
                        ("unexpected generator expression element: %s %d"
                         % (node, lineno))
        fors[0].is_outmost = True
        return GenExpr(GenExprInner(expr, fors), lineno=lineno) 
Example #14
Source File: transformer.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def testlist_comp(self, nodelist):
        # test ( comp_for | (',' test)* [','] )
        assert nodelist[0][0] == symbol.test
        if len(nodelist) == 2 and nodelist[1][0] == symbol.comp_for:
            test = self.com_node(nodelist[0])
            return self.com_generator_expression(test, nodelist[1])
        return self.testlist(nodelist) 
Example #15
Source File: transformer.py    From oss-ftp with MIT License 5 votes vote down vote up
def com_call_function(self, primaryNode, nodelist):
        if nodelist[0] == token.RPAR:
            return CallFunc(primaryNode, [], lineno=extractLineNo(nodelist))
        args = []
        kw = 0
        star_node = dstar_node = None
        len_nodelist = len(nodelist)
        i = 1
        while i < len_nodelist:
            node = nodelist[i]

            if node[0]==token.STAR:
                if star_node is not None:
                    raise SyntaxError, 'already have the varargs indentifier'
                star_node = self.com_node(nodelist[i+1])
                i = i + 3
                continue
            elif node[0]==token.DOUBLESTAR:
                if dstar_node is not None:
                    raise SyntaxError, 'already have the kwargs indentifier'
                dstar_node = self.com_node(nodelist[i+1])
                i = i + 3
                continue

            # positional or named parameters
            kw, result = self.com_argument(node, kw, star_node)

            if len_nodelist != 2 and isinstance(result, GenExpr) \
               and len(node) == 3 and node[2][0] == symbol.comp_for:
                # allow f(x for x in y), but reject f(x for x in y, 1)
                # should use f((x for x in y), 1) instead of f(x for x in y, 1)
                raise SyntaxError, 'generator expression needs parenthesis'

            args.append(result)
            i = i + 2

        return CallFunc(primaryNode, args, star_node, dstar_node,
                        lineno=extractLineNo(nodelist)) 
Example #16
Source File: transformer.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def testlist_comp(self, nodelist):
        # test ( comp_for | (',' test)* [','] )
        assert nodelist[0][0] == symbol.test
        if len(nodelist) == 2 and nodelist[1][0] == symbol.comp_for:
            test = self.com_node(nodelist[0])
            return self.com_generator_expression(test, nodelist[1])
        return self.testlist(nodelist) 
Example #17
Source File: transformer.py    From oss-ftp with MIT License 5 votes vote down vote up
def com_generator_expression(self, expr, node):
        # comp_iter: comp_for | comp_if
        # comp_for: 'for' exprlist 'in' test [comp_iter]
        # comp_if: 'if' test [comp_iter]

        lineno = node[1][2]
        fors = []
        while node:
            t = node[1][1]
            if t == 'for':
                assignNode = self.com_assign(node[2], OP_ASSIGN)
                genNode = self.com_node(node[4])
                newfor = GenExprFor(assignNode, genNode, [],
                                    lineno=node[1][2])
                fors.append(newfor)
                if (len(node)) == 5:
                    node = None
                else:
                    node = self.com_comp_iter(node[5])
            elif t == 'if':
                test = self.com_node(node[2])
                newif = GenExprIf(test, lineno=node[1][2])
                newfor.ifs.append(newif)
                if len(node) == 3:
                    node = None
                else:
                    node = self.com_comp_iter(node[3])
            else:
                raise SyntaxError, \
                        ("unexpected generator expression element: %s %d"
                         % (node, lineno))
        fors[0].is_outmost = True
        return GenExpr(GenExprInner(expr, fors), lineno=lineno) 
Example #18
Source File: transformer.py    From oss-ftp with MIT License 5 votes vote down vote up
def testlist_comp(self, nodelist):
        # test ( comp_for | (',' test)* [','] )
        assert nodelist[0][0] == symbol.test
        if len(nodelist) == 2 and nodelist[1][0] == symbol.comp_for:
            test = self.com_node(nodelist[0])
            return self.com_generator_expression(test, nodelist[1])
        return self.testlist(nodelist) 
Example #19
Source File: transformer.py    From BinderFilter with MIT License 5 votes vote down vote up
def com_call_function(self, primaryNode, nodelist):
        if nodelist[0] == token.RPAR:
            return CallFunc(primaryNode, [], lineno=extractLineNo(nodelist))
        args = []
        kw = 0
        star_node = dstar_node = None
        len_nodelist = len(nodelist)
        i = 1
        while i < len_nodelist:
            node = nodelist[i]

            if node[0]==token.STAR:
                if star_node is not None:
                    raise SyntaxError, 'already have the varargs indentifier'
                star_node = self.com_node(nodelist[i+1])
                i = i + 3
                continue
            elif node[0]==token.DOUBLESTAR:
                if dstar_node is not None:
                    raise SyntaxError, 'already have the kwargs indentifier'
                dstar_node = self.com_node(nodelist[i+1])
                i = i + 3
                continue

            # positional or named parameters
            kw, result = self.com_argument(node, kw, star_node)

            if len_nodelist != 2 and isinstance(result, GenExpr) \
               and len(node) == 3 and node[2][0] == symbol.comp_for:
                # allow f(x for x in y), but reject f(x for x in y, 1)
                # should use f((x for x in y), 1) instead of f(x for x in y, 1)
                raise SyntaxError, 'generator expression needs parenthesis'

            args.append(result)
            i = i + 2

        return CallFunc(primaryNode, args, star_node, dstar_node,
                        lineno=extractLineNo(nodelist)) 
Example #20
Source File: transformer.py    From BinderFilter with MIT License 5 votes vote down vote up
def com_dictorsetmaker(self, nodelist):
        # dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
        #                   (test (comp_for | (',' test)* [','])) )
        assert nodelist[0] == symbol.dictorsetmaker
        nodelist = nodelist[1:]
        if len(nodelist) == 1 or nodelist[1][0] == token.COMMA:
            # set literal
            items = []
            for i in range(0, len(nodelist), 2):
                items.append(self.com_node(nodelist[i]))
            return Set(items, lineno=items[0].lineno)
        elif nodelist[1][0] == symbol.comp_for:
            # set comprehension
            expr = self.com_node(nodelist[0])
            return self.com_comprehension(expr, None, nodelist[1], 'set')
        elif len(nodelist) > 3 and nodelist[3][0] == symbol.comp_for:
            # dict comprehension
            assert nodelist[1][0] == token.COLON
            key = self.com_node(nodelist[0])
            value = self.com_node(nodelist[2])
            return self.com_comprehension(key, value, nodelist[3], 'dict')
        else:
            # dict literal
            items = []
            for i in range(0, len(nodelist), 4):
                items.append((self.com_node(nodelist[i]),
                              self.com_node(nodelist[i+2])))
            return Dict(items, lineno=items[0][0].lineno) 
Example #21
Source File: transformer.py    From BinderFilter with MIT License 5 votes vote down vote up
def com_generator_expression(self, expr, node):
        # comp_iter: comp_for | comp_if
        # comp_for: 'for' exprlist 'in' test [comp_iter]
        # comp_if: 'if' test [comp_iter]

        lineno = node[1][2]
        fors = []
        while node:
            t = node[1][1]
            if t == 'for':
                assignNode = self.com_assign(node[2], OP_ASSIGN)
                genNode = self.com_node(node[4])
                newfor = GenExprFor(assignNode, genNode, [],
                                    lineno=node[1][2])
                fors.append(newfor)
                if (len(node)) == 5:
                    node = None
                else:
                    node = self.com_comp_iter(node[5])
            elif t == 'if':
                test = self.com_node(node[2])
                newif = GenExprIf(test, lineno=node[1][2])
                newfor.ifs.append(newif)
                if len(node) == 3:
                    node = None
                else:
                    node = self.com_comp_iter(node[3])
            else:
                raise SyntaxError, \
                        ("unexpected generator expression element: %s %d"
                         % (node, lineno))
        fors[0].is_outmost = True
        return GenExpr(GenExprInner(expr, fors), lineno=lineno) 
Example #22
Source File: transformer.py    From BinderFilter with MIT License 5 votes vote down vote up
def testlist_comp(self, nodelist):
        # test ( comp_for | (',' test)* [','] )
        assert nodelist[0][0] == symbol.test
        if len(nodelist) == 2 and nodelist[1][0] == symbol.comp_for:
            test = self.com_node(nodelist[0])
            return self.com_generator_expression(test, nodelist[1])
        return self.testlist(nodelist) 
Example #23
Source File: transformer.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def com_call_function(self, primaryNode, nodelist):
        if nodelist[0] == token.RPAR:
            return CallFunc(primaryNode, [], lineno=extractLineNo(nodelist))
        args = []
        kw = 0
        star_node = dstar_node = None
        len_nodelist = len(nodelist)
        i = 1
        while i < len_nodelist:
            node = nodelist[i]

            if node[0]==token.STAR:
                if star_node is not None:
                    raise SyntaxError, 'already have the varargs indentifier'
                star_node = self.com_node(nodelist[i+1])
                i = i + 3
                continue
            elif node[0]==token.DOUBLESTAR:
                if dstar_node is not None:
                    raise SyntaxError, 'already have the kwargs indentifier'
                dstar_node = self.com_node(nodelist[i+1])
                i = i + 3
                continue

            # positional or named parameters
            kw, result = self.com_argument(node, kw, star_node)

            if len_nodelist != 2 and isinstance(result, GenExpr) \
               and len(node) == 3 and node[2][0] == symbol.comp_for:
                # allow f(x for x in y), but reject f(x for x in y, 1)
                # should use f((x for x in y), 1) instead of f(x for x in y, 1)
                raise SyntaxError, 'generator expression needs parenthesis'

            args.append(result)
            i = i + 2

        return CallFunc(primaryNode, args, star_node, dstar_node,
                        lineno=extractLineNo(nodelist)) 
Example #24
Source File: transformer.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def com_dictorsetmaker(self, nodelist):
        # dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
        #                   (test (comp_for | (',' test)* [','])) )
        assert nodelist[0] == symbol.dictorsetmaker
        nodelist = nodelist[1:]
        if len(nodelist) == 1 or nodelist[1][0] == token.COMMA:
            # set literal
            items = []
            for i in range(0, len(nodelist), 2):
                items.append(self.com_node(nodelist[i]))
            return Set(items, lineno=items[0].lineno)
        elif nodelist[1][0] == symbol.comp_for:
            # set comprehension
            expr = self.com_node(nodelist[0])
            return self.com_comprehension(expr, None, nodelist[1], 'set')
        elif len(nodelist) > 3 and nodelist[3][0] == symbol.comp_for:
            # dict comprehension
            assert nodelist[1][0] == token.COLON
            key = self.com_node(nodelist[0])
            value = self.com_node(nodelist[2])
            return self.com_comprehension(key, value, nodelist[3], 'dict')
        else:
            # dict literal
            items = []
            for i in range(0, len(nodelist), 4):
                items.append((self.com_node(nodelist[i]),
                              self.com_node(nodelist[i+2])))
            return Dict(items, lineno=items[0][0].lineno) 
Example #25
Source File: transformer.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def com_generator_expression(self, expr, node):
        # comp_iter: comp_for | comp_if
        # comp_for: 'for' exprlist 'in' test [comp_iter]
        # comp_if: 'if' test [comp_iter]

        lineno = node[1][2]
        fors = []
        while node:
            t = node[1][1]
            if t == 'for':
                assignNode = self.com_assign(node[2], OP_ASSIGN)
                genNode = self.com_node(node[4])
                newfor = GenExprFor(assignNode, genNode, [],
                                    lineno=node[1][2])
                fors.append(newfor)
                if (len(node)) == 5:
                    node = None
                else:
                    node = self.com_comp_iter(node[5])
            elif t == 'if':
                test = self.com_node(node[2])
                newif = GenExprIf(test, lineno=node[1][2])
                newfor.ifs.append(newif)
                if len(node) == 3:
                    node = None
                else:
                    node = self.com_comp_iter(node[3])
            else:
                raise SyntaxError, \
                        ("unexpected generator expression element: %s %d"
                         % (node, lineno))
        fors[0].is_outmost = True
        return GenExpr(GenExprInner(expr, fors), lineno=lineno)