Пример #1
0
        private Either <IParseState> _CreateExpression(IParseState OldState, IParseState NewState)
        {
            var t_Attempt = m_CreateExpression
                            .Select(F => _AttemptCreate(F, OldState, NewState))
                            .FirstOrDefault(E => !E.HasError());

            return(t_Attempt == null ?
                   new Exception("Unable to match any expression creation function") :
                   t_Attempt.HasError() ?
                   t_Attempt.Error :
                   new Either <IParseState>(
                       NewState.ToCopy(NewState.Tokens, OldState.Expressions.Append(t_Attempt.Value))
                       ));
        }
Пример #2
0
        protected virtual Either<IParseState> _PopAndMatch(Stack<Token> PatternTokens, IParseState State)
        {
            var t_CurrentTokens = State.Tokens;
            var t_MatchedTokens = new List<Token>();
            while (PatternTokens.Count != 0)
            {
                var t_PatternToken = PatternTokens.Pop();
                var t_Token = t_CurrentTokens.Pop();

                if (!_CompareTokens(t_PatternToken, t_Token))
                    return new Exception("Pattern Tokens do not match input Tokens.",
                        new Exception($"Expected: {t_PatternToken.Type} {t_PatternToken.Text} Found: {t_Token.Type} {t_Token.Text}"));

                t_MatchedTokens.Add(t_Token);
            }

            var t_CreatedExpression = _CreateExpression(t_MatchedTokens);

            return t_CreatedExpression.HasError() ?
                new Either<IParseState>(new Exception("No Tokens Matched")) :
                new Either<IParseState>(State.ToCopy(t_CurrentTokens, State.Expressions.Append(t_CreatedExpression.Value)));
        }