Search in sources :

Example 1 with If

use of org.python.pydev.parser.jython.ast.If in project Pydev by fabioz.

the class MakeAstValidForPrettyPrintingVisitor method visitIfPart.

private void visitIfPart(suiteType orelse, If node, boolean isElif) throws Exception {
    if (orelse != null) {
        fixNode(orelse);
    }
    fixNode(node);
    node.test.accept(this);
    if (node.body == null || node.body.length == 0) {
        node.body = new stmtType[] { new Pass() };
    }
    // write the body and dedent
    for (SimpleNode n : node.body) {
        n.accept(this);
    }
    if (orelse != null) {
        fixAfterNode(orelse);
    }
    if (node.orelse != null && ((Suite) node.orelse).body != null && ((Suite) node.orelse).body.length > 0) {
        stmtType[] body = ((Suite) node.orelse).body;
        if (body.length == 1 && body[0] instanceof If) {
            If if1 = (If) body[0];
            if (if1.test == null) {
                visitOrElsePart(node.orelse, "else");
            } else {
                boolean foundIf = false;
                if (if1.specialsBefore != null) {
                    for (Object o : if1.specialsBefore) {
                        if (o.toString().equals("if")) {
                            foundIf = true;
                            break;
                        }
                    }
                }
                if (foundIf) {
                    visitIfPart(node.orelse, if1, false);
                } else {
                    visitIfPart(node.orelse, if1, true);
                }
            }
        } else {
            visitOrElsePart(node.orelse, "else");
        }
    }
    fixAfterNode(node);
}
Also used : Suite(org.python.pydev.parser.jython.ast.Suite) Pass(org.python.pydev.parser.jython.ast.Pass) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) If(org.python.pydev.parser.jython.ast.If) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 2 with If

use of org.python.pydev.parser.jython.ast.If in project Pydev by fabioz.

the class PyParser25Test method testSuiteLineNumber.

public void testSuiteLineNumber() throws Exception {
    setDefaultVersion(IPythonNature.GRAMMAR_PYTHON_VERSION_2_5);
    String str = "" + "class Process:\n" + "\n" + "    def Foo(self):\n" + "        if a == 1:\n" + "            pass\n" + "        elif a == 1:\n" + "            pass\n" + "\n" + "";
    SimpleNode ast = parseLegalDocStr(str);
    stmtType[] body = ((Module) ast).body;
    assertEquals(1, body.length);
    ClassDef classFound = (ClassDef) body[0];
    body = classFound.body;
    assertEquals(1, body.length);
    FunctionDef func = (FunctionDef) body[0];
    If ifFound = (If) func.body[0];
    assertEquals(6, ifFound.orelse.beginLine);
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Module(org.python.pydev.parser.jython.ast.Module) If(org.python.pydev.parser.jython.ast.If) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 3 with If

use of org.python.pydev.parser.jython.ast.If in project Pydev by fabioz.

the class ASTEntry method getName.

public String getName() {
    if (name != null) {
        return name;
    }
    if (node instanceof ClassDef) {
        name = NodeUtils.getNameFromNameTok((NameTok) ((ClassDef) node).name);
    } else if (node instanceof FunctionDef) {
        name = NodeUtils.getNameFromNameTok((NameTok) ((FunctionDef) node).name);
    } else if (node instanceof Import) {
        aliasType[] names = ((Import) node).names;
        StringBuffer buffer = new StringBuffer("import ");
        for (int i = 0; i < names.length; i++) {
            buffer.append(((NameTok) names[i].name).id);
            if (names[i].asname != null) {
                buffer.append(" as ");
                buffer.append(((NameTok) names[i].asname).id);
            }
        }
        name = buffer.toString();
    } else if (node instanceof ImportFrom) {
        aliasType[] names = ((ImportFrom) node).names;
        StringBuffer buffer = new StringBuffer("from ");
        buffer.append(((NameTok) ((ImportFrom) node).module).id);
        buffer.append(" import ");
        if (names.length > 0) {
            for (int i = 0; i < names.length; i++) {
                buffer.append(((NameTok) names[i].name).id);
                if (names[i].asname != null) {
                    buffer.append(" as ");
                    buffer.append(((NameTok) names[i].asname).id);
                }
            }
        } else {
            buffer.append("*");
        }
        name = buffer.toString();
    } else if (node instanceof Attribute) {
        Attribute a = (Attribute) node;
        name = ((NameTok) a.attr).id;
    } else if (node instanceof Name) {
        Name a = (Name) node;
        name = a.id;
    } else if (node instanceof NameTok) {
        NameTok a = (NameTok) node;
        name = a.id;
    } else if (node instanceof Module) {
        name = "Module";
    } else if (node instanceof Str) {
        name = "Str";
    } else if (node instanceof While) {
        name = "While";
    } else if (node instanceof If) {
        name = "If";
    } else if (node instanceof For) {
        name = "For";
    } else if (node instanceof TryExcept) {
        name = "TryExcept";
    } else if (node instanceof TryFinally) {
        name = "TryFinally";
    } else if (node instanceof With) {
        name = "With";
    } else if (node instanceof commentType) {
        name = "comment";
    }
    if (name == null) {
        throw new RuntimeException("Unable to get node name: " + node);
    } else {
        return name;
    }
}
Also used : TryExcept(org.python.pydev.parser.jython.ast.TryExcept) Import(org.python.pydev.parser.jython.ast.Import) Attribute(org.python.pydev.parser.jython.ast.Attribute) For(org.python.pydev.parser.jython.ast.For) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) While(org.python.pydev.parser.jython.ast.While) Name(org.python.pydev.parser.jython.ast.Name) With(org.python.pydev.parser.jython.ast.With) Str(org.python.pydev.parser.jython.ast.Str) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) TryFinally(org.python.pydev.parser.jython.ast.TryFinally) org.python.pydev.parser.jython.ast.aliasType(org.python.pydev.parser.jython.ast.aliasType) ImportFrom(org.python.pydev.parser.jython.ast.ImportFrom) org.python.pydev.parser.jython.ast.commentType(org.python.pydev.parser.jython.ast.commentType) Module(org.python.pydev.parser.jython.ast.Module) If(org.python.pydev.parser.jython.ast.If) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 4 with If

use of org.python.pydev.parser.jython.ast.If in project Pydev by fabioz.

the class CodeFoldingVisitor method checkElse.

/**
 * Check if the passed if has an else... If it has, generate a 'fake' If entry for it (so that it's gotten later)
 */
private void checkElse(If entryIf, ASTEntry parentIf) {
    // treat elses
    if (entryIf.orelse != null && entryIf.orelse.body != null && entryIf.orelse.body.length > 0) {
        stmtType firstOrElseStmt = entryIf.orelse.body[0];
        if (!(firstOrElseStmt instanceof If) && firstOrElseStmt != null) {
            If generatedIf = new If(new BoolOp(BoolOp.And, new exprType[0]), new stmtType[0], new Suite(new stmtType[0]));
            generatedIf.beginLine = firstOrElseStmt.beginLine - 1;
            generatedIf.beginColumn = 1;
            ASTEntry generatedEntry = createEntry();
            generatedEntry.endLine = parentIf.endLine;
            parentIf.endLine = generatedIf.beginLine - 1;
            generatedEntry.node = generatedIf;
            if (generatedEntry.parent != null) {
                generatedEntry.parent = generatedEntry.parent.parent;
            }
            // actually go on and add the entry...
            super.doAddNode(generatedEntry);
        }
    }
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) Suite(org.python.pydev.parser.jython.ast.Suite) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) BoolOp(org.python.pydev.parser.jython.ast.BoolOp) If(org.python.pydev.parser.jython.ast.If)

Example 5 with If

use of org.python.pydev.parser.jython.ast.If in project Pydev by fabioz.

the class CodeFoldingVisitor method doAddNode.

@Override
protected void doAddNode(ASTEntry entry) {
    ASTEntry parent = entry.parent;
    if (entry.node instanceof If) {
        If entryIf = (If) entry.node;
        // treat elifs
        if (parent != null && parent.node instanceof If) {
            If parentIf = (If) parent.node;
            if (parentIf.orelse != null && parentIf.orelse.body != null && parentIf.orelse.body.length > 0 && parentIf.orelse.body[0] == entryIf) {
                parent.endLine = entry.node.beginLine - 1;
                if (entry.parent != null) {
                    entry.parent = entry.parent.parent;
                }
                super.doAddNode(entry);
                return;
            }
        }
    }
    super.doAddNode(entry);
}
Also used : If(org.python.pydev.parser.jython.ast.If)

Aggregations

If (org.python.pydev.parser.jython.ast.If)14 SimpleNode (org.python.pydev.parser.jython.SimpleNode)5 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)5 For (org.python.pydev.parser.jython.ast.For)5 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)5 Import (org.python.pydev.parser.jython.ast.Import)5 Module (org.python.pydev.parser.jython.ast.Module)5 Suite (org.python.pydev.parser.jython.ast.Suite)5 org.python.pydev.parser.jython.ast.stmtType (org.python.pydev.parser.jython.ast.stmtType)5 Attribute (org.python.pydev.parser.jython.ast.Attribute)4 ImportFrom (org.python.pydev.parser.jython.ast.ImportFrom)4 Name (org.python.pydev.parser.jython.ast.Name)4 NameTok (org.python.pydev.parser.jython.ast.NameTok)4 Str (org.python.pydev.parser.jython.ast.Str)4 ISpecialStr (org.python.pydev.parser.jython.ISpecialStr)3 Pass (org.python.pydev.parser.jython.ast.Pass)3 TryExcept (org.python.pydev.parser.jython.ast.TryExcept)3 TryFinally (org.python.pydev.parser.jython.ast.TryFinally)3 While (org.python.pydev.parser.jython.ast.While)3 With (org.python.pydev.parser.jython.ast.With)3