Example #1
0
        //for_stmt: 'for' target_list 'in' expression_list ':' suite ['else' ':' suite]
        private Statement ParseForStmt()
        {
            Eat(TokenKind.KeywordFor);
            string forWhiteSpace = _tokenWhiteSpace;
            var start = GetStart();

            bool trailingComma;
            List<string> listWhiteSpace;

            List<Expression> l = ParseExpressionList(out trailingComma, out listWhiteSpace);

            // expr list is something like:
            //  ()
            //  a
            //  a,b
            //  a,b,c
            // we either want just () or a or we want (a,b) and (a,b,c)
            // so we can do tupleExpr.EmitSet() or loneExpr.EmitSet()

            string inWhiteSpace = null, elseWhiteSpace = null;
            Expression lhs = MakeTupleOrExpr(l, listWhiteSpace, trailingComma, true);
            Expression list;
            Statement body, else_;
            bool incomplete = false;
            int header;
            string newlineWhiteSpace = "";
            int end;
            if ((lhs is ErrorExpression && MaybeEatNewLine(out newlineWhiteSpace)) || !Eat(TokenKind.KeywordIn)) {
                // error handling
                else_ = null;
                end = header = GetEnd();
                list = null;
                body = null;
                lhs = Error(newlineWhiteSpace, lhs);
                incomplete = true;
            } else {
                inWhiteSpace = _tokenWhiteSpace;
                list = ParseTestListAsExpr();
                header = GetEnd();
                body = ParseLoopSuite();
                else_ = null;
                end = body.EndIndex;
                if (MaybeEat(TokenKind.KeywordElse)) {
                    elseWhiteSpace = _tokenWhiteSpace;
                    else_ = ParseSuite();
                    end = else_.EndIndex;
                }
            }

            ForStatement ret = new ForStatement(lhs, list, body, else_);
            if (_verbatim) {
                AddPreceedingWhiteSpace(ret, forWhiteSpace);
                if (inWhiteSpace != null) {
                    AddSecondPreceedingWhiteSpace(ret, inWhiteSpace);
                }
                if (elseWhiteSpace != null) {
                    AddThirdPreceedingWhiteSpace(ret, elseWhiteSpace);
                }
                if (incomplete) {
                    AddErrorIsIncompleteNode(ret);
                }
            }
            ret.HeaderIndex = header;
            ret.SetLoc(start, end);
            return ret;
        }