Пример #1
0
 public ForStatement(Expression left, Expression list, Statement body, Statement else_)
 {
     _left = left;
     _list = list;
     _body = body;
     _else = else_;
 }
Пример #2
0
 public RaiseStatement(Expression exceptionType, Expression exceptionValue, Expression traceBack, Expression cause)
 {
     _type = exceptionType;
     _value = exceptionValue;
     _traceback = traceBack;
     _cause = cause;
 }
Пример #3
0
 public SliceExpression(Expression start, Expression stop, Expression step, bool stepProvided)
 {
     _sliceStart = start;
     _sliceStop = stop;
     _sliceStep = step;
     _stepProvided = stepProvided;
 }
Пример #4
0
 public AndExpression(Expression left, Expression right)
 {
     _left = left;
     _right = right;
     StartIndex = left.StartIndex;
     EndIndex = right.EndIndex;
 }
Пример #5
0
        public BinaryExpression(JOperator op, Expression left, Expression right)
        {
            Contract.Assert(left != null);
            Contract.Assert(right != null);
            if (op == JOperator.None) throw new ArgumentException("bad operator");

            _op = op;
            _left = left;
            _right = right;
            StartIndex = left.StartIndex;
            EndIndex = right.EndIndex;
        }
Пример #6
0
        internal void AppendCodeString(StringBuilder res, JAst ast, CodeFormattingOptions format, string start, string end, Expression item)
        {
            if (!String.IsNullOrEmpty(start)) {
                format.ReflowComment(res, this.GetProceedingWhiteSpace(ast));
                res.Append(start);
            }

            item.AppendCodeString(res, ast, format);

            for (int i = 0; i < Iterators.Count; i++) {
                Iterators[i].AppendCodeString(res, ast, format);
            }

            if (!String.IsNullOrEmpty(end)) {
                res.Append(this.GetSecondWhiteSpace(ast));
                res.Append(end);
            }
        }
Пример #7
0
 private ErrorExpression Error(string verbatimImage = null, Expression preceeding = null)
 {
     var res = new ErrorExpression(verbatimImage, preceeding);
     res.SetLoc(GetStart(), GetEnd());
     return res;
 }
Пример #8
0
 private Expression AddTrailers(Expression ret)
 {
     return AddTrailers(ret, true);
 }
Пример #9
0
        // trailer: '(' [ arglist_genexpr ] ')' | '[' subscriptlist ']' | '.' NAME
        private Expression AddTrailers(Expression ret, bool allowGeneratorExpression)
        {
            bool prevAllow = _allowIncomplete;
            try {
                _allowIncomplete = true;
                while (true) {
                    switch (PeekToken().Kind) {
                        case TokenKind.LeftParenthesis:
                            if (!allowGeneratorExpression) return ret;

                            NextToken();
                            string whitespace = _tokenWhiteSpace;
                            List<string> commaWhiteSpace;
                            bool ateTerminator;
                            Arg[] args = FinishArgListOrGenExpr(out commaWhiteSpace, out ateTerminator);
                            string closeParenWhiteSpace = _tokenWhiteSpace;
                            CallExpression call;
                            if (args != null) {
                                call = FinishCallExpr(ret, args);
                            } else {
                                call = new CallExpression(ret, new Arg[0]);
                            }

                            if (_verbatim) {
                                AddPreceedingWhiteSpace(call, whitespace);
                                AddSecondPreceedingWhiteSpace(call, closeParenWhiteSpace);
                                if (commaWhiteSpace != null) {
                                    AddListWhiteSpace(call, commaWhiteSpace.ToArray());
                                }
                                if (!ateTerminator) {
                                    AddErrorMissingCloseGrouping(call);
                                }
                            }

                            call.SetLoc(ret.StartIndex, GetEnd());
                            ret = call;
                            break;
                        case TokenKind.LeftBracket:
                            NextToken();
                            whitespace = _tokenWhiteSpace;

                            Expression index = ParseSubscriptList(out ateTerminator);
                            IndexExpression ie = new IndexExpression(ret, index);
                            string finishWhiteSpace = _tokenWhiteSpace;
                            ie.SetLoc(ret.StartIndex, GetEnd());
                            if (_verbatim) {
                                AddPreceedingWhiteSpace(ie, whitespace);
                                AddSecondPreceedingWhiteSpace(ie, finishWhiteSpace);
                                if (!ateTerminator) {
                                    AddErrorMissingCloseGrouping(ie);
                                }
                            }
                            ret = ie;
                            break;
                        case TokenKind.Dot:
                            NextToken();
                            whitespace = _tokenWhiteSpace;
                            var name = ReadNameMaybeNone();
                            string nameWhitespace = _tokenWhiteSpace;
                            MemberExpression fe = MakeMember(ret, name);
                            fe.SetLoc(ret.StartIndex, GetStart(), GetEnd());
                            if (_verbatim) {
                                AddPreceedingWhiteSpace(fe, whitespace);
                                AddSecondPreceedingWhiteSpace(fe, nameWhitespace);
                                if (!name.HasName) {
                                    AddErrorIsIncompleteNode(fe);
                                }
                            }
                            ret = fe;
                            break;
                        case TokenKind.Constant:
                            // abc.1, abc"", abc 1L, abc 0j
                            ReportSyntaxError("invalid syntax");
                            ret = Error(_verbatim ? _lookaheadWhiteSpace + _lookahead.Token.VerbatimImage : null, ret);
                            NextToken();
                            break;
                        default:
                            return ret;
                    }
                }
            } finally {
                _allowIncomplete = prevAllow;
            }
        }
Пример #10
0
 private Expression ParseTestListAsExpr(Expression expr)
 {
     List<string> itemWhiteSpace;
     bool trailingComma;
     List<Expression> l = ParseTestListAsExpr(expr, out itemWhiteSpace, out trailingComma);
     return MakeTupleOrExpr(l, itemWhiteSpace,  trailingComma, parenFreeTuple: true);
 }
Пример #11
0
        private List<Expression> ParseTestListAsExpr(Expression expr, out List<string> itemWhiteSpace, out bool trailingComma)
        {
            var l = new List<Expression>();
            itemWhiteSpace = MakeWhiteSpaceList();
            if (expr != null) {
                l.Add(expr);
                if (itemWhiteSpace != null) {
                    Debug.Assert(_token.Token.Kind == TokenKind.Comma);
                    itemWhiteSpace.Add(_tokenWhiteSpace);
                }
            }

            trailingComma = true;
            while (true) {
                if (NeverTestToken(PeekToken())) break;
                l.Add(ParseExpression());

                if (!MaybeEat(TokenKind.Comma)) {
                    trailingComma = false;
                    break;
                }
                if (itemWhiteSpace != null) {
                    itemWhiteSpace.Add(_tokenWhiteSpace);
                }
            }
            return l;
        }
Пример #12
0
 public WhileStatement(Expression test, Statement body, Statement else_)
 {
     _test = test;
     _body = body;
     _else = else_;
 }
Пример #13
0
        private Expression FinishExpressionListAsExpr(Expression expr)
        {
            var start = GetStart();
            bool trailingComma = true;
            List<Expression> l = new List<Expression>();
            List<string> itemWhiteSpace = MakeWhiteSpaceList();
            if (itemWhiteSpace != null) {
                itemWhiteSpace.Add(_tokenWhiteSpace);
            }
            l.Add(expr);

            while (true) {
                if (NeverTestToken(PeekToken())) break;
                expr = ParseExpression();
                l.Add(expr);
                if (!MaybeEat(TokenKind.Comma)) {
                    trailingComma = false;
                    break;
                }
                if (itemWhiteSpace != null) {
                    itemWhiteSpace.Add(_tokenWhiteSpace);
                }
                trailingComma = true;
            }

            Expression ret = MakeTupleOrExpr(l, itemWhiteSpace, trailingComma);
            ret.SetLoc(start, GetEnd());
            return ret;
        }
Пример #14
0
 public ErrorExpression(string verbatimImage, Expression preceeding)
 {
     _preceeding = preceeding;
     _verbatimImage = verbatimImage;
 }
Пример #15
0
 public ParenthesisExpression(Expression expression)
 {
     _expression = expression;
 }
Пример #16
0
        private Expression FinishSlice(Expression e0, int start)
        {
            Expression e1 = null;
            Expression e2 = null;
            bool stepProvided = false;
            Debug.Assert(_token.Token.Kind == TokenKind.Colon);
            string colonWhiteSpace = _tokenWhiteSpace;
            string secondColonWhiteSpace = null;

            switch (PeekToken().Kind) {
                case TokenKind.Comma:
                case TokenKind.RightBracket:
                    break;
                case TokenKind.Colon:
                    // x[?::?]
                    stepProvided = true;
                    NextToken();
                    secondColonWhiteSpace = _tokenWhiteSpace;
                    e2 = ParseSliceEnd();
                    break;
                default:
                    // x[?:val:?]
                    e1 = ParseExpression();
                    if (MaybeEat(TokenKind.Colon)) {
                        secondColonWhiteSpace = _tokenWhiteSpace;
                        stepProvided = true;
                        e2 = ParseSliceEnd();
                    }
                    break;
            }
            SliceExpression ret = new SliceExpression(e0, e1, e2, stepProvided);
            if (_verbatim) {
                AddPreceedingWhiteSpace(ret, colonWhiteSpace);
                if (secondColonWhiteSpace != null) {
                    AddSecondPreceedingWhiteSpace(ret, secondColonWhiteSpace);
                }
            }
            ret.SetLoc(start, GetEnd());
            return ret;
        }
Пример #17
0
 private MemberExpression MakeMember(Expression target, Name name)
 {
     var res = new MemberExpression(target, name.RealName);
     AddVerbatimName(name, res);
     return res;
 }
Пример #18
0
 // comp_iter '}'
 private SetComprehension FinishSetComp(Expression item, out bool ateTerminator)
 {
     ComprehensionIterator[] iters = ParseCompIter();
     ateTerminator = Eat(TokenKind.RightBrace);
     return new SetComprehension(item, iters);
 }
Пример #19
0
 // list_iter ']'
 private ListComprehension FinishListComp(Expression item, out bool ateRightBracket)
 {
     ComprehensionIterator[] iters = ParseListCompIter();
     ateRightBracket = Eat(TokenKind.RightBracket);
     return new ListComprehension(item, iters);
 }
Пример #20
0
        private Arg FinishKeywordArgument(Expression t)
        {
            Debug.Assert(_token.Token.Kind == TokenKind.Assign);
            string equalWhiteSpace = _tokenWhiteSpace;
            NameExpression n = t as NameExpression;

            string name;
            if (n == null) {
                ReportSyntaxError(t.StartIndex, t.EndIndex, "expected name");
                name = null;
            } else {
                name = n.Name;
            }

            Expression val = ParseExpression();
            Arg arg = new Arg(t, val);
            arg.SetLoc(t.StartIndex, val.EndIndex);
            if (_verbatim) {
                AddPreceedingWhiteSpace(arg, equalWhiteSpace);
            }

            // we're losing the name expression...
            return arg;
        }
Пример #21
0
 public TryStatementHandler(Expression test, Expression target, Statement body)
 {
     _test = test;
     _target = target;
     _body = body;
 }
Пример #22
0
        private Statement FinishAssignments(Expression right)
        {
            List<Expression> left = null;
            List<string> assignWhiteSpace = MakeWhiteSpaceList();
            Expression singleLeft = null;

            while (MaybeEat(TokenKind.Assign)) {
                if (assignWhiteSpace != null) {
                    assignWhiteSpace.Add(_tokenWhiteSpace);
                }
                string assignError = right.CheckAssign();
                if (assignError != null) {
                    ReportSyntaxError(right.StartIndex, right.EndIndex, assignError, ErrorCodes.SyntaxError | ErrorCodes.NoCaret);
                }

                if (singleLeft == null) {
                    singleLeft = right;
                } else {
                    if (left == null) {
                        left = new List<Expression>();
                        left.Add(singleLeft);
                    }
                    left.Add(right);
                }

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

            AssignmentStatement assign;
            if (left != null) {
                Debug.Assert(left.Count > 0);

                assign = new AssignmentStatement(left.ToArray(), right);
                assign.SetLoc(left[0].StartIndex, right.EndIndex);
            } else {
                Debug.Assert(singleLeft != null);

                assign = new AssignmentStatement(new[] { singleLeft }, right);
                assign.SetLoc(singleLeft.StartIndex, right.EndIndex);
            }
            if (assignWhiteSpace != null) {
                AddListWhiteSpace(assign, assignWhiteSpace.ToArray());
            }
            return assign;
        }
Пример #23
0
 public ConditionalExpression(Expression testExpression, Expression trueExpression, Expression falseExpression)
 {
     this._testExpr = testExpression;
     this._trueExpr = trueExpression;
     this._falseExpr = falseExpression;
 }
Пример #24
0
        //print_stmt: 'print' ( [ expression (',' expression)* [','] ] | '>>' expression [ (',' expression)+ [','] ] )
        private PrintStatement ParsePrintStmt()
        {
            Eat(TokenKind.KeywordPrint);
            string printWhiteSpace = _tokenWhiteSpace;
            var start = GetStart();
            Expression dest = null;
            PrintStatement ret;

            string rightShiftWhiteSpace = null;
            string theCommaWhiteSpace = null;
            bool needNonEmptyTestList = false;
            int end = 0;
            if (MaybeEat(TokenKind.RightShift)) {
                rightShiftWhiteSpace = _tokenWhiteSpace;
                dest = ParseExpression();
                if (MaybeEat(TokenKind.Comma)) {
                    theCommaWhiteSpace = _tokenWhiteSpace;
                    needNonEmptyTestList = true;
                    end = GetEnd();
                } else {
                    ret = new PrintStatement(dest, new Expression[0], false);
                    if (_verbatim) {
                        AddPreceedingWhiteSpace(ret, printWhiteSpace);
                        AddSecondPreceedingWhiteSpace(ret, rightShiftWhiteSpace);
                    }
                    ret.SetLoc(start, GetEnd());
                    return ret;
                }
            }

            bool trailingComma = false;
            List<string> commaWhiteSpace = null;

            Expression[] exprs;
            if (!NeverTestToken(PeekToken())) {
                var expr = ParseExpression();
                if (!MaybeEat(TokenKind.Comma)) {
                    exprs = new[] { expr };
                } else {
                    List<Expression> exprList = ParseTestListAsExpr(expr, out commaWhiteSpace, out trailingComma);
                    exprs = exprList.ToArray();
                }
            } else {
                if (needNonEmptyTestList) {
                    ReportSyntaxError(start, end, "print statement expected expression to be printed");
                    exprs = new[] { Error("") };
                } else {
                    exprs = new Expression[0];
                }
            }

            ret = new PrintStatement(dest, exprs, trailingComma);
            if (_verbatim) {
                AddPreceedingWhiteSpace(ret, printWhiteSpace);
                AddSecondPreceedingWhiteSpace(ret, rightShiftWhiteSpace);
                AddThirdPreceedingWhiteSpace(ret, theCommaWhiteSpace);
                if (commaWhiteSpace != null) {
                    AddListWhiteSpace(ret, commaWhiteSpace.ToArray());
                }
            }
            ret.SetLoc(start, GetEnd());
            return ret;
        }
Пример #25
0
 private Expression ParseConditionalTest(Expression trueExpr)
 {
     string ifWhiteSpace = _tokenWhiteSpace;
     Expression expr = ParseOrTest();
     bool ateElse = Eat(TokenKind.KeywordElse);
     string elseWhiteSpace;
     Expression falseExpr;
     if (ateElse) {
         elseWhiteSpace = _tokenWhiteSpace;
         falseExpr = ParseExpression();
     } else {
         elseWhiteSpace = null;
         falseExpr = Error("");
     }
     var res = new ConditionalExpression(expr, trueExpr, falseExpr);
     if (_verbatim) {
         AddPreceedingWhiteSpace(res, ifWhiteSpace);
         AddSecondPreceedingWhiteSpace(res, elseWhiteSpace);
         if (!ateElse) {
             AddErrorIsIncompleteNode(res);
         }
     }
     return res;
 }
Пример #26
0
        private CallExpression FinishCallExpr(Expression target, params Arg[] args)
        {
            bool hasArgsTuple = false;
            bool hasKeywordDict = false;
            int keywordCount = 0;
            int extraArgs = 0;

            foreach (Arg arg in args) {
                if (arg.Name == null) {
                    if (hasArgsTuple || hasKeywordDict || keywordCount > 0) {
                        ReportSyntaxError(arg.StartIndex, arg.EndIndex, "non-keyword arg after keyword arg");
                    }
                } else if (arg.Name == "*") {
                    if (hasArgsTuple || hasKeywordDict) {
                        ReportSyntaxError(arg.StartIndex, arg.EndIndex, "only one * allowed");
                    }
                    hasArgsTuple = true; extraArgs++;
                } else if (arg.Name == "**") {
                    if (hasKeywordDict) {
                        ReportSyntaxError(arg.StartIndex, arg.EndIndex, "only one ** allowed");
                    }
                    hasKeywordDict = true; extraArgs++;
                } else {
                    if (hasKeywordDict) {
                        ReportSyntaxError(arg.StartIndex, arg.EndIndex, "keywords must come before ** args");
                    }
                    keywordCount++;
                }
            }

            return new CallExpression(target, args);
        }
Пример #27
0
 public SetComprehension(Expression item, ComprehensionIterator[] iterators)
 {
     _item = item;
     _iterators = iterators;
 }
Пример #28
0
        //  genexpr_for  ::= "for" target_list "in" or_test [comp_iter]
        //
        //  "for" has NOT been eaten before entering this method
        private Expression ParseGeneratorExpression(Expression expr, string rightParenWhiteSpace = null)
        {
            ComprehensionIterator[] iters = ParseCompIter();

            GeneratorExpression ret = new GeneratorExpression(expr, iters);

            ret.SetLoc(expr.StartIndex, GetEnd());
            return ret;
        }
Пример #29
0
 public DelStatement(Expression[] expressions)
 {
     _expressions = expressions;
 }
Пример #30
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;
        }