コード例 #1
0
ファイル: Parser.cs プロジェクト: tnachen/ironruby
        //except_clause: 'except' [expression [',' expression]]
        //2.6: except_clause: 'except' [expression [(',' or 'as') expression]]
        private TryStatementHandler ParseTryStmtHandler() {
            Eat(TokenKind.KeywordExcept);

            // If this function has an except block, then it can set the current exception.
            FunctionDefinition current = CurrentFunction;
            if (current != null) {
                current.CanSetSysExcInfo = true;
            }

            SourceLocation start = GetStart();
            Expression test1 = null, test2 = null;
            if (PeekToken().Kind != TokenKind.Colon) {
                test1 = ParseExpression();
                if (MaybeEat(TokenKind.Comma) || (Python26 && MaybeEat(Symbols.As))) {
                    test2 = ParseExpression();
                }
            }
            SourceLocation mid = GetEnd();
            Statement body = ParseSuite();
            TryStatementHandler ret = new TryStatementHandler(test1, test2, body);
            ret.Header = mid;
            ret.SetLoc(start, GetEnd());
            return ret;
        }
コード例 #2
0
ファイル: JavascriptGenerator.cs プロジェクト: valdisz/PyToJs
 public override void PostWalk(TryStatementHandler node)
 {
     CommonPostWalk(node);
 }
コード例 #3
0
 // TryStatementHandler
 public bool Walk(TryStatementHandler node)
 {
     return Process(node);
 }
コード例 #4
0
 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);
         }
     }
 }
コード例 #5
0
ファイル: _ast.cs プロジェクト: rchandrashekara/main
            internal ExceptHandler(TryStatementHandler stmt)
                : this() {
                if (stmt.Test != null)
                    _type = Convert(stmt.Test);
                if (stmt.Target != null)
                    _name = Convert(stmt.Target, Store.Instance);

                _body = ConvertStatements(stmt.Body);
            }
コード例 #6
0
ファイル: Parser.cs プロジェクト: FabioNascimento/DICommander
 //except_clause: 'except' [test [',' test]]
 private TryStatementHandler ParseTryStmtHandler()
 {
     Eat(TokenKind.KeywordExcept);
     Location start = GetStart();
     Expression test1 = null, test2 = null;
     if (PeekToken().Kind != TokenKind.Colon) {
         test1 = ParseTest();
         if (MaybeEat(TokenKind.Comma)) {
             test2 = ParseTest();
         }
     }
     Location mid = GetEnd();
     Statement body = ParseSuite();
     TryStatementHandler ret = new TryStatementHandler(test1, test2, body);
     ret.Header = mid;
     ret.SetLoc(GetExternal(), start, GetEnd());
     return ret;
 }
コード例 #7
0
ファイル: TryStatement.cs プロジェクト: octavioh/ironruby
 public TryStatement(Statement body, TryStatementHandler[] handlers, Statement else_, Statement finally_) {
     _body = body;
     _handlers = handlers;
     _else = else_;
     _finally = finally_;
 }
コード例 #8
0
 public TryStatement(Statement body, TryStatementHandler[] handlers, Statement elseSuite, Statement finallySuite)
 {
     this.body = body;
     this.handlers = handlers;
     this.elseStmt = elseSuite;
     this.finallyStmt = finallySuite;
 }
コード例 #9
0
 public static string Format(TryStatementHandler node)
 {
     return("except " + Format(node.Target) + "  " + (node.Test == null ? "" : Format(node.Test)) + ":" + Environment.NewLine
            + Format(node.Body));
 }
コード例 #10
0
 public TryDefinition(TryStatement tryStatement, TryStatementHandler tryHandler)
 {
     this.tryStatement = tryStatement;
     this.tryHandler = tryHandler;
 }
コード例 #11
0
 // TryStatementHandler
 public override bool Walk(TryStatementHandler node) {
     node.Parent = _currentScope;
     return base.Walk(node);
 }
コード例 #12
0
 public virtual void PostWalk(TryStatementHandler node)
 {
 }
コード例 #13
0
 // TryStatementHandler
 public virtual bool Walk(TryStatementHandler node)
 {
     return true;
 }
コード例 #14
0
ファイル: JavascriptGenerator.cs プロジェクト: valdisz/PyToJs
 public override bool Walk(TryStatementHandler node)
 {
     CommonWalk(node);
     return true;
 }
コード例 #15
0
ファイル: _ast.cs プロジェクト: TerabyteX/main
 internal override Statement Revert()
 {
     TryStatementHandler[] tshs = new TryStatementHandler[handlers.Count];
     for (int i = 0; i < handlers.Count; i++) {
         tshs[i] = ((ExceptHandler)handlers[i]).RevertHandler();
     }
     return new TryStatement(RevertStmts(body), tshs, RevertStmts(orelse), null);
 }
コード例 #16
0
 // TryStatementHandler
 public override bool Walk(TryStatementHandler node)
 {
     node.Parent = _currentScope;
     return(base.Walk(node));
 }
コード例 #17
0
ファイル: _ast.cs プロジェクト: TerabyteX/main
 internal override Statement Revert()
 {
     if (body.Count == 1 && body[0] is TryExcept) {
         TryExcept te = (TryExcept)body[0];
         TryStatementHandler[] tshs = new TryStatementHandler[te.handlers.Count];
         for (int i = 0; i < te.handlers.Count; i++) {
             tshs[i] = ((ExceptHandler)te.handlers[i]).RevertHandler();
         }
         return new TryStatement(RevertStmts(te.body), tshs, RevertStmts(te.orelse), RevertStmts(finalbody));
     }
     return new TryStatement(RevertStmts(body), null, null, RevertStmts(finalbody));
 }
コード例 #18
0
        /// <summary>
        /// Transform multiple python except handlers for a try block into a single catch body.
        /// </summary>
        /// <param name="exception">The variable for the exception in the catch block.</param>
        /// <returns>Null if there are no except handlers. Else the statement to go inside the catch handler</returns>
        private MSAst.Expression TransformHandlers(MSAst.ParameterExpression exception)
        {
            Assert.NotEmpty(_handlers);

            MSAst.ParameterExpression extracted = Ast.Variable(typeof(object), "$extracted");

            var tests = new List <Microsoft.Scripting.Ast.IfStatementTest>(_handlers.Length);

            MSAst.ParameterExpression converted = null;
            MSAst.Expression          catchAll  = null;

            for (int index = 0; index < _handlers.Length; index++)
            {
                TryStatementHandler tsh = _handlers[index];

                if (tsh.Test != null)
                {
                    Microsoft.Scripting.Ast.IfStatementTest ist;

                    //  translating:
                    //      except Test ...
                    //
                    //  generate following AST for the Test (common part):
                    //      CheckException(exception, Test)
                    MSAst.Expression test =
                        Ast.Call(
                            AstMethods.CheckException,
                            Parent.LocalContext,
                            extracted,
                            AstUtils.Convert(tsh.Test, typeof(object))
                            );

                    if (tsh.Target != null)
                    {
                        //  translating:
                        //      except Test, Target:
                        //          <body>
                        //  into:
                        //      if ((converted = CheckException(exception, Test)) != null) {
                        //          Target = converted;
                        //          traceback-header
                        //          <body>
                        //      }

                        if (converted == null)
                        {
                            converted = Ast.Variable(typeof(object), "$converted");
                        }

                        ist = AstUtils.IfCondition(
                            Ast.NotEqual(
                                Ast.Assign(converted, test),
                                AstUtils.Constant(null)
                                ),
                            Ast.Block(
                                tsh.Target.TransformSet(SourceSpan.None, converted, PythonOperationKind.None),
                                GlobalParent.AddDebugInfo(
                                    GetTracebackHeader(
                                        this,
                                        exception,
                                        tsh.Body
                                        ),
                                    new SourceSpan(tsh.Start, tsh.Header)
                                    ),
                                AstUtils.Empty()
                                )
                            );
                    }
                    else
                    {
                        //  translating:
                        //      except Test:
                        //          <body>
                        //  into:
                        //      if (CheckException(exception, Test) != null) {
                        //          traceback-header
                        //          <body>
                        //      }
                        ist = AstUtils.IfCondition(
                            Ast.NotEqual(
                                test,
                                AstUtils.Constant(null)
                                ),
                            GlobalParent.AddDebugInfo(
                                GetTracebackHeader(
                                    this,
                                    exception,
                                    tsh.Body
                                    ),
                                new SourceSpan(tsh.Start, tsh.Header)
                                )
                            );
                    }

                    // Add the test to the if statement test cascade
                    tests.Add(ist);
                }
                else
                {
                    Debug.Assert(index == _handlers.Length - 1);
                    Debug.Assert(catchAll == null);

                    //  translating:
                    //      except:
                    //          <body>
                    //  into:
                    //  {
                    //          traceback-header
                    //          <body>
                    //  }

                    catchAll = GlobalParent.AddDebugInfo(
                        GetTracebackHeader(this, exception, tsh.Body),
                        new SourceSpan(tsh.Start, tsh.Header)
                        );
                }
            }

            MSAst.Expression body = null;

            if (tests.Count > 0)
            {
                // rethrow the exception if we have no catch-all block
                if (catchAll == null)
                {
                    catchAll = Ast.Block(
                        Parent.GetSaveLineNumberExpression(exception, true),
                        Ast.Throw(
                            Ast.Call(
                                typeof(ExceptionHelpers).GetMethod("UpdateForRethrow"),
                                exception
                                )
                            )
                        );
                }

                body = AstUtils.If(
                    tests.ToArray(),
                    catchAll
                    );
            }
            else
            {
                Debug.Assert(catchAll != null);
                body = catchAll;
            }

            IList <MSAst.ParameterExpression> args;

            if (converted != null)
            {
                args = new ReadOnlyCollectionBuilder <MSAst.ParameterExpression> {
                    converted, extracted
                };
            }
            else
            {
                args = new ReadOnlyCollectionBuilder <MSAst.ParameterExpression> {
                    extracted
                };
            }

            // Codegen becomes:
            //     extracted = PythonOps.SetCurrentException(exception)
            //      < dynamic exception analysis >
            return(Ast.Block(
                       args,
                       Ast.Assign(
                           extracted,
                           Ast.Call(
                               AstMethods.SetCurrentException,
                               Parent.LocalContext,
                               exception
                               )
                           ),
                       body,
                       Ast.Assign(extracted, Ast.Constant(null)),
                       AstUtils.Empty()
                       ));
        }
コード例 #19
0
 public void PostWalk(TryStatementHandler node)
 {
     PostProcess(node);
 }
コード例 #20
0
ファイル: Parser.cs プロジェクト: bdoot/IronLanguages
        //except_clause: 'except' [expression [',' expression]]
        //2.6: except_clause: 'except' [expression [(',' or 'as') expression]]
        private TryStatementHandler ParseTryStmtHandler() {
            Eat(TokenKind.KeywordExcept);

            // If this function has an except block, then it can set the current exception.
            FunctionDefinition current = CurrentFunction;
            if (current != null) {
                current.CanSetSysExcInfo = true;
            }

            var start = GetStart();
            Expression test1 = null, test2 = null;
            if (PeekToken().Kind != TokenKind.Colon) {
                test1 = ParseExpression();
                if (MaybeEat(TokenKind.Comma) || MaybeEat(TokenKind.KeywordAs)) {
                    test2 = ParseExpression();
                }
            }
            var mid = GetEnd();
            Statement body = ParseSuite();
            TryStatementHandler ret = new TryStatementHandler(test1, test2, body);
            ret.HeaderIndex = mid;
            ret.SetLoc(_globalParent, start, body.EndIndex);
            return ret;
        }
コード例 #21
0
 public void Visit(PyAst.TryStatementHandler node) => throw CreateNotImplementedEx();