// TryStmt public override bool Walk(TryStatement node) { BitArray save = _bits; _bits = new BitArray(_bits); // Flow the body node.Body.Walk(this); if (node.Else != null) { // Else is flown only after completion of Try with same bits node.Else.Walk(this); } if (node.Handlers != null) { foreach (TryStatementHandler tsh in node.Handlers) { // Restore to saved state _bits.SetAll(false); _bits.Or(save); // Flow the test if (tsh.Test != null) { tsh.Test.Walk(this); } // Define the target if (tsh.Target != null) { tsh.Target.Walk(_fdef); } // Flow the body tsh.Body.Walk(this); } } _bits = save; if (node.Finally != null) { // Flow finally - this executes no matter what node.Finally.Walk(this); } return(false); }
public void Visit(PyAst.TryStatement node) { AppendLineWithIndentation("try:"); using (new Indenter(this)) { Visit(node.Body); } foreach (var handler in node.Handlers) { var targetPart = (handler.Target is null ? "" : " as " + Visit(handler.Target)); AppendLineWithIndentation($"except {Visit(handler.Test)}{targetPart}:"); using (new Indenter(this)) { Visit(handler.Body); } } if (node.Finally != null) { AppendLineWithIndentation("finally:"); using (new Indenter(this)) { Visit(node.Finally); } } }
public override bool Walk(TryStatement node) { CommonWalk(node); return true; }
//try_stmt: ('try' ':' suite (except_clause ':' suite)+ // ['else' ':' suite] | 'try' ':' suite 'finally' ':' suite) //# NB compile.c makes sure that the default except clause is last // Python 2.5 grammar //try_stmt: 'try' ':' suite // ( // (except_clause ':' suite)+ // ['else' ':' suite] // ['finally' ':' suite] // | // 'finally' : suite // ) private Statement ParseTryStatement() { Eat(TokenKind.KeywordTry); SourceLocation start = GetStart(); SourceLocation mid = GetEnd(); Statement body = ParseSuite(); Statement finallySuite = null; Statement elseSuite = null; Statement ret; if (MaybeEat(TokenKind.KeywordFinally)) { MarkFunctionContainsFinally(); finallySuite = ParseSuite(); TryStatement tfs = new TryStatement(body, null, elseSuite, finallySuite); tfs.Header = mid; ret = tfs; } else { List<TryStatementHandler> handlers = new List<TryStatementHandler>(); TryStatementHandler dh = null; do { TryStatementHandler handler = ParseTryStmtHandler(); handlers.Add(handler); if (dh != null) { ReportSyntaxError(dh.Start, dh.End, "default 'except' must be last"); } if (handler.Test == null) { dh = handler; } } while (PeekToken().Kind == TokenKind.KeywordExcept); if (MaybeEat(TokenKind.KeywordElse)) { elseSuite = ParseSuite(); } if (MaybeEat(TokenKind.KeywordFinally)) { // If this function has an except block, then it can set the current exception. MarkFunctionContainsFinally(); finallySuite = ParseSuite(); } TryStatement ts = new TryStatement(body, handlers.ToArray(), elseSuite, finallySuite); ts.Header = mid; ret = ts; } ret.SetLoc(start, GetEnd()); return ret; }
public override void PostWalk(TryStatement node) { CommonPostWalk(node); }
//try_stmt: ('try' ':' suite (except_clause ':' suite)+ // ['else' ':' suite] | 'try' ':' suite 'finally' ':' suite) //# NB compile.c makes sure that the default except clause is last // Python 2.5 grammar //try_stmt: 'try' ':' suite // ( // (except_clause ':' suite)+ // ['else' ':' suite] // ['finally' ':' suite] // | // 'finally' : suite // ) private Statement ParseTryStatement() { Eat(TokenKind.KeywordTry); var start = GetStart(); var mid = GetEnd(); Statement body = ParseSuite(); Statement finallySuite = null; Statement elseSuite = null; Statement ret; int end; if (MaybeEat(TokenKind.KeywordFinally)) { finallySuite = ParseFinallySuite(finallySuite); end = finallySuite.EndIndex; TryStatement tfs = new TryStatement(body, null, elseSuite, finallySuite); tfs.HeaderIndex = mid; ret = tfs; } else { List<TryStatementHandler> handlers = new List<TryStatementHandler>(); TryStatementHandler dh = null; do { TryStatementHandler handler = ParseTryStmtHandler(); end = handler.EndIndex; handlers.Add(handler); if (dh != null) { ReportSyntaxError(dh.StartIndex, dh.EndIndex, "default 'except' must be last"); } if (handler.Test == null) { dh = handler; } } while (PeekToken().Kind == TokenKind.KeywordExcept); if (MaybeEat(TokenKind.KeywordElse)) { elseSuite = ParseSuite(); end = elseSuite.EndIndex; } if (MaybeEat(TokenKind.KeywordFinally)) { // If this function has an except block, then it can set the current exception. finallySuite = ParseFinallySuite(finallySuite); end = finallySuite.EndIndex; } TryStatement ts = new TryStatement(body, handlers.ToArray(), elseSuite, finallySuite); ts.HeaderIndex = mid; ret = ts; } ret.SetLoc(_globalParent, start, end); return ret; }
// TryStatement public virtual bool Walk(TryStatement node) { return true; }
public TryBlock(TryStatement stmt, State state, bool isPython24TryFinallyStmt) : base(state) { this.stmt = stmt; this.isPython24TryFinallyStmt = isPython24TryFinallyStmt; }
internal TryExcept(TryStatement stmt) : this() { _body = ConvertStatements(stmt.Body); _handlers = PythonOps.MakeEmptyList(stmt.Handlers.Count); foreach (TryStatementHandler tryStmt in stmt.Handlers) _handlers.Add(Convert(tryStmt)); _orelse = ConvertStatements(stmt.Else, true); }
// TryStmt public override bool Walk(TryStatement node) { BitArray save = _bits; _bits = new BitArray(_bits); // Flow the body node.Body.Walk(this); if (node.Else != null) { // Else is flown only after completion of Try with same bits node.Else.Walk(this); } if (node.Handlers != null) { foreach (TryStatementHandler tsh in node.Handlers) { // Restore to saved state _bits.SetAll(false); _bits.Or(save); // Flow the test if (tsh.Test != null) { tsh.Test.Walk(this); } // Define the target if (tsh.Target != null) { tsh.Target.Walk(_fdef); } // Flow the body tsh.Body.Walk(this); } } _bits = save; if (node.Finally != null) { // Flow finally - this executes no matter what node.Finally.Walk(this); } return false; }
// TryStatement public override bool Walk(TryStatement node) { if (node.Handlers != null) { foreach (TryStatementHandler tsh in node.Handlers) { if (tsh.Target != null) { tsh.Target.Walk(_define); } } } return true; }
public override bool Walk(TryStatement node) { Emit(node); return false; }
public TryDefinition(TryStatement tryStatement, TryStatementHandler tryHandler) { this.tryStatement = tryStatement; this.tryHandler = tryHandler; }
public override bool Walk(TryStatement node) { return true; }
internal static stmt Convert(TryStatement stmt) { if (stmt.Finally != null) { PythonList body; if (stmt.Handlers != null && stmt.Handlers.Count != 0) { stmt tryExcept = new TryExcept(stmt); tryExcept.GetSourceLocation(stmt); body = PythonOps.MakeListNoCopy(tryExcept); } else body = ConvertStatements(stmt.Body); return new TryFinally(body, ConvertStatements(stmt.Finally)); } return new TryExcept(stmt); }
public virtual void PostWalk(TryStatement node) { }
public override bool Walk(TryStatement node) { TryBlock tb = null; if (!Options.Python25 && node.Handlers == null) tb = new TryBlock(node, true); else tb = new TryBlock(node, false); tryBlocks.Push(tb); node.Body.Walk(this); if (node.Handlers != null) { tb.state = TryBlock.State.Handler; foreach (TryStatementHandler handler in node.Handlers) { handler.Walk(this); } } if (node.ElseStatement != null) { tb.state = TryBlock.State.Else; node.ElseStatement.Walk(this); } if (node.FinallyStatement != null) { tb.state = TryBlock.State.Finally; node.FinallyStatement.Walk(this); } ExceptionBlock eb = tryBlocks.Pop(); Debug.Assert((object)tb == (object)eb); return false; }
public void PostWalk(TryStatement node) { PostProcess(node); }
public TryBlock(TryStatement stmt, bool isPython24TryFinallyStmt) : this(stmt, State.Try, isPython24TryFinallyStmt) { }
// TryStatement public bool Walk(TryStatement node) { return Process(node); }
internal static stmt Convert(TryStatement stmt) { if (stmt.Finally != null) { PythonList body; if (stmt.Handlers != null && stmt.Handlers.Count != 0) body = PythonOps.MakeListNoCopy(new TryExcept(stmt)); else body = ConvertStatements(stmt.Body); return new TryFinally(body, ConvertStatements(stmt.Finally)); } return new TryExcept(stmt); }
//try_stmt: ('try' ':' suite (except_clause ':' suite)+ // ['else' ':' suite] | 'try' ':' suite 'finally' ':' suite) //# NB compile.c makes sure that the default except clause is last // Python 2.5 grammar //try_stmt: 'try' ':' suite // ( // (except_clause ':' suite)+ // ['else' ':' suite] // ['finally' ':' suite] // | // 'finally' : suite // ) private Statement ParseTryStmt() { Eat(TokenKind.KeywordTry); Location start = GetStart(); Location mid = GetEnd(); Statement body = ParseSuite(); Statement finallySuite = null; TryStatementHandler[] handlers = null; Statement elseSuite = null; Statement ret; if (MaybeEat(TokenKind.KeywordFinally)) { finallySuite = ParseSuite(); TryStatement tfs = new TryStatement(body, handlers, elseSuite, finallySuite); tfs.Header = mid; ret = tfs; } else { List<TryStatementHandler> l = new List<TryStatementHandler>(); do { l.Add(ParseTryStmtHandler()); } while (PeekToken().Kind == TokenKind.KeywordExcept); handlers = l.ToArray(); if (MaybeEat(TokenKind.KeywordElse)) { elseSuite = ParseSuite(); } if (Options.Python25 && MaybeEat(TokenKind.KeywordFinally)) { finallySuite = ParseSuite(); } TryStatement ts = new TryStatement(body, handlers, elseSuite, finallySuite); ts.Header = mid; ret = ts; } ret.SetLoc(GetExternal(), start, GetEnd()); return ret; }
/// <summary> /// WithStatement is translated to the DLR AST equivalent to /// the following Python code snippet (from with statement spec): /// /// mgr = (EXPR) /// exit = mgr.__exit__ # Not calling it yet /// value = mgr.__enter__() /// exc = True /// try: /// VAR = value # Only if "as VAR" is present /// BLOCK /// except: /// # The exceptional case is handled here /// exc = False /// if not exit(*sys.exc_info()): /// raise /// # The exception is swallowed if exit() returns true /// finally: /// # The normal and non-local-goto cases are handled here /// if exc: /// exit(None, None, None) /// /// </summary> public override MSAst.Expression Reduce() { // Five statements in the result... ReadOnlyCollectionBuilder <MSAst.Expression> statements = new ReadOnlyCollectionBuilder <MSAst.Expression>(6); ReadOnlyCollectionBuilder <MSAst.ParameterExpression> variables = new ReadOnlyCollectionBuilder <MSAst.ParameterExpression>(6); MSAst.ParameterExpression lineUpdated = Ast.Variable(typeof(bool), "$lineUpdated_with"); variables.Add(lineUpdated); //****************************************************************** // 1. mgr = (EXPR) //****************************************************************** MSAst.ParameterExpression manager = Ast.Variable(typeof(object), "with_manager"); variables.Add(manager); statements.Add( GlobalParent.AddDebugInfo( Ast.Assign( manager, _contextManager ), new SourceSpan(Start, _header) ) ); //****************************************************************** // 2. exit = mgr.__exit__ # Not calling it yet //****************************************************************** MSAst.ParameterExpression exit = Ast.Variable(typeof(object), "with_exit"); variables.Add(exit); statements.Add( MakeAssignment( exit, GlobalParent.Get( "__exit__", manager ) ) ); //****************************************************************** // 3. value = mgr.__enter__() //****************************************************************** MSAst.ParameterExpression value = Ast.Variable(typeof(object), "with_value"); variables.Add(value); statements.Add( GlobalParent.AddDebugInfoAndVoid( MakeAssignment( value, Parent.Invoke( new CallSignature(0), Parent.LocalContext, GlobalParent.Get( "__enter__", manager ) ) ), new SourceSpan(Start, _header) ) ); //****************************************************************** // 4. exc = True //****************************************************************** MSAst.ParameterExpression exc = Ast.Variable(typeof(bool), "with_exc"); variables.Add(exc); statements.Add( MakeAssignment( exc, AstUtils.Constant(true) ) ); //****************************************************************** // 5. The final try statement: // // try: // VAR = value # Only if "as VAR" is present // BLOCK // except: // # The exceptional case is handled here // exc = False // if not exit(*sys.exc_info()): // raise // # The exception is swallowed if exit() returns true // finally: // # The normal and non-local-goto cases are handled here // if exc: // exit(None, None, None) //****************************************************************** MSAst.ParameterExpression exception; MSAst.ParameterExpression nestedFrames = Ast.Variable(typeof(List <DynamicStackFrame>), "$nestedFrames"); variables.Add(nestedFrames); statements.Add( // try: AstUtils.Try( AstUtils.Try(// try statement body PushLineUpdated(false, lineUpdated), _var != null ? (MSAst.Expression)Ast.Block( // VAR = value _var.TransformSet(SourceSpan.None, value, PythonOperationKind.None), // BLOCK _body, AstUtils.Empty() ) : // BLOCK (MSAst.Expression)_body // except:, // try statement location ).Catch(exception = Ast.Variable(typeof(Exception), "exception"), // Python specific exception handling code TryStatement.GetTracebackHeader( this, exception, GlobalParent.AddDebugInfoAndVoid( Ast.Block( // exc = False MakeAssignment( exc, AstUtils.Constant(false) ), Ast.Assign( nestedFrames, Ast.Call(AstMethods.GetAndClearDynamicStackFrames) ), // if not exit(*sys.exc_info()): // raise AstUtils.IfThen( GlobalParent.Convert( typeof(bool), ConversionResultKind.ExplicitCast, GlobalParent.Operation( typeof(bool), PythonOperationKind.IsFalse, MakeExitCall(exit, exception) ) ), UpdateLineUpdated(true), Ast.Call( AstMethods.SetDynamicStackFrames, nestedFrames ), Ast.Throw( Ast.Call( AstMethods.MakeRethrowExceptionWorker, exception ) ) ) ), _body.Span ) ), Ast.Call( AstMethods.SetDynamicStackFrames, nestedFrames ), PopLineUpdated(lineUpdated), Ast.Empty() ) // finally: ).Finally( // if exc: // exit(None, None, None) AstUtils.IfThen( exc, GlobalParent.AddDebugInfoAndVoid( Ast.Block( Ast.Dynamic( GlobalParent.PyContext.Invoke( new CallSignature(3) // signature doesn't include function ), typeof(object), new MSAst.Expression[] { Parent.LocalContext, exit, AstUtils.Constant(null), AstUtils.Constant(null), AstUtils.Constant(null) } ), Ast.Empty() ), _contextManager.Span ) ) ) ); statements.Add(AstUtils.Empty()); return(Ast.Block(variables.ToReadOnlyCollection(), statements.ToReadOnlyCollection())); }
public void Analyze(TryStatement ts) { this.tryStatement = ts; foreach (TryStatementHandler tsh in tryStatement.Handlers) { this.tryHandler = tsh; if (tsh.Target != null) { tsh.Target.Walk(this); } } }
// TryStatement public override bool Walk(TryStatement node) { tryAnalyzer.Analyze(node); return true; }
// TryStmt public override bool Walk(TryStatement node) { if (node.Handlers != null) { foreach (TryStatementHandler tsh in node.Handlers) { if (tsh.Target != null) { tsh.Target.Walk(define); } } } // Add locals Debug.Assert(current != null); current.TempsCount += TryStatement.LocalSlots; return true; }