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(); }
protected override bool IsValidContext(IStatementContext context) { bool isType = context.Parents.FirstParentIsOfType <ClassStatement>(); bool isNext = context.IsNextToken <OpenCurlyBracketToken>() == false; return(isType && isNext); }
public override void SetUp() { base.SetUp(); _factory = new IndexerStatementFactory(); _context = Mocker.DynamicMock <IStatementContext>(); }
public virtual void SetUp() { _mocker = new MockRepository(); IStatementContext context = _mocker.DynamicMock <IStatementContext>(); _tokenCreator = new TokenCreator(); _statementCreator = new StatementCreator(_tokenCreator, context); }
public void StatementBaseInheritorsShouldBeEqualWhenNotAddingAdditionalProperties() { IStatementContext context = Mocker.DynamicMock <IStatementContext>(); StatementBaseImpl alpha = new StatementBaseImpl(context); StatementBaseImpl bravo = new StatementBaseImpl(context); Assert.AreEqual(alpha, bravo); }
/// <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"); } }
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>()); }
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>()); }
/// <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);
/// <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)); }
protected override StubStatement BuildStatement(IEnumerable <TokenBase> input, IStatementContext context) { throw new NotImplementedException(); }
protected override IndexerStatement BuildStatement(IEnumerable <TokenBase> input, IStatementContext context) { return(new IndexerStatement(input, context)); }
protected override RegionEndStatement BuildStatement(IEnumerable <TokenBase> input, IStatementContext context) { return(new RegionEndStatement(input, context)); }
protected override NameSpaceStatement BuildStatement(IEnumerable <TokenBase> input, IStatementContext context) { return(new NameSpaceStatement(input, context)); }
/// <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; }
/// <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) { }
/// <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) { }
/// <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); }
/// <summary> /// Checks if the statement context is valid for the expression /// </summary> /// <param name="context">The context</param> protected abstract bool IsValidContext(IStatementContext context);
/// <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) { }
/// <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) { }
protected override CloseBlockStatement BuildStatement(IEnumerable <TokenBase> input, IStatementContext context) { return(new CloseBlockStatement(input, context)); }
/// <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; }
/// <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) { }
protected override bool IsValidContext(IStatementContext context) { return(context.Parents.FirstParentIsOfType <ClassStatement>()); }
protected override AttributeStatement BuildStatement(IEnumerable <TokenBase> input, IStatementContext context) { return(new AttributeStatement(input, context)); }
/// <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) { }
public StatementBase Create(IEnumerable <TokenBase> tokenList, IStatementContext context) { return(new GenericStatement(tokenList, context)); }
protected override bool IsValidContext(IStatementContext context) { return(true); }
public bool CanCreateStatementFrom(IEnumerable <TokenBase> tokenList, IStatementContext context) { return(true); }