static SourceLocation GetBodyEndLocation(Statement body) { if (body.Parent != null) { return body.End; } return SourceLocation.Invalid; }
public PythonAst(Statement body, bool isModule, PythonLanguageFeatures languageFeatures, bool printExpressions) { ContractUtils.RequiresNotNull(body, "body"); _body = body; _isModule = isModule; _printExpressions = printExpressions; _languageFeatures = languageFeatures; }
public void Visit(PyAst.Statement node) { switch (node) { case PyAst.AssertStatement n: Visit(n); return; case PyAst.AssignmentStatement n: Visit(n); return; case PyAst.AugmentedAssignStatement n: Visit(n); return; case PyAst.BreakStatement n: Visit(n); return; case PyAst.ClassDefinition n: Visit(n); return; case PyAst.ContinueStatement n: Visit(n); return; case PyAst.DelStatement n: Visit(n); return; case PyAst.EmptyStatement n: Visit(n); return; case PyAst.ExecStatement n: Visit(n); return; case PyAst.ExpressionStatement n: Visit(n); return; case PyAst.ForStatement n: Visit(n); return; case PyAst.FromImportStatement n: Visit(n); return; case PyAst.FunctionDefinition n: Visit(n); return; case PyAst.GlobalStatement n: Visit(n); return; case PyAst.IfStatement n: Visit(n); return; case PyAst.ImportStatement n: Visit(n); return; case PyAst.PrintStatement n: Visit(n); return; //case PyAst.PythonAst n: Visit(n); return; case PyAst.RaiseStatement n: Visit(n); return; case PyAst.ReturnStatement n: Visit(n); return; case PyAst.SuiteStatement n: Visit(n); return; case PyAst.TryStatement n: Visit(n); return; case PyAst.WhileStatement n: Visit(n); return; case PyAst.WithStatement n: Visit(n); return; default: throw new NotImplementedException($"Printing of statement {node} not implemented"); } }
internal static IEnumerable<IScopeNode> EnumerateBody(Statement body) { SuiteStatement suite = body as SuiteStatement; if (suite != null) { foreach (Statement stmt in suite.Statements) { ClassDefinition klass = stmt as ClassDefinition; if (klass != null) { yield return new ClassScopeNode(klass); } FunctionDefinition func = stmt as FunctionDefinition; if (func != null) { yield return new FunctionScopeNode(func); } } } }
public IfStatement(IfStatementTest[] tests, Statement else_) { _tests = tests; _else = else_; }
public IfStatement(IfStatementTest[] tests, Statement else_) { this.tests = tests; this.elseStmt = else_; }
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 TryStatementHandler(Expression test, Expression target, Statement body) { this.test = test; this.target = target; this.body = body; }
public TryStatement(Statement body, TryStatementHandler[] handlers, Statement elseSuite, Statement finallySuite) { this.body = body; this.handlers = handlers; this.elseStmt = elseSuite; this.finallyStmt = finallySuite; }
private PythonAst FinishParsing(Statement ret) { var res = _globalParent; _globalParent = null; var lineLocs = _tokenizer.GetLineLocations(); // update line mapping if (_sourceUnit.HasLineMapping) { List<int> newLineMapping = new List<int>(); int last = 0; for (int i = 0; i < lineLocs.Length; i++) { while (newLineMapping.Count < i) { newLineMapping.Add(last); } last = lineLocs[i] + 1; newLineMapping.Add(lineLocs[i]); } lineLocs = newLineMapping.ToArray(); } res.ParsingFinished(lineLocs, ret, _languageFeatures); return res; }
public FunctionDefinition(string name, Parameter[] parameters, Statement body, SourceUnit sourceUnit) : this(name, parameters, body) { }
public WhileStatement(Expression test, Statement body, Statement else_) { _test = test; _body = body; _else = else_; }
public IfStatement(IfStatementTest[] tests, Statement else_) { _tests = tests; _else = else_; }
public WithStatement(Expression contextManager, Expression var, Statement body) { _contextManager = contextManager; _var = var; _body = body; }
internal static PythonList ConvertStatements(Statement stmt) { return ConvertStatements(stmt, false); }
internal static stmt Convert(Statement stmt) { stmt ast; if (stmt is FunctionDefinition) ast = new FunctionDef((FunctionDefinition)stmt); else if (stmt is ReturnStatement) ast = new Return((ReturnStatement)stmt); else if (stmt is AssignmentStatement) ast = new Assign((AssignmentStatement)stmt); else if (stmt is AugmentedAssignStatement) ast = new AugAssign((AugmentedAssignStatement)stmt); else if (stmt is DelStatement) ast = new Delete((DelStatement)stmt); else if (stmt is PrintStatement) ast = new Print((PrintStatement)stmt); else if (stmt is ExpressionStatement) ast = new Expr((ExpressionStatement)stmt); else if (stmt is ForStatement) ast = new For((ForStatement)stmt); else if (stmt is WhileStatement) ast = new While((WhileStatement)stmt); else if (stmt is IfStatement) ast = new If((IfStatement)stmt); else if (stmt is WithStatement) ast = new With((WithStatement)stmt); else if (stmt is RaiseStatement) ast = new Raise((RaiseStatement)stmt); else if (stmt is TryStatement) ast = Convert((TryStatement)stmt); else if (stmt is AssertStatement) ast = new Assert((AssertStatement)stmt); else if (stmt is ImportStatement) ast = new Import((ImportStatement)stmt); else if (stmt is FromImportStatement) ast = new ImportFrom((FromImportStatement)stmt); else if (stmt is ExecStatement) ast = new Exec((ExecStatement)stmt); else if (stmt is GlobalStatement) ast = new Global((GlobalStatement)stmt); else if (stmt is ClassDefinition) ast = new ClassDef((ClassDefinition)stmt); else if (stmt is BreakStatement) ast = new Break(); else if (stmt is ContinueStatement) ast = new Continue(); else if (stmt is EmptyStatement) ast = new Pass(); else throw new ArgumentTypeException("Unexpected statement type: " + stmt.GetType()); ast.GetSourceLocation(stmt); return ast; }
internal static MSAst.Expression TransformFor(ScopeStatement parent, MSAst.ParameterExpression enumerator, Expression list, Expression left, MSAst.Expression body, Statement else_, SourceSpan span, SourceLocation header, MSAst.LabelTarget breakLabel, MSAst.LabelTarget continueLabel, bool isStatement) { // enumerator, isDisposable = Dynamic(GetEnumeratorBinder, list) MSAst.Expression init = Ast.Assign( enumerator, new PythonDynamicExpression1 <KeyValuePair <IEnumerator, IDisposable> >( Binders.UnaryOperationBinder( parent.GlobalParent.PyContext, PythonOperationKind.GetEnumeratorForIteration ), parent.GlobalParent.CompilationMode, AstUtils.Convert(list, typeof(object)) ) ); // while enumerator.MoveNext(): // left = enumerator.Current // body // else: // else MSAst.Expression ls = AstUtils.Loop( parent.GlobalParent.AddDebugInfo( Ast.Call( Ast.Property( enumerator, typeof(KeyValuePair <IEnumerator, IDisposable>).GetProperty("Key") ), typeof(IEnumerator).GetMethod("MoveNext") ), left.Span ), null, Ast.Block( left.TransformSet( SourceSpan.None, Ast.Call( Ast.Property( enumerator, typeof(KeyValuePair <IEnumerator, IDisposable>).GetProperty("Key") ), typeof(IEnumerator).GetProperty("Current").GetGetMethod() ), PythonOperationKind.None ), body, isStatement ? UpdateLineNumber(parent.GlobalParent.IndexToLocation(list.StartIndex).Line) : AstUtils.Empty(), AstUtils.Empty() ), else_, breakLabel, continueLabel ); return(Ast.Block( init, Ast.TryFinally( ls, Ast.Call(AstMethods.ForLoopDispose, enumerator) ) )); }
private Statement FinishSmallStmt(Statement stmt) { NextToken(); stmt.SetLoc(_globalParent, GetStart(), GetEnd()); return stmt; }
public TryStatementHandler(Expression test, Expression target, Statement body) { _test = test; _target = target; _body = body; }
public SuiteStatement(Statement[] statements) { this.stmts = statements; }
public TryStatement(Statement body, TryStatementHandler[] handlers, Statement else_, Statement finally_) { _body = body; _handlers = handlers; _else = else_; _finally = finally_; }
public static bool FindControlFlow(Statement statement, out bool foundLoopControl) { // No return in null statement if (statement == null) { foundLoopControl = false; return false; } // find it now. ControlFlowFinder rf = new ControlFlowFinder(); statement.Walk(rf); foundLoopControl = rf.foundLoopControl; return rf.found; }
public FunctionDefinition(SymbolId name, Parameter[] parameters, Statement body, SourceUnit sourceUnit) { _name = name; _parameters = parameters; _body = body; _sourceUnit = sourceUnit; }
public WhileStatement(Expression test, Statement body, Statement else_) { this.test = test; this.body = body; this.elseStmt = else_; }
public ClassDefinition(SymbolId name, Expression[] bases, Statement body) { _name = name; _bases = bases; _body = body; }
public ForStatement(Expression lhs, Expression list, Statement body, Statement else_) { this.lhs = lhs; this.list = list; this.body = body; this.elseStmt = else_; }
internal static Statement RevertStmts(PythonList stmts) { if (stmts.Count == 1) return ((stmt)stmts[0]).Revert(); Statement[] statements = new Statement[stmts.Count]; for (int i = 0; i < stmts.Count; i++) statements[i] = ((stmt)stmts[i]).Revert(); return new SuiteStatement(statements); }
public IfStatementTest(Expression test, Statement body) { this.test = test; this.body = body; }
private Module DoAnalyze(Modules modules, string name, Statement root) { GlobalSuite global = new GlobalSuite(root); module = new Module(modules, name, global, scopes); ModuleScope modsc; module.ModuleScope = modsc = new ModuleScope(module, null, global); PushScope(modsc); root.Walk(this); foreach (FieldAssignment fer in this.fields) { fer.Infer(module); } return module; }
private static stmt ConvertToAST(Statement stmt) { ContractUtils.RequiresNotNull(stmt, "stmt"); return AST.Convert(stmt); }
public ForStatement(Expression left, Expression list, Statement body, Statement else_) { _left = left; _list = list; _body = body; _else = else_; }
internal static PythonList ConvertStatements(Statement stmt, bool allowNull) { if (stmt == null) if (allowNull) return PythonOps.MakeEmptyList(0); else throw new ArgumentNullException("stmt"); if (stmt is SuiteStatement) { SuiteStatement suite = (SuiteStatement)stmt; PythonList l = PythonOps.MakeEmptyList(suite.Statements.Count); foreach (Statement s in suite.Statements) l.Add(Convert(s)); return l; } return PythonOps.MakeListNoCopy(Convert(stmt)); }
internal static MSAst.Expression TransformForStatement(AstGenerator ag, MSAst.ParameterExpression enumerator, Expression list, Expression left, MSAst.Expression body, Statement else_, SourceSpan span, SourceLocation header, MSAst.LabelTarget breakLabel, MSAst.LabelTarget continueLabel) { // enumerator = PythonOps.GetEnumeratorForIteration(list) MSAst.Expression init = Ast.Assign( enumerator, ag.Operation( typeof(IEnumerator), PythonOperationKind.GetEnumeratorForIteration, ag.TransformAsObject(list) ) ); // while enumerator.MoveNext(): // left = enumerator.Current // body // else: // else MSAst.Expression ls = AstUtils.Loop( ag.AddDebugInfo(Ast.Call( enumerator, typeof(IEnumerator).GetMethod("MoveNext") ), left.Span), null, Ast.Block( left.TransformSet( ag, SourceSpan.None, Ast.Call( enumerator, typeof(IEnumerator).GetProperty("Current").GetGetMethod() ), PythonOperationKind.None ), body, ag.UpdateLineNumber(list.Start.Line), AstUtils.Empty() ), ag.Transform(else_), breakLabel, continueLabel ); return Ast.Block( init, ls, AstUtils.Empty() ); }
private Statement ParseFinallySuite(Statement finallySuite) { MarkFunctionContainsFinally(); bool inFinally = _inFinally, inFinallyLoop = _inFinallyLoop; try { _inFinally = true; _inFinallyLoop = false; finallySuite = ParseSuite(); } finally { _inFinally = inFinally; _inFinallyLoop = inFinallyLoop; } return finallySuite; }
public FunctionDefinition(SymbolId name, Expression[] parameters, Expression[] defaults, FunctionAttributes flags, Statement body, string sourceFile) : base(body) { this.name = name; this.parameters = parameters; this.defaults = defaults; this.flags = flags; this.decorators = null; this.filename = sourceFile; }
private static Statement NestGenExpr(Statement current, Statement nested) { ForStatement fes = current as ForStatement; IfStatement ifs; if (fes != null) { fes.Body = nested; } else if ((ifs = current as IfStatement) != null) { ifs.Tests[0].Body = nested; } return nested; }
public WhileStatement(Expression test, Statement body, Statement else_) { _test = test; _body = body; _else = else_; }
public DropDownEntryInfo(Statement body) { Body = body; }
public IfStatementTest(Expression test, Statement body) { _test = test; _body = body; }