Search in sources :

Example 1 with Tuple

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

the class NodeUtils method getRepresentationString.

/**
 * @param node this is the node from whom we want to get the representation
 * @return A suitable String representation for some node.
 */
public static String getRepresentationString(SimpleNode node, boolean useTypeRepr) {
    if (node instanceof NameTok) {
        NameTok tok = (NameTok) node;
        return tok.id;
    }
    if (node instanceof Name) {
        Name name = (Name) node;
        return name.id;
    }
    if (node instanceof aliasType) {
        aliasType type = (aliasType) node;
        return ((NameTok) type.name).id;
    }
    if (node instanceof Attribute) {
        Attribute attribute = (Attribute) node;
        return discoverRep(attribute.attr);
    }
    if (node instanceof keywordType) {
        keywordType type = (keywordType) node;
        return discoverRep(type.arg);
    }
    if (node instanceof ClassDef) {
        ClassDef def = (ClassDef) node;
        return ((NameTok) def.name).id;
    }
    if (node instanceof FunctionDef) {
        FunctionDef def = (FunctionDef) node;
        return ((NameTok) def.name).id;
    }
    if (node instanceof Call) {
        Call call = ((Call) node);
        return getRepresentationString(call.func, useTypeRepr);
    }
    if (node instanceof org.python.pydev.parser.jython.ast.List || node instanceof ListComp) {
        String val = "[]";
        if (useTypeRepr) {
            val = getBuiltinType(val);
        }
        return val;
    }
    if (node instanceof org.python.pydev.parser.jython.ast.Dict || node instanceof org.python.pydev.parser.jython.ast.DictComp) {
        String val = "{}";
        if (useTypeRepr) {
            val = getBuiltinType(val);
        }
        return val;
    }
    if (node instanceof BinOp) {
        BinOp binOp = (BinOp) node;
        if (binOp.left instanceof Str && binOp.op == BinOp.Mod) {
            node = binOp.left;
        // Just change the node... the check below will work with the Str already.
        }
    }
    if (node instanceof Str) {
        String val;
        if (useTypeRepr) {
            val = getBuiltinType("''");
        } else {
            val = "'" + ((Str) node).s + "'";
        }
        return val;
    }
    if (node instanceof Tuple) {
        StringBuffer buf = new StringBuffer();
        Tuple t = (Tuple) node;
        for (exprType e : t.elts) {
            buf.append(getRepresentationString(e, useTypeRepr));
            buf.append(", ");
        }
        if (t.elts.length > 0) {
            int l = buf.length();
            buf.deleteCharAt(l - 1);
            buf.deleteCharAt(l - 2);
        }
        String val = "(" + buf + ")";
        if (useTypeRepr) {
            val = getBuiltinType(val);
        }
        return val;
    }
    if (node instanceof Num) {
        String val = ((Num) node).n.toString();
        if (useTypeRepr) {
            val = getBuiltinType(val);
        }
        return val;
    }
    if (node instanceof Import) {
        aliasType[] names = ((Import) node).names;
        for (aliasType n : names) {
            if (n.asname != null) {
                return ((NameTok) n.asname).id;
            }
            return ((NameTok) n.name).id;
        }
    }
    if (node instanceof commentType) {
        commentType type = (commentType) node;
        return type.id;
    }
    if (node instanceof excepthandlerType) {
        excepthandlerType type = (excepthandlerType) node;
        return type.name.toString();
    }
    return null;
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) Import(org.python.pydev.parser.jython.ast.Import) Attribute(org.python.pydev.parser.jython.ast.Attribute) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) DictComp(org.python.pydev.parser.jython.ast.DictComp) Name(org.python.pydev.parser.jython.ast.Name) Str(org.python.pydev.parser.jython.ast.Str) ISpecialStr(org.python.pydev.parser.jython.ISpecialStr) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) org.python.pydev.parser.jython.ast.commentType(org.python.pydev.parser.jython.ast.commentType) org.python.pydev.parser.jython.ast.keywordType(org.python.pydev.parser.jython.ast.keywordType) Call(org.python.pydev.parser.jython.ast.Call) Dict(org.python.pydev.parser.jython.ast.Dict) Num(org.python.pydev.parser.jython.ast.Num) ListComp(org.python.pydev.parser.jython.ast.ListComp) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) org.python.pydev.parser.jython.ast.aliasType(org.python.pydev.parser.jython.ast.aliasType) org.python.pydev.parser.jython.ast.excepthandlerType(org.python.pydev.parser.jython.ast.excepthandlerType) BinOp(org.python.pydev.parser.jython.ast.BinOp) NameTok(org.python.pydev.parser.jython.ast.NameTok) Tuple(org.python.pydev.parser.jython.ast.Tuple)

Example 2 with Tuple

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

the class AbstractTreeBuilderHelpers method makeTuple.

protected final SimpleNode makeTuple(SimpleNode n) throws ParseException {
    try {
        boolean endsWithComma = false;
        // it's not actually treated as a tuple, but as its only item.
        if (stack.nodeArity() > 0 && stack.peekNode().getId() == JJTCOMMA) {
            endsWithComma = true;
        }
        if (!endsWithComma && stack.nodeArity() == 1) {
            java.util.List<Object> tokenSourceSpecialTokensList = this.stack.getGrammar().getTokenSourceSpecialTokensList();
            for (Object object : tokenSourceSpecialTokensList) {
                if (object instanceof Object[]) {
                    Object[] objects = (Object[]) object;
                    object = objects[0];
                }
                if (object instanceof ISpecialStr) {
                    ISpecialStr specialStr = (ISpecialStr) object;
                    if (specialStr.toString().equals(",")) {
                        endsWithComma = true;
                        break;
                    }
                }
            }
        }
        final exprType[] exp = makeExprs();
        Tuple t = new Tuple(exp, Tuple.Load, endsWithComma);
        addSpecialsAndClearOriginal(n, t);
        return t;
    } catch (ClassCastException e) {
        if (e.getMessage().equals(ExtraArgValue.class.getName())) {
            this.stack.getGrammar().addAndReport(new ParseException("Token: '*' is not expected inside tuples.", lastPop), "Treated class cast exception on tuple");
        }
        this.stack.getGrammar().addAndReport(new ParseException("Syntax error while detecting tuple.", lastPop), "Treated class cast exception on tuple");
        while (stack.nodeArity() > 0) {
            // clear whatever we had in this construct...
            stack.popNode();
        }
        // recover properly!
        return new Tuple(new exprType[0], Tuple.Load, false);
    }
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) ParseException(org.python.pydev.parser.jython.ParseException) ISpecialStr(org.python.pydev.parser.jython.ISpecialStr) Tuple(org.python.pydev.parser.jython.ast.Tuple)

Example 3 with Tuple

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

the class ExtractCallEdit method initSubstituteCall.

private SimpleNode initSubstituteCall(Call methodCall) {
    if (returnVariables.size() == 0) {
        return methodCall;
    } else {
        List<exprType> returnExpr = new ArrayList<exprType>();
        for (String returnVar : returnVariables) {
            returnExpr.add(new Name(returnVar, Name.Store, false));
        }
        exprType[] expr = returnExpr.toArray(new exprType[0]);
        if (expr.length > 1) {
            expr = new exprType[] { new Tuple(expr, Tuple.Load, false) };
        }
        return new Assign(expr, methodCall, null);
    }
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) ArrayList(java.util.ArrayList) Assign(org.python.pydev.parser.jython.ast.Assign) Tuple(org.python.pydev.parser.jython.ast.Tuple) Name(org.python.pydev.parser.jython.ast.Name)

Example 4 with Tuple

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

the class AbstractScopeAnalyzerVisitor method visitNeededAttributeParts.

/**
 * In this function, the visitor will traverse the value of the attribute as needed,
 * if it is a subscript, call, etc, as those things are not actually a part of the attribute,
 * but are rather 'in' the attribute.
 *
 * @param node the attribute to visit
 * @param base the visitor that should visit the elements inside the attribute
 * @return true if there's no need to keep visiting other stuff in the attribute
 * @throws Exception
 */
public static boolean visitNeededAttributeParts(final Attribute node, VisitorBase base) throws Exception {
    exprType value = node.value;
    boolean valueVisited = false;
    boolean doReturn = false;
    if (value instanceof Subscript) {
        Subscript subs = (Subscript) value;
        base.traverse(subs.slice);
        if (subs.value instanceof Name) {
            base.visitName((Name) subs.value);
        } else {
            base.traverse(subs.value);
        }
        // No need to keep visiting. Reason:
        // Let's take the example:
        // print function()[0].strip()
        // function()[0] is part 1 of attribute
        // 
        // and the .strip will constitute the second part of the attribute
        // and its value (from the subscript) constitutes the 'function' part,
        // so, when we visit it directly, we don't have to visit the first part anymore,
        // because it was just visited... kind of strange to think about it though.
        doReturn = true;
    } else if (value instanceof Call) {
        visitCallAttr((Call) value, base);
        valueVisited = true;
    } else if (value instanceof Tuple) {
        base.visitTuple((Tuple) value);
        valueVisited = true;
    } else if (value instanceof Dict) {
        base.visitDict((Dict) value);
        doReturn = true;
    }
    if (!doReturn && !valueVisited) {
        if (visitNeededValues(value, base)) {
            doReturn = true;
        }
    }
    return doReturn;
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) Call(org.python.pydev.parser.jython.ast.Call) Dict(org.python.pydev.parser.jython.ast.Dict) Subscript(org.python.pydev.parser.jython.ast.Subscript) Tuple(org.python.pydev.parser.jython.ast.Tuple) Name(org.python.pydev.parser.jython.ast.Name)

Example 5 with Tuple

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

the class FindDefinitionModelVisitor method visitListComp.

@Override
public Object visitListComp(ListComp node) throws Exception {
    exprType elt = node.elt;
    elt = fixMissingAttribute(elt);
    if (this.line == elt.beginLine) {
        ILocalScope scope = new LocalScope(nature, this.defsStack);
        scope.setFoundAtASTNode(node);
        if (foundAsDefinition && !scope.equals(definitionFound.scope)) {
            // if it is found as a definition it is an 'exact' match, so, we do not keep checking it
            return super.visitListComp(node);
        }
        if (this.tokenToFind.equals(NodeUtils.getRepresentationString(elt))) {
            // Something as [a for a in [F(), C()]]
            if (node.generators != null && node.generators.length == 1) {
                comprehensionType comprehensionType = node.generators[0];
                if (comprehensionType instanceof Comprehension) {
                    Comprehension comprehension = (Comprehension) comprehensionType;
                    if (comprehension.iter != null) {
                        if (this.tokenToFind.equals(NodeUtils.getRepresentationString(comprehension.target))) {
                            exprType[] elts = NodeUtils.getEltsFromCompoundObject(comprehension.iter);
                            String rep = "";
                            if (elts != null && elts.length > 0) {
                                rep = NodeUtils.getRepresentationString(elts[0]);
                            }
                            ListCompDefinition definition = new ListCompDefinition(rep, this.tokenToFind, node, line, col, scope, module.get());
                            definitions.add(definition);
                        }
                    }
                }
            }
        } else if (elt instanceof Tuple || elt instanceof List) {
            // something as [(a, b) for (a, b) in [(F(), G()), ...]]
            exprType[] eltsFromCompoundObject = NodeUtils.getEltsFromCompoundObject(elt);
            if (eltsFromCompoundObject != null) {
                int length = eltsFromCompoundObject.length;
                for (int i = 0; i < length; i++) {
                    exprType eltFromCompound = fixMissingAttribute(eltsFromCompoundObject[i]);
                    if (this.tokenToFind.equals(NodeUtils.getRepresentationString(eltFromCompound))) {
                        if (node.generators != null && node.generators.length == 1) {
                            comprehensionType comprehensionType = node.generators[0];
                            if (comprehensionType instanceof Comprehension) {
                                Comprehension comprehension = (Comprehension) comprehensionType;
                                if (comprehension.iter != null) {
                                    exprType target = comprehension.target;
                                    if (target != null) {
                                        exprType[] targetElts = NodeUtils.getEltsFromCompoundObject(target);
                                        if (targetElts != null) {
                                            for (int j = 0; j < targetElts.length; j++) {
                                                exprType targetElt = targetElts[j];
                                                if (this.tokenToFind.equals(NodeUtils.getRepresentationString(targetElt))) {
                                                    exprType[] elts = NodeUtils.getEltsFromCompoundObject(comprehension.iter);
                                                    String rep = "";
                                                    if (elts != null && elts.length > j) {
                                                        rep = NodeUtils.getRepresentationString(elts[j]);
                                                    }
                                                    ListCompDefinition definition = new ListCompDefinition(rep, this.tokenToFind, node, line, col, scope, module.get());
                                                    definitions.add(definition);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return super.visitListComp(node);
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) ArrayList(java.util.ArrayList) List(java.util.List) Comprehension(org.python.pydev.parser.jython.ast.Comprehension) org.python.pydev.parser.jython.ast.comprehensionType(org.python.pydev.parser.jython.ast.comprehensionType) Tuple(org.python.pydev.parser.jython.ast.Tuple) ILocalScope(org.python.pydev.core.ILocalScope) ILocalScope(org.python.pydev.core.ILocalScope)

Aggregations

Tuple (org.python.pydev.parser.jython.ast.Tuple)15 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)13 Name (org.python.pydev.parser.jython.ast.Name)9 ArrayList (java.util.ArrayList)7 Attribute (org.python.pydev.parser.jython.ast.Attribute)6 Call (org.python.pydev.parser.jython.ast.Call)6 SimpleNode (org.python.pydev.parser.jython.SimpleNode)4 Dict (org.python.pydev.parser.jython.ast.Dict)4 Subscript (org.python.pydev.parser.jython.ast.Subscript)4 List (java.util.List)3 ISpecialStr (org.python.pydev.parser.jython.ISpecialStr)3 Assign (org.python.pydev.parser.jython.ast.Assign)3 DictComp (org.python.pydev.parser.jython.ast.DictComp)3 Expr (org.python.pydev.parser.jython.ast.Expr)3 ListComp (org.python.pydev.parser.jython.ast.ListComp)3 Str (org.python.pydev.parser.jython.ast.Str)3 HashSet (java.util.HashSet)2 ILocalScope (org.python.pydev.core.ILocalScope)2 ParseException (org.python.pydev.parser.jython.ParseException)2 Assert (org.python.pydev.parser.jython.ast.Assert)2