public override LuaSyntaxNode VisitTryStatement(TryStatementSyntax node)
        {
            LuaInvocationExpressionSyntax tryInvocationExpression = new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.Try);

            LuaTryAdapterExpressionSyntax tryBlockFunctionExpress = new LuaTryAdapterExpressionSyntax();

            PushFunction(tryBlockFunctionExpress);
            var block = (LuaBlockSyntax)node.Block.Accept(this);

            PopFunction();
            tryBlockFunctionExpress.AddStatements(block.Statements);
            tryInvocationExpression.AddArgument(tryBlockFunctionExpress);

            if (node.Catches.Count > 0)
            {
                var catchesExpress = VisitTryCatchesExpress(node.Catches);
                tryInvocationExpression.AddArgument(catchesExpress);
            }
            else
            {
                tryInvocationExpression.AddArgument(LuaIdentifierNameSyntax.Nil);
            }

            if (node.Finally != null)
            {
                var finallyfunctionExpress = (LuaFunctionExpressionSyntax)node.Finally.Accept(this);
                tryInvocationExpression.AddArgument(finallyfunctionExpress);
            }

            return(BuildCheckReturnInvocationExpression(tryInvocationExpression, node));
        }
        private LuaTryAdapterExpressionSyntax VisitTryCatchesExpress(SyntaxList <CatchClauseSyntax> catches)
        {
            LuaTryAdapterExpressionSyntax functionExpress = new LuaTryAdapterExpressionSyntax();

            PushFunction(functionExpress);
            var temp = GetTempIdentifier(catches.First());

            functionExpress.CatchTemp = temp;
            functionExpress.AddParameter(temp);

            LuaIfStatementSyntax ifStatement = null;
            bool hasCatchRoot = false;

            foreach (var catchNode in catches)
            {
                bool isRootExceptionDeclaration = false;
                LuaExpressionSyntax ifCondition = null;
                if (catchNode.Filter != null)
                {
                    ifCondition = (LuaExpressionSyntax)catchNode.Filter.Accept(this);
                }
                if (catchNode.Declaration != null)
                {
                    var typeName = (LuaIdentifierNameSyntax)catchNode.Declaration.Type.Accept(this);
                    if (typeName.ValueText != "System.Exception")
                    {
                        var mathcTypeInvocation = new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.Is, temp, typeName);
                        if (ifCondition != null)
                        {
                            ifCondition = new LuaBinaryExpressionSyntax(ifCondition, LuaSyntaxNode.Tokens.And, mathcTypeInvocation);
                        }
                        else
                        {
                            ifCondition = mathcTypeInvocation;
                        }
                    }
                    else
                    {
                        if (!catchNode.Declaration.Identifier.IsKind(SyntaxKind.None))
                        {
                            isRootExceptionDeclaration = true;
                        }
                        hasCatchRoot = true;
                    }
                }
                else
                {
                    hasCatchRoot = true;
                }

                var block = (LuaBlockSyntax)catchNode.Block.Accept(this);
                if (ifCondition != null)
                {
                    LuaBlockSyntax body;
                    if (ifStatement == null)
                    {
                        ifStatement = new LuaIfStatementSyntax(ifCondition);
                        body        = ifStatement.Body;
                    }
                    else
                    {
                        LuaElseIfStatementSyntax elseIfStatement = new LuaElseIfStatementSyntax(ifCondition);
                        body = elseIfStatement.Body;
                        ifStatement.ElseIfStatements.Add(elseIfStatement);
                    }
                    if (catchNode.Declaration != null && !catchNode.Declaration.Identifier.IsKind(SyntaxKind.None))
                    {
                        var variableDeclarator = (LuaVariableDeclaratorSyntax)catchNode.Declaration.Accept(this);
                        variableDeclarator.Initializer = new LuaEqualsValueClauseSyntax(temp);
                        body.Statements.Add(new LuaLocalVariableDeclaratorSyntax(variableDeclarator));
                    }
                    body.Statements.AddRange(block.Statements);
                }
                else
                {
                    if (isRootExceptionDeclaration)
                    {
                        var variableDeclarator = (LuaVariableDeclaratorSyntax)catchNode.Declaration.Accept(this);
                        variableDeclarator.Initializer = new LuaEqualsValueClauseSyntax(temp);
                        block.Statements.Insert(0, new LuaLocalVariableDeclaratorSyntax(variableDeclarator));
                    }

                    if (ifStatement != null)
                    {
                        LuaElseClauseSyntax elseClause = new LuaElseClauseSyntax();
                        elseClause.Body.Statements.AddRange(block.Statements);
                        ifStatement.Else = elseClause;
                    }
                    else
                    {
                        functionExpress.AddStatements(block.Statements);
                    }
                    break;
                }
            }

            if (ifStatement != null)
            {
                if (!hasCatchRoot)
                {
                    Contract.Assert(ifStatement.Else == null);
                    LuaMultipleReturnStatementSyntax rethrowStatement = new LuaMultipleReturnStatementSyntax();
                    rethrowStatement.Expressions.Add(LuaIdentifierNameSyntax.One);
                    rethrowStatement.Expressions.Add(temp);
                    LuaBlockSyntax block = new LuaBlockSyntax();
                    block.Statements.Add(rethrowStatement);
                    LuaElseClauseSyntax elseClause = new LuaElseClauseSyntax();
                    elseClause.Body.Statements.AddRange(block.Statements);
                    ifStatement.Else = elseClause;
                }
                functionExpress.AddStatement(ifStatement);
            }

            PopFunction();
            return(functionExpress);
        }