Exemplo n.º 1
0
def build_atom(builder, nb):
    atoms = get_atoms(builder, nb)
    top = atoms[0]
    if isinstance(top, TokenObject):
        # assert isinstance(top, TokenObject) # rtyper
        if top.name == builder.parser.tokens['LPAR']:
            if len(atoms) == 2:
                builder.push(ast.Tuple([], top.lineno))
            else:
                builder.push(atoms[1])
        elif top.name == builder.parser.tokens['LSQB']:
            if len(atoms) == 2:
                builder.push(ast.List([], top.lineno))
            else:
                list_node = atoms[1]
                list_node.lineno = top.lineno
                builder.push(list_node)
        elif top.name == builder.parser.tokens['LBRACE']:
            items = []
            for index in range(1, len(atoms) - 1, 4):
                # a   :   b   ,   c : d
                # ^  +1  +2  +3  +4
                items.append((atoms[index], atoms[index + 2]))
            builder.push(ast.Dict(items, top.lineno))
        elif top.name == builder.parser.tokens['NAME']:
            val = top.get_value()
            builder.push(ast.Name(val, top.lineno))
        elif top.name == builder.parser.tokens['NUMBER']:
            builder.push(
                ast.Const(builder.eval_number(top.get_value()), top.lineno))
        elif top.name == builder.parser.tokens['STRING']:
            # need to concatenate strings in atoms
            s = ''
            if len(atoms) == 1:
                token = atoms[0]
                assert isinstance(token, TokenObject)
                builder.push(
                    ast.Const(
                        parsestr(builder.space, builder.source_encoding,
                                 token.get_value()), top.lineno))
            else:
                space = builder.space
                empty = space.wrap('')
                accum = []
                for token in atoms:
                    assert isinstance(token, TokenObject)
                    accum.append(
                        parsestr(builder.space, builder.source_encoding,
                                 token.get_value()))
                w_s = space.call_method(empty, 'join', space.newlist(accum))
                builder.push(ast.Const(w_s, top.lineno))
        elif top.name == builder.parser.tokens['BACKQUOTE']:
            builder.push(ast.Backquote(atoms[1], atoms[1].lineno))
        else:
            raise SyntaxError("unexpected tokens", top.lineno, top.col)
Exemplo n.º 2
0
def build_listmaker(builder, nb):
    """listmaker: test ( list_for | (',' test)* [','] )"""
    atoms = get_atoms(builder, nb)
    if len(atoms) >= 2:
        token = atoms[1]
        lineno = token.lineno
        if isinstance(token, TokenObject):
            if token.get_value() == 'for':
                # list comp
                expr = atoms[0]
                list_for = parse_listcomp(atoms[1:], builder)
                builder.push(ast.ListComp(expr, list_for, lineno))
                return
    # regular list building (like in [1, 2, 3,])
    index = 0
    nodes = []
    while index < len(atoms):
        nodes.append(atoms[index])
        index += 2  # skip comas
    if atoms:
        lineno = atoms[0].lineno
    else:
        lineno = -1
    builder.push(ast.List(nodes, lineno))
Exemplo n.º 3
0
 def visit_List(self, l):
     if l.ctx == ast.Load and l.elts:
         new_elts = self._optimize_constant_star_unpacks(l.elts)
         if new_elts:
             return ast.List(new_elts, ast.Load, l.lineno, l.col_offset)
     return l
Exemplo n.º 4
0
 def test_starred(self):
     left = ast.List(
         [ast.Starred(ast.Name("x", ast.Load, 0, 0), ast.Store, 0, 0)],
         ast.Store, 0, 0)
     assign = ast.Assign([left], ast.Num(self.space.wrap(4), 0, 0), 0, 0)
     self.stmt(assign, "must have Store context")