Esempio n. 1
0
        internal sealed override bool TryParse(ref ParseState <TToken> state, ref ExpectedCollector <TToken> expecteds, out TEnumerable result)
        {
            var span = state.LookAhead(_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.Location,
                    null
                    );
                expecteds.Add(new Expected <TToken>(_valueTokens));
                result = default;
                return(false);
            }

            if (span.Length < _valueTokens.Length)
            {
                // strings matched but reached EOF
                state.Advance(span.Length);
                state.Error = new InternalError <TToken>(
                    Maybe.Nothing <TToken>(),
                    true,
                    state.Location,
                    null
                    );
                expecteds.Add(new Expected <TToken>(_valueTokens));
                result = default;
                return(false);
            }

            // OK
            state.Advance(_valueTokens.Length);
            result = _value;
            return(true);
        }
Esempio n. 2
0
        internal sealed override bool TryParse(ref ParseState <char> state, ref ExpectedCollector <char> expecteds, out string result)
        {
            var span = state.LookAhead(_value.Length);  // span.Length <= _valueTokens.Length

            var errorPos = -1;

            for (var i = 0; i < span.Length; i++)
            {
                if (!char.ToLowerInvariant(span[i]).Equals(char.ToLowerInvariant(_value[i])))
                {
                    errorPos = i;
                    break;
                }
            }

            if (errorPos != -1)
            {
                // strings didn't match
                state.Advance(errorPos);
                state.Error = new InternalError <char>(
                    Maybe.Just(span[errorPos]),
                    false,
                    state.Location,
                    null
                    );
                expecteds.Add(Expected);
                result = null;
                return(false);
            }

            if (span.Length < _value.Length)
            {
                // strings matched but reached EOF
                state.Advance(span.Length);
                state.Error = new InternalError <char>(
                    Maybe.Nothing <char>(),
                    true,
                    state.Location,
                    null
                    );
                expecteds.Add(Expected);
                result = null;
                return(false);
            }

            // OK
            state.Advance(_value.Length);
            result = span.ToString();
            return(true);
        }
Esempio n. 3
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. 4
0
            internal sealed override InternalResult <TToken> Parse(ref ParseState <TToken> state)
            {
                if (!state.HasCurrent)
                {
                    state.Error = new InternalError <TToken>(
                        Maybe.Nothing <TToken>(),
                        true,
                        state.SourcePos,
                        null
                        );
                    return(InternalResult.Failure <TToken>(false));
                }
                var token = state.Current;

                if (!_predicate(token))
                {
                    state.Error = new InternalError <TToken>(
                        Maybe.Just(token),
                        false,
                        state.SourcePos,
                        null
                        );
                    return(InternalResult.Failure <TToken>(false));
                }
                state.Advance();
                return(InternalResult.Success <TToken>(token, true));
            }
Esempio n. 5
0
            internal sealed override InternalResult <TToken> Parse(ref ParseState <TToken> state)
            {
                if (!state.HasCurrent)
                {
                    state.Error = new InternalError <TToken>(
                        Maybe.Nothing <TToken>(),
                        true,
                        state.SourcePos,
                        null
                        );
                    state.AddExpected(Expected);
                    return(InternalResult.Failure <TToken>(false));
                }
                var token = state.Current;

                if (!EqualityComparer <TToken> .Default.Equals(token, _token))
                {
                    state.Error = new InternalError <TToken>(
                        Maybe.Just(token),
                        false,
                        state.SourcePos,
                        null
                        );
                    state.AddExpected(Expected);
                    return(InternalResult.Failure <TToken>(false));
                }
                state.Advance();
                return(InternalResult.Success <TToken>(token, true));
            }
Esempio n. 6
0
        internal sealed override bool TryParse(ref ParseState <TToken> state, ref ExpectedCollector <TToken> expecteds, out TToken result)
        {
            if (!state.HasCurrent)
            {
                state.Error = new InternalError <TToken>(
                    Maybe.Nothing <TToken>(),
                    true,
                    state.Location,
                    null
                    );
                result = default;
                return(false);
            }
            var token = state.Current;

            if (!_predicate(token))
            {
                state.Error = new InternalError <TToken>(
                    Maybe.Just(token),
                    false,
                    state.Location,
                    null
                    );
                result = default;
                return(false);
            }
            state.Advance();
            result = token;
            return(true);
        }
Esempio n. 7
0
            internal sealed override InternalResult <string> Parse(ref ParseState <char> state)
            {
                var span = state.LookAhead(_value.Length);  // span.Length <= _valueTokens.Length

                var errorPos = -1;

                for (var i = 0; i < span.Length; i++)
                {
                    if (!char.ToLowerInvariant(span[i]).Equals(char.ToLowerInvariant(_value[i])))
                    {
                        errorPos = i;
                        break;
                    }
                }

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

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

                // OK
                state.Advance(_value.Length);
                return(InternalResult.Success <string>(span.ToString(), _value.Length > 0));
            }
Esempio n. 8
0
            internal sealed override InternalResult <TEnumerable> Parse(ref ParseState <TToken> state)
            {
                var span = state.LookAhead(_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.Location,
                        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.Location,
                        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. 9
0
        internal override InternalResult <Unit> Parse(ref ParseState <char> state)
        {
            var startingLoc = state.Location;
            var chunk       = state.LookAhead(32);

            while (chunk.Length > 0)
            {
                for (var i = 0; i < chunk.Length; i++)
                {
                    if (!char.IsWhiteSpace(chunk[i]))
                    {
                        state.Advance(i);
                        return(InternalResult.Success(Unit.Value, state.Location > startingLoc));
                    }
                }
                state.Advance(chunk.Length);
                chunk = state.LookAhead(32);
            }
            return(InternalResult.Success(Unit.Value, state.Location > startingLoc));
        }
Esempio n. 10
0
        internal override bool TryParse(ref ParseState <char> state, ref ExpectedCollector <char> expecteds, out Unit result)
        {
            result = Unit.Value;
            var chunk = state.LookAhead(32);

            while (chunk.Length > 0)
            {
                for (var i = 0; i < chunk.Length; i++)
                {
                    if (!char.IsWhiteSpace(chunk[i]))
                    {
                        state.Advance(i);
                        return(true);
                    }
                }
                state.Advance(chunk.Length);
                chunk = state.LookAhead(32);
            }
            return(true);
        }
Esempio n. 11
0
        private static Result <TToken, T> DoParse <TToken, T>(Parser <TToken, T> parser, ITokenStream <TToken> stream, Func <TToken, SourcePos, SourcePos> calculatePos)
        {
            var state = new ParseState <TToken>(calculatePos, stream);

            state.Advance();  // pull the first element from the input

            var result = parser.Parse(ref state);

            state.Dispose();  // ensure we return the state's buffer to the buffer pool

            return(result.Success
                ? new Result <TToken, T>(result.ConsumedInput, result.Value)
                : new Result <TToken, T>(result.ConsumedInput, state.Error));
        }
Esempio n. 12
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. 13
0
            internal sealed override InternalResult <string> Parse(ref ParseState <char> state)
            {
                var consumedInput = false;

                var builder = new InplaceStringBuilder(_value.Length);

                foreach (var c in _value)
                {
                    if (!state.HasCurrent)
                    {
                        state.Error = new InternalError <char>(
                            Maybe.Nothing <char>(),
                            true,
                            state.SourcePos,
                            null
                            );
                        state.AddExpected(Expected);
                        return(InternalResult.Failure <string>(consumedInput));
                    }

                    var token = state.Current;
                    if (char.ToLowerInvariant(token) != char.ToLowerInvariant(c))
                    {
                        state.Error = new InternalError <char>(
                            Maybe.Just(token),
                            false,
                            state.SourcePos,
                            null
                            );
                        state.AddExpected(Expected);
                        return(InternalResult.Failure <string>(consumedInput));
                    }

                    consumedInput = true;
                    builder.Append(token);
                    state.Advance();
                }
                return(InternalResult.Success <string>(builder.ToString(), consumedInput));
            }
Esempio n. 14
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));
            }