Search in sources :

Example 16 with FunctionDef

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

the class NodeHelper method getFirstLineConsideringDecorators.

public int getFirstLineConsideringDecorators(SimpleNode node) {
    decoratorsType[] decs = null;
    if (node instanceof FunctionDef) {
        FunctionDef functionDef = (FunctionDef) node;
        decs = functionDef.decs;
    } else if (node instanceof ClassDef) {
        ClassDef classDef = (ClassDef) node;
        decs = classDef.decs;
    }
    if (decs != null && decs.length > 0) {
        for (decoratorsType dec : decs) {
            return dec.beginLine;
        }
    }
    return node.beginLine;
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) org.python.pydev.parser.jython.ast.decoratorsType(org.python.pydev.parser.jython.ast.decoratorsType) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef)

Example 17 with FunctionDef

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

the class PyAstFactory method createSetterFunctionDef.

public FunctionDef createSetterFunctionDef(String accessorName, String attributeName) {
    NameTok functionName = new NameTok(accessorName, NameTok.FunctionName);
    argumentsType args = createArguments(true, "value");
    stmtType[] body = createSetterBody(attributeName);
    return new FunctionDef(functionName, args, body, null, null, false);
}
Also used : org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) org.python.pydev.parser.jython.ast.argumentsType(org.python.pydev.parser.jython.ast.argumentsType) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 18 with FunctionDef

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

the class PyAstFactory method createOverrideBody.

/**
 * @param functionDef the function for the override body
 * @param currentClassName
 */
public stmtType createOverrideBody(FunctionDef functionDef, String parentClassName, String currentClassName) {
    // create a copy because we do not want to retain the original line/col and we may change the originals here.
    final boolean[] addReturn = new boolean[] { false };
    VisitorBase visitor = new VisitorBase() {

        @Override
        public Object visitClassDef(ClassDef node) throws Exception {
            return null;
        }

        @Override
        public Object visitFunctionDef(FunctionDef node) throws Exception {
            // don't visit internal scopes.
            return null;
        }

        @Override
        protected Object unhandled_node(SimpleNode node) throws Exception {
            if (node instanceof Return) {
                addReturn[0] = true;
                throw stopVisitingException;
            }
            return null;
        }

        @Override
        public void traverse(SimpleNode node) throws Exception {
            node.traverse(this);
        }
    };
    try {
        visitor.traverse(functionDef);
    } catch (Exception e) {
        if (e != stopVisitingException) {
            Log.log(e);
        }
    }
    boolean isClassMethod = false;
    if (functionDef.decs != null) {
        for (decoratorsType dec : functionDef.decs) {
            String rep = NodeUtils.getRepresentationString(dec.func);
            if ("classmethod".equals(rep)) {
                isClassMethod = true;
                break;
            }
        }
    }
    argumentsType args = functionDef.args.createCopy(false);
    List<exprType> params = new ArrayList<exprType>();
    for (exprType expr : args.args) {
        // note: self should be there already!
        params.add(expr);
    }
    exprType starargs = args.vararg != null ? new Name(((NameTok) args.vararg).id, Name.Load, false) : null;
    exprType kwargs = args.kwarg != null ? new Name(((NameTok) args.kwarg).id, Name.Load, false) : null;
    List<keywordType> keywords = new ArrayList<keywordType>();
    if (args.defaults != null) {
        int diff = args.args.length - args.defaults.length;
        FastStack<Integer> removePositions = new FastStack<Integer>(args.defaults.length);
        for (int i = 0; i < args.defaults.length; i++) {
            exprType expr = args.defaults[i];
            if (expr != null) {
                exprType name = params.get(i + diff);
                if (name instanceof Name) {
                    // it's removed backwards, that's why it's a stack
                    removePositions.push(i + diff);
                    keywords.add(new keywordType(new NameTok(((Name) name).id, NameTok.KeywordName), name, false));
                } else {
                    Log.log("Expected: " + name + " to be a Name instance.");
                }
            }
        }
        while (removePositions.size() > 0) {
            Integer pop = removePositions.pop();
            params.remove((int) pop);
        }
    }
    Call call;
    if (isClassMethod && params.size() > 0) {
        // We need to use the super() construct
        // Something as:
        // Expr[value=
        // Call[func=
        // Attribute[value=
        // Call[func=Name[id=super, ctx=Load, reserved=false], args=[Name[id=Current, ctx=Load, reserved=false], Name[id=cls, ctx=Load, reserved=false]], keywords=[], starargs=null, kwargs=null],
        // attr=NameTok[id=test, ctx=Attrib], ctx=Load],
        // args=[], keywords=[], starargs=null, kwargs=null]
        // ]
        exprType firstParam = params.remove(0);
        Call innerCall = createCall("super", currentClassName, NodeUtils.getRepresentationString(firstParam));
        Attribute attr = new Attribute(innerCall, new NameTok(NodeUtils.getRepresentationString(functionDef), NameTok.Attrib), Attribute.Load);
        call = new Call(attr, params.toArray(new Name[params.size()]), keywords.toArray(new keywordType[keywords.size()]), starargs, kwargs);
    } else {
        call = createCall(parentClassName + "." + NodeUtils.getRepresentationString(functionDef), params, keywords.toArray(new keywordType[keywords.size()]), starargs, kwargs);
    }
    if (addReturn[0]) {
        return new Return(call);
    } else {
        return new Expr(call);
    }
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) FastStack(org.python.pydev.shared_core.structure.FastStack) Attribute(org.python.pydev.parser.jython.ast.Attribute) org.python.pydev.parser.jython.ast.decoratorsType(org.python.pydev.parser.jython.ast.decoratorsType) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Name(org.python.pydev.parser.jython.ast.Name) VisitorBase(org.python.pydev.parser.jython.ast.VisitorBase) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) org.python.pydev.parser.jython.ast.keywordType(org.python.pydev.parser.jython.ast.keywordType) org.python.pydev.parser.jython.ast.argumentsType(org.python.pydev.parser.jython.ast.argumentsType) Call(org.python.pydev.parser.jython.ast.Call) Return(org.python.pydev.parser.jython.ast.Return) Expr(org.python.pydev.parser.jython.ast.Expr) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 19 with FunctionDef

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

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

the class FastDefinitionsParserTest method testDefinitionsParser9.

public void testDefinitionsParser9() {
    Module m = (Module) FastDefinitionsParser.parse("class Bar(object):\n" + "    class \tZoo\t(object):\n" + "        def     m1(self):pass\n" + "        def m2(self):pass\n" + "            def m3(self):pass\n" + "def mGlobal(self):pass\n");
    assertEquals(2, m.body.length);
    ClassDef classDefBar = (ClassDef) m.body[0];
    assertEquals(1, classDefBar.beginColumn);
    assertEquals(1, classDefBar.beginLine);
    assertEquals("Bar", ((NameTok) classDefBar.name).id);
    assertEquals("mGlobal", ((NameTok) ((FunctionDef) m.body[1]).name).id);
    ClassDef classDefZoo = (ClassDef) classDefBar.body[0];
    assertEquals("Zoo", ((NameTok) classDefZoo.name).id);
    assertEquals(2, classDefZoo.body.length);
    assertEquals("m1", ((NameTok) ((FunctionDef) classDefZoo.body[0]).name).id);
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) 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