Exemple #1
0
 public ForStatement(Expression left, Expression list, Statement body, Statement else_)
 {
     _left = left;
     _list = list;
     _body = body;
     _else = else_;
 }
Exemple #2
0
 private void DefineExpr(Expression lhs) {
     if (lhs is NameExpression) {
         lhs.Walk(Define);
     } else {
         // fob.oar = 42, fob[oar] = 42, we don't actually define any variables
         lhs.Walk(this);
     }
 }
Exemple #3
0
        public BinaryExpression(PythonOperator op, Expression left, Expression right) {
            Contract.Assert(left != null);
            Contract.Assert(right != null);
            if (op == PythonOperator.None) throw new ArgumentException("bad operator");

            _op = op;
            _left = left;
            _right = right;
            StartIndex = left.StartIndex;
            EndIndex = right.EndIndex;
        }
Exemple #4
0
        internal void AppendCodeString(StringBuilder res, PythonAst 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);
            }
        }
Exemple #5
0
        private IMember GetValueFromExpression(Expression expr, Dictionary<string, IMember> scope) {
            var ne = expr as NameExpression;
            if (ne != null) {
                IMember existing = null;
                if (scope != null && scope.TryGetValue(ne.Name, out existing) && existing != null) {
                    return existing;
                }
                if (_members != null && _members.TryGetValue(ne.Name, out existing) && existing != null) {
                    return existing;
                }
            }

            var type = GetTypeFromExpression(expr);
            if (type != null) {
                return new AstPythonConstant(type, GetLoc(expr));
            }

            return null;
        }
Exemple #6
0
        private static NameExpression GetFirstNameExpression(Expression expr) {
            NameExpression nameExpr;
            CallExpression callExpr;
            MemberExpression membExpr;

            if ((nameExpr = expr as NameExpression) != null) {
                return nameExpr;
            }
            if ((callExpr = expr as CallExpression) != null) {
                return GetFirstNameExpression(callExpr.Target);
            }
            if ((membExpr = expr as MemberExpression) != null) {
                return GetFirstNameExpression(membExpr.Target);
            }

            return null;
        }
Exemple #7
0
 public AssertStatement(Expression test, Expression message) {
     _test = test;
     _message = message;
 }
Exemple #8
0
        private static Expression ForceExplicitSign(Expression expr, out int sign) {
            var constExpr = expr as ConstantExpression;
            if (constExpr != null && (constExpr.Value is int || constExpr.Value is double || constExpr.Value is BigInteger || constExpr.Value is Complex)) {
                sign = 1;
                return new UnaryExpression(PythonOperator.Pos, constExpr);
            }

            var unaryExpr = expr as UnaryExpression;
            if (unaryExpr != null) {
                switch (unaryExpr.Op) {
                    case PythonOperator.Pos:
                        sign = 1;
                        return unaryExpr;
                    case PythonOperator.Negate:
                        sign = -1;
                        return unaryExpr;
                }
            }

            sign = 0;
            return expr;
        }
Exemple #9
0
 public IfStatementTest(Expression test, Statement body) {
     _test = test;
     _body = body;
 }
 private bool WalkCollection(Expression node, IEnumerable<Expression> items) {
     CanComplete = HasCaret(node);
     int count = 0;
     Expression last = null;
     foreach (var e in items) {
         count += 1;
         last = e;
     }
     if (count == 0) {
         CommitByDefault = false;
     } else if (count == 1) {
         last.Walk(this);
         CommitByDefault = false;
     } else {
         CommitByDefault = true;
         last.Walk(this);
     }
     return false;
 }
Exemple #11
0
 // comp_iter '}'
 private SetComprehension FinishSetComp(Expression item, out bool ateTerminator) {
     ComprehensionIterator[] iters = ParseCompIter();
     ateTerminator = Eat(TokenKind.RightBrace);
     return new SetComprehension(item, iters);
 }
Exemple #12
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;
        }
Exemple #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;
        }
Exemple #14
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;
        }
Exemple #15
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);
 }
Exemple #16
0
 public DecoratorStatement(Expression[] decorators) {
     _decorators = decorators;
 }
 private static bool IsActualExpression(Expression node) {
     return node != null && !(node is ErrorExpression);
 }
Exemple #18
0
 // list_iter ']'
 private ListComprehension FinishListComp(Expression item, out bool ateRightBracket) {
     ComprehensionIterator[] iters = ParseListCompIter();
     ateRightBracket = Eat(TokenKind.RightBracket);
     return new ListComprehension(item, iters);
 }
 public AugmentedAssignStatement(PythonOperator op, Expression left, Expression right) {
     _op = op;
     _left = left; 
     _right = right;
 }
Exemple #20
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);
        }
Exemple #21
0
 private static string ToDottedNameString(Expression expr, PythonAst ast) {
     NameExpression name;
     MemberExpression member;
     ParenthesisExpression paren;
     if ((name = expr as NameExpression) != null) {
         return name.Name;
     } else if ((member = expr as MemberExpression) != null) {
         while (member.Target is MemberExpression) {
             member = (MemberExpression)member.Target;
         }
         if (member.Target is NameExpression) {
             return expr.ToCodeString(ast);
         }
     } else if ((paren = expr as ParenthesisExpression) != null) {
         return ToDottedNameString(paren.Expression, ast);
     }
     return null;
 }
Exemple #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 >= PythonLanguageVersion.V25 && PeekToken(TokenKind.KeywordYield)) {
                    if (!AllowYieldSyntax && AllowAsyncAwaitSyntax) {
                        ReportSyntaxError("'yield' inside async function");
                    }
                    Eat(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;
        }
Exemple #23
0
 public TryStatementHandler(Expression test, Expression target, Statement body) {
     _test = test;
     _target = target;
     _body = body;
 }
Exemple #24
0
 public ParenthesisExpression(Expression expression) {
     _expression = expression;
 }
Exemple #25
0
 public SetComprehension(Expression item, ComprehensionIterator[] iterators) {
     _item = item;
     _iterators = iterators;
 }
Exemple #26
0
 public AndExpression(Expression left, Expression right) {
     _left = left;
     _right = right;
     StartIndex = left.StartIndex;
     EndIndex = right.EndIndex;
 }
Exemple #27
0
 public UnaryExpression(PythonOperator op, Expression expression) {
     _op = op;
     _expression = expression;
     EndIndex = expression.EndIndex;
 }
 private bool IsInRange(Expression node) {
     var span = node.GetSpan(_ast);
     int isct0 = Math.Max(span.Start.Line, _startLine);
     int isct1 = Math.Min(span.End.Line, _endLine);
     return isct0 <= isct1;
 }
Exemple #29
0
 public WithItem(Expression contextManager, Expression variable) {
     _contextManager = contextManager;
     _variable = variable;
 }
Exemple #30
0
 public AwaitExpression(Expression expression) {
     _expression = expression;
 }