예제 #1
0
파일: Parser.cs 프로젝트: omnimark/PTVS
        // 3.x: star_expr: ['*'] expr
        private Expression ParseStarExpression() {
            
            if (MaybeEat(TokenKind.Multiply)) {
                string whitespace = _tokenWhiteSpace;
                if (_langVersion.Is2x()) {
                    ReportSyntaxError("invalid syntax");
                }
                var start = GetStart();
                var expr = ParseExpr();
                var res = new StarredExpression(expr);
                if (_verbatim) {
                    AddPreceedingWhiteSpace(res, whitespace);
                }
                res.SetLoc(start, expr.EndIndex);
                return res;
            }

            return ParseExpr();
        }
예제 #2
0
 public override void PostWalk(StarredExpression node) { PostWalkWorker(node); }
예제 #3
0
파일: Parser.cs 프로젝트: jsschultz/PTVS
        /*
        // "for" target_list "in" or_test
        private ForStatement ParseGenExprFor() {
            var start = GetStart();
            Eat(TokenKind.KeywordFor);
            bool trailingComma;
            List<string> listWhiteSpace;
            List<Expression> l = ParseTargetList(out trailingComma, out listWhiteSpace);
            Expression lhs = MakeTupleOrExpr(l, listWhiteSpace, trailingComma);
            Eat(TokenKind.KeywordIn);

            Expression expr = null;
            expr = ParseOrTest();

            ForStatement gef = new ForStatement(lhs, expr, null, null);
            var end = GetEnd();
            gef.SetLoc(start, end);
            gef.HeaderIndex = end;
            return gef;
        }

        //  genexpr_if: "if" old_test
        private IfStatement ParseGenExprIf() {
            var start = GetStart();
            Eat(TokenKind.KeywordIf);
            Expression expr = ParseOldExpression();
            IfStatementTest ist = new IfStatementTest(expr, null);
            var end = GetEnd();
            ist.HeaderIndex = end;
            ist.SetLoc(start, end);
            IfStatement gei = new IfStatement(new IfStatementTest[] { ist }, null);
            gei.SetLoc(start, end);
            return gei;
        }
        */

        // dict_display: '{' [dictorsetmaker] '}'
        // dictorsetmaker: ( (test ':' test (comp_for | (',' test ':' test)* [','])) |
        //                   (test (comp_for | (',' test)* [','])) )


        private Expression FinishDictOrSetValue() {
            string startWhiteSpace = _tokenWhiteSpace, finishWhiteSpace;
            var oStart = GetStart();
            var oEnd = GetEnd();

            List<SliceExpression> dictMembers = null;
            List<Expression> setMembers = null;
            List<string> itemWhiteSpace = MakeWhiteSpaceList();
            bool prevAllow = _allowIncomplete;
            bool reportedError = false;
            bool ateTerminator = false;
            bool hasSequenceUnpack = false, hasDictUnpack = false;
            try {
                _allowIncomplete = true;
                while (true) {
                    if (MaybeEat(TokenKind.RightBrace)) { // empty dict literal
                        finishWhiteSpace = _tokenWhiteSpace;
                        ateTerminator = true;
                        break;
                    }

                    bool isUnpack = false;
                    if (MaybeEat(TokenKind.Multiply)) {
                        if (hasDictUnpack || _langVersion < PythonLanguageVersion.V35) {
                            ReportSyntaxError("invalid syntax");
                        }
                        isUnpack = true;
                        hasSequenceUnpack = true;
                    } else if (MaybeEat(TokenKind.Power)) {
                        if (hasSequenceUnpack || _langVersion < PythonLanguageVersion.V35) {
                            ReportSyntaxError("invalid syntax");
                        }
                        isUnpack = true;
                        hasDictUnpack = true;
                    }

                    bool first = false;
                    Expression e1 = ParseExpression();
                    if (MaybeEat(TokenKind.Colon)) { // dict literal
                        string colonWhiteSpace = _tokenWhiteSpace;
                        if (setMembers == null && dictMembers == null) {
                            dictMembers = new List<SliceExpression>();
                            first = true;
                        }
                        Expression e2 = ParseExpression();

                        if (setMembers != null || hasSequenceUnpack || isUnpack) {
                            if (!reportedError) {
                                ReportSyntaxError(e1.StartIndex, e2.EndIndex, "invalid syntax");
                            }
                        }


                        SliceExpression se = new SliceExpression(e1, e2, null, false);
                        if (_verbatim) {
                            AddPreceedingWhiteSpace(se, colonWhiteSpace);
                        }
                        se.SetLoc(e1.StartIndex, e2.EndIndex);

                        if (PeekToken(Tokens.KeywordForToken)) {
                            if (!first || _langVersion < PythonLanguageVersion.V27) {
                                ReportSyntaxError("invalid syntax");
                            }

                            var dictComp = FinishDictComp(se, out ateTerminator);
                            if (_verbatim) {
                                AddPreceedingWhiteSpace(dictComp, startWhiteSpace);
                                AddSecondPreceedingWhiteSpace(dictComp, _tokenWhiteSpace);
                                if (!ateTerminator) {
                                    AddErrorMissingCloseGrouping(dictComp);
                                }
                            }
                            dictComp.SetLoc(oStart, GetEnd());
                            return dictComp;
                        }

                        if (dictMembers != null) {
                            dictMembers.Add(se);
                        } else {
                            setMembers.Add(se);
                        }
                    } else { // set literal or dict unpack
                        if (_langVersion < PythonLanguageVersion.V27 && !reportedError) {
                            ReportSyntaxError(e1.StartIndex, e1.EndIndex, "invalid syntax, set literals require Python 2.7 or later.");
                            reportedError = true;
                        }

                        if (isUnpack && hasDictUnpack) {
                            // **{}, we don't have a colon and a value...
                            if (setMembers != null && !reportedError) {
                                ReportSyntaxError(e1.StartIndex, e1.EndIndex, "invalid syntax");
                                reportedError = true;
                            }

                            if (dictMembers == null) {
                                dictMembers = new List<SliceExpression>();
                            }
                            dictMembers.Add(new SliceExpression(null, e1, null, false));
                        } else {
                            if (dictMembers != null) {
                                if (!reportedError) {
                                    ReportSyntaxError(e1.StartIndex, e1.EndIndex, "invalid syntax");
                                    reportedError = true;
                                }
                            } else if (setMembers == null) {
                                setMembers = new List<Expression>();
                                first = true;
                            }

                            if (PeekToken(Tokens.KeywordForToken)) {
                                if (!first) {
                                    ReportSyntaxError("invalid syntax");
                                }
                                var setComp = FinishSetComp(e1, out ateTerminator);
                                if (_verbatim) {
                                    AddPreceedingWhiteSpace(setComp, startWhiteSpace);
                                    AddSecondPreceedingWhiteSpace(setComp, _tokenWhiteSpace);
                                    if (!ateTerminator) {
                                        AddErrorMissingCloseGrouping(setComp);
                                    }
                                }
                                setComp.SetLoc(oStart, GetEnd());
                                return setComp;
                            }

                            // error recovery
                            if (setMembers != null) {
                                if (isUnpack) {
                                    e1 = new StarredExpression(e1);
                                }

                                setMembers.Add(e1);
                            } else {
                                var slice = new SliceExpression(e1, null, null, false);
                                if (_verbatim) {
                                    AddErrorIsIncompleteNode(slice);
                                }
                                dictMembers.Add(slice);
                            }
                        }
                    }

                    if (!MaybeEat(TokenKind.Comma)) {
                        ateTerminator = Eat(TokenKind.RightBrace);
                        finishWhiteSpace = _tokenWhiteSpace;
                        break;
                    }
                    if (itemWhiteSpace != null) {
                        itemWhiteSpace.Add(_tokenWhiteSpace);
                    }
                }
            } finally {
                _allowIncomplete = prevAllow;
            }


            var cStart = GetStart();
            var cEnd = GetEnd();

            Expression ret;
            if (dictMembers != null || setMembers == null) {
                SliceExpression[] exprs;
                if (dictMembers != null) {
                    exprs = dictMembers.ToArray();
                } else {
                    exprs = new SliceExpression[0];
                }
                ret = new DictionaryExpression(exprs);
            } else {
                ret = new SetExpression(setMembers.ToArray());
            }
            ret.SetLoc(oStart, cEnd);
            if (_verbatim) {
                AddPreceedingWhiteSpace(ret, startWhiteSpace);
                AddSecondPreceedingWhiteSpace(ret, finishWhiteSpace);
                AddListWhiteSpace(ret, itemWhiteSpace.ToArray());
                if (!ateTerminator) {
                    AddErrorMissingCloseGrouping(ret);
                }
            }
            return ret;
        }
예제 #4
0
 // StarredExpression
 public override bool Walk(StarredExpression node) { return ShouldWalkWorker(node); }