Search in sources :

Example 11 with FunctionDef

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

the class NodeUtils method getReturnTypeFromStaticTyping.

public static TypeInfo getReturnTypeFromStaticTyping(SimpleNode node) {
    if (node instanceof FunctionDef) {
        FunctionDef functionDef = (FunctionDef) node;
        exprType returns = functionDef.returns;
        if (returns != null) {
            return new TypeInfo(NodeUtils.extractOptionalValueSubscript(returns));
        }
    }
    return null;
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) ITypeInfo(org.python.pydev.core.ITypeInfo)

Example 12 with FunctionDef

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

the class NodeUtils method getNodeArgs.

/**
 * @param node a function definition (if other will return an empty string)
 * @return a string with the representation of the parameters of the function
 */
public static String getNodeArgs(SimpleNode node) {
    if (node instanceof ClassDef) {
        node = getClassDefInit((ClassDef) node);
    }
    if (node instanceof FunctionDef) {
        FunctionDef f = (FunctionDef) node;
        String startPar = "( ";
        FastStringBuffer buffer = new FastStringBuffer(startPar, 40);
        for (int i = 0; i < f.args.args.length; i++) {
            if (buffer.length() > startPar.length()) {
                buffer.append(", ");
            }
            buffer.append(getRepresentationString(f.args.args[i]));
        }
        buffer.append(" )");
        return buffer.toString();
    }
    return "";
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef)

Example 13 with FunctionDef

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

the class NodeUtils method getNodeDocStringNode.

public static Str getNodeDocStringNode(SimpleNode node) {
    Str s = null;
    stmtType[] body = null;
    if (node instanceof FunctionDef) {
        FunctionDef def = (FunctionDef) node;
        body = def.body;
    } else if (node instanceof ClassDef) {
        ClassDef def = (ClassDef) node;
        body = def.body;
    }
    if (body != null && body.length > 0) {
        if (body[0] instanceof Expr) {
            Expr e = (Expr) body[0];
            if (e.value instanceof Str) {
                s = (Str) e.value;
            }
        }
    }
    return s;
}
Also used : Str(org.python.pydev.parser.jython.ast.Str) ISpecialStr(org.python.pydev.parser.jython.ISpecialStr) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) Expr(org.python.pydev.parser.jython.ast.Expr) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef)

Example 14 with FunctionDef

use of org.python.pydev.parser.jython.ast.FunctionDef 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 15 with FunctionDef

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

the class PyParser39Test method testDecorator1.

public void testDecorator1() {
    SimpleNode node = parseLegalDocStr("buttons = []\n" + "\n" + "@buttons[0].clicked().connect()\n" + "def eggs():\n" + "    pass");
    assertTrue(node instanceof Module);
    Module m = (Module) node;
    assertEquals(2, m.body.length);
    assertTrue(m.body[0] instanceof Assign);
    Assign a = (Assign) m.body[0];
    assertTrue(a.value instanceof org.python.pydev.parser.jython.ast.List);
    assertTrue(m.body[1] instanceof FunctionDef);
    FunctionDef f = (FunctionDef) m.body[1];
    assertTrue(f.decs != null);
    assertEquals(1, f.decs.length);
    decoratorsType dec = f.decs[0];
    exprType it = dec.func;
    boolean valid = false;
    // i.e.; search for `buttons[0]` in decorator
    while (true) {
        if (it == null) {
            break;
        }
        if (it instanceof Attribute) {
            it = ((Attribute) it).value;
        } else if (it instanceof Call) {
            it = ((Call) it).func;
        } else if (it instanceof Subscript) {
            if ("buttons".equals(NodeUtils.getFullRepresentationString(it))) {
                valid = true;
            }
            break;
        } else {
            break;
        }
    }
    assertTrue(valid);
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) Call(org.python.pydev.parser.jython.ast.Call) Attribute(org.python.pydev.parser.jython.ast.Attribute) org.python.pydev.parser.jython.ast.decoratorsType(org.python.pydev.parser.jython.ast.decoratorsType) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Subscript(org.python.pydev.parser.jython.ast.Subscript) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Assign(org.python.pydev.parser.jython.ast.Assign) Module(org.python.pydev.parser.jython.ast.Module)

Aggregations

FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)137 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)63 Module (org.python.pydev.parser.jython.ast.Module)44 SimpleNode (org.python.pydev.parser.jython.SimpleNode)36 NameTok (org.python.pydev.parser.jython.ast.NameTok)30 ArrayList (java.util.ArrayList)27 org.python.pydev.parser.jython.ast.stmtType (org.python.pydev.parser.jython.ast.stmtType)27 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)26 Name (org.python.pydev.parser.jython.ast.Name)25 Assign (org.python.pydev.parser.jython.ast.Assign)17 org.python.pydev.parser.jython.ast.argumentsType (org.python.pydev.parser.jython.ast.argumentsType)17 Attribute (org.python.pydev.parser.jython.ast.Attribute)15 Call (org.python.pydev.parser.jython.ast.Call)14 SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)13 Expr (org.python.pydev.parser.jython.ast.Expr)13 Return (org.python.pydev.parser.jython.ast.Return)12 AdapterPrefs (org.python.pydev.parser.jython.ast.factory.AdapterPrefs)12 PyAstFactory (org.python.pydev.parser.jython.ast.factory.PyAstFactory)12 TokensList (org.python.pydev.core.TokensList)10 Yield (org.python.pydev.parser.jython.ast.Yield)10