Example #1
0
def build_single_input(builder, nb):
    atoms = get_atoms(builder, nb)
    l = len(atoms)
    if l == 1 or l==2:
        atom0 = atoms[0]
        if isinstance(atom0, TokenObject) and atom0.name == builder.parser.tokens['NEWLINE']:
            # atom0 = ast.Pass(atom0.lineno) # break test_astcompiler
            atom0 = ast.Stmt([], atom0.lineno) # break test_astbuilder
        elif not isinstance(atom0, ast.Stmt):
            atom0 = ast.Stmt([atom0], atom0.lineno)
        builder.push(ast.Module(builder.space.w_None, atom0, atom0.lineno))
    else:
        assert False, "Forbidden path"
Example #2
0
def build_suite(builder, nb):
    """suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT"""
    atoms = get_atoms(builder, nb)
    if len(atoms) == 1:
        builder.push(atoms[0])
    elif len(atoms) == 4:
        # Only one statement for (stmt+)
        stmt = atoms[2]
        if not isinstance(stmt, ast.Stmt):
            stmt = ast.Stmt([stmt], atoms[0].lineno)
        builder.push(stmt)
    else:
        # several statements
        stmts = []
        nodes = slicecut(atoms, 2, -1)
        for node in nodes:
            if isinstance(node, ast.Stmt):
                stmts.extend(node.nodes)
            else:
                stmts.append(node)
        builder.push(ast.Stmt(stmts, atoms[0].lineno))
Example #3
0
def build_simple_stmt(builder, nb):
    atoms = get_atoms(builder, nb)
    l = len(atoms)
    nodes = []
    if atoms:
        lineno = atoms[0].lineno
    else:
        lineno = -1
    for n in range(0,l,2):
        node = atoms[n]
        if isinstance(node, TokenObject) and node.name == builder.parser.tokens['NEWLINE']:
            nodes.append(ast.Discard(ast.Const(builder.wrap_none()), node.lineno))
        else:
            nodes.append(node)
    builder.push(ast.Stmt(nodes, lineno))
Example #4
0
def build_file_input(builder, nb):
    stmts = []
    atoms = get_atoms(builder, nb)
    if atoms:
        lineno = atoms[0].lineno
    else:
        lineno = -1
    for node in atoms:
        if isinstance(node, ast.Stmt):
            stmts.extend(node.nodes)
        elif isinstance(node, TokenObject) and node.name == builder.parser.tokens['ENDMARKER']:
            # XXX Can't we just remove the last element of the list ?
            break
        elif isinstance(node, TokenObject) and node.name == builder.parser.tokens['NEWLINE']:
            continue
        else:
            stmts.append(node)
    main_stmt = ast.Stmt(stmts, lineno)
    doc = get_docstring(builder,main_stmt)
    return builder.push(ast.Module(doc, main_stmt, lineno))