Пример #1
0
        internal Variable ProcessTry(ParsingScript script)
        {
            int       startTryCondition = script.Pointer - 1;
            int       currentStackLevel = ParserFunction.GetCurrentStackLevel();
            Exception exception         = null;

            Variable result = null;

            try {
                result = ProcessBlock(script);
            } catch (Exception exc) {
                exception = exc;
            }

            if (exception != null || result.IsReturn ||
                result.Type == Variable.VarType.BREAK ||
                result.Type == Variable.VarType.CONTINUE)
            {
                // We are here from the middle of the try-block either because
                // an exception was thrown or because of a Break/Continue. Skip it.
                script.Pointer = startTryCondition;
                SkipBlock(script);
            }

            string catchToken = Utils.GetNextToken(script);

            script.Forward(); // skip opening parenthesis
                              // The next token after the try block must be a catch.
            if (!Constants.CATCH_LIST.Contains(catchToken))
            {
                throw new ArgumentException("Expecting a 'catch()' but got [" +
                                            catchToken + "]");
            }

            string exceptionName = Utils.GetNextToken(script);

            script.Forward(); // skip closing parenthesis

            if (exception != null)
            {
                string excStack = CreateExceptionStack(exceptionName,currentStackLevel);
                ParserFunction.InvalidateStacksAfterLevel(currentStackLevel);

                GetVarFunction excMsgFunc = new GetVarFunction(new Variable(exception.Message));
                ParserFunction.AddGlobalOrLocalVariable(exceptionName,excMsgFunc);
                GetVarFunction excStackFunc = new GetVarFunction(new Variable(excStack));
                ParserFunction.AddGlobalOrLocalVariable(exceptionName + ".Stack",excStackFunc);

                result = ProcessBlock(script);
                ParserFunction.PopLocalVariable(exceptionName);
            }
            else
            {
                SkipBlock(script);
            }

            SkipRestBlocks(script);
            return(result);
        }