Search in sources :

Example 1 with BoolOp

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

the class CodeFoldingVisitor method checkElse.

/**
 * Check if the passed if has an else... If it has, generate a 'fake' If entry for it (so that it's gotten later)
 */
private void checkElse(If entryIf, ASTEntry parentIf) {
    // treat elses
    if (entryIf.orelse != null && entryIf.orelse.body != null && entryIf.orelse.body.length > 0) {
        stmtType firstOrElseStmt = entryIf.orelse.body[0];
        if (!(firstOrElseStmt instanceof If) && firstOrElseStmt != null) {
            If generatedIf = new If(new BoolOp(BoolOp.And, new exprType[0]), new stmtType[0], new Suite(new stmtType[0]));
            generatedIf.beginLine = firstOrElseStmt.beginLine - 1;
            generatedIf.beginColumn = 1;
            ASTEntry generatedEntry = createEntry();
            generatedEntry.endLine = parentIf.endLine;
            parentIf.endLine = generatedIf.beginLine - 1;
            generatedEntry.node = generatedIf;
            if (generatedEntry.parent != null) {
                generatedEntry.parent = generatedEntry.parent.parent;
            }
            // actually go on and add the entry...
            super.doAddNode(generatedEntry);
        }
    }
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) Suite(org.python.pydev.parser.jython.ast.Suite) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) BoolOp(org.python.pydev.parser.jython.ast.BoolOp) If(org.python.pydev.parser.jython.ast.If)

Example 2 with BoolOp

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

the class AbstractTreeBuilder method closeNode.

/**
 * Subclasses must implement this method to deal with any node that's not properly handled in this base.
 * @param n the node that should be closed
 * @param arity the current number of nodes in the stack (found after the context was opened)
 * @return a new node representing the node that's having it's context closed.
 * @throws Exception
 */
@Override
public final SimpleNode closeNode(final SimpleNode n, final int arity) throws Exception {
    exprType value;
    suiteType orelseSuite;
    stmtType[] body;
    exprType iter;
    exprType target;
    if (DEBUG_TREE_BUILDER) {
        System.out.println("\n\n\n---------------------------");
        System.out.println("Closing node scope: " + n);
        System.out.println("Arity: " + arity);
        if (arity > 0) {
            System.out.println("Nodes in scope: ");
            for (int i = 0; i < arity; i++) {
                System.out.println(stack.peekNode(i));
            }
        }
    }
    exprType[] exprs;
    switch(n.getId()) {
        case -1:
            throw new ParseException("Illegal node found: " + n, n);
        case JJTFILE_INPUT:
            Module m = (Module) n;
            m.body = makeStmts(arity);
            return m;
        case JJTFALSE:
        case JJTTRUE:
        case JJTNONE:
        case JJTNAME:
        case JJTNUM:
        case JJTPASS_STMT:
        case JJTBREAK_STMT:
        case JJTCONTINUE_STMT:
        case JJTSTRING:
        case JJTUNICODE:
        case JJTBINARY:
        case JJTFSTRING:
        case JJTBEGIN_DECORATOR:
        case JJTCOMMA:
        case JJTCOLON:
            // it's already the correct node (and it's value is already properly set)
            return n;
        case JJTSUITE:
            stmtType[] stmts = new stmtType[arity];
            for (int i = arity - 1; i >= 0; i--) {
                SimpleNode yield_or_stmt = stack.popNode();
                if (yield_or_stmt instanceof Yield) {
                    stmts[i] = new Expr((Yield) yield_or_stmt);
                } else {
                    try {
                        stmts[i] = (stmtType) yield_or_stmt;
                    } catch (ClassCastException e) {
                        recoverFromClassCastException(yield_or_stmt, e);
                        // recover from it with a valid node!
                        stmts[i] = new Pass();
                    }
                }
            }
            return new Suite(stmts);
        case JJTFOR_STMT:
            orelseSuite = null;
            if (stack.nodeArity() == 5) {
                orelseSuite = popSuiteAndSuiteType();
            }
            body = popSuite();
            iter = (exprType) stack.popNode();
            target = (exprType) stack.popNode();
            ctx.setStore(target);
            For forStmt = (For) n;
            forStmt.target = target;
            forStmt.iter = iter;
            forStmt.body = body;
            forStmt.orelse = orelseSuite;
            return forStmt;
        case JJTBEGIN_ELIF_STMT:
            return new If(null, null, null);
        case JJTIF_STMT:
            return handleIfConstruct(n, arity);
        case JJTEXEC_STMT:
            exprType locals = arity >= 3 ? ((exprType) stack.popNode()) : null;
            exprType globals = arity >= 2 ? ((exprType) stack.popNode()) : null;
            value = (exprType) stack.popNode();
            Exec exec = (Exec) n;
            exec.body = value;
            exec.locals = locals;
            exec.globals = globals;
            return exec;
        case JJTDECORATORS:
            ArrayList<SimpleNode> list2 = new ArrayList<SimpleNode>();
            ArrayList<SimpleNode> listArgs = new ArrayList<SimpleNode>();
            while (stack.nodeArity() > 0) {
                SimpleNode node = stack.popNode();
                while (!(node instanceof decoratorsType)) {
                    if (node instanceof comprehensionType) {
                        listArgs.add(node);
                        // target
                        listArgs.add(stack.popNode());
                    } else if (node instanceof ComprehensionCollection) {
                        listArgs.add(((ComprehensionCollection) node).getGenerators()[0]);
                        // target
                        listArgs.add(stack.popNode());
                    } else {
                        listArgs.add(node);
                    }
                    node = stack.popNode();
                }
                // the decoratorsType
                listArgs.add(node);
                list2.add(0, makeDecorator(listArgs));
                listArgs.clear();
            }
            return new Decorators(list2.toArray(new decoratorsType[0]), JJTDECORATORS);
        case JJTSUBSCRIPTLIST:
            sliceType[] dims = new sliceType[arity];
            for (int i = arity - 1; i >= 0; i--) {
                SimpleNode sliceNode = stack.popNode();
                if (sliceNode instanceof sliceType) {
                    dims[i] = (sliceType) sliceNode;
                } else if (sliceNode instanceof IdentityNode) {
                // this should be ignored...
                // this happens when parsing something like a[1,], whereas a[1,2] would not have this.
                } else {
                    throw new RuntimeException("Expected a sliceType or an IdentityNode. Received :" + sliceNode.getClass());
                }
            }
            return new ExtSlice(dims);
        case JJTAUG_PLUS:
        case JJTAUG_MINUS:
        case JJTAUG_MULTIPLY:
        case JJTAUG_DOT:
        case JJTAUG_DIVIDE:
        case JJTAUG_MODULO:
        case JJTAUG_AND:
        case JJTAUG_OR:
        case JJTAUG_XOR:
        case JJTAUG_LSHIFT:
        case JJTAUG_RSHIFT:
        case JJTAUG_POWER:
        case JJTAUG_FLOORDIVIDE:
            AugAssign augAssign = (AugAssign) n;
            exprType value1 = (exprType) stack.popNode();
            exprType target1 = (exprType) stack.popNode();
            ctx.setAugStore(target1);
            augAssign.target = target1;
            augAssign.value = value1;
            return n;
        case JJTOR_BOOLEAN:
            return new BoolOp(BoolOp.Or, makeExprs());
        case JJTAND_BOOLEAN:
            return new BoolOp(BoolOp.And, makeExprs());
        case JJTCOMPARISION:
            if (arity <= 2) {
                throw new ParseException("Internal error: To make a compare, at least 3 nodes are needed.", n);
            }
            int l = arity / 2;
            exprType[] comparators = new exprType[l];
            int[] ops = new int[l];
            for (int i = l - 1; i >= 0; i--) {
                comparators[i] = (exprType) stack.popNode();
                SimpleNode op = stack.popNode();
                switch(op.getId()) {
                    case JJTLESS_CMP:
                        ops[i] = Compare.Lt;
                        break;
                    case JJTGREATER_CMP:
                        ops[i] = Compare.Gt;
                        break;
                    case JJTEQUAL_CMP:
                        ops[i] = Compare.Eq;
                        break;
                    case JJTGREATER_EQUAL_CMP:
                        ops[i] = Compare.GtE;
                        break;
                    case JJTLESS_EQUAL_CMP:
                        ops[i] = Compare.LtE;
                        break;
                    case JJTNOTEQUAL_CMP:
                        ops[i] = Compare.NotEq;
                        break;
                    case JJTIN_CMP:
                        ops[i] = Compare.In;
                        break;
                    case JJTNOT_IN_CMP:
                        ops[i] = Compare.NotIn;
                        break;
                    case JJTIS_NOT_CMP:
                        ops[i] = Compare.IsNot;
                        break;
                    case JJTIS_CMP:
                        ops[i] = Compare.Is;
                        break;
                    default:
                        throw new RuntimeException("Unknown cmp op:" + op.getId());
                }
            }
            return new Compare(((exprType) stack.popNode()), ops, comparators);
        case JJTLESS_CMP:
        case JJTGREATER_CMP:
        case JJTEQUAL_CMP:
        case JJTGREATER_EQUAL_CMP:
        case JJTLESS_EQUAL_CMP:
        case JJTNOTEQUAL_CMP:
        case JJTIN_CMP:
        case JJTNOT_IN_CMP:
        case JJTIS_NOT_CMP:
        case JJTIS_CMP:
            return n;
        case JJTOR_2OP:
        case JJTXOR_2OP:
        case JJTAND_2OP:
        case JJTLSHIFT_2OP:
        case JJTRSHIFT_2OP:
        case JJTADD_2OP:
        case JJTSUB_2OP:
        case JJTMUL_2OP:
        case JJTDOT_2OP:
        case JJTDIV_2OP:
        case JJTMOD_2OP:
        case JJTPOW_2OP:
        case JJTFLOORDIV_2OP:
            BinOp op = (BinOp) n;
            exprType right = (exprType) stack.popNode();
            exprType left = (exprType) stack.popNode();
            op.right = right;
            op.left = left;
            return n;
        case JJTPOS_1OP:
        case JJTNEG_1OP:
        case JJTINVERT_1OP:
        case JJTNOT_1OP:
            ((UnaryOp) n).operand = ((exprType) stack.popNode());
            return n;
        case JJTIMPORT:
            ((Import) n).names = makeAliases(arity);
            return n;
        case JJTDOT_OP:
            NameTok attr = makeNameTok(NameTok.Attrib);
            value = (exprType) stack.popNode();
            Attribute attribute = (Attribute) n;
            attribute.value = value;
            attribute.attr = attr;
            return n;
        case JJTBEGIN_DEL_STMT:
            return new Delete(null);
        case JJTDEL_STMT:
            exprs = makeExprs(arity - 1);
            ctx.setDelete(exprs);
            Delete d = (Delete) stack.popNode();
            d.targets = exprs;
            return d;
        case JJTDOTTED_NAME:
            Name name = (Name) n;
            FastStringBuffer sb = tempBuffer.clear();
            for (int i = 0; i < arity; i++) {
                if (i > 0) {
                    sb.insert(0, '.');
                }
                Name name0 = (Name) stack.popNode();
                sb.insert(0, name0.id);
                addSpecials(name0, name);
                // we have to set that, because if we later add things to the previous Name, we will now want it to be added to
                // the new name (comments will only appear later and may be added to the previous name -- so, we replace the previous
                // name specials list).
                name0.specialsBefore = name.getSpecialsBefore();
                name0.specialsAfter = name.getSpecialsAfter();
            }
            name.id = sb.toString();
            return name;
        case JJTDOTTED_AS_NAME:
            NameTok asname = null;
            if (arity > 1) {
                asname = makeNameTok(NameTok.ImportName);
            }
            return new aliasType(makeNameTok(NameTok.ImportName), asname);
        case JJTIMPORT_AS_NAME:
            asname = null;
            if (arity > 1) {
                asname = makeNameTok(NameTok.ImportName);
            }
            return new aliasType(makeNameTok(NameTok.ImportName), asname);
        case JJTSTRJOIN:
            Str str2 = (Str) stack.popNode();
            Object o = stack.popNode();
            StrJoin ret;
            if (o instanceof Str) {
                Str str1 = (Str) o;
                ret = new StrJoin(new exprType[] { str1, str2 });
            } else {
                StrJoin strJ = (StrJoin) o;
                exprType[] newStrs = new exprType[strJ.strs.length + 1];
                System.arraycopy(strJ.strs, 0, newStrs, 0, strJ.strs.length);
                newStrs[strJ.strs.length] = str2;
                strJ.strs = newStrs;
                ret = strJ;
            }
            ret.beginLine = ret.strs[0].beginLine;
            ret.beginColumn = ret.strs[0].beginColumn;
            return ret;
    }
    // if we found a node not expected in the base, let's give subclasses an opportunity for dealing with it.
    return onCloseNode(n, arity);
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) Delete(org.python.pydev.parser.jython.ast.Delete) ExtSlice(org.python.pydev.parser.jython.ast.ExtSlice) AugAssign(org.python.pydev.parser.jython.ast.AugAssign) 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) org.python.pydev.parser.jython.ast.comprehensionType(org.python.pydev.parser.jython.ast.comprehensionType) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Name(org.python.pydev.parser.jython.ast.Name) Suite(org.python.pydev.parser.jython.ast.Suite) Exec(org.python.pydev.parser.jython.ast.Exec) Str(org.python.pydev.parser.jython.ast.Str) ISpecialStr(org.python.pydev.parser.jython.ISpecialStr) Pass(org.python.pydev.parser.jython.ast.Pass) Compare(org.python.pydev.parser.jython.ast.Compare) StrJoin(org.python.pydev.parser.jython.ast.StrJoin) FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) For(org.python.pydev.parser.jython.ast.For) org.python.pydev.parser.jython.ast.suiteType(org.python.pydev.parser.jython.ast.suiteType) Expr(org.python.pydev.parser.jython.ast.Expr) NamedExpr(org.python.pydev.parser.jython.ast.NamedExpr) org.python.pydev.parser.jython.ast.aliasType(org.python.pydev.parser.jython.ast.aliasType) Yield(org.python.pydev.parser.jython.ast.Yield) ParseException(org.python.pydev.parser.jython.ParseException) BoolOp(org.python.pydev.parser.jython.ast.BoolOp) BinOp(org.python.pydev.parser.jython.ast.BinOp) Module(org.python.pydev.parser.jython.ast.Module) If(org.python.pydev.parser.jython.ast.If) org.python.pydev.parser.jython.ast.sliceType(org.python.pydev.parser.jython.ast.sliceType) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Aggregations

BoolOp (org.python.pydev.parser.jython.ast.BoolOp)2 If (org.python.pydev.parser.jython.ast.If)2 Suite (org.python.pydev.parser.jython.ast.Suite)2 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)2 org.python.pydev.parser.jython.ast.stmtType (org.python.pydev.parser.jython.ast.stmtType)2 ArrayList (java.util.ArrayList)1 ISpecialStr (org.python.pydev.parser.jython.ISpecialStr)1 ParseException (org.python.pydev.parser.jython.ParseException)1 SimpleNode (org.python.pydev.parser.jython.SimpleNode)1 Attribute (org.python.pydev.parser.jython.ast.Attribute)1 AugAssign (org.python.pydev.parser.jython.ast.AugAssign)1 BinOp (org.python.pydev.parser.jython.ast.BinOp)1 Compare (org.python.pydev.parser.jython.ast.Compare)1 Delete (org.python.pydev.parser.jython.ast.Delete)1 Exec (org.python.pydev.parser.jython.ast.Exec)1 Expr (org.python.pydev.parser.jython.ast.Expr)1 ExtSlice (org.python.pydev.parser.jython.ast.ExtSlice)1 For (org.python.pydev.parser.jython.ast.For)1 Module (org.python.pydev.parser.jython.ast.Module)1 Name (org.python.pydev.parser.jython.ast.Name)1