Example #1
0
        private Expression ParseLambdaHelperEnd(FunctionDefinition func, Expression expr, string whitespace, string colonWhiteSpace, List<string> commaWhiteSpace, bool ateTerminator)
        {
            // Pep 342 in J 2.5 allows Yield Expressions, which can occur inside a Lambda body.
            // In this case, the lambda is a generator and will yield it's final result instead of just return it.
            Statement body;
            if (func.IsGenerator) {
                YieldExpression y = new YieldExpression(expr);
                y.SetLoc(expr.IndexSpan);
                body = new ExpressionStatement(y);
            } else {
                body = new ReturnStatement(expr);
            }
            body.SetLoc(expr.StartIndex, expr.EndIndex);

            FunctionDefinition func2 = PopFunction();
            System.Diagnostics.Debug.Assert(func == func2);

            func.SetBody(body);
            func.EndIndex = GetEnd();

            LambdaExpression ret = new LambdaExpression(func);
            func.SetLoc(func.IndexSpan);
            ret.SetLoc(func.IndexSpan);
            if (_verbatim) {
                AddPreceedingWhiteSpace(ret, whitespace);
                AddSecondPreceedingWhiteSpace(ret, colonWhiteSpace);
                AddListWhiteSpace(ret, commaWhiteSpace.ToArray());
                if (!ateTerminator) {
                    AddErrorIsIncompleteNode(ret);
                }
            }
            return ret;
        }
Example #2
0
        private Statement ParseYieldStmt()
        {
            // For yield statements, continue to enforce that it's currently in a function.
            // This gives us better syntax error reporting for yield-statements than for yield-expressions.
            FunctionDefinition current = CurrentFunction;
            if (current == null) {
                ReportSyntaxError("misplaced yield");
            }

            _isGenerator = true;
            if (_returnsWithValue != null && _langVersion < JLanguageVersion.V33) {
                foreach (var span in _returnsWithValue) {
                    ReportSyntaxError(span.Start, span.End, "'return' with argument inside generator");
                }
            }

            Eat(TokenKind.KeywordYield);

            // See Pep 342: a yield statement is now just an expression statement around a yield expression.
            Expression e = ParseYieldExpression();
            Debug.Assert(e != null); // caller already verified we have a yield.

            Statement s = new ExpressionStatement(e);
            s.SetLoc(e.IndexSpan);
            return s;
        }
Example #3
0
        // expr_stmt: expression_list
        // expression_list: expression ( "," expression )* [","]
        // assignment_stmt: (target_list "=")+ (expression_list | yield_expression)
        // augmented_assignment_stmt ::= target augop (expression_list | yield_expression)
        // augop: '+=' | '-=' | '*=' | '/=' | '%=' | '**=' | '>>=' | '<<=' | '&=' | '^=' | '|=' | '//='
        private Statement ParseExprStmt()
        {
            Expression ret = ParseTestListAsExpr();

            if (PeekToken(TokenKind.Assign)) {
                if (_langVersion >= JLanguageVersion.V30) {
                    SequenceExpression seq = ret as SequenceExpression;
                    bool hasStar = false;
                    if (seq != null) {
                        for (int i = 0; i < seq.Items.Count; i++) {
                            if (seq.Items[i] is StarredExpression) {
                                if (hasStar) {
                                    ReportSyntaxError(seq.Items[i].StartIndex, seq.Items[i].EndIndex, "two starred expressions in assignment");
                                }
                                hasStar = true;
                            }
                        }
                    }
                }

                return FinishAssignments(ret);
            } else {
                JOperator op = GetAssignOperator(PeekToken());
                if (op != JOperator.None) {
                    NextToken();
                    string whiteSpace = _tokenWhiteSpace;
                    Expression rhs;

                    if (_langVersion >= JLanguageVersion.V71 && MaybeEat(TokenKind.KeywordYield)) {
                        rhs = ParseYieldExpression();
                    } else {
                        rhs = ParseTestListAsExpr();
                    }

                    string assignError = ret.CheckAugmentedAssign();
                    if (assignError != null) {
                        ReportSyntaxError(ret.StartIndex, ret.EndIndex, assignError);
                    }

                    AugmentedAssignStatement aug = new AugmentedAssignStatement(op, ret, rhs);
                    if (_verbatim) {
                        AddPreceedingWhiteSpace(aug, whiteSpace);
                    }
                    aug.SetLoc(ret.StartIndex, GetEnd());
                    return aug;
                } else {
                    Statement stmt = new ExpressionStatement(ret);
                    stmt.SetLoc(ret.IndexSpan);
                    return stmt;
                }
            }
        }
Example #4
0
 private static bool IsDocString(ExpressionStatement exprStmt)
 {
     ConstantExpression constExpr;
     return exprStmt != null &&
             (constExpr = exprStmt.Expression as ConstantExpression) != null &&
             (constExpr.Value is string || constExpr.Value is AsciiString);
 }
Example #5
0
 public override bool Walk(ExpressionStatement node)
 {
     UpdateChildRanges(node);
     return base.Walk(node);
 }
Example #6
0
 // ExpressionStatement
 public override bool Walk(ExpressionStatement node)
 {
     return ShouldWalkWorker(node);
 }
Example #7
0
 public override void PostWalk(ExpressionStatement node)
 {
     PostWalkWorker(node);
 }