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