Esempio n. 1
0
            internal sealed override InternalResult <TToken> Parse(ref ParseState <TToken> state)
            {
                var x = state.Peek();

                if (!x.HasValue)
                {
                    state.Error = new ParseError <TToken>(
                        x,
                        true,
                        Expected,
                        state.SourcePos,
                        null
                        );
                    return(InternalResult.Failure <TToken>(false));
                }
                var val = x.GetValueOrDefault();

                if (!_predicate(val))
                {
                    state.Error = new ParseError <TToken>(
                        x,
                        false,
                        Expected,
                        state.SourcePos,
                        null
                        );
                    return(InternalResult.Failure <TToken>(false));
                }
                state.Advance();
                return(InternalResult.Success <TToken>(val, true));
            }
Esempio n. 2
0
            internal sealed override InternalResult <TEnumerable> Parse(ref ParseState <TToken> state)
            {
                var span = state.Peek(_valueTokens.Length);  // span.Length <= _valueTokens.Length

                var errorPos = -1;

                for (var i = 0; i < span.Length; i++)
                {
                    if (!EqualityComparer <TToken> .Default.Equals(span[i], _valueTokens[i]))
                    {
                        errorPos = i;
                        break;
                    }
                }

                if (errorPos != -1)
                {
                    // strings didn't match
                    state.Advance(errorPos);
                    state.Error = new InternalError <TToken>(
                        Maybe.Just(span[errorPos]),
                        false,
                        state.SourcePos,
                        null
                        );
                    state.AddExpected(new Expected <TToken>(_valueTokens));
                    return(InternalResult.Failure <TEnumerable>(errorPos > 0));
                }

                if (span.Length < _valueTokens.Length)
                {
                    // strings matched but reached EOF
                    state.Advance(span.Length);
                    state.Error = new InternalError <TToken>(
                        Maybe.Nothing <TToken>(),
                        true,
                        state.SourcePos,
                        null
                        );
                    state.AddExpected(new Expected <TToken>(_valueTokens));
                    return(InternalResult.Failure <TEnumerable>(span.Length > 0));
                }

                // OK
                state.Advance(_valueTokens.Length);
                return(InternalResult.Success <TEnumerable>(_value, _valueTokens.Length > 0));
            }
Esempio n. 3
0
            internal sealed override InternalResult <Unit> Parse(ref ParseState <TToken> state)
            {
                var result = state.Peek();

                if (result.HasValue)
                {
                    state.Error = new ParseError <TToken>(
                        result,
                        false,
                        Expected,
                        state.SourcePos,
                        null
                        );
                    return(InternalResult.Failure <Unit>(false));
                }
                return(InternalResult.Success(Unit.Value, false));
            }
Esempio n. 4
0
            internal sealed override InternalResult <string> Parse(ref ParseState <char> state)
            {
                var consumedInput = false;

                var builder = new PooledStringBuilder(_value.Length);

                foreach (var c in _value)
                {
                    var result = state.Peek();
                    if (!result.HasValue)
                    {
                        state.Error = new ParseError <char>(
                            result,
                            true,
                            Expected,
                            state.SourcePos,
                            null
                            );
                        return(InternalResult.Failure <string>(consumedInput));
                    }

                    var token = result.GetValueOrDefault();
                    if (char.ToLowerInvariant(token) != c)
                    {
                        state.Error = new ParseError <char>(
                            result,
                            false,
                            Expected,
                            state.SourcePos,
                            null
                            );
                        return(InternalResult.Failure <string>(consumedInput));
                    }

                    consumedInput = true;
                    builder.Append(token);
                    state.Advance();
                }
                return(InternalResult.Success <string>(builder.GetStringAndClear(), consumedInput));
            }
Esempio n. 5
0
            internal sealed override InternalResult <Unit> Parse(ref ParseState <TToken> state)
            {
                var startingPosition = state.SourcePos;
                var token            = state.Peek();
                var result           = _parser.Parse(ref state);

                if (result.Success)
                {
                    state.Error = new ParseError <TToken>(
                        token,
                        false,
                        null,
                        startingPosition,
                        null
                        );
                    return(InternalResult.Failure <Unit>(result.ConsumedInput));
                }

                return(InternalResult.Success(
                           Unit.Value,
                           result.ConsumedInput
                           ));
            }
Esempio n. 6
0
            internal sealed override InternalResult <TEnumerable> Parse(ref ParseState <TToken> state)
            {
                var consumedInput = false;

                foreach (var x in _valueTokens)
                {
                    var result = state.Peek();
                    if (!result.HasValue)
                    {
                        state.Error = new ParseError <TToken>(
                            result,
                            true,
                            Expected,
                            state.SourcePos,
                            null
                            );
                        return(InternalResult.Failure <TEnumerable>(consumedInput));
                    }

                    TToken token = result.GetValueOrDefault();
                    if (!EqualityComparer <TToken> .Default.Equals(token, x))
                    {
                        state.Error = new ParseError <TToken>(
                            result,
                            false,
                            Expected,
                            state.SourcePos,
                            null
                            );
                        return(InternalResult.Failure <TEnumerable>(consumedInput));
                    }

                    consumedInput = true;
                    state.Advance();
                }
                return(InternalResult.Success <TEnumerable>(_value, consumedInput));
            }