예제 #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();
        }
예제 #2
0
        protected override bool IsValidContext(IStatementContext context)
        {
            bool isType = context.Parents.FirstParentIsOfType <ClassStatement>();
            bool isNext = context.IsNextToken <OpenCurlyBracketToken>() == false;

            return(isType && isNext);
        }
예제 #3
0
        public override void SetUp()
        {
            base.SetUp();

            _factory = new IndexerStatementFactory();
            _context = Mocker.DynamicMock <IStatementContext>();
        }
예제 #4
0
        public virtual void SetUp()
        {
            _mocker = new MockRepository();

            IStatementContext context = _mocker.DynamicMock <IStatementContext>();

            _tokenCreator     = new TokenCreator();
            _statementCreator = new StatementCreator(_tokenCreator, context);
        }
예제 #5
0
        public void StatementBaseInheritorsShouldBeEqualWhenNotAddingAdditionalProperties()
        {
            IStatementContext context = Mocker.DynamicMock <IStatementContext>();

            StatementBaseImpl alpha = new StatementBaseImpl(context);
            StatementBaseImpl bravo = new StatementBaseImpl(context);

            Assert.AreEqual(alpha, bravo);
        }
예제 #6
0
 /// <summary>
 /// Resolves the token list as a statement and returns the statement
 /// </summary>
 /// <param name="input">The tokens to parse</param>
 /// <param name="context">The context for the stamement</param>
 /// <returns>The statement</returns>
 public StatementBase Create(IEnumerable <TokenBase> input, IStatementContext context)
 {
     if (CanCreateStatementFrom(input, context))
     {
         return(BuildStatement(input, context));
     }
     else
     {
         throw new CalidusException("The factory cannot parse the token list into a statement");
     }
 }
예제 #7
0
        public void StatementBaseFindFirstOccurenceOfShouldReturnNullIfNoOccurenceFound()
        {
            IStatementContext context = Mocker.DynamicMock <IStatementContext>();

            IList <TokenBase> input = new List <TokenBase>();

            input.Add(TokenCreator.Create <PublicModifierToken>());
            input.Add(TokenCreator.Create <SpaceToken>());
            input.Add(TokenCreator.Create <ClassToken>());
            input.Add(TokenCreator.Create <SpaceToken>());
            input.Add(TokenCreator.Create <IdentifierToken>("Test"));

            StatementBaseImpl imp = new StatementBaseImpl(input, context);

            Assert.IsNull(imp.OccurenceOf <TabToken>());
        }
예제 #8
0
        public void StatementBaseFindFirstOccurenceOfShouldReturnFirstOccurenceOfTokenType()
        {
            IStatementContext context = Mocker.DynamicMock <IStatementContext>();

            IList <TokenBase> input = new List <TokenBase>();

            input.Add(TokenCreator.Create <PublicModifierToken>());
            input.Add(TokenCreator.Create <SpaceToken>());
            TokenBase expected = TokenCreator.Create <ClassToken>();

            input.Add(expected);
            input.Add(TokenCreator.Create <SpaceToken>());
            input.Add(TokenCreator.Create <IdentifierToken>("Test"));

            StatementBaseImpl imp = new StatementBaseImpl(input, context);

            Assert.AreEqual(expected, imp.OccurenceOf <ClassToken>());
        }
예제 #9
0
 /// <summary>
 /// Builds the appropriate statement for the token list
 /// </summary>
 /// <param name="input">The token list</param>
 /// <param name="context">The statement context</param>
 /// <returns>The statement</returns>
 protected abstract TStatementType BuildStatement(IEnumerable <TokenBase> input, IStatementContext context);
예제 #10
0
 /// <summary>
 /// Checks to see if the current factory is able to create a statement from the token list
 /// </summary>
 /// <param name="tokenList">The tokens to try to parse as a statement</param>
 /// <param name="context">The statement context</param>
 /// <returns>True if able to parse, otherwise false</returns>
 public bool CanCreateStatementFrom(IEnumerable <TokenBase> tokenList, IStatementContext context)
 {
     return(IsValidContext(context) && Expression.Matches(tokenList));
 }
예제 #11
0
 protected override StubStatement BuildStatement(IEnumerable <TokenBase> input, IStatementContext context)
 {
     throw new NotImplementedException();
 }
예제 #12
0
 protected override IndexerStatement BuildStatement(IEnumerable <TokenBase> input, IStatementContext context)
 {
     return(new IndexerStatement(input, context));
 }
예제 #13
0
 protected override RegionEndStatement BuildStatement(IEnumerable <TokenBase> input, IStatementContext context)
 {
     return(new RegionEndStatement(input, context));
 }
예제 #14
0
 protected override NameSpaceStatement BuildStatement(IEnumerable <TokenBase> input, IStatementContext context)
 {
     return(new NameSpaceStatement(input, context));
 }
예제 #15
0
 /// <summary>
 /// Create a new instance of this class
 /// </summary>
 /// <param name="tokens">The list of tokens in the statement</param>
 /// <param name="context">The context the statement is in</param>
 protected StatementBase(IEnumerable <TokenBase> tokens, IStatementContext context)
 {
     Tokens  = tokens;
     Context = context;
 }
예제 #16
0
 /// <summary>
 /// Create a new instance of this class
 /// </summary>
 /// <param name="tokens">The list of tokens in the statement</param>
 /// <param name="context">The context</param>
 public RegionEndStatement(IEnumerable <TokenBase> tokens, IStatementContext context)
     : base(tokens, context)
 {
 }
예제 #17
0
 /// <summary>
 /// Create a new instance of this class
 /// </summary>
 /// <param name="tokens">The tokens in this statement</param>
 /// <param name="context">The context</param>
 public GenericStatement(IEnumerable <TokenBase> tokens, IStatementContext context)
     : base(tokens, context)
 {
 }
예제 #18
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);
        }
예제 #19
0
 /// <summary>
 /// Checks if the statement context is valid for the expression
 /// </summary>
 /// <param name="context">The context</param>
 protected abstract bool IsValidContext(IStatementContext context);
예제 #20
0
 /// <summary>
 /// Create a new instance of this class
 /// </summary>
 /// <param name="tokens">The list of tokens in the statement</param>
 /// <param name="context">The statement context</param>
 public MethodStatement(IEnumerable <TokenBase> tokens, IStatementContext context)
     : base(tokens, context)
 {
 }
예제 #21
0
 /// <summary>
 /// Create a new instance of this class
 /// </summary>
 /// <param name="tokens">The list of tokens in the statement</param>
 /// <param name="context">The statement context</param>
 public InterfaceStatement(IEnumerable <TokenBase> tokens, IStatementContext context)
     : base(tokens, context)
 {
 }
예제 #22
0
 protected override CloseBlockStatement BuildStatement(IEnumerable <TokenBase> input, IStatementContext context)
 {
     return(new CloseBlockStatement(input, context));
 }
예제 #23
0
 /// <summary>
 /// Create a new instance of this object
 /// </summary>
 /// <param name="tokenCreator">The token creator to use</param>
 /// <param name="context">The statement context to pass</param>
 public StatementCreator(TokenCreator tokenCreator, IStatementContext context)
 {
     TokenCreator = tokenCreator;
     Context      = context;
 }
예제 #24
0
 /// <summary>
 /// Create a new instance of this class
 /// </summary>
 /// <param name="tokens">The list of tokens in the statement</param>
 /// <param name="context">The context</param>
 protected AccessModifierStatement(IEnumerable <TokenBase> tokens, IStatementContext context)
     : base(tokens, context)
 {
 }
예제 #25
0
 protected override bool IsValidContext(IStatementContext context)
 {
     return(context.Parents.FirstParentIsOfType <ClassStatement>());
 }
예제 #26
0
 protected override AttributeStatement BuildStatement(IEnumerable <TokenBase> input, IStatementContext context)
 {
     return(new AttributeStatement(input, context));
 }
예제 #27
0
 /// <summary>
 /// Create a new instance of this class
 /// </summary>
 /// <param name="tokens">The list of tokens in the statement</param>
 /// <param name="context">The context</param>
 public OpenBlockStatement(IEnumerable <TokenBase> tokens, IStatementContext context)
     : base(tokens, context)
 {
 }
예제 #28
0
 public StatementBase Create(IEnumerable <TokenBase> tokenList, IStatementContext context)
 {
     return(new GenericStatement(tokenList, context));
 }
예제 #29
0
 protected override bool IsValidContext(IStatementContext context)
 {
     return(true);
 }
예제 #30
0
 public bool CanCreateStatementFrom(IEnumerable <TokenBase> tokenList, IStatementContext context)
 {
     return(true);
 }