public IEither <T, ParseError> Parse(IInputReader input) { if (input.EndOfStream) { return(ParseResult.Success(default(T))); } return(ParseResult.Error <T>(input, "Expected end of input")); }
public static ParseResult <string, string> Parse(string insertQuery) { if (string.IsNullOrWhiteSpace(insertQuery)) { return(ParseResult.Error <string, string>("Insert query is empty")); } return(insertQuery.Aggregate(CreateInsertQueryParserState(), (x, character) => { x.Process(character); return x; }) .GetResult()); }
public IEither <T, ParseError> Parse(IInputReader input) { var result = this.parser.Parse(input); if (result.IsSuccess) { return(result); } return(ParseResult.Error <T>(input, msgFunc, result.FromError())); }
public IEither <TResult, ParseError> Parse(IInputReader input) { var result = this.parser.Parse(input); if (result.IsError) { return(ParseResult.Error <TResult>(result.FromError())); } return(ParseResult.Success(this.func(result.FromSuccess()))); }
public IEither <char, ParseError> Parse(IInputReader input) { if (input.EndOfStream) { return(ParseResult.Error <char>(input, "Unexpected end of input")); } char c = input.Read(); return(ParseResult.Success(c)); }
public void CantConvertParseResultBackToInsertIntoStatementWhenResultIsNotSuccesful() { // Arrange var parseResult = ParseResult.Error <string, string>("Some error"); // Act var actual = Parsers.InsertQueryParser.InsertQueryParser.ToInsertIntoString(parseResult, "Tabel"); // Assert actual.Should().Be("Error: Parse result was not successful. Error messages: Some error"); }
/// <summary> /// Constructs a parser, that takes a single element from the input stream. /// </summary> /// <typeparam name="TItem">The item type of the stream.</typeparam> /// <returns>A new parser, that takes a single element from the input stream, if there is any.</returns> public static Parser <TItem, TItem> Item <TItem>() => (stream, offset) => { if (stream.TryLookAhead(offset, out var item)) { return(ParseResult.Ok(item, offset + 1, null)); } // TODO: Better error info? else { return(ParseResult.Error("item", null, offset, string.Empty)); } };
protected override bool OnParse(ParseResult result, bool errorPass) { var next = result.Peek(_inner, errorPass); var success = next.Success; if (success) { result.Apply(next, errorPass); } else { result.Error(next, errorPass, false); } return(success); }
public IEither <string, ParseError> Parse(IInputReader input) { for (int i = 0; i < str.Length; i++) { this.predicateParser.Predicate = str[i].Equals; var result = this.parser.Parse(input); if (result.IsError) { return(ParseResult.Error <string>(result.FromError())); } } return(ParseResult.Success(this.str)); }
public static ParseResult <string, object> Parse(string formatString, IEnumerable <object> args) { if (string.IsNullOrWhiteSpace(formatString)) { return(ParseResult.Error <string, object>("Format string is empty")); } var state = new StringFormatParserState(args); foreach (var character in formatString) { if (character == '{') { state.BeginPlaceholder(); if (state.OpenBracketCount > 1) { return(ParseResult.Error <string, object>("Too many opening brackets found")); } else { state.ClearCurrentSection(); } } else if (character == '}') { state.EndPlaceholder(); if (state.OpenBracketCount < 0) { return(ParseResult.Error <string, object>("Too many close brackets found")); } else { state.ProcessCurrentSection(); } } else if (character != '\r' && character != '\n' && character != '\t') { state.CurrentSection.Append(character); } } state.AddWarningsForMissingPlaceholders(); //important: sort the placeholders! (i.e. Hello {1} {0} --> {0}, {1}) state.SortPlaceholders(); return(state.GetResult()); }
public ProcessResult Process(InsertQueryParserState state) { if (state.ColumnNames.Count > 0 && state.ColumnValues.Count < state.ColumnNames.Count) { var result = ParseResult.Error ( $"Column names count ({state.ColumnNames.Count}) is not equal to column values count ({state.ColumnValues.Count}), see #MISSING# in column values list (values)", state.ColumnNames.Zip(state.ColumnValues, (name, value) => new KeyValuePair <string, string>(name.Trim(), value.Trim())) ); state.ColumnValues.AddRange(Enumerable.Range(1, state.ColumnNames.Count - state.ColumnValues.Count).Select(_ => "#MISSING#")); return(ProcessResult.Fail(result)); } return(ProcessResult.NotUnderstood()); }
protected override ParseResult Parse(ArraySegment <byte> data) { InitializeHttpReader(false); int headerLength = httpReader.Parse(data.Array, data.Offset, data.Count); if (httpReader.IsFinal) { return(ParseResult.HeaderDone(headerLength, httpReader.HasContentLength ? httpReader.ContentLength : 0)); } if (httpReader.IsError) { return(ParseResult.Error()); } return(ParseResult.NotEnoughData()); }
public IEither <IEnumerable <T>, ParseError> Parse(IInputReader input) { List <T> acc = new List <T>(parsers.Count()); foreach (IParser <T> parser in parsers) { IEither <T, ParseError> result = parser.Parse(input); if (result.IsSuccess) { acc.Add(result.FromSuccess()); } else { return(ParseResult.Error <IEnumerable <T> >(result.FromError())); } } return(ParseResult.Success(acc)); }
public static ParseResult <string, object> ParseWithArgumentsString(string formatString, string argumentsString) { if (string.IsNullOrWhiteSpace(argumentsString)) { return(ParseResult.Error <string, object>("Arguments string is empty")); } var currentSection = new StringBuilder(); var inValue = false; var arguments = new List <string>(); foreach (var character in argumentsString) { if (character == ',' && !inValue) { arguments.Add(currentSection.ToString().Trim()); currentSection.Clear(); } else if (character == '"' && !inValue) { inValue = true; currentSection.Append(character); } else if (character == '"' && inValue) { inValue = false; currentSection.Append(character); } else if (inValue || (character != '\r' && character != '\n' && character != '\t')) { currentSection.Append(character); } } if (currentSection.Length > 0) { arguments.Add(currentSection.ToString().Trim()); currentSection.Clear(); } return(Parse(formatString, arguments.ToArray())); }
private ParseResult SkipKeepAlive(ArraySegment <byte> data) { int processed; for (processed = 0; keepAliveCount < 4 && processed < data.Count; processed++) { if (IsKeepAliveByte(data.Array[data.Offset + processed], keepAliveCount) == false) { return(ParseResult.Error()); } keepAliveCount++; } if (keepAliveCount == 4) { keepAliveCount = 0; } return(ParseResult.Skip(processed)); }
public ParseResult <string, object> GetResult() { if (FormatValueCountUnequalToFormatPlaceholderCount) { var result = ParseResult.Error(ValidationErrors.Concat(new[] { $"Format values count ({FormatValues.Count}) is not equal to column placeholders count ({FormatPlaceholders.Count}), see #MISSING# in format placeholders list (keys)" }), FormatPlaceholders.Zip(FormatValues, (name, value) => new KeyValuePair <string, object>(name, value))); FormatPlaceholders.AddRange(Enumerable.Range(1, FormatValues.Count - FormatPlaceholders.Count).Select(_ => "#MISSING#")); return(result); } else if (FormatPlaceholderCountUnequalToFormatValueCount) { var result = ParseResult.Error(ValidationErrors.Concat(new[] { $"Format placeholders count ({FormatPlaceholders.Count}) is not equal to column values count ({FormatValues.Count}), see #MISSING# in format values list (values)" }), FormatPlaceholders.Zip(FormatValues, (name, value) => new KeyValuePair <string, object>(name, value))); FormatValues.AddRange(Enumerable.Range(1, FormatPlaceholders.Count - FormatValues.Count).Select(_ => "#MISSING#")); return(result); } else if (FormatPlaceholders.Count == 0) { return(ParseResult.Error(ValidationErrors.Concat(new[] { "No format placeholders were found" }), Array.Empty <KeyValuePair <string, object> >())); } return(ParseResult.Create(ValidationErrors.Count == 0, FormatPlaceholders.Zip(FormatValues, (name, value) => new KeyValuePair <string, object>(name, value)), ValidationErrors)); }
public IEither <TResult, ParseError> Parse(IInputReader input) { TAccum acc = this.seed(); IEither <T, ParseError> result = null; Position position = input.GetPosition(); while ((result = this.parser.Parse(input)).IsSuccess) { acc = this.func(acc, result.FromSuccess()); position = input.GetPosition(); } if (input.GetPosition() == position) { return(ParseResult.Success(this.resultSelector(acc))); } else { return(ParseResult.Error <TResult>(result.FromError())); } }
protected override ParseResult Parse(ArraySegment <byte> data) { switch (mode) { case Modes.Sip: { if (keepAliveCount != 0) { return(SkipKeepAlive(data)); } else { var sipReader = SipReader.GetEmpty(); int headerLength = sipReader.Parse(data.Array, data.Offset, data.Count); if (sipReader.IsFinal) { return(ParseResult.HeaderDone(headerLength, sipReader.HasContentLength ? sipReader.ContentLength : 0)); } if (sipReader.IsError) { if (IsKeepAliveByte(data.Array[data.Offset], 0)) { return(SkipKeepAlive(data)); } else { mode = Modes.Http; goto case Modes.Http; } } return(ParseResult.NotEnoughData()); } } case Modes.Http: { var httpReader = HttpReaderx.GetEmpty(); httpHeaderLength = httpReader.Parse(data.Array, data.Offset, data.Count); if (httpReader.IsFinal) { httpContentLength = httpReader.HasContentLength ? httpReader.ContentLength : 0; if (AjaxWebsocket.IsAjax(httpReader, data.Array)) { mode = Modes.Ajax; goto case Modes.Ajax; } return(ParseResult.HeaderDone(httpHeaderLength, httpContentLength)); } if (httpReader.IsError) { return(ParseResult.Error()); } return(ParseResult.NotEnoughData()); } case Modes.WebSocket: { if (wsWaitHeader) { int used = wsHeader.Parse(data); if (wsHeader.IsDone) { wsWaitHeader = false; } return(ParseResult.Skip(used)); } else { if (IsSipWebSocket == false) { return(ParseResult.HeaderDone(0, wsHeader.PayloadLength)); } else { var sipReader = SipReader.GetEmpty(); int headerLength = sipReader.Parse(data.Array, data.Offset, data.Count); if (sipReader.IsFinal) { return(ParseResult.HeaderDone(headerLength, sipReader.HasContentLength ? sipReader.ContentLength : 0)); } if (sipReader.IsError) { return(ParseResult.Error()); } return(ParseResult.NotEnoughData()); } } } case Modes.Ajax: { if (httpContentLength > 0) { if (httpHeaderLength < data.Count) { var sipReader = SipReader.GetEmpty(); int sipHeaderLength = sipReader.Parse(data.Array, data.Offset + httpHeaderLength, data.Count - httpHeaderLength); if (sipReader.IsFinal) { return(ParseResult.HeaderDone(httpHeaderLength + sipHeaderLength, sipReader.HasContentLength ? sipReader.ContentLength : 0)); } if (sipReader.IsError || httpContentLength <= data.Count - httpHeaderLength) { return(ParseResult.Error()); } } HttpReaderx.Invalidate(); return(ParseResult.NotEnoughData()); } else { return(ParseResult.HeaderDone(httpHeaderLength, 0)); } } default: throw new NotImplementedException(); } }
internal static ProcessResult Fail(string errorMessage) => new ProcessResult(true, ParseResult.Error <string, string>(errorMessage));
public IEither <T, ParseError> Parse(IInputReader input) { return(ParseResult.Error <T>(input, this.message)); }