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);
}
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;
}
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]);
}
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;
}
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);
}
Aggregations