public AndExpression(Expression left, Expression right) { this.left = left; this.right = right; this.Start = left.Start; this.End = right.End; }
/// <summary> /// Gets the fully qualified name of the type from the expression. /// </summary> /// <remarks> /// The expression is the first target of a call expression. /// /// A call expression is a method or constructor call (right hand side of expression below): /// /// a = Root.Test.Class1() /// /// So the expression passed to this method will be a field expression in the /// above example which refers to Class1. The next target will be a field /// expression referring to Test. The The last target will be a name expression /// referring to Root. /// /// If we have /// /// a = Class1() /// /// then the expression will be a name expression referring to Class1. /// </remarks> string GetTypeName(Expression expression) { NameExpression nameExpression = expression as NameExpression; if (nameExpression != null) { return nameExpression.Name; } return PythonControlFieldExpression.GetMemberName(expression as MemberExpression); }
public string Visit(PyAst.Expression node) { switch (node) { case PyAst.AndExpression n: return(Visit(n)); case PyAst.BackQuoteExpression n: return(Visit(n)); case PyAst.BinaryExpression n: return(Visit(n)); case PyAst.CallExpression n: return(Visit(n)); case PyAst.ConditionalExpression n: return(Visit(n)); case PyAst.ConstantExpression n: return(Visit(n)); case PyAst.DictionaryComprehension n: return(Visit(n)); case PyAst.DictionaryExpression n: return(Visit(n)); case PyAst.ErrorExpression n: return(Visit(n)); case PyAst.GeneratorExpression n: return(Visit(n)); case PyAst.IndexExpression n: return(Visit(n)); case PyAst.LambdaExpression n: return(Visit(n)); case PyAst.ListComprehension n: return(Visit(n)); case PyAst.ListExpression n: return(Visit(n)); case PyAst.MemberExpression n: return(Visit(n)); case PyAst.NameExpression n: return(Visit(n)); case PyAst.OrExpression n: return(Visit(n)); case PyAst.ParenthesisExpression n: return(Visit(n)); case PyAst.SetComprehension n: return(Visit(n)); case PyAst.SetExpression n: return(Visit(n)); case PyAst.SliceExpression n: return(Visit(n)); case PyAst.TupleExpression n: return(Visit(n)); case PyAst.UnaryExpression n: return(Visit(n)); case PyAst.YieldExpression n: return(Visit(n)); default: throw new NotImplementedException($"Printing of expression node {node} not implemented"); } }
void AddBaseType(Expression baseTypeExpression) { NameExpression nameExpression = baseTypeExpression as NameExpression; MemberExpression memberExpression = baseTypeExpression as MemberExpression; if (nameExpression != null) { AddBaseType(nameExpression.Name); } else if (memberExpression != null) { AddBaseType(memberExpression); } }
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(IronPython.Resources.NonKeywordAfterKeywordArg); } } else if (arg.Name == "*") { if (hasArgsTuple || hasKeywordDict) { ReportSyntaxError(IronPython.Resources.OneListArgOnly); } hasArgsTuple = true; extraArgs++; } else if (arg.Name == "**") { if (hasKeywordDict) { ReportSyntaxError(IronPython.Resources.OneKeywordArgOnly); } hasKeywordDict = true; extraArgs++; } else { if (hasKeywordDict) { ReportSyntaxError(IronPython.Resources.KeywordOutOfSequence); } keywordCount++; } } return new CallExpression(target, args); }
// comp_iter '}' private DictionaryComprehension FinishDictComp(Expression key, Expression value, int oStart, int oEnd) { ComprehensionIterator[] iters = ParseCompIter(); Eat(TokenKind.RightBrace); var cStart = GetStart(); var cEnd = GetEnd(); if (_sink != null) { _sink.MatchPair( new SourceSpan(_tokenizer.IndexToLocation(oStart), _tokenizer.IndexToLocation(oEnd)), new SourceSpan(_tokenizer.IndexToLocation(cStart), _tokenizer.IndexToLocation(cEnd)), 1 ); } var ret = new DictionaryComprehension(key, value, iters); ret.SetLoc(_globalParent, oStart, cEnd); return ret; }
private Expression FinishExpressionListAsExpr(Expression expr) { var start = GetStart(); bool trailingComma = true; List<Expression> l = new List<Expression>(); l.Add(expr); while (true) { if (NeverTestToken(PeekToken())) break; expr = ParseExpression(); l.Add(expr); if (!MaybeEat(TokenKind.Comma)) { trailingComma = false; break; } trailingComma = true; } Expression ret = MakeTupleOrExpr(l, trailingComma); ret.SetLoc(_globalParent, start, GetEnd()); return ret; }
private Arg FinishKeywordArgument(Expression t) { NameExpression n = t as NameExpression; if (n == null) { ReportSyntaxError(IronPython.Resources.ExpectedName); Arg arg = new Arg(null, t); arg.SetLoc(_globalParent, t.StartIndex, t.EndIndex); return arg; } else { Expression val = ParseExpression(); Arg arg = new Arg(n.Name, val); arg.SetLoc(_globalParent, n.StartIndex, val.EndIndex); return arg; } }
// 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(); Arg[] args = FinishArgListOrGenExpr(); CallExpression call; if (args != null) { call = FinishCallExpr(ret, args); } else { call = new CallExpression(ret, new Arg[0]); } call.SetLoc(_globalParent, ret.StartIndex, GetEnd()); ret = call; break; case TokenKind.LeftBracket: NextToken(); Expression index = ParseSubscriptList(); IndexExpression ie = new IndexExpression(ret, index); ie.SetLoc(_globalParent, ret.StartIndex, GetEnd()); ret = ie; break; case TokenKind.Dot: NextToken(); string name = ReadNameMaybeNone(); MemberExpression fe = new MemberExpression(ret, name); fe.SetLoc(_globalParent, ret.StartIndex, GetEnd()); ret = fe; break; case TokenKind.Constant: // abc.1, abc"", abc 1L, abc 0j ReportSyntaxError("invalid syntax"); return Error(); default: return ret; } } } finally { _allowIncomplete = prevAllow; } }
private Expression ParseConditionalTest(Expression trueExpr) { Expression expr = ParseOrTest(); Eat(TokenKind.KeywordElse); Expression falseExpr = ParseExpression(); return new ConditionalExpression(expr, trueExpr, falseExpr); }
private Expression ParseLambdaHelperEnd(FunctionDefinition func, Expression expr) { // Pep 342 in Python 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(_globalParent, expr.IndexSpan); body = new ExpressionStatement(y); } else { body = new ReturnStatement(expr); } body.SetLoc(_globalParent, expr.StartIndex, expr.EndIndex); FunctionDefinition func2 = PopFunction(); System.Diagnostics.Debug.Assert(func == func2); func.Body = body; func.EndIndex = GetEnd(); LambdaExpression ret = new LambdaExpression(func); func.SetLoc(_globalParent, func.IndexSpan); ret.SetLoc(_globalParent, func.IndexSpan); return ret; }
public YieldStatement(Expression expression, int index) { this.expr = expression; this.index = index; }
private List<YieldTarget> yieldTargets; // = new List<YieldTarget>(); #endregion Fields #region Constructors public WithStatement(Expression contextManager, Expression var, Statement body) { this.contextManager = contextManager; this.var = var; this.body = body; }
public WhileStatement(Expression test, Statement body, Statement else_) { this.test = test; this.body = body; this.elseStmt = else_; }
public TryStatementHandler(Expression test, Expression target, Statement body) { this.test = test; this.target = target; this.body = body; }
public override MSAst.Expression Reduce() { if (_names == _star) { // from a[.b] import * return GlobalParent.AddDebugInfo( Ast.Call( AstMethods.ImportStar, Parent.LocalContext, AstUtils.Constant(_root.MakeString()), AstUtils.Constant(GetLevel()) ), Span ); } else { // from a[.b] import x [as xx], [ y [ as yy] ] [ , ... ] ReadOnlyCollectionBuilder<MSAst.Expression> statements = new ReadOnlyCollectionBuilder<MSAst.Expression>(); MSAst.ParameterExpression module = Ast.Variable(typeof(object), "module"); // Create initializer of the array of names being passed to ImportWithNames MSAst.Expression[] names = new MSAst.Expression[_names.Length]; for (int i = 0; i < names.Length; i++) { names[i] = AstUtils.Constant(_names[i]); } // module = PythonOps.ImportWithNames(<context>, _root, make_array(_names)) statements.Add( GlobalParent.AddDebugInfoAndVoid( AssignValue( module, LightExceptions.CheckAndThrow( Expression.Call( AstMethods.ImportWithNames, Parent.LocalContext, AstUtils.Constant(_root.MakeString()), Ast.NewArrayInit(typeof(string), names), AstUtils.Constant(GetLevel()) ) ) ), _root.Span ) ); // now load all the names being imported and assign the variables for (int i = 0; i < names.Length; i++) { statements.Add( GlobalParent.AddDebugInfoAndVoid( AssignValue( Parent.GetVariableExpression(_variables[i]), Ast.Call( AstMethods.ImportFrom, Parent.LocalContext, module, names[i] ) ), Span ) ); } statements.Add(AstUtils.Empty()); return GlobalParent.AddDebugInfo(Ast.Block(new[] { module }, statements.ToArray()), Span); } }
//classdef: 'class' NAME ['(' testlist ')'] ':' suite private ClassDefinition ParseClassDef() { Eat(TokenKind.KeywordClass); var start = GetStart(); string name = ReadName(); if (name == null) { // no name, assume there's no class. return new ClassDefinition(null, new Expression[0], ErrorStmt()); } Expression[] bases = new Expression[0]; if (MaybeEat(TokenKind.LeftParenthesis)) { List<Expression> l = ParseTestList(); if (l.Count == 1 && l[0] is ErrorExpression) { // error handling, classes is incomplete. return new ClassDefinition(name, new Expression[0], ErrorStmt()); } bases = l.ToArray(); Eat(TokenKind.RightParenthesis); } var mid = GetEnd(); // Save private prefix string savedPrefix = SetPrivatePrefix(name); // Parse the class body Statement body = ParseClassOrFuncBody(); // Restore the private prefix _privatePrefix = savedPrefix; ClassDefinition ret = new ClassDefinition(name, bases, body); ret.HeaderIndex = mid; ret.SetLoc(_globalParent, start, GetEnd()); return ret; }
public DelStatement(Expression[] expressions) { this.exprs = expressions; }
public WithItem(int start, Expression contextManager, Expression variable) { Start = start; ContextManager = contextManager; Variable = variable; }
public ExecStatement(Expression code, Expression locals, Expression globals) { this.code = code; this.locals = locals; this.globals = globals; }
private Expression AddTrailers(Expression ret) { return AddTrailers(ret, true); }
public AssertStatement(Expression test, Expression message) { this.test = test; this.message = message; }
private Expression FinishSlice(Expression e0, int start) { Expression e1 = null; Expression e2 = null; bool stepProvided = false; switch (PeekToken().Kind) { case TokenKind.Comma: case TokenKind.RightBracket: break; case TokenKind.Colon: // x[?::?] stepProvided = true; NextToken(); e2 = ParseSliceEnd(); break; default: // x[?:val:?] e1 = ParseExpression(); if (MaybeEat(TokenKind.Colon)) { stepProvided = true; e2 = ParseSliceEnd(); } break; } SliceExpression ret = new SliceExpression(e0, e1, e2, stepProvided); ret.SetLoc(_globalParent, start, GetEnd()); return ret; }
public ExpressionStatement(Expression expression) { this.expr = expression; }
private Expression ParseTestListAsExpr(Expression expr) { List<Expression> l = new List<Expression>(); l.Add(expr); bool trailingComma = true; while (true) { if (NeverTestToken(PeekToken())) break; l.Add(ParseExpression()); if (!MaybeEat(TokenKind.Comma)) { trailingComma = false; break; } } return MakeTupleOrExpr(l, trailingComma); }
public ForStatement(Expression lhs, Expression list, Statement body, Statement else_) { this.lhs = lhs; this.list = list; this.body = body; this.elseStmt = else_; }
// genexpr_for ::= "for" target_list "in" or_test [genexpr_iter] // genexpr_iter ::= (genexpr_for | genexpr_if) * // // "for" has NOT been eaten before entering this method private Expression ParseGeneratorExpression(Expression expr) { ForStatement root = ParseGenExprFor(); Statement current = root; for (; ; ) { if (PeekToken(Tokens.KeywordForToken)) { current = NestGenExpr(current, ParseGenExprFor()); } else if (PeekToken(Tokens.KeywordIfToken)) { current = NestGenExpr(current, ParseGenExprIf()); } else { // Generator Expressions have an implicit function definition and yield around their expression. // (x for i in R) // becomes: // def f(): // for i in R: yield (x) ExpressionStatement ys = new ExpressionStatement(new YieldExpression(expr)); ys.Expression.SetLoc(_globalParent, expr.IndexSpan); ys.SetLoc(_globalParent, expr.IndexSpan); NestGenExpr(current, ys); break; } } // We pass the outermost iterable in as a parameter because Python semantics // say that this one piece is computed at definition time rather than iteration time const string fname = "<genexpr>"; Parameter parameter = new Parameter("__gen_$_parm__", 0); FunctionDefinition func = new FunctionDefinition(fname, new Parameter[] { parameter }, root); func.IsGenerator = true; func.SetLoc(_globalParent, root.StartIndex, GetEnd()); func.HeaderIndex = root.EndIndex; // Transform the root "for" statement Expression outermost = root.List; NameExpression ne = new NameExpression("__gen_$_parm__"); ne.SetLoc(_globalParent, outermost.IndexSpan); root.List = ne; GeneratorExpression ret = new GeneratorExpression(func, outermost); ret.SetLoc(_globalParent, expr.StartIndex, GetEnd()); return ret; }
public IfStatementTest(Expression test, Statement body) { this.test = test; this.body = body; }
// list_iter ']' private ListComprehension FinishListComp(Expression item) { ComprehensionIterator[] iters = ParseListCompIter(); Eat(TokenKind.RightBracket); return new ListComprehension(item, iters); }
// list_iter: list_for | list_if private ListComprehension FinishListComp(Expression item) { List<ListComprehensionIterator> iters = new List<ListComprehensionIterator>(); ListComprehensionFor firstFor = ParseListCompFor(); iters.Add(firstFor); while (true) { if (PeekToken(Tokens.KeywordForToken)) { iters.Add(ParseListCompFor()); } else if (PeekToken(Tokens.KeywordIfToken)) { iters.Add(ParseListCompIf()); } else { break; } } Eat(TokenKind.RightBracket); return new ListComprehension(item, iters.ToArray()); }
private Statement FinishAssignments(Expression right) { List<Expression> left = null; Expression singleLeft = null; while (MaybeEat(TokenKind.Assign)) { 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 (MaybeEat(TokenKind.KeywordYield)) { right = ParseYieldExpression(); } else { right = ParseTestListAsExpr(); } } if (left != null) { Debug.Assert(left.Count > 0); AssignmentStatement assign = new AssignmentStatement(left.ToArray(), right); assign.SetLoc(_globalParent, left[0].StartIndex, right.EndIndex); return assign; } else { Debug.Assert(singleLeft != null); AssignmentStatement assign = new AssignmentStatement(new[] { singleLeft }, right); assign.SetLoc(_globalParent, singleLeft.StartIndex, right.EndIndex); return assign; } }
private Statement FinishAssignments(Expression right) { List<Expression> left = new List<Expression>(); while (MaybeEat(TokenKind.Assign)) { string assignError = right.CheckAssign(); if (assignError != null) { ReportSyntaxError(right.Span.Start, right.Span.End, assignError, ErrorCodes.SyntaxError); } left.Add(right); if (MaybeEat(TokenKind.KeywordYield)) { right = ParseYieldExpression(); } else { bool trailingComma; var exprs = ParseExpressionList(out trailingComma); if (exprs.Count == 0) { ReportSyntaxError(left[0].Start, left[0].End, "invalid syntax"); } right = MakeTupleOrExpr(exprs, trailingComma); } } Debug.Assert(left.Count > 0); AssignmentStatement assign = new AssignmentStatement(left.ToArray(), right); assign.SetLoc(left[0].Start, right.End); return assign; }