Beispiel #1
0
        public void ParserShouldCallStatementFactoryAndContextManagerWhenParsingTokens()
        {
            IList <TokenBase> input = new List <TokenBase>();

            input.Add(TokenCreator.Create <GenericToken>("source", null));
            input.Add(TokenCreator.Create <GenericToken>("code", null));

            IStatementFactory        factory        = Mocker.StrictMock <IStatementFactory>();
            IStatementContext        context        = Mocker.StrictMock <IStatementContext>();
            IStatementContextManager contextManager = Mocker.StrictMock <IStatementContextManager>();

            Expect.Call(factory.CanCreateStatementFrom(new List <TokenBase>(), context)).IgnoreArguments().Return(true).Repeat.Once();
            Expect.Call(factory.Create(input, context)).Return(new GenericStatement(input, context)).Repeat.Once();
            Expect.Call(() => contextManager.Encountered(new[] { new GenericStatement(input, context) }, input.Count, input)).Repeat.Once();
            Expect.Call(contextManager.GetContext(input)).Return(context).Repeat.Once();

            input.Add(TokenCreator.Create <SemiColonToken>());

            StubStatementFactoryProvider provider = new StubStatementFactoryProvider(factory);
            CalidusStatementParser       parser   = new CalidusStatementParser(provider, contextManager);

            Mocker.ReplayAll();

            parser.Parse(input);

            Mocker.VerifyAll();
        }
Beispiel #2
0
        /// <summary>
        /// Parses the list of tokens into statements
        /// </summary>
        /// <param name="tokens">The list of tokens</param>
        /// <returns>The list of statements</returns>
        public IEnumerable <StatementBase> Parse(IEnumerable <TokenBase> tokens)
        {
            IList <StatementBase> res = new List <StatementBase>();
            IList <TokenBase>     currentStatementTokens = new List <TokenBase>();

            for (int i = 0; i < tokens.Count(); i++)
            {
                TokenBase aToken = tokens.ElementAt(i);
                currentStatementTokens.Add(aToken);

                //when the token is a valid statement ender, add a statement and reset the list
                if (aToken is SemiColonToken ||
                    aToken is OpenCurlyBracketToken ||
                    aToken is CloseCurlyBracketToken ||
                    aToken is LineCommentToken ||
                    aToken is CloseSquareBracketToken ||
                    aToken is PreProcessorToken ||
                    NextIsOfType(i, tokens, typeof(OpenCurlyBracketToken)) ||
                    NextIsOfType(i, tokens, typeof(AssignmentToken))
                    )
                {
                    IList <StatementBase> createdStatements = new List <StatementBase>();
                    bool wasStatement = false;
                    //check all statement factories
                    IStatementContext context = _contextManager.GetContext(currentStatementTokens);
                    foreach (IStatementFactory statementFactory in _statementFactoryProvider.GetFactories())
                    {
                        if (statementFactory.CanCreateStatementFrom(currentStatementTokens, context))
                        {
                            createdStatements.Add(statementFactory.Create(new List <TokenBase>(currentStatementTokens), context));
                            wasStatement = true;
                        }
                    }

                    //check: if the current statements were not successfully parsed
                    //the list is not cleared, add as a generic statement
                    if (!wasStatement)
                    {
                        createdStatements.Add(new GenericStatement(new List <TokenBase>(currentStatementTokens), context));
                    }

                    currentStatementTokens.Clear();
                    //notify context manager
                    _contextManager.Encountered(createdStatements, i, tokens);

                    //add results
                    foreach (StatementBase aStatement in createdStatements)
                    {
                        res.Add(aStatement);
                    }
                }
            }

            //check: if the current statements were not successfully parsed
            //the list is not cleared, add as a generic statement
            if (currentStatementTokens.Count != 0)
            {
                res.Add(new GenericStatement(new List <TokenBase>(currentStatementTokens), _contextManager.GetContext(currentStatementTokens)));
                currentStatementTokens.Clear();
            }

            return(res);
        }