Search in sources :

Example 96 with FunctionDef

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

the class PyParser30Test method testYieldFrom.

public void testYieldFrom() {
    String s = "" + "def m1():\n" + "    yield from a" + "";
    Module ast = (Module) parseLegalDocStr(s);
    FunctionDef f = (FunctionDef) ast.body[0];
    Expr e = (Expr) f.body[0];
    Yield y = (Yield) e.value;
    assertTrue("Expected yield to be a yield from.", y.yield_from);
    parseLegalDocStrWithoutTree(s);
}
Also used : Expr(org.python.pydev.parser.jython.ast.Expr) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Yield(org.python.pydev.parser.jython.ast.Yield) Module(org.python.pydev.parser.jython.ast.Module)

Example 97 with FunctionDef

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

the class ClassDefAdapterFromTokens method getFunctionsInitFiltered.

@Override
public synchronized List<FunctionDefAdapter> getFunctionsInitFiltered() {
    if (cache == null) {
        cache = new ArrayList<FunctionDefAdapter>();
        for (IToken tok : this.tokens) {
            if (tok.getType() == IToken.TYPE_FUNCTION || tok.getType() == IToken.TYPE_BUILTIN || tok.getType() == IToken.TYPE_UNKNOWN) {
                String args = tok.getArgs();
                List<exprType> arguments = new ArrayList<exprType>();
                boolean useAnyArgs = false;
                if (args.length() > 0) {
                    StringTokenizer strTok = new StringTokenizer(args, "( ,)");
                    if (!strTok.hasMoreTokens()) {
                        useAnyArgs = true;
                    } else {
                        while (strTok.hasMoreTokens()) {
                            String nextArg = strTok.nextToken();
                            arguments.add(new Name(nextArg, Name.Load, false));
                        }
                    }
                } else {
                    useAnyArgs = true;
                }
                argumentsType functionArguments = new argumentsType(arguments.toArray(new exprType[0]), null, null, null, null, null, null, null, null, null);
                if (useAnyArgs) {
                    Name name = new Name("self", Name.Store, false);
                    name.addSpecial(new SpecialStr(",", -1, -1), true);
                    functionArguments.args = new exprType[] { name };
                    functionArguments.vararg = new NameTok("args", NameTok.VarArg);
                    functionArguments.kwarg = new NameTok("kwargs", NameTok.KwArg);
                }
                // System.out.println(tok.getRepresentation()+tok.getArgs());
                FunctionDef functionDef = new FunctionDef(new NameTok(tok.getRepresentation(), NameTok.FunctionName), functionArguments, null, null, null, false);
                cache.add(new FunctionDefAdapter(this.getModule(), null, functionDef, adapterPrefs));
            }
        }
    }
    return cache;
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) SpecialStr(org.python.pydev.parser.jython.SpecialStr) Name(org.python.pydev.parser.jython.ast.Name) StringTokenizer(java.util.StringTokenizer) IToken(org.python.pydev.core.IToken) org.python.pydev.parser.jython.ast.argumentsType(org.python.pydev.parser.jython.ast.argumentsType) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 98 with FunctionDef

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

the class FunctionDefAdapter method getNodeBodyIndent.

@Override
public String getNodeBodyIndent() {
    FunctionDef functionNode = getASTNode();
    if (functionNode.body == null || functionNode.body.length == 0) {
        PySelection pySelection = new PySelection(getModule().getDoc());
        String indentationFromLine = PySelection.getIndentationFromLine(pySelection.getLine(functionNode.beginLine - 1));
        return indentationFromLine + DefaultIndentPrefs.get(this.getAdapterPrefs().projectAdaptable).getIndentationString();
    }
    return getModule().getIndentationFromAst(functionNode.body[0]);
}
Also used : FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) PySelection(org.python.pydev.core.docutils.PySelection)

Example 99 with FunctionDef

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

the class ModuleAdapter method resolveImportedClass.

/**
 * This method fills the bases list (out) with asts for the methods that can be overridden.
 *
 * Still, compiled modules will not have an actual ast, but a list of tokens (that should be used
 * to know what should be overridden), so, this method should actually be changed so that
 * it works with tokens (that are resolved when a completion is requested), so, if we request a completion
 * for each base class, all the tokens from it will be returned, what's missing in this approach is that currently
 * the tokens returned don't have an associated context, so, after getting them, it may be hard to actually
 * tell the whole class structure above it (but this can be considered secondary for now).
 * @throws MisconfigurationException
 */
private Set<IClassDefAdapter> resolveImportedClass(Set<String> importedBase, CompletionCache completionCache) throws MisconfigurationException {
    Set<IClassDefAdapter> bases = new HashSet<IClassDefAdapter>();
    Set<ClassDef> alreadyTreated = new HashSet<ClassDef>();
    // let's create the module only once (this way the classdefs will be the same as reparses should not be needed).
    IModule module;
    try {
        module = AbstractASTManager.createModule(file, doc, nature);
    } catch (MisconfigurationException e1) {
        throw new RuntimeException(e1);
    }
    for (String baseName : importedBase) {
        ICompletionState state = new CompletionState(-1, -1, baseName, nature, "", completionCache);
        TokensList ret = null;
        try {
            ret = nature.getAstManager().getCompletionsForModule(module, state);
        } catch (CompletionRecursionException e) {
            throw new RuntimeException(e);
        }
        Map<String, List<IToken>> map = new HashMap<String, List<IToken>>();
        Set<ClassDef> classDefAsts = new HashSet<ClassDef>();
        for (IterTokenEntry entry : ret) {
            IToken tok = entry.getToken();
            if (tok instanceof SourceToken) {
                SourceToken token = (SourceToken) tok;
                SimpleNode ast = token.getAst();
                if (ast instanceof ClassDef || ast instanceof FunctionDef) {
                    if (ast.parent instanceof ClassDef) {
                        ClassDef classDefAst = (ClassDef) ast.parent;
                        if (!alreadyTreated.contains(classDefAst)) {
                            classDefAsts.add(classDefAst);
                            alreadyTreated.add(classDefAst);
                        }
                    }
                }
            } else if (tok instanceof CompiledToken) {
                CompiledToken token = (CompiledToken) tok;
                List<IToken> toks = map.get(token.getParentPackage());
                if (toks == null) {
                    toks = new ArrayList<IToken>();
                    map.put(token.getParentPackage(), toks);
                }
                toks.add(token);
            } else {
                throw new RuntimeException("Unexpected token:" + tok.getClass());
            }
        }
        for (Map.Entry<String, List<IToken>> entry : map.entrySet()) {
            // TODO: The module adapter should probably not be 'this' (make test to break it!)
            bases.add(new ClassDefAdapterFromTokens(this, entry.getKey(), entry.getValue(), getAdapterPrefs()));
        }
        for (ClassDef classDef : classDefAsts) {
            // TODO: The module adapter should probably not be 'this' (make test to break it!)
            bases.add(new ClassDefAdapterFromClassDef(this, classDef, getAdapterPrefs()));
        }
    }
    return bases;
}
Also used : IModule(org.python.pydev.core.IModule) HashMap(java.util.HashMap) MisconfigurationException(org.python.pydev.core.MisconfigurationException) ArrayList(java.util.ArrayList) ICompletionState(org.python.pydev.core.ICompletionState) CompletionState(org.python.pydev.ast.codecompletion.revisited.CompletionState) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) SimpleNode(org.python.pydev.parser.jython.SimpleNode) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) IToken(org.python.pydev.core.IToken) ICompletionState(org.python.pydev.core.ICompletionState) ArrayList(java.util.ArrayList) TokensList(org.python.pydev.core.TokensList) List(java.util.List) TokensList(org.python.pydev.core.TokensList) HashSet(java.util.HashSet) IterTokenEntry(org.python.pydev.core.IterTokenEntry) CompletionRecursionException(org.python.pydev.core.structure.CompletionRecursionException) CompiledToken(org.python.pydev.ast.codecompletion.revisited.modules.CompiledToken) HashMap(java.util.HashMap) Map(java.util.Map) SortedMap(java.util.SortedMap) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)

Example 100 with FunctionDef

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

the class ConstructorMethodEdit method getEditNode.

/**
 * Tricky :)
 * @throws MisconfigurationException
 */
@Override
protected SimpleNode getEditNode() throws MisconfigurationException {
    List<IClassDefAdapter> bases = classAdapter.getBaseClasses();
    // extractArguments
    NameTokType varArg = null;
    NameTokType kwArg = null;
    Set<String> argsNames = new LinkedHashSet<String>();
    for (IClassDefAdapter baseClass : bases) {
        FunctionDefAdapter init = baseClass.getFirstInit();
        if (init != null) {
            if (!init.getArguments().hasOnlySelf()) {
                argsNames.addAll(init.getArguments().getSelfFilteredArgNames());
            }
            if (varArg == null && init.getArguments().hasVarArg()) {
                varArg = new NameTok(ARGS, NameTok.VarArg);
            }
            if (kwArg == null && init.getArguments().hasKwArg()) {
                kwArg = new NameTok(KWARGS, NameTok.KwArg);
            }
        }
    }
    // addOwnArguments
    for (INodeAdapter adapter1 : attributes) {
        argsNames.add(nodeHelper.getPublicAttr(adapter1.getName()));
    }
    List<exprType> argsExprList = new ArrayList<exprType>();
    Name selfArg = new Name(NodeHelper.KEYWORD_SELF, Name.Param, false);
    argsExprList.add(selfArg);
    for (String parameter : argsNames) {
        argsExprList.add(new Name(parameter.trim(), Name.Param, false));
    }
    exprType[] argsExpr = argsExprList.toArray(new exprType[0]);
    argumentsType args = new argumentsType(argsExpr, varArg, kwArg, null, null, null, null, null, null, null);
    // constructorCalls
    List<stmtType> body = new ArrayList<stmtType>();
    for (IClassDefAdapter base : bases) {
        Expr init = extractConstructorInit(base);
        if (init != null) {
            body.add(init);
        }
    }
    // initAttributes
    for (INodeAdapter adapter : attributes) {
        exprType target = new Attribute(new Name(NodeHelper.KEYWORD_SELF, Name.Load, false), new NameTok(adapter.getName(), NameTok.Attrib), Attribute.Store);
        Assign initParam1 = new Assign(new exprType[] { target }, new Name(nodeHelper.getPublicAttr(adapter.getName()), Name.Load, false), null);
        Assign initParam = initParam1;
        body.add(initParam);
    }
    // create function def
    return new FunctionDef(new NameTok(NodeHelper.KEYWORD_INIT, NameTok.FunctionName), args, body.toArray(new stmtType[0]), null, null, false);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) INodeAdapter(org.python.pydev.refactoring.ast.adapters.INodeAdapter) NameTokType(org.python.pydev.parser.jython.ast.NameTokType) Attribute(org.python.pydev.parser.jython.ast.Attribute) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Name(org.python.pydev.parser.jython.ast.Name) Expr(org.python.pydev.parser.jython.ast.Expr) FunctionDefAdapter(org.python.pydev.refactoring.ast.adapters.FunctionDefAdapter) Assign(org.python.pydev.parser.jython.ast.Assign) org.python.pydev.parser.jython.ast.argumentsType(org.python.pydev.parser.jython.ast.argumentsType) IClassDefAdapter(org.python.pydev.refactoring.ast.adapters.IClassDefAdapter) NameTok(org.python.pydev.parser.jython.ast.NameTok)

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