Search in sources :

Example 1 with ComprehensionCollection

use of org.python.pydev.parser.grammarcommon.ComprehensionCollection in project Pydev by fabioz.

the class TreeBuilder27 method onCloseNode.

@Override
public final SimpleNode onCloseNode(SimpleNode n, int arity) throws Exception {
    exprType value;
    exprType[] exprs;
    Suite orelseSuite;
    stmtType[] body;
    Suite suite;
    int l;
    switch(n.getId()) {
        case JJTEXPR_STMT:
            value = (exprType) stack.popNode();
            if (arity > 1) {
                exprs = makeExprs(arity - 1);
                ctx.setStore(exprs);
                return new Assign(exprs, value, null);
            } else {
                return new Expr(value);
            }
        case JJTINDEX_OP:
            sliceType slice = (sliceType) stack.popNode();
            value = (exprType) stack.popNode();
            return new Subscript(value, slice, Subscript.Load);
        case JJTPRINT_STMT:
            boolean nl = true;
            if (stack.nodeArity() == 0) {
                Print p = new Print(null, null, true);
                return p;
            }
            if (stack.peekNode().getId() == JJTCOMMA) {
                stack.popNode();
                nl = false;
            }
            Print p = new Print(null, makeExprs(), nl);
            return p;
        case JJTPRINTEXT_STMT:
            nl = true;
            if (stack.peekNode().getId() == JJTCOMMA) {
                stack.popNode();
                nl = false;
            }
            exprs = makeExprs(stack.nodeArity() - 1);
            p = new Print(((exprType) stack.popNode()), exprs, nl);
            return p;
        case JJTBEGIN_FOR_ELSE_STMT:
            return new Suite(null);
        case JJTBEGIN_ELSE_STMT:
            return new Suite(null);
        case JJTBEGIN_WHILE_STMT:
            return new While(null, null, null);
        case JJTWHILE_STMT:
            orelseSuite = null;
            if (stack.nodeArity() == 5) {
                orelseSuite = popSuiteAndSuiteType();
            }
            body = popSuite();
            exprType test = (exprType) stack.popNode();
            While w = (While) stack.popNode();
            w.test = test;
            w.body = body;
            w.orelse = orelseSuite;
            return w;
        case JJTDECORATED:
            if (stack.nodeArity() != 2) {
                throw new RuntimeException("Expected 2 nodes at this context, found: " + arity);
            }
            SimpleNode def = stack.popNode();
            Decorators decorators = (Decorators) stack.popNode();
            if (def instanceof ClassDef) {
                ClassDef classDef = (ClassDef) def;
                classDef.decs = decorators.exp;
            } else {
                FunctionDef fDef = (FunctionDef) def;
                fDef.decs = decorators.exp;
            }
            return def;
        case JJTCALL_OP:
            exprType starargs = null;
            exprType kwargs = null;
            java.util.List<exprType> args = new ArrayList<exprType>();
            java.util.List<keywordType> keywords = new ArrayList<keywordType>();
            for (int i = arity - 2; i >= 0; i--) {
                SimpleNode node = stack.popNode();
                if (node instanceof keywordType) {
                    keywords.add(0, (keywordType) node);
                } else if (node.getId() == JJTEXTRAARGVALUELIST) {
                    ExtraArgValue nstarargs = (ExtraArgValue) node;
                    starargs = nstarargs.value;
                    this.addSpecialsAndClearOriginal(nstarargs, starargs);
                } else if (node.getId() == JJTEXTRAKEYWORDVALUELIST) {
                    ExtraArgValue nkwargs = (ExtraArgValue) node;
                    kwargs = nkwargs.value;
                    this.addSpecialsAndClearOriginal(nkwargs, kwargs);
                } else if (node instanceof ComprehensionCollection) {
                    // what can happen is something like print sum(x for x in y), where we have already passed x in the args, and then get 'for x in y'
                    args.add(0, new ListComp((exprType) stack.popNode(), ((ComprehensionCollection) node).getGenerators(), ListComp.EmptyCtx));
                    // popped node
                    i--;
                } else {
                    args.add(0, (exprType) node);
                }
            }
            exprType func = (exprType) stack.popNode();
            Call c = new Call(func, args.toArray(new exprType[args.size()]), keywords.toArray(new keywordType[keywords.size()]), starargs, kwargs);
            addSpecialsAndClearOriginal(n, c);
            return c;
        case JJTFUNCDEF:
            suite = (Suite) stack.popNode();
            body = suite.body;
            argumentsType arguments = makeArguments(stack.nodeArity() - 1);
            NameTok nameTok = makeNameTok(NameTok.FunctionName);
            // decorator is always null at this point... it's decorated later on
            FunctionDef funcDef = new FunctionDef(nameTok, arguments, body, null, null, false);
            addSpecialsAndClearOriginal(suite, funcDef);
            setParentForFuncOrClass(body, funcDef);
            return funcDef;
        case JJTDEFAULTARG:
            value = (arity == 1) ? null : ((exprType) stack.popNode());
            return new DefaultArg(((exprType) stack.popNode()), value, n.getId());
        case JJTEXTRAARGLIST:
            return new ExtraArg(makeNameTok(NameTok.VarArg), JJTEXTRAARGLIST);
        case JJTEXTRAKEYWORDLIST:
            return new ExtraArg(makeNameTok(NameTok.KwArg), JJTEXTRAKEYWORDLIST);
        case JJTCLASSDEF:
            suite = (Suite) stack.popNode();
            body = suite.body;
            exprType[] bases = makeExprs(stack.nodeArity() - 1);
            nameTok = makeNameTok(NameTok.ClassName);
            ClassDef classDef = new ClassDef(nameTok, bases, body, null, null, null, null);
            addSpecialsAndClearOriginal(suite, classDef);
            setParentForFuncOrClass(body, classDef);
            return classDef;
        case JJTBEGIN_RETURN_STMT:
            return new Return(null);
        case JJTRETURN_STMT:
            value = arity == 2 ? ((exprType) stack.popNode()) : null;
            Return ret = (Return) stack.popNode();
            ret.value = value;
            return ret;
        case JJTYIELD_STMT:
            return stack.popNode();
        case JJTYIELD_EXPR:
            exprType yieldExpr = null;
            if (arity > 0) {
                // we may have an empty yield, so, we have to check it before
                yieldExpr = (exprType) stack.popNode();
            }
            return new Yield(yieldExpr, false);
        case JJTRAISE_STMT:
            exprType tback = arity >= 3 ? ((exprType) stack.popNode()) : null;
            exprType inst = arity >= 2 ? ((exprType) stack.popNode()) : null;
            exprType type = arity >= 1 ? ((exprType) stack.popNode()) : null;
            return new Raise(type, inst, tback, null);
        case JJTGLOBAL_STMT:
            Global global = new Global(makeIdentifiers(NameTok.GlobalName), null);
            return global;
        case JJTASSERT_STMT:
            exprType msg = arity == 2 ? ((exprType) stack.popNode()) : null;
            test = (exprType) stack.popNode();
            return new Assert(test, msg);
        case JJTBEGIN_TRY_STMT:
            // we do that just to get the specials
            return new TryExcept(null, null, null);
        case JJTTRYELSE_STMT:
            orelseSuite = popSuiteAndSuiteType();
            return orelseSuite;
        case JJTTRYFINALLY_OUTER_STMT:
            orelseSuite = popSuiteAndSuiteType();
            // it does not have a body at this time... it will be filled with the inner try..except
            return new TryFinally(null, orelseSuite);
        case JJTTRY_STMT:
            TryFinally outer = null;
            if (stack.peekNode() instanceof TryFinally) {
                outer = (TryFinally) stack.popNode();
                arity--;
            }
            orelseSuite = null;
            if (stack.peekNode() instanceof suiteType) {
                orelseSuite = (Suite) stack.popNode();
                arity--;
            }
            l = arity;
            excepthandlerType[] handlers = new excepthandlerType[l];
            for (int i = l - 1; i >= 0; i--) {
                handlers[i] = (excepthandlerType) stack.popNode();
            }
            suite = (Suite) stack.popNode();
            TryExcept tryExc = (TryExcept) stack.popNode();
            if (outer != null) {
                outer.beginLine = tryExc.beginLine;
            }
            tryExc.body = suite.body;
            tryExc.handlers = handlers;
            tryExc.orelse = orelseSuite;
            addSpecials(suite, tryExc);
            if (outer == null) {
                return tryExc;
            } else {
                if (outer.body != null) {
                    throw new RuntimeException("Error. Expecting null body to be filled on try..except..finally");
                }
                outer.body = new stmtType[] { tryExc };
                return outer;
            }
        case JJTBEGIN_TRY_ELSE_STMT:
            // we do that just to get the specials
            return new Suite(null);
        case JJTBEGIN_EXCEPT_CLAUSE:
            return new excepthandlerType(null, null, null);
        case JJTEXCEPT_CLAUSE:
            suite = (Suite) stack.popNode();
            body = suite.body;
            exprType excname = arity == 4 ? ((exprType) stack.popNode()) : null;
            if (excname != null) {
                ctx.setStore(excname);
            }
            type = arity >= 3 ? ((exprType) stack.popNode()) : null;
            excepthandlerType handler = (excepthandlerType) stack.popNode();
            handler.type = type;
            handler.name = excname;
            handler.body = body;
            addSpecials(suite, handler);
            return handler;
        case JJTBEGIN_FINALLY_STMT:
            // we do that just to get the specials
            return new Suite(null);
        case JJTTRYFINALLY_STMT:
            suiteType finalBody = popSuiteAndSuiteType();
            body = popSuite();
            // We have a try..except in the stack, but we will change it for a try..finally
            // This is because we recognize a try..except in the 'try:' token, but actually end up with a try..finally
            TryExcept tryExcept = (TryExcept) stack.popNode();
            TryFinally tryFinally = new TryFinally(body, finalBody);
            tryFinally.beginLine = tryExcept.beginLine;
            tryFinally.beginColumn = tryExcept.beginColumn;
            addSpecialsAndClearOriginal(tryExcept, tryFinally);
            return tryFinally;
        case JJTWITH_STMT:
            return makeWithStmt(arity);
        case JJTWITH_ITEM:
            return makeWithItem(arity);
        case JJTEXTRAKEYWORDVALUELIST:
            return new ExtraArgValue(((exprType) stack.popNode()), JJTEXTRAKEYWORDVALUELIST);
        case JJTEXTRAARGVALUELIST:
            return new ExtraArgValue(((exprType) stack.popNode()), JJTEXTRAARGVALUELIST);
        case JJTKEYWORD:
            value = (exprType) stack.popNode();
            nameTok = makeNameTok(NameTok.KeywordName);
            return new keywordType(nameTok, value, false);
        case JJTTUPLE:
            if (stack.nodeArity() > 0) {
                SimpleNode peeked = stack.peekNode();
                if (peeked instanceof ComprehensionCollection) {
                    ComprehensionCollection col = (ComprehensionCollection) stack.popNode();
                    return new ListComp(((exprType) stack.popNode()), col.getGenerators(), ListComp.TupleCtx);
                }
            }
            return makeTuple(n);
        case JJTLIST:
            if (stack.nodeArity() > 0 && stack.peekNode() instanceof ComprehensionCollection) {
                ComprehensionCollection col = (ComprehensionCollection) stack.popNode();
                return new ListComp(((exprType) stack.popNode()), col.getGenerators(), ListComp.ListCtx);
            }
            return new List(makeExprs(), List.Load);
        case JJTSET:
            return new Set(null);
        case JJTDICTIONARY:
            return makeDictionaryOrSet(arity);
        case JJTSTR_1OP:
            return new Repr(((exprType) stack.popNode()));
        case JJTTEST:
            if (arity == 2) {
                IfExp node = (IfExp) stack.popNode();
                node.body = (exprType) stack.popNode();
                return node;
            } else {
                return stack.popNode();
            }
        case JJTIF_EXP:
            exprType ifExprOrelse = (exprType) stack.popNode();
            exprType ifExprTest = (exprType) stack.popNode();
            return new IfExp(ifExprTest, null, ifExprOrelse);
        case JJTOLD_LAMBDEF:
        case JJTLAMBDEF:
            test = (exprType) stack.popNode();
            arguments = makeArguments(arity - 1);
            Lambda lambda = new Lambda(arguments, test);
            // }
            return lambda;
        case JJTELLIPSIS:
            return new Ellipsis();
        case JJTSLICE:
            SimpleNode[] arr = new SimpleNode[arity];
            for (int i = arity - 1; i >= 0; i--) {
                arr[i] = stack.popNode();
            }
            exprType[] values = new exprType[3];
            int k = 0;
            java.util.List<Object> specialsBefore = new ArrayList<Object>();
            java.util.List<Object> specialsAfter = new ArrayList<Object>();
            for (int j = 0; j < arity; j++) {
                if (arr[j].getId() == JJTCOLON) {
                    if (arr[j].specialsBefore != null) {
                        specialsBefore.addAll(arr[j].specialsBefore);
                        // this nodes may be reused among parses, so, we have to erase the specials
                        arr[j].specialsBefore.clear();
                    }
                    if (arr[j].specialsAfter != null) {
                        specialsAfter.addAll(arr[j].specialsAfter);
                        arr[j].specialsAfter.clear();
                    }
                    k++;
                } else {
                    values[k] = (exprType) arr[j];
                    if (specialsBefore.size() > 0) {
                        values[k].getSpecialsBefore().addAll(specialsBefore);
                        specialsBefore.clear();
                    }
                    if (specialsAfter.size() > 0) {
                        values[k].getSpecialsBefore().addAll(specialsAfter);
                        specialsAfter.clear();
                    }
                }
            }
            SimpleNode sliceRet;
            if (k == 0) {
                sliceRet = new Index(values[0]);
            } else {
                sliceRet = new Slice(values[0], values[1], values[2]);
            }
            // this may happen if we have no values
            sliceRet.getSpecialsBefore().addAll(specialsBefore);
            sliceRet.getSpecialsAfter().addAll(specialsAfter);
            specialsBefore.clear();
            specialsAfter.clear();
            return sliceRet;
        case JJTCOMP_FOR:
            return makeCompFor(arity);
        case JJTIMPORTFROM:
            return makeImportFrom25Onwards(arity);
        case JJTSTAR_EXPR:
            Starred starred = (Starred) n;
            starred.value = (exprType) this.stack.popNode();
            ctx.setStore(starred);
            return starred;
        default:
            Log.log(("Error at TreeBuilder: default not treated:" + n.getId()));
            return null;
    }
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) TryExcept(org.python.pydev.parser.jython.ast.TryExcept) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Index(org.python.pydev.parser.jython.ast.Index) Global(org.python.pydev.parser.jython.ast.Global) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Suite(org.python.pydev.parser.jython.ast.Suite) Print(org.python.pydev.parser.jython.ast.Print) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) Assert(org.python.pydev.parser.jython.ast.Assert) IfExp(org.python.pydev.parser.jython.ast.IfExp) List(org.python.pydev.parser.jython.ast.List) ArrayList(java.util.ArrayList) org.python.pydev.parser.jython.ast.argumentsType(org.python.pydev.parser.jython.ast.argumentsType) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) DefaultArg(org.python.pydev.parser.grammarcommon.DefaultArg) Repr(org.python.pydev.parser.jython.ast.Repr) Expr(org.python.pydev.parser.jython.ast.Expr) TryFinally(org.python.pydev.parser.jython.ast.TryFinally) Slice(org.python.pydev.parser.jython.ast.Slice) org.python.pydev.parser.jython.ast.sliceType(org.python.pydev.parser.jython.ast.sliceType) Set(org.python.pydev.parser.jython.ast.Set) ExtraArg(org.python.pydev.parser.grammarcommon.ExtraArg) Decorators(org.python.pydev.parser.grammarcommon.Decorators) Subscript(org.python.pydev.parser.jython.ast.Subscript) org.python.pydev.parser.jython.ast.keywordType(org.python.pydev.parser.jython.ast.keywordType) Lambda(org.python.pydev.parser.jython.ast.Lambda) Call(org.python.pydev.parser.jython.ast.Call) Return(org.python.pydev.parser.jython.ast.Return) Ellipsis(org.python.pydev.parser.jython.ast.Ellipsis) ListComp(org.python.pydev.parser.jython.ast.ListComp) While(org.python.pydev.parser.jython.ast.While) org.python.pydev.parser.jython.ast.suiteType(org.python.pydev.parser.jython.ast.suiteType) Print(org.python.pydev.parser.jython.ast.Print) ExtraArgValue(org.python.pydev.parser.grammarcommon.ExtraArgValue) org.python.pydev.parser.jython.ast.excepthandlerType(org.python.pydev.parser.jython.ast.excepthandlerType) ComprehensionCollection(org.python.pydev.parser.grammarcommon.ComprehensionCollection) Assign(org.python.pydev.parser.jython.ast.Assign) Yield(org.python.pydev.parser.jython.ast.Yield) Starred(org.python.pydev.parser.jython.ast.Starred) Raise(org.python.pydev.parser.jython.ast.Raise) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 2 with ComprehensionCollection

use of org.python.pydev.parser.grammarcommon.ComprehensionCollection in project Pydev by fabioz.

the class TreeBuilder38 method onCloseNode.

@Override
public final SimpleNode onCloseNode(SimpleNode n, int arity) throws Exception {
    exprType value;
    exprType[] exprs;
    Suite orelseSuite;
    stmtType[] body;
    Suite suite;
    int l;
    exprType awaitExpr;
    exprType target;
    switch(n.getId()) {
        case JJTEXPR_STMT:
            value = (exprType) stack.popNode();
            if (arity > 1) {
                exprs = makeExprs(arity - 1);
                ctx.setStore(exprs);
                return new Assign(exprs, value, null);
            } else {
                return new Expr(value);
            }
        case JJTINDEX_OP:
            sliceType slice = (sliceType) stack.popNode();
            value = (exprType) stack.popNode();
            return new Subscript(value, slice, Subscript.Load);
        case JJTBEGIN_FOR_ELSE_STMT:
            return new Suite(null);
        case JJTBEGIN_ELSE_STMT:
            return new Suite(null);
        case JJTBEGIN_WHILE_STMT:
            return new While(null, null, null);
        case JJTWHILE_STMT:
            orelseSuite = null;
            if (stack.nodeArity() == 5) {
                orelseSuite = popSuiteAndSuiteType();
            }
            body = popSuite();
            exprType test = (exprType) stack.popNode();
            While w = (While) stack.popNode();
            w.test = test;
            w.body = body;
            w.orelse = orelseSuite;
            return w;
        case JJTCALL_OP:
            exprType starargs = null;
            exprType kwargs = null;
            java.util.List<exprType> args = new ArrayList<exprType>();
            java.util.List<keywordType> keywords = new ArrayList<keywordType>();
            for (int i = arity - 2; i >= 0; i--) {
                SimpleNode node = stack.popNode();
                if (node instanceof keywordType) {
                    keywordType keyword = (keywordType) node;
                    keywords.add(0, keyword);
                    if (starargs == null) {
                        // note that we get things backward in the stack
                        keyword.afterstarargs = true;
                    }
                } else if (node.getId() == JJTEXTRAARGVALUELIST) {
                    ExtraArgValue nstarargs = (ExtraArgValue) node;
                    starargs = nstarargs.value;
                    this.addSpecialsAndClearOriginal(nstarargs, starargs);
                } else if (node.getId() == JJTEXTRAKEYWORDVALUELIST) {
                    ExtraArgValue nkwargs = (ExtraArgValue) node;
                    kwargs = nkwargs.value;
                    this.addSpecialsAndClearOriginal(nkwargs, kwargs);
                } else if (node instanceof ComprehensionCollection) {
                    // what can happen is something like print sum(x for x in y), where we have already passed x in the args, and then get 'for x in y'
                    args.add(0, new ListComp((exprType) stack.popNode(), ((ComprehensionCollection) node).getGenerators(), ListComp.EmptyCtx));
                    // popped node
                    i--;
                } else {
                    args.add(0, (exprType) node);
                }
            }
            exprType func = (exprType) stack.popNode();
            Call c = new Call(func, args.toArray(new exprType[args.size()]), keywords.toArray(new keywordType[keywords.size()]), starargs, kwargs);
            addSpecialsAndClearOriginal(n, c);
            return c;
        case JJTFUNCDEF_RETURN_ANNOTTATION:
            SimpleNode funcdefReturn = stack.popNode();
            return new FuncDefReturnAnn(funcdefReturn);
        case JJTFUNCDEF:
            suite = (Suite) stack.popNode();
            body = suite.body;
            arity--;
            SimpleNode funcDefReturnAnn = stack.peekNode();
            exprType actualReturnAnnotation = null;
            if (funcDefReturnAnn instanceof FuncDefReturnAnn) {
                stack.popNode();
                actualReturnAnnotation = (exprType) ((FuncDefReturnAnn) funcDefReturnAnn).node;
                arity--;
                addSpecialsAndClearOriginal(funcDefReturnAnn, actualReturnAnnotation);
            }
            argumentsType arguments = makeArguments(arity - 1);
            NameTok nameTok = makeNameTok(NameTok.FunctionName);
            // decorator is always null at this point... it's decorated later on
            FunctionDef funcDef = new FunctionDef(nameTok, arguments, body, null, actualReturnAnnotation, this.stack.getGrammar().getInsideAsync());
            addSpecialsAndClearOriginal(suite, funcDef);
            setParentForFuncOrClass(body, funcDef);
            return funcDef;
        case JJTTFPDEF:
            Name tfpdefName = null;
            exprType typeDef = null;
            if (arity == 1) {
                tfpdefName = (Name) stack.popNode();
            } else if (arity == 2) {
                typeDef = (exprType) stack.popNode();
                tfpdefName = (Name) stack.popNode();
            } else {
                throw new RuntimeException("Unexpected arity: " + arity);
            }
            return new JfpDef(tfpdefName, typeDef);
        case JJTONLYKEYWORDARG2:
        case JJTDEFAULTARG2:
            DefaultArg defaultArg;
            JfpDef jfpDef;
            if (arity == 1) {
                jfpDef = (JfpDef) stack.popNode();
                defaultArg = new DefaultArg(jfpDef.nameNode, null, jfpDef.typeDef, n.getId());
            } else if (arity == 2) {
                exprType defaultValue = (exprType) stack.popNode();
                jfpDef = (JfpDef) stack.popNode();
                defaultArg = new DefaultArg(jfpDef.nameNode, defaultValue, jfpDef.typeDef, n.getId());
            } else {
                throw new RuntimeException("Unexpected arity: " + arity);
            }
            return defaultArg;
        case JJTONLYKEYWORDARG:
        case JJTDEFAULTARG:
            // no type definition in this case
            if (arity == 1) {
                return new DefaultArg(((exprType) stack.popNode()), null, null, n.getId());
            }
            exprType parameter = (exprType) stack.popNode();
            return new DefaultArg((exprType) stack.popNode(), parameter, null, n.getId());
        case JJTEXTRAARGLIST:
            if (arity == 0) {
                // nothing here (just '*')
                return new ExtraArg(null, JJTEXTRAARGLIST, null);
            }
            return new ExtraArg(makeNameTok(NameTok.VarArg), JJTEXTRAARGLIST);
        case JJTEXTRAKEYWORDLIST:
            return new ExtraArg(makeNameTok(NameTok.KwArg), JJTEXTRAKEYWORDLIST);
        case // with type declaration
        JJTEXTRAARGLIST2:
            if (arity == 0) {
                // nothing here (just '*')
                return new ExtraArg(null, JJTEXTRAARGLIST, null);
            }
            jfpDef = (JfpDef) stack.popNode();
            NameTok jfpDefName = makeNameTok(NameTok.VarArg, jfpDef.nameNode);
            ExtraArg extra = new ExtraArg(jfpDefName, JJTEXTRAARGLIST, jfpDef.typeDef);
            return extra;
        case // with type declaration
        JJTEXTRAKEYWORDLIST2:
            jfpDef = (JfpDef) stack.popNode();
            return new ExtraArg(makeNameTok(NameTok.KwArg, jfpDef.nameNode), JJTEXTRAKEYWORDLIST, jfpDef.typeDef);
        case JJTDECORATED:
            if (stack.nodeArity() != 2) {
                throw new RuntimeException("Expected 2 nodes at this context, found: " + arity);
            }
            SimpleNode def = stack.popNode();
            Decorators decorators = (Decorators) stack.popNode();
            if (def instanceof ClassDef) {
                ClassDef classDef = (ClassDef) def;
                classDef.decs = decorators.exp;
            } else {
                FunctionDef fDef = (FunctionDef) def;
                fDef.decs = decorators.exp;
            }
            return def;
        case JJTCLASSDEF:
            suite = (Suite) stack.popNode();
            body = suite.body;
            int nodeArity = stack.nodeArity() - 1;
            ArrayList<keywordType> classDefKeywords = new ArrayList<keywordType>();
            starargs = null;
            kwargs = null;
            int loopTo = nodeArity;
            for (int i = 0; i < loopTo; i++) {
                SimpleNode node = stack.peekNode();
                if (node instanceof keywordType) {
                    stack.popNode();
                    keywordType keyword = (keywordType) node;
                    classDefKeywords.add(keyword);
                    if (starargs == null) {
                        // note that we get things backward in the stack
                        keyword.afterstarargs = true;
                    }
                    nodeArity--;
                } else if (node instanceof ExtraArgValue) {
                    if (node.getId() == JJTEXTRAARGVALUELIST) {
                        ExtraArgValue nstarargs = (ExtraArgValue) stack.popNode();
                        starargs = nstarargs.value;
                        this.addSpecialsAndClearOriginal(nstarargs, starargs);
                        nodeArity--;
                    } else if (node.getId() == JJTEXTRAKEYWORDVALUELIST) {
                        ExtraArgValue nkwargs = (ExtraArgValue) stack.popNode();
                        kwargs = nkwargs.value;
                        this.addSpecialsAndClearOriginal(nkwargs, kwargs);
                        nodeArity--;
                    }
                } else {
                    break;
                }
            }
            if (classDefKeywords.size() > 1) {
                Collections.reverse(classDefKeywords);
            }
            exprType[] bases = makeExprs(nodeArity);
            nameTok = makeNameTok(NameTok.ClassName);
            // decorator is always null at this point... it's decorated later on
            ClassDef classDef = new ClassDef(nameTok, bases, body, null, classDefKeywords.toArray(new keywordType[classDefKeywords.size()]), starargs, kwargs);
            addSpecialsAndClearOriginal(suite, classDef);
            setParentForFuncOrClass(body, classDef);
            return classDef;
        case JJTBEGIN_RETURN_STMT:
            return new Return(null);
        case JJTRETURN_STMT:
            value = arity == 2 ? ((exprType) stack.popNode()) : null;
            Return ret = (Return) stack.popNode();
            ret.value = value;
            return ret;
        case JJTYIELD_STMT:
            return stack.popNode();
        case JJTYIELD_EXPR:
            exprType yieldExpr = null;
            if (arity > 0) {
                // we may have an empty yield, so, we have to check it before
                yieldExpr = (exprType) stack.popNode();
            }
            return new Yield(yieldExpr, false);
        case JJTRAISE_STMT:
            exprType from = arity >= 2 ? ((exprType) stack.popNode()) : null;
            exprType type = arity >= 1 ? ((exprType) stack.popNode()) : null;
            return new Raise(type, null, null, from);
        case JJTGLOBAL_STMT:
            return new Global(makeIdentifiers(NameTok.GlobalName), null);
        case JJTNONLOCAL_STMT:
            return new NonLocal(makeIdentifiers(NameTok.NonLocalName), null);
        case JJTASSERT_STMT:
            exprType msg = arity == 2 ? ((exprType) stack.popNode()) : null;
            test = (exprType) stack.popNode();
            return new Assert(test, msg);
        case JJTBEGIN_TRY_STMT:
            // we do that just to get the specials
            return new TryExcept(null, null, null);
        case JJTTRYELSE_STMT:
            orelseSuite = popSuiteAndSuiteType();
            return orelseSuite;
        case JJTTRYFINALLY_OUTER_STMT:
            orelseSuite = popSuiteAndSuiteType();
            // it does not have a body at this time... it will be filled with the inner try..except
            return new TryFinally(null, orelseSuite);
        case JJTTRY_STMT:
            TryFinally outer = null;
            if (stack.peekNode() instanceof TryFinally) {
                outer = (TryFinally) stack.popNode();
                arity--;
            }
            orelseSuite = null;
            if (stack.peekNode() instanceof suiteType) {
                orelseSuite = (Suite) stack.popNode();
                arity--;
            }
            l = arity;
            excepthandlerType[] handlers = new excepthandlerType[l];
            for (int i = l - 1; i >= 0; i--) {
                handlers[i] = (excepthandlerType) stack.popNode();
            }
            suite = (Suite) stack.popNode();
            TryExcept tryExc = (TryExcept) stack.popNode();
            if (outer != null) {
                outer.beginLine = tryExc.beginLine;
            }
            tryExc.body = suite.body;
            tryExc.handlers = handlers;
            tryExc.orelse = orelseSuite;
            addSpecials(suite, tryExc);
            if (outer == null) {
                return tryExc;
            } else {
                if (outer.body != null) {
                    throw new RuntimeException("Error. Expecting null body to be filled on try..except..finally");
                }
                outer.body = new stmtType[] { tryExc };
                return outer;
            }
        case JJTBEGIN_TRY_ELSE_STMT:
            // we do that just to get the specials
            return new Suite(null);
        case JJTBEGIN_EXCEPT_CLAUSE:
            return new excepthandlerType(null, null, null);
        case JJTEXCEPT_CLAUSE:
            suite = (Suite) stack.popNode();
            body = suite.body;
            exprType excname = arity == 4 ? ((exprType) stack.popNode()) : null;
            if (excname != null) {
                ctx.setStore(excname);
            }
            type = arity >= 3 ? ((exprType) stack.popNode()) : null;
            excepthandlerType handler = (excepthandlerType) stack.popNode();
            handler.type = type;
            handler.name = excname;
            handler.body = body;
            addSpecials(suite, handler);
            return handler;
        case JJTBEGIN_FINALLY_STMT:
            // we do that just to get the specials
            return new Suite(null);
        case JJTTRYFINALLY_STMT:
            suiteType finalBody = popSuiteAndSuiteType();
            body = popSuite();
            // We have a try..except in the stack, but we will change it for a try..finally
            // This is because we recognize a try..except in the 'try:' token, but actually end up with a try..finally
            TryExcept tryExcept = (TryExcept) stack.popNode();
            TryFinally tryFinally = new TryFinally(body, finalBody);
            tryFinally.beginLine = tryExcept.beginLine;
            tryFinally.beginColumn = tryExcept.beginColumn;
            addSpecialsAndClearOriginal(tryExcept, tryFinally);
            return tryFinally;
        case JJTNAMEDEXPR_TEST:
            if (arity == 1) {
                return stack.popNode();
            }
            value = (exprType) stack.popNode();
            target = (exprType) stack.popNode();
            ctx.setNamedStore(target);
            NamedExpr namedExpr = (NamedExpr) n;
            namedExpr.value = value;
            namedExpr.target = target;
            return n;
        case JJTWITH_STMT:
            return makeWithStmt(arity);
        case JJTWITH_ITEM:
            return makeWithItem(arity);
        case JJTEXTRAKEYWORDVALUELIST:
            return new ExtraArgValue(((exprType) stack.popNode()), JJTEXTRAKEYWORDVALUELIST);
        case JJTEXTRAARGVALUELIST:
            return new ExtraArgValue(((exprType) stack.popNode()), JJTEXTRAARGVALUELIST);
        case JJTARGUMENT:
            SimpleNode keyword = stack.popNode();
            if (keyword instanceof keywordType) {
                nameTok = makeNameTok(NameTok.KeywordName);
                ((keywordType) keyword).arg = nameTok;
            }
            return keyword;
        case JJTKEYWORD:
            value = (exprType) stack.popNode();
            return new keywordType(null, value, false);
        case JJTTUPLE:
            if (stack.nodeArity() > 0) {
                SimpleNode peeked = stack.peekNode();
                if (peeked instanceof ComprehensionCollection) {
                    ComprehensionCollection col = (ComprehensionCollection) stack.popNode();
                    return new ListComp(((exprType) stack.popNode()), col.getGenerators(), ListComp.TupleCtx);
                }
            }
            return makeTuple(n);
        case JJTLIST:
            if (stack.nodeArity() > 0 && stack.peekNode() instanceof ComprehensionCollection) {
                ComprehensionCollection col = (ComprehensionCollection) stack.popNode();
                return new ListComp(((exprType) stack.popNode()), col.getGenerators(), ListComp.ListCtx);
            }
            return new List(makeExprs(), List.Load);
        case JJTSET:
            return new Set(null);
        case JJTDICTIONARY:
            return makeDictionaryOrSet(arity);
        // return new Repr(((exprType) stack.popNode()));
        case JJTTEST:
            if (arity == 2) {
                IfExp node = (IfExp) stack.popNode();
                node.body = (exprType) stack.popNode();
                return node;
            } else {
                return stack.popNode();
            }
        case JJTIF_EXP:
            exprType ifExprOrelse = (exprType) stack.popNode();
            exprType ifExprTest = (exprType) stack.popNode();
            return new IfExp(ifExprTest, null, ifExprOrelse);
        case JJTLAMBDEF_NOCOND:
        case JJTLAMBDEF:
            test = (exprType) stack.popNode();
            arguments = makeArguments(arity - 1);
            Lambda lambda = new Lambda(arguments, test);
            // }
            return lambda;
        case JJTELLIPSIS:
            return new Ellipsis();
        case JJTELLIPSIS_AS_NAME:
            return new Name("...", Name.Load, true);
        case JJTSLICE:
            SimpleNode[] arr = new SimpleNode[arity];
            for (int i = arity - 1; i >= 0; i--) {
                arr[i] = stack.popNode();
            }
            exprType[] values = new exprType[3];
            int k = 0;
            java.util.List<Object> specialsBefore = new ArrayList<Object>();
            java.util.List<Object> specialsAfter = new ArrayList<Object>();
            for (int j = 0; j < arity; j++) {
                if (arr[j].getId() == JJTCOLON) {
                    if (arr[j].specialsBefore != null) {
                        specialsBefore.addAll(arr[j].specialsBefore);
                        // this nodes may be reused among parses, so, we have to erase the specials
                        arr[j].specialsBefore.clear();
                    }
                    if (arr[j].specialsAfter != null) {
                        specialsAfter.addAll(arr[j].specialsAfter);
                        arr[j].specialsAfter.clear();
                    }
                    k++;
                } else {
                    values[k] = (exprType) arr[j];
                    if (specialsBefore.size() > 0) {
                        values[k].getSpecialsBefore().addAll(specialsBefore);
                        specialsBefore.clear();
                    }
                    if (specialsAfter.size() > 0) {
                        values[k].getSpecialsBefore().addAll(specialsAfter);
                        specialsAfter.clear();
                    }
                }
            }
            SimpleNode sliceRet;
            if (k == 0) {
                sliceRet = new Index(values[0]);
            } else {
                sliceRet = new Slice(values[0], values[1], values[2]);
            }
            // this may happen if we have no values
            sliceRet.getSpecialsBefore().addAll(specialsBefore);
            sliceRet.getSpecialsAfter().addAll(specialsAfter);
            specialsBefore.clear();
            specialsAfter.clear();
            return sliceRet;
        case JJTCOMP_FOR:
            return makeCompFor(arity);
        case JJTIMPORTFROM:
            return makeImportFrom25Onwards(arity);
        case JJTAWAIT_ATOM_EXPR:
            awaitExpr = (exprType) stack.popNode();
            return new Await(awaitExpr);
        case JJTSTAR_EXPR:
            Starred starred = (Starred) n;
            starred.value = (exprType) this.stack.popNode();
            ctx.setCtx(starred, starred.ctx);
            return starred;
        case JJTANN_ASSIGN:
            return typedDeclaration(arity, stack, ctx);
        case JJTEVAL_INPUT:
            Expr expr = (Expr) n;
            if (arity != 1) {
                Log.log("Expected arity to be == 1 here.");
            }
            for (int i = arity - 1; i >= 0; i--) {
                SimpleNode popNode = stack.popNode();
                try {
                    exprType node = (exprType) popNode;
                    expr = new Expr(node);
                } catch (Exception e) {
                    Log.log("Expected expr. Found: " + popNode);
                }
            }
            return expr;
        default:
            Log.log(("Error at TreeBuilder: default not treated:" + n.getId()));
            return null;
    }
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) TryExcept(org.python.pydev.parser.jython.ast.TryExcept) NamedExpr(org.python.pydev.parser.jython.ast.NamedExpr) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Index(org.python.pydev.parser.jython.ast.Index) Global(org.python.pydev.parser.jython.ast.Global) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Name(org.python.pydev.parser.jython.ast.Name) Suite(org.python.pydev.parser.jython.ast.Suite) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) Assert(org.python.pydev.parser.jython.ast.Assert) IfExp(org.python.pydev.parser.jython.ast.IfExp) FuncDefReturnAnn(org.python.pydev.parser.grammarcommon.FuncDefReturnAnn) List(org.python.pydev.parser.jython.ast.List) ArrayList(java.util.ArrayList) org.python.pydev.parser.jython.ast.argumentsType(org.python.pydev.parser.jython.ast.argumentsType) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) DefaultArg(org.python.pydev.parser.grammarcommon.DefaultArg) Expr(org.python.pydev.parser.jython.ast.Expr) NamedExpr(org.python.pydev.parser.jython.ast.NamedExpr) TryFinally(org.python.pydev.parser.jython.ast.TryFinally) Slice(org.python.pydev.parser.jython.ast.Slice) NonLocal(org.python.pydev.parser.jython.ast.NonLocal) org.python.pydev.parser.jython.ast.sliceType(org.python.pydev.parser.jython.ast.sliceType) Set(org.python.pydev.parser.jython.ast.Set) ExtraArg(org.python.pydev.parser.grammarcommon.ExtraArg) JfpDef(org.python.pydev.parser.grammarcommon.JfpDef) Decorators(org.python.pydev.parser.grammarcommon.Decorators) Subscript(org.python.pydev.parser.jython.ast.Subscript) org.python.pydev.parser.jython.ast.keywordType(org.python.pydev.parser.jython.ast.keywordType) Lambda(org.python.pydev.parser.jython.ast.Lambda) Call(org.python.pydev.parser.jython.ast.Call) Return(org.python.pydev.parser.jython.ast.Return) Ellipsis(org.python.pydev.parser.jython.ast.Ellipsis) ListComp(org.python.pydev.parser.jython.ast.ListComp) While(org.python.pydev.parser.jython.ast.While) org.python.pydev.parser.jython.ast.suiteType(org.python.pydev.parser.jython.ast.suiteType) ParseException(org.python.pydev.parser.jython.ParseException) ExtraArgValue(org.python.pydev.parser.grammarcommon.ExtraArgValue) org.python.pydev.parser.jython.ast.excepthandlerType(org.python.pydev.parser.jython.ast.excepthandlerType) ComprehensionCollection(org.python.pydev.parser.grammarcommon.ComprehensionCollection) Assign(org.python.pydev.parser.jython.ast.Assign) Yield(org.python.pydev.parser.jython.ast.Yield) Starred(org.python.pydev.parser.jython.ast.Starred) Raise(org.python.pydev.parser.jython.ast.Raise) Await(org.python.pydev.parser.jython.ast.Await) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 3 with ComprehensionCollection

use of org.python.pydev.parser.grammarcommon.ComprehensionCollection in project Pydev by fabioz.

the class TreeBuilder30 method onCloseNode.

@Override
public final SimpleNode onCloseNode(SimpleNode n, int arity) throws Exception {
    exprType value;
    exprType[] exprs;
    Suite orelseSuite;
    stmtType[] body;
    Suite suite;
    int l;
    exprType awaitExpr;
    switch(n.getId()) {
        case JJTEXPR_STMT:
            value = (exprType) stack.popNode();
            if (arity > 1) {
                exprs = makeExprs(arity - 1);
                ctx.setStore(exprs);
                return new Assign(exprs, value, null);
            } else {
                return new Expr(value);
            }
        case JJTINDEX_OP:
            sliceType slice = (sliceType) stack.popNode();
            value = (exprType) stack.popNode();
            return new Subscript(value, slice, Subscript.Load);
        case JJTBEGIN_FOR_ELSE_STMT:
            return new Suite(null);
        case JJTBEGIN_ELSE_STMT:
            return new Suite(null);
        case JJTBEGIN_WHILE_STMT:
            return new While(null, null, null);
        case JJTWHILE_STMT:
            orelseSuite = null;
            if (stack.nodeArity() == 5) {
                orelseSuite = popSuiteAndSuiteType();
            }
            body = popSuite();
            exprType test = (exprType) stack.popNode();
            While w = (While) stack.popNode();
            w.test = test;
            w.body = body;
            w.orelse = orelseSuite;
            return w;
        case JJTCALL_OP:
            exprType starargs = null;
            exprType kwargs = null;
            java.util.List<exprType> args = new ArrayList<exprType>();
            java.util.List<keywordType> keywords = new ArrayList<keywordType>();
            for (int i = arity - 2; i >= 0; i--) {
                SimpleNode node = stack.popNode();
                if (node instanceof keywordType) {
                    keywordType keyword = (keywordType) node;
                    keywords.add(0, keyword);
                    if (starargs == null) {
                        // note that we get things backward in the stack
                        keyword.afterstarargs = true;
                    }
                } else if (node.getId() == JJTEXTRAARGVALUELIST) {
                    ExtraArgValue nstarargs = (ExtraArgValue) node;
                    starargs = nstarargs.value;
                    this.addSpecialsAndClearOriginal(nstarargs, starargs);
                } else if (node.getId() == JJTEXTRAKEYWORDVALUELIST) {
                    ExtraArgValue nkwargs = (ExtraArgValue) node;
                    kwargs = nkwargs.value;
                    this.addSpecialsAndClearOriginal(nkwargs, kwargs);
                } else if (node instanceof ComprehensionCollection) {
                    // what can happen is something like print sum(x for x in y), where we have already passed x in the args, and then get 'for x in y'
                    args.add(0, new ListComp((exprType) stack.popNode(), ((ComprehensionCollection) node).getGenerators(), ListComp.EmptyCtx));
                    // popped node
                    i--;
                } else {
                    args.add(0, (exprType) node);
                }
            }
            exprType func = (exprType) stack.popNode();
            Call c = new Call(func, args.toArray(new exprType[args.size()]), keywords.toArray(new keywordType[keywords.size()]), starargs, kwargs);
            addSpecialsAndClearOriginal(n, c);
            return c;
        case JJTFUNCDEF_RETURN_ANNOTTATION:
            SimpleNode funcdefReturn = stack.popNode();
            return new FuncDefReturnAnn(funcdefReturn);
        case JJTFUNCDEF:
            suite = (Suite) stack.popNode();
            body = suite.body;
            arity--;
            SimpleNode funcDefReturnAnn = stack.peekNode();
            exprType actualReturnAnnotation = null;
            if (funcDefReturnAnn instanceof FuncDefReturnAnn) {
                stack.popNode();
                actualReturnAnnotation = (exprType) ((FuncDefReturnAnn) funcDefReturnAnn).node;
                arity--;
                addSpecialsAndClearOriginal(funcDefReturnAnn, actualReturnAnnotation);
            }
            argumentsType arguments = makeArguments(arity - 1);
            NameTok nameTok = makeNameTok(NameTok.FunctionName);
            // decorator is always null at this point... it's decorated later on
            FunctionDef funcDef = new FunctionDef(nameTok, arguments, body, null, actualReturnAnnotation, this.stack.getGrammar().getInsideAsync());
            addSpecialsAndClearOriginal(suite, funcDef);
            setParentForFuncOrClass(body, funcDef);
            return funcDef;
        case JJTTFPDEF:
            Name tfpdefName = null;
            exprType typeDef = null;
            if (arity == 1) {
                tfpdefName = (Name) stack.popNode();
            } else if (arity == 2) {
                typeDef = (exprType) stack.popNode();
                tfpdefName = (Name) stack.popNode();
            } else {
                throw new RuntimeException("Unexpected arity: " + arity);
            }
            return new JfpDef(tfpdefName, typeDef);
        case JJTONLYKEYWORDARG2:
        case JJTDEFAULTARG2:
            DefaultArg defaultArg;
            JfpDef jfpDef;
            if (arity == 1) {
                jfpDef = (JfpDef) stack.popNode();
                defaultArg = new DefaultArg(jfpDef.nameNode, null, jfpDef.typeDef, n.getId());
            } else if (arity == 2) {
                exprType defaultValue = (exprType) stack.popNode();
                jfpDef = (JfpDef) stack.popNode();
                defaultArg = new DefaultArg(jfpDef.nameNode, defaultValue, jfpDef.typeDef, n.getId());
            } else {
                throw new RuntimeException("Unexpected arity: " + arity);
            }
            return defaultArg;
        case JJTONLYKEYWORDARG:
        case JJTDEFAULTARG:
            // no type definition in this case
            if (arity == 1) {
                return new DefaultArg(((exprType) stack.popNode()), null, null, n.getId());
            }
            exprType parameter = (exprType) stack.popNode();
            return new DefaultArg((exprType) stack.popNode(), parameter, null, n.getId());
        case JJTEXTRAARGLIST:
            if (arity == 0) {
                // nothing here (just '*')
                return new ExtraArg(null, JJTEXTRAARGLIST, null);
            }
            return new ExtraArg(makeNameTok(NameTok.VarArg), JJTEXTRAARGLIST);
        case JJTEXTRAKEYWORDLIST:
            return new ExtraArg(makeNameTok(NameTok.KwArg), JJTEXTRAKEYWORDLIST);
        case // with type declaration
        JJTEXTRAARGLIST2:
            if (arity == 0) {
                // nothing here (just '*')
                return new ExtraArg(null, JJTEXTRAARGLIST, null);
            }
            jfpDef = (JfpDef) stack.popNode();
            NameTok jfpDefName = makeNameTok(NameTok.VarArg, jfpDef.nameNode);
            ExtraArg extra = new ExtraArg(jfpDefName, JJTEXTRAARGLIST, jfpDef.typeDef);
            return extra;
        case // with type declaration
        JJTEXTRAKEYWORDLIST2:
            jfpDef = (JfpDef) stack.popNode();
            return new ExtraArg(makeNameTok(NameTok.KwArg, jfpDef.nameNode), JJTEXTRAKEYWORDLIST, jfpDef.typeDef);
        case JJTDECORATED:
            if (stack.nodeArity() != 2) {
                throw new RuntimeException("Expected 2 nodes at this context, found: " + arity);
            }
            SimpleNode def = stack.popNode();
            Decorators decorators = (Decorators) stack.popNode();
            if (def instanceof ClassDef) {
                ClassDef classDef = (ClassDef) def;
                classDef.decs = decorators.exp;
            } else {
                FunctionDef fDef = (FunctionDef) def;
                fDef.decs = decorators.exp;
            }
            return def;
        case JJTCLASSDEF:
            suite = (Suite) stack.popNode();
            body = suite.body;
            int nodeArity = stack.nodeArity() - 1;
            ArrayList<keywordType> classDefKeywords = new ArrayList<keywordType>();
            starargs = null;
            kwargs = null;
            int loopTo = nodeArity;
            for (int i = 0; i < loopTo; i++) {
                SimpleNode node = stack.peekNode();
                if (node instanceof keywordType) {
                    stack.popNode();
                    keywordType keyword = (keywordType) node;
                    classDefKeywords.add(keyword);
                    if (starargs == null) {
                        // note that we get things backward in the stack
                        keyword.afterstarargs = true;
                    }
                    nodeArity--;
                } else if (node instanceof ExtraArgValue) {
                    if (node.getId() == JJTEXTRAARGVALUELIST) {
                        ExtraArgValue nstarargs = (ExtraArgValue) stack.popNode();
                        starargs = nstarargs.value;
                        this.addSpecialsAndClearOriginal(nstarargs, starargs);
                        nodeArity--;
                    } else if (node.getId() == JJTEXTRAKEYWORDVALUELIST) {
                        ExtraArgValue nkwargs = (ExtraArgValue) stack.popNode();
                        kwargs = nkwargs.value;
                        this.addSpecialsAndClearOriginal(nkwargs, kwargs);
                        nodeArity--;
                    }
                } else {
                    break;
                }
            }
            if (classDefKeywords.size() > 1) {
                Collections.reverse(classDefKeywords);
            }
            exprType[] bases = makeExprs(nodeArity);
            nameTok = makeNameTok(NameTok.ClassName);
            // decorator is always null at this point... it's decorated later on
            ClassDef classDef = new ClassDef(nameTok, bases, body, null, classDefKeywords.toArray(new keywordType[classDefKeywords.size()]), starargs, kwargs);
            addSpecialsAndClearOriginal(suite, classDef);
            setParentForFuncOrClass(body, classDef);
            return classDef;
        case JJTBEGIN_RETURN_STMT:
            return new Return(null);
        case JJTRETURN_STMT:
            value = arity == 2 ? ((exprType) stack.popNode()) : null;
            Return ret = (Return) stack.popNode();
            ret.value = value;
            return ret;
        case JJTYIELD_STMT:
            return stack.popNode();
        case JJTYIELD_EXPR:
            exprType yieldExpr = null;
            if (arity > 0) {
                // we may have an empty yield, so, we have to check it before
                yieldExpr = (exprType) stack.popNode();
            }
            return new Yield(yieldExpr, false);
        case JJTRAISE_STMT:
            exprType from = arity >= 2 ? ((exprType) stack.popNode()) : null;
            exprType type = arity >= 1 ? ((exprType) stack.popNode()) : null;
            return new Raise(type, null, null, from);
        case JJTGLOBAL_STMT:
            return new Global(makeIdentifiers(NameTok.GlobalName), null);
        case JJTNONLOCAL_STMT:
            return new NonLocal(makeIdentifiers(NameTok.NonLocalName), null);
        case JJTASSERT_STMT:
            exprType msg = arity == 2 ? ((exprType) stack.popNode()) : null;
            test = (exprType) stack.popNode();
            return new Assert(test, msg);
        case JJTBEGIN_TRY_STMT:
            // we do that just to get the specials
            return new TryExcept(null, null, null);
        case JJTTRYELSE_STMT:
            orelseSuite = popSuiteAndSuiteType();
            return orelseSuite;
        case JJTTRYFINALLY_OUTER_STMT:
            orelseSuite = popSuiteAndSuiteType();
            // it does not have a body at this time... it will be filled with the inner try..except
            return new TryFinally(null, orelseSuite);
        case JJTTRY_STMT:
            TryFinally outer = null;
            if (stack.peekNode() instanceof TryFinally) {
                outer = (TryFinally) stack.popNode();
                arity--;
            }
            orelseSuite = null;
            if (stack.peekNode() instanceof suiteType) {
                orelseSuite = (Suite) stack.popNode();
                arity--;
            }
            l = arity;
            excepthandlerType[] handlers = new excepthandlerType[l];
            for (int i = l - 1; i >= 0; i--) {
                handlers[i] = (excepthandlerType) stack.popNode();
            }
            suite = (Suite) stack.popNode();
            TryExcept tryExc = (TryExcept) stack.popNode();
            if (outer != null) {
                outer.beginLine = tryExc.beginLine;
            }
            tryExc.body = suite.body;
            tryExc.handlers = handlers;
            tryExc.orelse = orelseSuite;
            addSpecials(suite, tryExc);
            if (outer == null) {
                return tryExc;
            } else {
                if (outer.body != null) {
                    throw new RuntimeException("Error. Expecting null body to be filled on try..except..finally");
                }
                outer.body = new stmtType[] { tryExc };
                return outer;
            }
        case JJTBEGIN_TRY_ELSE_STMT:
            // we do that just to get the specials
            return new Suite(null);
        case JJTBEGIN_EXCEPT_CLAUSE:
            return new excepthandlerType(null, null, null);
        case JJTEXCEPT_CLAUSE:
            suite = (Suite) stack.popNode();
            body = suite.body;
            exprType excname = arity == 4 ? ((exprType) stack.popNode()) : null;
            if (excname != null) {
                ctx.setStore(excname);
            }
            type = arity >= 3 ? ((exprType) stack.popNode()) : null;
            excepthandlerType handler = (excepthandlerType) stack.popNode();
            handler.type = type;
            handler.name = excname;
            handler.body = body;
            addSpecials(suite, handler);
            return handler;
        case JJTBEGIN_FINALLY_STMT:
            // we do that just to get the specials
            return new Suite(null);
        case JJTTRYFINALLY_STMT:
            suiteType finalBody = popSuiteAndSuiteType();
            body = popSuite();
            // We have a try..except in the stack, but we will change it for a try..finally
            // This is because we recognize a try..except in the 'try:' token, but actually end up with a try..finally
            TryExcept tryExcept = (TryExcept) stack.popNode();
            TryFinally tryFinally = new TryFinally(body, finalBody);
            tryFinally.beginLine = tryExcept.beginLine;
            tryFinally.beginColumn = tryExcept.beginColumn;
            addSpecialsAndClearOriginal(tryExcept, tryFinally);
            return tryFinally;
        case JJTWITH_STMT:
            return makeWithStmt(arity);
        case JJTWITH_ITEM:
            return makeWithItem(arity);
        case JJTEXTRAKEYWORDVALUELIST:
            return new ExtraArgValue(((exprType) stack.popNode()), JJTEXTRAKEYWORDVALUELIST);
        case JJTEXTRAARGVALUELIST:
            return new ExtraArgValue(((exprType) stack.popNode()), JJTEXTRAARGVALUELIST);
        case JJTARGUMENT:
            SimpleNode keyword = stack.popNode();
            if (keyword instanceof keywordType) {
                nameTok = makeNameTok(NameTok.KeywordName);
                ((keywordType) keyword).arg = nameTok;
            }
            return keyword;
        case JJTKEYWORD:
            value = (exprType) stack.popNode();
            return new keywordType(null, value, false);
        case JJTTUPLE:
            if (stack.nodeArity() > 0) {
                SimpleNode peeked = stack.peekNode();
                if (peeked instanceof ComprehensionCollection) {
                    ComprehensionCollection col = (ComprehensionCollection) stack.popNode();
                    return new ListComp(((exprType) stack.popNode()), col.getGenerators(), ListComp.TupleCtx);
                }
            }
            return makeTuple(n);
        case JJTLIST:
            if (stack.nodeArity() > 0 && stack.peekNode() instanceof ComprehensionCollection) {
                ComprehensionCollection col = (ComprehensionCollection) stack.popNode();
                return new ListComp(((exprType) stack.popNode()), col.getGenerators(), ListComp.ListCtx);
            }
            return new List(makeExprs(), List.Load);
        case JJTSET:
            return new Set(null);
        case JJTDICTIONARY:
            return makeDictionaryOrSet(arity);
        // return new Repr(((exprType) stack.popNode()));
        case JJTTEST:
            if (arity == 2) {
                IfExp node = (IfExp) stack.popNode();
                node.body = (exprType) stack.popNode();
                return node;
            } else {
                return stack.popNode();
            }
        case JJTIF_EXP:
            exprType ifExprOrelse = (exprType) stack.popNode();
            exprType ifExprTest = (exprType) stack.popNode();
            return new IfExp(ifExprTest, null, ifExprOrelse);
        case JJTLAMBDEF_NOCOND:
        case JJTLAMBDEF:
            test = (exprType) stack.popNode();
            arguments = makeArguments(arity - 1);
            Lambda lambda = new Lambda(arguments, test);
            // }
            return lambda;
        case JJTELLIPSIS:
            return new Ellipsis();
        case JJTELLIPSIS_AS_NAME:
            return new Name("...", Name.Load, true);
        case JJTSLICE:
            SimpleNode[] arr = new SimpleNode[arity];
            for (int i = arity - 1; i >= 0; i--) {
                arr[i] = stack.popNode();
            }
            exprType[] values = new exprType[3];
            int k = 0;
            java.util.List<Object> specialsBefore = new ArrayList<Object>();
            java.util.List<Object> specialsAfter = new ArrayList<Object>();
            for (int j = 0; j < arity; j++) {
                if (arr[j].getId() == JJTCOLON) {
                    if (arr[j].specialsBefore != null) {
                        specialsBefore.addAll(arr[j].specialsBefore);
                        // this nodes may be reused among parses, so, we have to erase the specials
                        arr[j].specialsBefore.clear();
                    }
                    if (arr[j].specialsAfter != null) {
                        specialsAfter.addAll(arr[j].specialsAfter);
                        arr[j].specialsAfter.clear();
                    }
                    k++;
                } else {
                    values[k] = (exprType) arr[j];
                    if (specialsBefore.size() > 0) {
                        values[k].getSpecialsBefore().addAll(specialsBefore);
                        specialsBefore.clear();
                    }
                    if (specialsAfter.size() > 0) {
                        values[k].getSpecialsBefore().addAll(specialsAfter);
                        specialsAfter.clear();
                    }
                }
            }
            SimpleNode sliceRet;
            if (k == 0) {
                sliceRet = new Index(values[0]);
            } else {
                sliceRet = new Slice(values[0], values[1], values[2]);
            }
            // this may happen if we have no values
            sliceRet.getSpecialsBefore().addAll(specialsBefore);
            sliceRet.getSpecialsAfter().addAll(specialsAfter);
            specialsBefore.clear();
            specialsAfter.clear();
            return sliceRet;
        case JJTCOMP_FOR:
            return makeCompFor(arity);
        case JJTIMPORTFROM:
            return makeImportFrom25Onwards(arity);
        case JJTAWAIT_ATOM_EXPR:
            awaitExpr = (exprType) stack.popNode();
            return new Await(awaitExpr);
        case JJTSTAR_EXPR:
            Starred starred = (Starred) n;
            starred.value = (exprType) this.stack.popNode();
            ctx.setCtx(starred, starred.ctx);
            return starred;
        default:
            Log.log(("Error at TreeBuilder: default not treated:" + n.getId()));
            return null;
    }
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) TryExcept(org.python.pydev.parser.jython.ast.TryExcept) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Index(org.python.pydev.parser.jython.ast.Index) Global(org.python.pydev.parser.jython.ast.Global) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Name(org.python.pydev.parser.jython.ast.Name) Suite(org.python.pydev.parser.jython.ast.Suite) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) Assert(org.python.pydev.parser.jython.ast.Assert) IfExp(org.python.pydev.parser.jython.ast.IfExp) FuncDefReturnAnn(org.python.pydev.parser.grammarcommon.FuncDefReturnAnn) List(org.python.pydev.parser.jython.ast.List) ArrayList(java.util.ArrayList) org.python.pydev.parser.jython.ast.argumentsType(org.python.pydev.parser.jython.ast.argumentsType) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) DefaultArg(org.python.pydev.parser.grammarcommon.DefaultArg) Expr(org.python.pydev.parser.jython.ast.Expr) TryFinally(org.python.pydev.parser.jython.ast.TryFinally) Slice(org.python.pydev.parser.jython.ast.Slice) NonLocal(org.python.pydev.parser.jython.ast.NonLocal) org.python.pydev.parser.jython.ast.sliceType(org.python.pydev.parser.jython.ast.sliceType) Set(org.python.pydev.parser.jython.ast.Set) ExtraArg(org.python.pydev.parser.grammarcommon.ExtraArg) JfpDef(org.python.pydev.parser.grammarcommon.JfpDef) Decorators(org.python.pydev.parser.grammarcommon.Decorators) Subscript(org.python.pydev.parser.jython.ast.Subscript) org.python.pydev.parser.jython.ast.keywordType(org.python.pydev.parser.jython.ast.keywordType) Lambda(org.python.pydev.parser.jython.ast.Lambda) Call(org.python.pydev.parser.jython.ast.Call) Return(org.python.pydev.parser.jython.ast.Return) Ellipsis(org.python.pydev.parser.jython.ast.Ellipsis) ListComp(org.python.pydev.parser.jython.ast.ListComp) While(org.python.pydev.parser.jython.ast.While) org.python.pydev.parser.jython.ast.suiteType(org.python.pydev.parser.jython.ast.suiteType) ExtraArgValue(org.python.pydev.parser.grammarcommon.ExtraArgValue) org.python.pydev.parser.jython.ast.excepthandlerType(org.python.pydev.parser.jython.ast.excepthandlerType) ComprehensionCollection(org.python.pydev.parser.grammarcommon.ComprehensionCollection) Assign(org.python.pydev.parser.jython.ast.Assign) Yield(org.python.pydev.parser.jython.ast.Yield) Starred(org.python.pydev.parser.jython.ast.Starred) Raise(org.python.pydev.parser.jython.ast.Raise) Await(org.python.pydev.parser.jython.ast.Await) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 4 with ComprehensionCollection

use of org.python.pydev.parser.grammarcommon.ComprehensionCollection in project Pydev by fabioz.

the class TreeBuilder310 method onCloseNode.

@Override
public final SimpleNode onCloseNode(SimpleNode n, int arity) throws Exception {
    exprType value;
    exprType[] exprs;
    Suite orelseSuite;
    stmtType[] body;
    Suite suite;
    int l;
    exprType awaitExpr;
    exprType target;
    switch(n.getId()) {
        case JJTEXPR_STMT:
            value = (exprType) stack.popNode();
            if (arity > 1) {
                exprs = makeExprs(arity - 1);
                ctx.setStore(exprs);
                return new Assign(exprs, value, null);
            } else {
                return new Expr(value);
            }
        case JJTINDEX_OP:
            sliceType slice = (sliceType) stack.popNode();
            value = (exprType) stack.popNode();
            return new Subscript(value, slice, Subscript.Load);
        case JJTBEGIN_FOR_ELSE_STMT:
            return new Suite(null);
        case JJTBEGIN_ELSE_STMT:
            return new Suite(null);
        case JJTBEGIN_WHILE_STMT:
            return new While(null, null, null);
        case JJTWHILE_STMT:
            orelseSuite = null;
            if (stack.nodeArity() == 5) {
                orelseSuite = popSuiteAndSuiteType();
            }
            body = popSuite();
            exprType test = (exprType) stack.popNode();
            While w = (While) stack.popNode();
            w.test = test;
            w.body = body;
            w.orelse = orelseSuite;
            return w;
        case JJTCALL_OP:
            exprType starargs = null;
            exprType kwargs = null;
            java.util.List<exprType> args = new ArrayList<exprType>();
            java.util.List<keywordType> keywords = new ArrayList<keywordType>();
            for (int i = arity - 2; i >= 0; i--) {
                SimpleNode node = stack.popNode();
                if (node instanceof keywordType) {
                    keywordType keyword = (keywordType) node;
                    keywords.add(0, keyword);
                    if (starargs == null) {
                        // note that we get things backward in the stack
                        keyword.afterstarargs = true;
                    }
                } else if (node.getId() == JJTEXTRAARGVALUELIST) {
                    ExtraArgValue nstarargs = (ExtraArgValue) node;
                    starargs = nstarargs.value;
                    this.addSpecialsAndClearOriginal(nstarargs, starargs);
                } else if (node.getId() == JJTEXTRAKEYWORDVALUELIST) {
                    ExtraArgValue nkwargs = (ExtraArgValue) node;
                    kwargs = nkwargs.value;
                    this.addSpecialsAndClearOriginal(nkwargs, kwargs);
                } else if (node instanceof ComprehensionCollection) {
                    // what can happen is something like print sum(x for x in y), where we have already passed x in the args, and then get 'for x in y'
                    args.add(0, new ListComp((exprType) stack.popNode(), ((ComprehensionCollection) node).getGenerators(), ListComp.EmptyCtx));
                    // popped node
                    i--;
                } else {
                    args.add(0, (exprType) node);
                }
            }
            exprType func = (exprType) stack.popNode();
            Call c = new Call(func, args.toArray(new exprType[args.size()]), keywords.toArray(new keywordType[keywords.size()]), starargs, kwargs);
            addSpecialsAndClearOriginal(n, c);
            return c;
        case JJTFUNCDEF_RETURN_ANNOTTATION:
            SimpleNode funcdefReturn = stack.popNode();
            return new FuncDefReturnAnn(funcdefReturn);
        case JJTFUNCDEF:
            suite = (Suite) stack.popNode();
            body = suite.body;
            arity--;
            SimpleNode funcDefReturnAnn = stack.peekNode();
            exprType actualReturnAnnotation = null;
            if (funcDefReturnAnn instanceof FuncDefReturnAnn) {
                stack.popNode();
                actualReturnAnnotation = (exprType) ((FuncDefReturnAnn) funcDefReturnAnn).node;
                arity--;
                addSpecialsAndClearOriginal(funcDefReturnAnn, actualReturnAnnotation);
            }
            argumentsType arguments = makeArguments(arity - 1);
            NameTok nameTok = makeNameTok(NameTok.FunctionName);
            // decorator is always null at this point... it's decorated later on
            FunctionDef funcDef = new FunctionDef(nameTok, arguments, body, null, actualReturnAnnotation, this.stack.getGrammar().getInsideAsync());
            addSpecialsAndClearOriginal(suite, funcDef);
            setParentForFuncOrClass(body, funcDef);
            return funcDef;
        case JJTTFPDEF:
            Name tfpdefName = null;
            exprType typeDef = null;
            if (arity == 1) {
                tfpdefName = (Name) stack.popNode();
            } else if (arity == 2) {
                typeDef = (exprType) stack.popNode();
                tfpdefName = (Name) stack.popNode();
            } else {
                throw new RuntimeException("Unexpected arity: " + arity);
            }
            return new JfpDef(tfpdefName, typeDef);
        case JJTONLYKEYWORDARG2:
        case JJTDEFAULTARG2:
            DefaultArg defaultArg;
            JfpDef jfpDef;
            if (arity == 1) {
                jfpDef = (JfpDef) stack.popNode();
                defaultArg = new DefaultArg(jfpDef.nameNode, null, jfpDef.typeDef, n.getId());
            } else if (arity == 2) {
                exprType defaultValue = (exprType) stack.popNode();
                jfpDef = (JfpDef) stack.popNode();
                defaultArg = new DefaultArg(jfpDef.nameNode, defaultValue, jfpDef.typeDef, n.getId());
            } else {
                throw new RuntimeException("Unexpected arity: " + arity);
            }
            return defaultArg;
        case JJTONLYKEYWORDARG:
        case JJTDEFAULTARG:
            // no type definition in this case
            if (arity == 1) {
                return new DefaultArg(((exprType) stack.popNode()), null, null, n.getId());
            }
            exprType parameter = (exprType) stack.popNode();
            return new DefaultArg((exprType) stack.popNode(), parameter, null, n.getId());
        case JJTEXTRAARGLIST:
            if (arity == 0) {
                // nothing here (just '*')
                return new ExtraArg(null, JJTEXTRAARGLIST, null);
            }
            return new ExtraArg(makeNameTok(NameTok.VarArg), JJTEXTRAARGLIST);
        case JJTEXTRAKEYWORDLIST:
            return new ExtraArg(makeNameTok(NameTok.KwArg), JJTEXTRAKEYWORDLIST);
        case // with type declaration
        JJTEXTRAARGLIST2:
            if (arity == 0) {
                // nothing here (just '*')
                return new ExtraArg(null, JJTEXTRAARGLIST, null);
            }
            jfpDef = (JfpDef) stack.popNode();
            NameTok jfpDefName = makeNameTok(NameTok.VarArg, jfpDef.nameNode);
            ExtraArg extra = new ExtraArg(jfpDefName, JJTEXTRAARGLIST, jfpDef.typeDef);
            return extra;
        case // with type declaration
        JJTEXTRAKEYWORDLIST2:
            jfpDef = (JfpDef) stack.popNode();
            return new ExtraArg(makeNameTok(NameTok.KwArg, jfpDef.nameNode), JJTEXTRAKEYWORDLIST, jfpDef.typeDef);
        case JJTDECORATED:
            if (stack.nodeArity() != 2) {
                throw new RuntimeException("Expected 2 nodes at this context, found: " + arity);
            }
            SimpleNode def = stack.popNode();
            Decorators decorators = (Decorators) stack.popNode();
            if (def instanceof ClassDef) {
                ClassDef classDef = (ClassDef) def;
                classDef.decs = decorators.exp;
            } else {
                FunctionDef fDef = (FunctionDef) def;
                fDef.decs = decorators.exp;
            }
            return def;
        case JJTCLASSDEF:
            suite = (Suite) stack.popNode();
            body = suite.body;
            int nodeArity = stack.nodeArity() - 1;
            ArrayList<keywordType> classDefKeywords = new ArrayList<keywordType>();
            starargs = null;
            kwargs = null;
            int loopTo = nodeArity;
            for (int i = 0; i < loopTo; i++) {
                SimpleNode node = stack.peekNode();
                if (node instanceof keywordType) {
                    stack.popNode();
                    keywordType keyword = (keywordType) node;
                    classDefKeywords.add(keyword);
                    if (starargs == null) {
                        // note that we get things backward in the stack
                        keyword.afterstarargs = true;
                    }
                    nodeArity--;
                } else if (node instanceof ExtraArgValue) {
                    if (node.getId() == JJTEXTRAARGVALUELIST) {
                        ExtraArgValue nstarargs = (ExtraArgValue) stack.popNode();
                        starargs = nstarargs.value;
                        this.addSpecialsAndClearOriginal(nstarargs, starargs);
                        nodeArity--;
                    } else if (node.getId() == JJTEXTRAKEYWORDVALUELIST) {
                        ExtraArgValue nkwargs = (ExtraArgValue) stack.popNode();
                        kwargs = nkwargs.value;
                        this.addSpecialsAndClearOriginal(nkwargs, kwargs);
                        nodeArity--;
                    }
                } else {
                    break;
                }
            }
            if (classDefKeywords.size() > 1) {
                Collections.reverse(classDefKeywords);
            }
            exprType[] bases = makeExprs(nodeArity);
            nameTok = makeNameTok(NameTok.ClassName);
            // decorator is always null at this point... it's decorated later on
            ClassDef classDef = new ClassDef(nameTok, bases, body, null, classDefKeywords.toArray(new keywordType[classDefKeywords.size()]), starargs, kwargs);
            addSpecialsAndClearOriginal(suite, classDef);
            setParentForFuncOrClass(body, classDef);
            return classDef;
        case JJTBEGIN_RETURN_STMT:
            return new Return(null);
        case JJTRETURN_STMT:
            value = arity == 2 ? ((exprType) stack.popNode()) : null;
            Return ret = (Return) stack.popNode();
            ret.value = value;
            return ret;
        case JJTYIELD_STMT:
            return stack.popNode();
        case JJTYIELD_EXPR:
            exprType yieldExpr = null;
            if (arity > 0) {
                // we may have an empty yield, so, we have to check it before
                yieldExpr = (exprType) stack.popNode();
            }
            return new Yield(yieldExpr, false);
        case JJTRAISE_STMT:
            exprType from = arity >= 2 ? ((exprType) stack.popNode()) : null;
            exprType type = arity >= 1 ? ((exprType) stack.popNode()) : null;
            return new Raise(type, null, null, from);
        case JJTGLOBAL_STMT:
            return new Global(makeIdentifiers(NameTok.GlobalName), null);
        case JJTNONLOCAL_STMT:
            return new NonLocal(makeIdentifiers(NameTok.NonLocalName), null);
        case JJTASSERT_STMT:
            exprType msg = arity == 2 ? ((exprType) stack.popNode()) : null;
            test = (exprType) stack.popNode();
            return new Assert(test, msg);
        case JJTBEGIN_TRY_STMT:
            // we do that just to get the specials
            return new TryExcept(null, null, null);
        case JJTTRYELSE_STMT:
            orelseSuite = popSuiteAndSuiteType();
            return orelseSuite;
        case JJTTRYFINALLY_OUTER_STMT:
            orelseSuite = popSuiteAndSuiteType();
            // it does not have a body at this time... it will be filled with the inner try..except
            return new TryFinally(null, orelseSuite);
        case JJTTRY_STMT:
            TryFinally outer = null;
            if (stack.peekNode() instanceof TryFinally) {
                outer = (TryFinally) stack.popNode();
                arity--;
            }
            orelseSuite = null;
            if (stack.peekNode() instanceof suiteType) {
                orelseSuite = (Suite) stack.popNode();
                arity--;
            }
            l = arity;
            excepthandlerType[] handlers = new excepthandlerType[l];
            for (int i = l - 1; i >= 0; i--) {
                handlers[i] = (excepthandlerType) stack.popNode();
            }
            suite = (Suite) stack.popNode();
            TryExcept tryExc = (TryExcept) stack.popNode();
            if (outer != null) {
                outer.beginLine = tryExc.beginLine;
            }
            tryExc.body = suite.body;
            tryExc.handlers = handlers;
            tryExc.orelse = orelseSuite;
            addSpecials(suite, tryExc);
            if (outer == null) {
                return tryExc;
            } else {
                if (outer.body != null) {
                    throw new RuntimeException("Error. Expecting null body to be filled on try..except..finally");
                }
                outer.body = new stmtType[] { tryExc };
                return outer;
            }
        case JJTBEGIN_TRY_ELSE_STMT:
            // we do that just to get the specials
            return new Suite(null);
        case JJTBEGIN_EXCEPT_CLAUSE:
            return new excepthandlerType(null, null, null);
        case JJTEXCEPT_CLAUSE:
            suite = (Suite) stack.popNode();
            body = suite.body;
            exprType excname = arity == 4 ? ((exprType) stack.popNode()) : null;
            if (excname != null) {
                ctx.setStore(excname);
            }
            type = arity >= 3 ? ((exprType) stack.popNode()) : null;
            excepthandlerType handler = (excepthandlerType) stack.popNode();
            handler.type = type;
            handler.name = excname;
            handler.body = body;
            addSpecials(suite, handler);
            return handler;
        case JJTBEGIN_FINALLY_STMT:
            // we do that just to get the specials
            return new Suite(null);
        case JJTTRYFINALLY_STMT:
            suiteType finalBody = popSuiteAndSuiteType();
            body = popSuite();
            // We have a try..except in the stack, but we will change it for a try..finally
            // This is because we recognize a try..except in the 'try:' token, but actually end up with a try..finally
            TryExcept tryExcept = (TryExcept) stack.popNode();
            TryFinally tryFinally = new TryFinally(body, finalBody);
            tryFinally.beginLine = tryExcept.beginLine;
            tryFinally.beginColumn = tryExcept.beginColumn;
            addSpecialsAndClearOriginal(tryExcept, tryFinally);
            return tryFinally;
        case JJTNAMEDEXPR_TEST:
            if (arity == 1) {
                return stack.popNode();
            }
            value = (exprType) stack.popNode();
            target = (exprType) stack.popNode();
            ctx.setNamedStore(target);
            NamedExpr namedExpr = (NamedExpr) n;
            namedExpr.value = value;
            namedExpr.target = target;
            return n;
        case JJTWITH_STMT:
            return makeWithStmt(arity);
        case JJTWITH_ITEM:
            return makeWithItem(arity);
        case JJTEXTRAKEYWORDVALUELIST:
            return new ExtraArgValue(((exprType) stack.popNode()), JJTEXTRAKEYWORDVALUELIST);
        case JJTEXTRAARGVALUELIST:
            return new ExtraArgValue(((exprType) stack.popNode()), JJTEXTRAARGVALUELIST);
        case JJTARGUMENT:
            SimpleNode keyword = stack.popNode();
            if (keyword instanceof keywordType) {
                nameTok = makeNameTok(NameTok.KeywordName);
                ((keywordType) keyword).arg = nameTok;
            }
            return keyword;
        case JJTKEYWORD:
            value = (exprType) stack.popNode();
            return new keywordType(null, value, false);
        case JJTTUPLE:
            if (stack.nodeArity() > 0) {
                SimpleNode peeked = stack.peekNode();
                if (peeked instanceof ComprehensionCollection) {
                    ComprehensionCollection col = (ComprehensionCollection) stack.popNode();
                    return new ListComp(((exprType) stack.popNode()), col.getGenerators(), ListComp.TupleCtx);
                }
            }
            return makeTuple(n);
        case JJTLIST:
            if (stack.nodeArity() > 0 && stack.peekNode() instanceof ComprehensionCollection) {
                ComprehensionCollection col = (ComprehensionCollection) stack.popNode();
                return new ListComp(((exprType) stack.popNode()), col.getGenerators(), ListComp.ListCtx);
            }
            return new List(makeExprs(), List.Load);
        case JJTSET:
            return new Set(null);
        case JJTDICTIONARY:
            return makeDictionaryOrSet(arity);
        // return new Repr(((exprType) stack.popNode()));
        case JJTTEST:
            if (arity == 2) {
                IfExp node = (IfExp) stack.popNode();
                node.body = (exprType) stack.popNode();
                return node;
            } else {
                return stack.popNode();
            }
        case JJTIF_EXP:
            exprType ifExprOrelse = (exprType) stack.popNode();
            exprType ifExprTest = (exprType) stack.popNode();
            return new IfExp(ifExprTest, null, ifExprOrelse);
        case JJTLAMBDEF_NOCOND:
        case JJTLAMBDEF:
            test = (exprType) stack.popNode();
            arguments = makeArguments(arity - 1);
            Lambda lambda = new Lambda(arguments, test);
            // }
            return lambda;
        case JJTELLIPSIS:
            return new Ellipsis();
        case JJTELLIPSIS_AS_NAME:
            return new Name("...", Name.Load, true);
        case JJTSLICE:
            SimpleNode[] arr = new SimpleNode[arity];
            for (int i = arity - 1; i >= 0; i--) {
                arr[i] = stack.popNode();
            }
            exprType[] values = new exprType[3];
            int k = 0;
            java.util.List<Object> specialsBefore = new ArrayList<Object>();
            java.util.List<Object> specialsAfter = new ArrayList<Object>();
            for (int j = 0; j < arity; j++) {
                if (arr[j].getId() == JJTCOLON) {
                    if (arr[j].specialsBefore != null) {
                        specialsBefore.addAll(arr[j].specialsBefore);
                        // this nodes may be reused among parses, so, we have to erase the specials
                        arr[j].specialsBefore.clear();
                    }
                    if (arr[j].specialsAfter != null) {
                        specialsAfter.addAll(arr[j].specialsAfter);
                        arr[j].specialsAfter.clear();
                    }
                    k++;
                } else {
                    values[k] = (exprType) arr[j];
                    if (specialsBefore.size() > 0) {
                        values[k].getSpecialsBefore().addAll(specialsBefore);
                        specialsBefore.clear();
                    }
                    if (specialsAfter.size() > 0) {
                        values[k].getSpecialsBefore().addAll(specialsAfter);
                        specialsAfter.clear();
                    }
                }
            }
            SimpleNode sliceRet;
            if (k == 0) {
                sliceRet = new Index(values[0]);
            } else {
                sliceRet = new Slice(values[0], values[1], values[2]);
            }
            // this may happen if we have no values
            sliceRet.getSpecialsBefore().addAll(specialsBefore);
            sliceRet.getSpecialsAfter().addAll(specialsAfter);
            specialsBefore.clear();
            specialsAfter.clear();
            return sliceRet;
        case JJTCOMP_FOR:
            return makeCompFor(arity);
        case JJTIMPORTFROM:
            return makeImportFrom25Onwards(arity);
        case JJTAWAIT_ATOM_EXPR:
            awaitExpr = (exprType) stack.popNode();
            return new Await(awaitExpr);
        case JJTSTAR_EXPR:
            Starred starred = (Starred) n;
            starred.value = (exprType) this.stack.popNode();
            ctx.setCtx(starred, starred.ctx);
            return starred;
        case JJTANN_ASSIGN:
            return typedDeclaration(arity, stack, ctx);
        case JJTEVAL_INPUT:
            Expr expr = (Expr) n;
            if (arity != 1) {
                Log.log("Expected arity to be == 1 here.");
            }
            for (int i = arity - 1; i >= 0; i--) {
                SimpleNode popNode = stack.popNode();
                try {
                    exprType node = (exprType) popNode;
                    expr = new Expr(node);
                } catch (Exception e) {
                    Log.log("Expected expr. Found: " + popNode);
                }
            }
            return expr;
        case JJTMATCH_STMT:
            if (arity > 1) {
                match_caseType[] cases = new match_caseType[arity - 1];
                for (int i = arity - 2; i >= 0; i--) {
                    cases[i] = (match_caseType) securePop(match_caseType.class);
                }
                exprType subject = (exprType) securePop(exprType.class);
                return new Match(subject, cases);
            }
            addAndReportException(Match.class.getName());
            return new Match(getDefaultInvalidExpr(), new match_caseType[] { getDefaultInvalidMatchCase() });
        case JJTSUBJECT_EXPR:
            popSurplus(arity, 2);
            if (arity == 1) {
                return stack.popNode();
            } else if (arity == 2) {
                exprType[] elts = new exprType[arity];
                for (int i = 0; i < arity; i++) {
                    exprType secureExpr = (exprType) securePop(exprType.class);
                    elts[i] = new Starred(secureExpr, Starred.Load);
                }
                return new Tuple(elts, Starred.Load, false);
            }
            addAndReportException(exprType.class.getName());
            return getDefaultInvalidExpr();
        case JJTCASE_BLOCK:
            if (arity > 1) {
                suite = (Suite) securePop(Suite.class);
                exprType guard = null;
                if (arity > 2) {
                    guard = (exprType) securePopNullable(exprType.class);
                }
                patternType pattern = (patternType) securePop(patternType.class);
                return new match_caseType(pattern, guard, suite.body);
            }
            addAndReportException(match_caseType.class.getName());
            return getDefaultInvalidMatchCase();
        case JJTPATTERN:
            popSurplus(arity, 2);
            if (arity == 1) {
                SimpleNode popNode = stack.popNode();
                if (popNode instanceof exprType) {
                    exprType exprNode = (exprType) popNode;
                    if (exprNode instanceof Name) {
                        Name name = (Name) exprNode;
                        name.ctx = Name.Store;
                    }
                    return new MatchValue((exprType) popNode);
                }
                return popNode;
            } else if (arity == 2) {
                NameTok asname = makeNameTok(NameTok.PatternName);
                patternType pattern = (patternType) securePop(patternType.class);
                return new MatchAs(pattern, asname);
            }
            addAndReportException(patternType.class.getName());
            return getDefaultInvalidPattern();
        case JJTCLOSED_PATTERN:
            popSurplus(arity, 2);
            if (arity == 1) {
                return stack.popNode();
            } else if (arity == 2) {
                MatchClass classPattern = (MatchClass) securePop(MatchClass.class);
                exprType cls = (exprType) securePop(exprType.class);
                return new MatchClass(cls, classPattern.args);
            }
            addAndReportException(exprType.class.getName());
            return getDefaultInvalidExpr();
        case JJTKEY_VALUE_PATTERN:
            popSurplus(arity, 2);
            if (arity == 2) {
                patternType pattern = (patternType) securePop(patternType.class);
                exprType arg = (exprType) securePop(exprType.class);
                return new MatchKeyword(arg, pattern);
            }
            addAndReportException(exprType.class.getName());
            return getDefaultInvalidExpr();
        case JJTMAPPING_PATTERN:
            if (arity > 0) {
                exprType[] keys = new exprType[arity];
                patternType[] patterns = new patternType[arity];
                for (int i = arity - 1; i >= 0; i--) {
                    MatchKeyword pattern = (MatchKeyword) securePop(MatchKeyword.class);
                    keys[i] = pattern.arg;
                    patterns[i] = pattern.value;
                }
                return new MatchMapping(keys, patterns);
            }
            addAndReportException(MatchKeyword.class.getName());
            return new MatchMapping(new exprType[0], new patternType[0]);
        case JJTLIST_PATTERN:
            return createMatchSequence(arity, enclosingType.LIST);
        case JJTTUPLE_PATTERN:
            return createMatchSequence(arity, enclosingType.TUPLE);
        case JJTATTR:
            if (arity == 1) {
                if (isPeekedNodeWildcardPattern()) {
                    return new MatchValue(makeName(Name.Pattern));
                }
                return stack.popNode();
            } else if (arity >= 2) {
                SimpleNode peekedNode = stack.peekNode();
                if (peekedNode instanceof patternType) {
                    popSurplus(arity, 2);
                    patternType pattern = (patternType) securePop(patternType.class);
                    Name arg = makeName(Name.Artificial);
                    return new MatchKeyword(arg, pattern);
                } else {
                    return popAttribute(arity);
                }
            }
            addAndReportException(Attribute.class.getName());
            return getDefaultInvalidExpr();
        case JJTCLASS_PATTERN:
            if (arity > 0) {
                patternType[] patterns = new patternType[arity];
                for (int i = arity - 1; i >= 0; i--) {
                    patterns[i] = (patternType) securePop(patternType.class);
                }
                return new MatchClass(null, patterns);
            }
            // This is expected when the class_pattern has no given args.
            return new MatchClass(null, null);
        case JJTOR_PATTERN:
            if (arity == 1) {
                return stack.popNode();
            } else if (arity > 1) {
                patternType[] patterns = new patternType[arity];
                for (int i = arity - 1; i >= 0; i--) {
                    SimpleNode popNode = stack.popNode();
                    if (popNode instanceof patternType) {
                        patterns[i] = (patternType) popNode;
                    } else if (popNode instanceof exprType) {
                        patterns[i] = new MatchValue((exprType) popNode);
                    } else {
                        patterns[i] = getDefaultInvalidPattern();
                    }
                }
                return new MatchOr(patterns);
            }
            addAndReportException(patternType.class.getName());
            return new MatchOr(new patternType[0]);
        case JJTSTAR_PATTERN:
            popSurplus(arity, 1);
            if (arity == 1) {
                Name name = makeName(Name.Store);
                return new Starred(name, name.ctx);
            }
            addAndReportException(SimpleNode.class.getName());
            return new Starred(getDefaultInvalidExpr(), 0);
        case JJTOPEN_SEQUENCE_PATTERN:
            if (arity == 1) {
                return stack.popNode();
            } else if (arity > 1) {
                patternType[] patterns = new patternType[arity];
                for (int i = arity - 1; i >= 0; i--) {
                    SimpleNode popNode = stack.popNode();
                    if (popNode instanceof patternType) {
                        patterns[i] = (patternType) popNode;
                    } else if (popNode instanceof exprType) {
                        patterns[i] = new MatchValue((exprType) popNode);
                    } else {
                        patterns[i] = getDefaultInvalidPattern();
                    }
                }
                return new MatchSequence(patterns, 0);
            }
            addAndReportException(SimpleNode.class.getName());
            return new MatchSequence(new patternType[0], 0);
        default:
            Log.log(("Error at TreeBuilder: default not treated:" + n.getId()));
            return null;
    }
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) TryExcept(org.python.pydev.parser.jython.ast.TryExcept) NamedExpr(org.python.pydev.parser.jython.ast.NamedExpr) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Index(org.python.pydev.parser.jython.ast.Index) Global(org.python.pydev.parser.jython.ast.Global) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Name(org.python.pydev.parser.jython.ast.Name) Suite(org.python.pydev.parser.jython.ast.Suite) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) Assert(org.python.pydev.parser.jython.ast.Assert) IfExp(org.python.pydev.parser.jython.ast.IfExp) FuncDefReturnAnn(org.python.pydev.parser.grammarcommon.FuncDefReturnAnn) List(org.python.pydev.parser.jython.ast.List) ArrayList(java.util.ArrayList) MatchMapping(org.python.pydev.parser.jython.ast.MatchMapping) org.python.pydev.parser.jython.ast.argumentsType(org.python.pydev.parser.jython.ast.argumentsType) MatchClass(org.python.pydev.parser.jython.ast.MatchClass) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) DefaultArg(org.python.pydev.parser.grammarcommon.DefaultArg) Expr(org.python.pydev.parser.jython.ast.Expr) NamedExpr(org.python.pydev.parser.jython.ast.NamedExpr) TryFinally(org.python.pydev.parser.jython.ast.TryFinally) Slice(org.python.pydev.parser.jython.ast.Slice) NonLocal(org.python.pydev.parser.jython.ast.NonLocal) MatchValue(org.python.pydev.parser.jython.ast.MatchValue) org.python.pydev.parser.jython.ast.sliceType(org.python.pydev.parser.jython.ast.sliceType) MatchOr(org.python.pydev.parser.jython.ast.MatchOr) org.python.pydev.parser.jython.ast.match_caseType(org.python.pydev.parser.jython.ast.match_caseType) Set(org.python.pydev.parser.jython.ast.Set) ExtraArg(org.python.pydev.parser.grammarcommon.ExtraArg) Attribute(org.python.pydev.parser.jython.ast.Attribute) JfpDef(org.python.pydev.parser.grammarcommon.JfpDef) Decorators(org.python.pydev.parser.grammarcommon.Decorators) Subscript(org.python.pydev.parser.jython.ast.Subscript) Match(org.python.pydev.parser.jython.ast.Match) org.python.pydev.parser.jython.ast.keywordType(org.python.pydev.parser.jython.ast.keywordType) Lambda(org.python.pydev.parser.jython.ast.Lambda) MatchSequence(org.python.pydev.parser.jython.ast.MatchSequence) MatchAs(org.python.pydev.parser.jython.ast.MatchAs) Call(org.python.pydev.parser.jython.ast.Call) Return(org.python.pydev.parser.jython.ast.Return) Ellipsis(org.python.pydev.parser.jython.ast.Ellipsis) ListComp(org.python.pydev.parser.jython.ast.ListComp) org.python.pydev.parser.jython.ast.patternType(org.python.pydev.parser.jython.ast.patternType) While(org.python.pydev.parser.jython.ast.While) org.python.pydev.parser.jython.ast.suiteType(org.python.pydev.parser.jython.ast.suiteType) ParseException(org.python.pydev.parser.jython.ParseException) ExtraArgValue(org.python.pydev.parser.grammarcommon.ExtraArgValue) org.python.pydev.parser.jython.ast.excepthandlerType(org.python.pydev.parser.jython.ast.excepthandlerType) MatchKeyword(org.python.pydev.parser.jython.ast.MatchKeyword) ComprehensionCollection(org.python.pydev.parser.grammarcommon.ComprehensionCollection) Assign(org.python.pydev.parser.jython.ast.Assign) Yield(org.python.pydev.parser.jython.ast.Yield) Starred(org.python.pydev.parser.jython.ast.Starred) Raise(org.python.pydev.parser.jython.ast.Raise) Await(org.python.pydev.parser.jython.ast.Await) NameTok(org.python.pydev.parser.jython.ast.NameTok) Tuple(org.python.pydev.parser.jython.ast.Tuple)

Example 5 with ComprehensionCollection

use of org.python.pydev.parser.grammarcommon.ComprehensionCollection in project Pydev by fabioz.

the class TreeBuilder25 method onCloseNode.

@Override
public final SimpleNode onCloseNode(SimpleNode n, int arity) throws Exception {
    exprType value;
    exprType[] exprs;
    Suite orelseSuite;
    stmtType[] body;
    exprType iter;
    exprType target;
    Suite suite;
    int l;
    switch(n.getId()) {
        case JJTEXPR_STMT:
            value = (exprType) stack.popNode();
            if (arity > 1) {
                exprs = makeExprs(arity - 1);
                ctx.setStore(exprs);
                return new Assign(exprs, value, null);
            } else {
                return new Expr(value);
            }
        case JJTINDEX_OP:
            sliceType slice = (sliceType) stack.popNode();
            value = (exprType) stack.popNode();
            return new Subscript(value, slice, Subscript.Load);
        case JJTPRINT_STMT:
            boolean nl = true;
            if (stack.nodeArity() == 0) {
                Print p = new Print(null, null, true);
                return p;
            }
            if (stack.peekNode().getId() == JJTCOMMA) {
                stack.popNode();
                nl = false;
            }
            Print p = new Print(null, makeExprs(), nl);
            return p;
        case JJTPRINTEXT_STMT:
            nl = true;
            if (stack.peekNode().getId() == JJTCOMMA) {
                stack.popNode();
                nl = false;
            }
            exprs = makeExprs(stack.nodeArity() - 1);
            p = new Print(((exprType) stack.popNode()), exprs, nl);
            return p;
        case JJTBEGIN_FOR_ELSE_STMT:
            return new Suite(null);
        case JJTBEGIN_ELSE_STMT:
            return new Suite(null);
        case JJTBEGIN_WHILE_STMT:
            return new While(null, null, null);
        case JJTWHILE_STMT:
            orelseSuite = null;
            if (stack.nodeArity() == 5) {
                orelseSuite = popSuiteAndSuiteType();
            }
            body = popSuite();
            exprType test = (exprType) stack.popNode();
            While w = (While) stack.popNode();
            w.test = test;
            w.body = body;
            w.orelse = orelseSuite;
            return w;
        case JJTCALL_OP:
            exprType starargs = null;
            exprType kwargs = null;
            l = arity - 1;
            if (l > 0 && stack.peekNode().getId() == JJTEXTRAKEYWORDVALUELIST) {
                ExtraArgValue nkwargs = (ExtraArgValue) stack.popNode();
                kwargs = nkwargs.value;
                this.addSpecialsAndClearOriginal(nkwargs, kwargs);
                l--;
            }
            if (l > 0 && stack.peekNode().getId() == JJTEXTRAARGVALUELIST) {
                ExtraArgValue nstarargs = (ExtraArgValue) stack.popNode();
                starargs = nstarargs.value;
                this.addSpecialsAndClearOriginal(nstarargs, starargs);
                l--;
            }
            int nargs = l;
            SimpleNode[] tmparr = new SimpleNode[l];
            for (int i = l - 1; i >= 0; i--) {
                tmparr[i] = stack.popNode();
                if (tmparr[i] instanceof keywordType) {
                    nargs = i;
                }
            }
            exprType[] args = new exprType[nargs];
            for (int i = 0; i < nargs; i++) {
                // what can happen is something like print sum(x for x in y), where we have already passed x in the args, and then get 'for x in y'
                if (tmparr[i] instanceof ComprehensionCollection) {
                    args = new exprType[] { new ListComp(args[0], ((ComprehensionCollection) tmparr[i]).getGenerators(), ListComp.EmptyCtx) };
                } else {
                    args[i] = (exprType) tmparr[i];
                }
            }
            keywordType[] keywords = new keywordType[l - nargs];
            for (int i = nargs; i < l; i++) {
                if (!(tmparr[i] instanceof keywordType)) {
                    throw new ParseException("non-keyword argument following keyword", tmparr[i]);
                }
                keywords[i - nargs] = (keywordType) tmparr[i];
            }
            exprType func = (exprType) stack.popNode();
            Call c = new Call(func, args, keywords, starargs, kwargs);
            addSpecialsAndClearOriginal(n, c);
            return c;
        case JJTFUNCDEF:
            // get the decorators
            // and clear them for the next call (they always must be before a function def)
            suite = (Suite) stack.popNode();
            body = suite.body;
            argumentsType arguments = makeArguments(stack.nodeArity() - 2);
            NameTok nameTok = makeNameTok(NameTok.FunctionName);
            Decorators decs = (Decorators) stack.popNode();
            decoratorsType[] decsexp = decs.exp;
            FunctionDef funcDef = new FunctionDef(nameTok, arguments, body, decsexp, null, false);
            if (decs.exp.length == 0) {
                addSpecialsBefore(decs, funcDef);
            }
            addSpecialsAndClearOriginal(suite, funcDef);
            setParentForFuncOrClass(body, funcDef);
            return funcDef;
        case JJTDEFAULTARG:
            value = (arity == 1) ? null : ((exprType) stack.popNode());
            return new DefaultArg(((exprType) stack.popNode()), value, n.getId());
        case JJTEXTRAARGLIST:
            return new ExtraArg(makeNameTok(NameTok.VarArg), JJTEXTRAARGLIST);
        case JJTEXTRAKEYWORDLIST:
            return new ExtraArg(makeNameTok(NameTok.KwArg), JJTEXTRAKEYWORDLIST);
        case JJTCLASSDEF:
            suite = (Suite) stack.popNode();
            body = suite.body;
            exprType[] bases = makeExprs(stack.nodeArity() - 1);
            nameTok = makeNameTok(NameTok.ClassName);
            ClassDef classDef = new ClassDef(nameTok, bases, body, null, null, null, null);
            addSpecialsAndClearOriginal(suite, classDef);
            setParentForFuncOrClass(body, classDef);
            return classDef;
        case JJTBEGIN_RETURN_STMT:
            return new Return(null);
        case JJTRETURN_STMT:
            value = arity == 2 ? ((exprType) stack.popNode()) : null;
            Return ret = (Return) stack.popNode();
            ret.value = value;
            return ret;
        case JJTYIELD_STMT:
            return stack.popNode();
        case JJTYIELD_EXPR:
            exprType yieldExpr = null;
            if (arity > 0) {
                // we may have an empty yield, so, we have to check it before
                yieldExpr = (exprType) stack.popNode();
            }
            return new Yield(yieldExpr, false);
        case JJTRAISE_STMT:
            exprType tback = arity >= 3 ? ((exprType) stack.popNode()) : null;
            exprType inst = arity >= 2 ? ((exprType) stack.popNode()) : null;
            exprType type = arity >= 1 ? ((exprType) stack.popNode()) : null;
            return new Raise(type, inst, tback, null);
        case JJTGLOBAL_STMT:
            Global global = new Global(makeIdentifiers(NameTok.GlobalName), null);
            return global;
        case JJTASSERT_STMT:
            exprType msg = arity == 2 ? ((exprType) stack.popNode()) : null;
            test = (exprType) stack.popNode();
            return new Assert(test, msg);
        case JJTBEGIN_TRY_STMT:
            // we do that just to get the specials
            return new TryExcept(null, null, null);
        case JJTTRYELSE_STMT:
            orelseSuite = popSuiteAndSuiteType();
            return orelseSuite;
        case JJTTRYFINALLY_OUTER_STMT:
            orelseSuite = popSuiteAndSuiteType();
            // it does not have a body at this time... it will be filled with the inner try..except
            return new TryFinally(null, orelseSuite);
        case JJTTRY_STMT:
            TryFinally outer = null;
            if (stack.peekNode() instanceof TryFinally) {
                outer = (TryFinally) stack.popNode();
                arity--;
            }
            orelseSuite = null;
            if (stack.peekNode() instanceof suiteType) {
                orelseSuite = (Suite) stack.popNode();
                arity--;
            }
            l = arity;
            excepthandlerType[] handlers = new excepthandlerType[l];
            for (int i = l - 1; i >= 0; i--) {
                handlers[i] = (excepthandlerType) stack.popNode();
            }
            suite = (Suite) stack.popNode();
            TryExcept tryExc = (TryExcept) stack.popNode();
            if (outer != null) {
                outer.beginLine = tryExc.beginLine;
            }
            tryExc.body = suite.body;
            tryExc.handlers = handlers;
            tryExc.orelse = orelseSuite;
            addSpecials(suite, tryExc);
            if (outer == null) {
                return tryExc;
            } else {
                if (outer.body != null) {
                    throw new RuntimeException("Error. Expecting null body to be filled on try..except..finally");
                }
                outer.body = new stmtType[] { tryExc };
                return outer;
            }
        case JJTBEGIN_TRY_ELSE_STMT:
            // we do that just to get the specials
            return new Suite(null);
        case JJTBEGIN_EXCEPT_CLAUSE:
            return new excepthandlerType(null, null, null);
        case JJTEXCEPT_CLAUSE:
            suite = (Suite) stack.popNode();
            body = suite.body;
            exprType excname = arity == 4 ? ((exprType) stack.popNode()) : null;
            if (excname != null) {
                ctx.setStore(excname);
            }
            type = arity >= 3 ? ((exprType) stack.popNode()) : null;
            excepthandlerType handler = (excepthandlerType) stack.popNode();
            handler.type = type;
            handler.name = excname;
            handler.body = body;
            addSpecials(suite, handler);
            return handler;
        case JJTBEGIN_FINALLY_STMT:
            // we do that just to get the specials
            return new Suite(null);
        case JJTTRYFINALLY_STMT:
            suiteType finalBody = popSuiteAndSuiteType();
            body = popSuite();
            // We have a try..except in the stack, but we will change it for a try..finally
            // This is because we recognize a try..except in the 'try:' token, but actually end up with a try..finally
            TryExcept tryExcept = (TryExcept) stack.popNode();
            TryFinally tryFinally = new TryFinally(body, finalBody);
            tryFinally.beginLine = tryExcept.beginLine;
            tryFinally.beginColumn = tryExcept.beginColumn;
            addSpecialsAndClearOriginal(tryExcept, tryFinally);
            return tryFinally;
        case JJTWITH_STMT:
            suite = (Suite) stack.popNode();
            arity--;
            exprType asOrExpr = (exprType) stack.popNode();
            arity--;
            exprType expr = null;
            if (arity > 0) {
                expr = (exprType) stack.popNode();
                arity--;
            } else {
                expr = asOrExpr;
                asOrExpr = null;
            }
            suiteType s = new Suite(suite.body);
            addSpecialsAndClearOriginal(suite, s);
            return new With(new WithItem[] { new WithItem(expr, asOrExpr) }, s, false);
        case JJTWITH_VAR:
            // expr
            expr = (exprType) stack.popNode();
            if (expr != null) {
                ctx.setStore(expr);
            }
            return expr;
        case JJTEXTRAKEYWORDVALUELIST:
            return new ExtraArgValue(((exprType) stack.popNode()), JJTEXTRAKEYWORDVALUELIST);
        case JJTEXTRAARGVALUELIST:
            return new ExtraArgValue(((exprType) stack.popNode()), JJTEXTRAARGVALUELIST);
        case JJTKEYWORD:
            value = (exprType) stack.popNode();
            nameTok = makeNameTok(NameTok.KeywordName);
            return new keywordType(nameTok, value, false);
        case JJTTUPLE:
            if (stack.nodeArity() > 0) {
                SimpleNode peeked = stack.peekNode();
                if (peeked instanceof ComprehensionCollection) {
                    ComprehensionCollection col = (ComprehensionCollection) stack.popNode();
                    return new ListComp(((exprType) stack.popNode()), col.getGenerators(), ListComp.TupleCtx);
                }
            }
            return makeTuple(n);
        case JJTLIST:
            if (stack.nodeArity() > 0 && stack.peekNode() instanceof ComprehensionCollection) {
                ComprehensionCollection col = (ComprehensionCollection) stack.popNode();
                return new ListComp(((exprType) stack.popNode()), col.getGenerators(), ListComp.ListCtx);
            }
            return new List(makeExprs(), List.Load);
        case JJTDICTIONARY:
            return defaultCreateDictionary(arity);
        case JJTSTR_1OP:
            return new Repr(((exprType) stack.popNode()));
        case JJTTEST:
            if (arity == 2) {
                IfExp node = (IfExp) stack.popNode();
                node.body = (exprType) stack.popNode();
                return node;
            } else {
                return stack.popNode();
            }
        case JJTIF_EXP:
            exprType ifExprOrelse = (exprType) stack.popNode();
            exprType ifExprTest = (exprType) stack.popNode();
            return new IfExp(ifExprTest, null, ifExprOrelse);
        case JJTOLD_LAMBDEF:
        case JJTLAMBDEF:
            test = (exprType) stack.popNode();
            arguments = makeArguments(arity - 1);
            Lambda lambda = new Lambda(arguments, test);
            // }
            return lambda;
        case JJTELLIPSIS:
            return new Ellipsis();
        case JJTSLICE:
            SimpleNode[] arr = new SimpleNode[arity];
            for (int i = arity - 1; i >= 0; i--) {
                arr[i] = stack.popNode();
            }
            exprType[] values = new exprType[3];
            int k = 0;
            java.util.List<Object> specialsBefore = new ArrayList<Object>();
            java.util.List<Object> specialsAfter = new ArrayList<Object>();
            for (int j = 0; j < arity; j++) {
                if (arr[j].getId() == JJTCOLON) {
                    if (arr[j].specialsBefore != null) {
                        specialsBefore.addAll(arr[j].specialsBefore);
                        // this nodes may be reused among parses, so, we have to erase the specials
                        arr[j].specialsBefore.clear();
                    }
                    if (arr[j].specialsAfter != null) {
                        specialsAfter.addAll(arr[j].specialsAfter);
                        arr[j].specialsAfter.clear();
                    }
                    k++;
                } else {
                    values[k] = (exprType) arr[j];
                    if (specialsBefore.size() > 0) {
                        values[k].getSpecialsBefore().addAll(specialsBefore);
                        specialsBefore.clear();
                    }
                    if (specialsAfter.size() > 0) {
                        values[k].getSpecialsBefore().addAll(specialsAfter);
                        specialsAfter.clear();
                    }
                }
            }
            SimpleNode sliceRet;
            if (k == 0) {
                sliceRet = new Index(values[0]);
            } else {
                sliceRet = new Slice(values[0], values[1], values[2]);
            }
            // this may happen if we have no values
            sliceRet.getSpecialsBefore().addAll(specialsBefore);
            sliceRet.getSpecialsAfter().addAll(specialsAfter);
            specialsBefore.clear();
            specialsAfter.clear();
            return sliceRet;
        case JJTLIST_FOR:
            ComprehensionCollection col = null;
            if (stack.peekNode() instanceof ComprehensionCollection) {
                col = (ComprehensionCollection) stack.popNode();
                arity--;
            } else {
                col = new ComprehensionCollection();
            }
            ArrayList<exprType> ifs = new ArrayList<exprType>();
            for (int i = arity - 3; i >= 0; i--) {
                SimpleNode ifsNode = stack.popNode();
                ifs.add((exprType) ifsNode);
            }
            iter = (exprType) stack.popNode();
            target = (exprType) stack.popNode();
            ctx.setStore(target);
            col.added.add(new Comprehension(target, iter, ifs.toArray(new exprType[0])));
            return col;
        case JJTIMPORTFROM:
            return makeImportFrom25Onwards(arity);
        case JJTSTAR_EXPR:
            Starred starred = (Starred) n;
            starred.value = (exprType) this.stack.popNode();
            ctx.setStore(starred);
            return starred;
        default:
            Log.log(("Error at TreeBuilder: default not treated:" + n.getId()));
            return null;
    }
}
Also used : org.python.pydev.parser.jython.ast.exprType(org.python.pydev.parser.jython.ast.exprType) TryExcept(org.python.pydev.parser.jython.ast.TryExcept) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Index(org.python.pydev.parser.jython.ast.Index) Global(org.python.pydev.parser.jython.ast.Global) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Suite(org.python.pydev.parser.jython.ast.Suite) Print(org.python.pydev.parser.jython.ast.Print) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) Assert(org.python.pydev.parser.jython.ast.Assert) IfExp(org.python.pydev.parser.jython.ast.IfExp) List(org.python.pydev.parser.jython.ast.List) ArrayList(java.util.ArrayList) org.python.pydev.parser.jython.ast.argumentsType(org.python.pydev.parser.jython.ast.argumentsType) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) Comprehension(org.python.pydev.parser.jython.ast.Comprehension) DefaultArg(org.python.pydev.parser.grammarcommon.DefaultArg) Repr(org.python.pydev.parser.jython.ast.Repr) Expr(org.python.pydev.parser.jython.ast.Expr) TryFinally(org.python.pydev.parser.jython.ast.TryFinally) Slice(org.python.pydev.parser.jython.ast.Slice) org.python.pydev.parser.jython.ast.sliceType(org.python.pydev.parser.jython.ast.sliceType) ExtraArg(org.python.pydev.parser.grammarcommon.ExtraArg) org.python.pydev.parser.jython.ast.decoratorsType(org.python.pydev.parser.jython.ast.decoratorsType) Decorators(org.python.pydev.parser.grammarcommon.Decorators) Subscript(org.python.pydev.parser.jython.ast.Subscript) With(org.python.pydev.parser.jython.ast.With) org.python.pydev.parser.jython.ast.keywordType(org.python.pydev.parser.jython.ast.keywordType) WithItem(org.python.pydev.parser.jython.ast.WithItem) Lambda(org.python.pydev.parser.jython.ast.Lambda) Call(org.python.pydev.parser.jython.ast.Call) Return(org.python.pydev.parser.jython.ast.Return) Ellipsis(org.python.pydev.parser.jython.ast.Ellipsis) ListComp(org.python.pydev.parser.jython.ast.ListComp) While(org.python.pydev.parser.jython.ast.While) org.python.pydev.parser.jython.ast.suiteType(org.python.pydev.parser.jython.ast.suiteType) Print(org.python.pydev.parser.jython.ast.Print) ExtraArgValue(org.python.pydev.parser.grammarcommon.ExtraArgValue) org.python.pydev.parser.jython.ast.excepthandlerType(org.python.pydev.parser.jython.ast.excepthandlerType) ComprehensionCollection(org.python.pydev.parser.grammarcommon.ComprehensionCollection) Assign(org.python.pydev.parser.jython.ast.Assign) Yield(org.python.pydev.parser.jython.ast.Yield) ParseException(org.python.pydev.parser.jython.ParseException) Starred(org.python.pydev.parser.jython.ast.Starred) Raise(org.python.pydev.parser.jython.ast.Raise) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Aggregations

ArrayList (java.util.ArrayList)7 ComprehensionCollection (org.python.pydev.parser.grammarcommon.ComprehensionCollection)7 Decorators (org.python.pydev.parser.grammarcommon.Decorators)7 DefaultArg (org.python.pydev.parser.grammarcommon.DefaultArg)7 ExtraArg (org.python.pydev.parser.grammarcommon.ExtraArg)7 ExtraArgValue (org.python.pydev.parser.grammarcommon.ExtraArgValue)7 SimpleNode (org.python.pydev.parser.jython.SimpleNode)7 Assert (org.python.pydev.parser.jython.ast.Assert)7 Assign (org.python.pydev.parser.jython.ast.Assign)7 Call (org.python.pydev.parser.jython.ast.Call)7 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)7 Ellipsis (org.python.pydev.parser.jython.ast.Ellipsis)7 Expr (org.python.pydev.parser.jython.ast.Expr)7 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)7 Global (org.python.pydev.parser.jython.ast.Global)7 IfExp (org.python.pydev.parser.jython.ast.IfExp)7 Index (org.python.pydev.parser.jython.ast.Index)7 Lambda (org.python.pydev.parser.jython.ast.Lambda)7 List (org.python.pydev.parser.jython.ast.List)7 ListComp (org.python.pydev.parser.jython.ast.ListComp)7