예제 #1
0
        private void ProcessStatement(Statement statement, ParserContext context)
        {
            context = new ParserContext(context.Nodes);

            if (statement is FunctionDeclaration)
            {
                ProcessFunctionDeclaration(statement.As<FunctionDeclaration>(), context);
                return;
            }

            if (statement is VariableDeclaration)
            {
                ProcessVariableDeclaration(statement.As<VariableDeclaration>(), context);
                return;
            }

            if (statement is ExpressionStatement)
            {
                ProcessExpression(statement.As<ExpressionStatement>().Expression, context);
                return;
            }

            if (statement is ReturnStatement)
            {
                ProcessExpression(statement.As<ReturnStatement>().Argument, context);
                return;
            }

            if (statement is BlockStatement)
            {
                ProcessStatements(statement.As<BlockStatement>().Body, context);
            }

            if (statement is IfStatement)
            {
                ProcessExpression(statement.As<IfStatement>().Test, context);
                ProcessStatement(statement.As<IfStatement>().Consequent, context);
                ProcessStatement(statement.As<IfStatement>().Alternate, context);
            }

            if (statement is TryStatement)
            {
                ProcessStatement(statement.As<TryStatement>().Block, context);
                ProcessStatement(statement.As<TryStatement>().Finalizer, context);
                ProcessStatements(statement.As<TryStatement>().Handlers.OfType<Statement>(), context);
                ProcessStatements(statement.As<TryStatement>().GuardedHandlers, context);
            }

            if (statement is CatchClause)
            {
                ProcessStatement(statement.As<CatchClause>().Body, context);
            }

            if (statement is SwitchStatement)
            {
                ProcessExpression(statement.As<SwitchStatement>().Discriminant, context);
                ProcessStatements(statement.As<SwitchStatement>().Cases.SelectMany(c => c.Consequent), context);
            }
        }
예제 #2
0
파일: Engine.cs 프로젝트: skipme/jint
        public Completion ExecuteStatement(Statement statement)
        {
            var maxStatements = Options.GetMaxStatements();
            if (maxStatements > 0 && _statementsCount++ > maxStatements)
            {
                throw new StatementsCountOverflowException();
            }

            if (_timeoutTicks > 0 && _timeoutTicks < DateTime.UtcNow.Ticks)
            {
                throw new TimeoutException();
            }

            _lastSyntaxNode = statement;
            
            if (Options.IsDebugMode())
            {
                DebugHandler.OnStep(statement);
            }

            switch (statement.Type)
            {
                case SyntaxNodes.BlockStatement:
                    return _statements.ExecuteBlockStatement(statement.As<BlockStatement>());
                    
                case SyntaxNodes.BreakStatement:
                    return _statements.ExecuteBreakStatement(statement.As<BreakStatement>());
                    
                case SyntaxNodes.ContinueStatement:
                    return _statements.ExecuteContinueStatement(statement.As<ContinueStatement>());
                    
                case SyntaxNodes.DoWhileStatement:
                    return _statements.ExecuteDoWhileStatement(statement.As<DoWhileStatement>());
                    
                case SyntaxNodes.DebuggerStatement:
                    return _statements.ExecuteDebuggerStatement(statement.As<DebuggerStatement>());
                    
                case SyntaxNodes.EmptyStatement:
                    return _statements.ExecuteEmptyStatement(statement.As<EmptyStatement>());
                    
                case SyntaxNodes.ExpressionStatement:
                    return _statements.ExecuteExpressionStatement(statement.As<ExpressionStatement>());

                case SyntaxNodes.ForStatement:
                    return _statements.ExecuteForStatement(statement.As<ForStatement>());
                    
                case SyntaxNodes.ForInStatement:
                    return _statements.ExecuteForInStatement(statement.As<ForInStatement>());

                case SyntaxNodes.FunctionDeclaration:
                    return new Completion(Completion.Normal, null, null);
                    
                case SyntaxNodes.IfStatement:
                    return _statements.ExecuteIfStatement(statement.As<IfStatement>());
                    
                case SyntaxNodes.LabeledStatement:
                    return _statements.ExecuteLabelledStatement(statement.As<LabelledStatement>());

                case SyntaxNodes.ReturnStatement:
                    return _statements.ExecuteReturnStatement(statement.As<ReturnStatement>());
                    
                case SyntaxNodes.SwitchStatement:
                    return _statements.ExecuteSwitchStatement(statement.As<SwitchStatement>());
                    
                case SyntaxNodes.ThrowStatement:
                    return _statements.ExecuteThrowStatement(statement.As<ThrowStatement>());

                case SyntaxNodes.TryStatement:
                    return _statements.ExecuteTryStatement(statement.As<TryStatement>());
                    
                case SyntaxNodes.VariableDeclaration:
                    return _statements.ExecuteVariableDeclaration(statement.As<VariableDeclaration>());
                    
                case SyntaxNodes.WhileStatement:
                    return _statements.ExecuteWhileStatement(statement.As<WhileStatement>());
                    
                case SyntaxNodes.WithStatement:
                    return _statements.ExecuteWithStatement(statement.As<WithStatement>());

                case SyntaxNodes.Program:
                    return _statements.ExecuteProgram(statement.As<Program>());
                    
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }
예제 #3
0
        private void VisitStatement(Statement statement, RequireCall parentCall, NodeWithChildren parentNode)
        {
            if (statement == null || this.ShouldSkipNode(statement))
            {
                return;
            }

            var currentNode = new NodeWithChildren { Node = statement };
            if (parentNode != null)
            {
                currentNode.Parent = parentNode;
                parentNode.Children.Add(currentNode);
            }
            else
            {
                visitedNodes.Add(currentNode);
            }

            switch (statement.Type)
            {
                case SyntaxNodes.BlockStatement:
                    var block = statement.As<BlockStatement>();
                    VisitNodeEnumerator(block.Body, parentCall, currentNode);
                    break;
                case SyntaxNodes.DoWhileStatement:
                    var doWhile = statement.As<DoWhileStatement>();
                    VisitStatement(doWhile.Body, parentCall, currentNode);
                    break;
                case SyntaxNodes.ExpressionStatement:
                    var expression = statement.As<ExpressionStatement>();
                    VisitExpression(expression.Expression, parentCall, currentNode);
                    break;
                case SyntaxNodes.ForInStatement:
                    var forIn = statement.As<ForInStatement>();
                    VisitStatement(forIn.Body, parentCall, currentNode);
                    break;
                case SyntaxNodes.ForStatement:
                    var forStatement = statement.As<ForStatement>();
                    VisitStatement(forStatement.Body, parentCall, currentNode);
                    break;
                case SyntaxNodes.FunctionDeclaration:
                    var funcDeclaration = statement.As<FunctionDeclaration>();
                    VisitStatement(funcDeclaration.Body, parentCall, currentNode);
                    break;
                case SyntaxNodes.IfStatement:
                    var ifStatement = statement.As<IfStatement>();
                    VisitStatement(ifStatement.Alternate, parentCall, currentNode);
                    VisitStatement(ifStatement.Consequent, parentCall, currentNode);
                    break;
                case SyntaxNodes.ReturnStatement:
                    var returnStatement = statement.As<ReturnStatement>();
                    VisitExpression(returnStatement.Argument, parentCall, currentNode);
                    break;
                case SyntaxNodes.TryStatement:
                    var tryStatement = statement.As<TryStatement>();
                    VisitStatement(tryStatement.Block, parentCall, currentNode);
                    break;
                case SyntaxNodes.CatchClause:
                    var catchClause = statement.As<CatchClause>();
                    VisitStatement(catchClause.Body, parentCall, currentNode);
                    break;
                case SyntaxNodes.VariableDeclaration:
                    var varDeclaration = statement.As<VariableDeclaration>();
                    VisitNodeEnumerator(varDeclaration.Declarations, parentCall, currentNode);
                    break;
                case SyntaxNodes.WhileStatement:
                    var whileStatement = statement.As<WhileStatement>();
                    VisitStatement(whileStatement.Body, parentCall, currentNode);
                    break;
                case SyntaxNodes.WithStatement:
                    var withStatement = statement.As<WithStatement>();
                    VisitStatement(withStatement.Body, parentCall, currentNode);
                    break;
                default:
                    break;
            }
        }