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)); }
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); }
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); }
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); }
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)); }
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)); }