public static Parser <char, string> String(string str) { if (str == null) { throw new ArgumentNullException(nameof(str)); } if (string.IsNullOrEmpty(str)) { throw new ArgumentException("String cannot be empty", nameof(str)); } return(input => { IParserInput <char> next = input; for (int i = 0, count = str.Length; i < count; i++) { if (next.Eof || next.Current != str[i]) { return new FailParsingResult <char, string>(input, new ParsingError() { Position = input.Position, Message = str + " expected", Method = "Parse.String(string)", Type = typeof(string) }); } next = next.Next; } return new SuccessParsingResult <char, string>(str, next); }); }
public static Parser <TInput, List <T> > Until <TInput, T, TParser>( this Parser <TInput, T> parser, Parser <TInput, TParser> conditionParser) { if (parser == null) { throw new ArgumentNullException(nameof(parser)); } return(input => { List <T> list = new List <T>(); IParsingResult <TInput, TParser> conditionResult = conditionParser(input); IParserInput <TInput> next = input; while (!conditionResult.Success) { IParsingResult <TInput, T> result = parser(next); if (!result.Success) { return new FailParsingResult <TInput, List <T> >(input, result.Error); } list.Add(result.Value); next = result.Input; conditionResult = conditionParser(next); } return new SuccessParsingResult <TInput, List <T> >(list, next); }); }
public static Parser <TInput, List <T> > Repeat <TInput, T>( this Parser <TInput, T> parser, int times) { if (parser == null) { throw new ArgumentNullException(nameof(parser)); } if (times <= 0) { throw new ArgumentOutOfRangeException(nameof(times)); } return(input => { List <T> list = new List <T>(); IParserInput <TInput> next = input; for (int i = 0; i < times; i++) { IParsingResult <TInput, T> result = parser(input); if (!result.Success) { return new FailParsingResult <TInput, List <T> >(input, result.Error); } list.Add(result.Value); next = result.Input; } return new SuccessParsingResult <TInput, List <T> >(list, next); }); }
public string Parse(IParserInput input) { var sb = new StringBuilder(); if (!input.TryReadChar(out var ch)) { throw new ParserException("parser error in word."); } sb.Append(ch); var done = false; do { if (input.TryPeekChar(out ch) && _isValidWordCharFunc(ch)) { input.ReadLookahead(); sb.Append(ch); } else { done = true; } } while (!done); return(sb.ToString()); }
public static Parser <TInput, List <T> > RequiredWhile <TInput, T, TPrefix>( this Parser <TInput, T> parser, Parser <TInput, TPrefix> prefix) { if (parser == null) { throw new ArgumentNullException(nameof(parser)); } if (prefix == null) { throw new ArgumentNullException(nameof(prefix)); } return(input => { List <T> list = new List <T>(); IParserInput <TInput> next = input; IParsingResult <TInput, TPrefix> prefixResult = prefix(input); while (prefixResult.Success) { IParsingResult <TInput, T> result = parser(prefixResult.Input); if (!result.Success) { return new FailParsingResult <TInput, List <T> >(input, result.Error); } list.Add(result.Value); next = result.Input; prefixResult = prefix(next); } return new SuccessParsingResult <TInput, List <T> >(list, next); }); }
/// <summary> /// Attempt to parse an input with a given parser. /// </summary> public static Either <T, ParseError> TryParse <T, S> (this Parser <T, S> parser, IParserInput <S> input) { var res = parser(input); return(res ? Either <T, ParseError> .Create(res.Result) : Either <T, ParseError> .Create(ParseError.FromParseResult(res))); }
public SuccessParsingResult(TValue value, IParserInput <TInput> next) { if (next == null) { throw new ArgumentNullException(nameof(next)); } _value = value; _next = next; _corrections = ReadOnlyList <ParsingError> .Empty; }
/// <summary> /// Reads all remaining input chars as string. /// </summary> /// <param name="input">Input</param> /// <returns>All remaining chars of the input.</returns> public static string ReadAll(this IParserInput input) { var sb = new StringBuilder(); while (input.TryReadChar(out var readChar)) { sb.Append(readChar); } return(sb.ToString()); }
public static Parser <char, string> String(string str, bool ignoreCase, CultureInfo culture = null) { if (!ignoreCase) { return(String(str)); } if (str == null) { throw new ArgumentNullException(nameof(str)); } if (string.IsNullOrEmpty(str)) { throw new ArgumentException("String cannot be empty", nameof(str)); } if (culture == null) { culture = CultureInfo.CurrentCulture; } return(input => { IParserInput <char> next = input; int count = str.Length; char[] data = new char[count]; for (int i = 0; i < count; i++) { char stri = str[i]; if (!next.Eof) { char current = next.Current; if (current == stri || current == char.ToLower(stri, culture) || current == char.ToUpper(stri, culture)) { data[i] = current; next = next.Next; continue; } } return new FailParsingResult <char, string>(input, new ParsingError() { Position = input.Position, Message = str + " expected", Method = "Parse.String(string, bool, CultureInfo)", Type = typeof(string) }); } return new SuccessParsingResult <char, string>(new string(data), next); }); }
public IGeoCircle Parse(IParserInput input) { var inputAsString = input.ReadAll(); var values = _doubleParser.ParseToArrayOrEmpty(inputAsString); switch (values.Length) { case 2: return(_factory.CreateCircle(values[0], values[1], _fallbackRadius)); case 3: return(_factory.CreateCircle(values[0], values[1], values[2])); default: throw new InvalidOperationException("expected two or 3 entries as input"); } }
public string Parse(IParserInput input) { _builder.Clear(); if (!input.TryReadChar(out var quotation)) { if (quotation != _quotation) { throw new ArgumentException($"strings must start with the quotation char '{quotation}' 0x{quotation.ToHexString()}", nameof(input)); } } var done = false; while (!done) { if (!input.TryReadChar(out var nextChar)) { throw new ParserException("end of input in string error"); } if (nextChar == '\n') { throw new ParserException("unclosed string error"); } if (nextChar == _quotation) { if (_allowDoubleQuotationEscaping && input.TryPeekChar(out var doubleQuoteEscapeCandidate) && doubleQuoteEscapeCandidate == _quotation) { input.ReadLookahead(); _builder.Append(nextChar); } else { done = true; } }
public FailParsingResult(IParserInput <TInput> input, IReadOnlyList <ParsingError> corrections, ParsingError error) { _input = input; _corrections = corrections; _error = error; }
public FailParsingResult(IParserInput <TInput> input, ParsingError[] corrections, ParsingError error) { _input = input; _corrections = corrections == null ? ReadOnlyList <ParsingError> .Empty : new ReadOnlyList <ParsingError>(corrections); _error = error; }
public T Parse(IParserInput input) { var inputAsString = input.ReadAll(); return(_converter.Convert(inputAsString)); }
protected ParserBase(IParserInput <char> input) { SetInput(input); }
protected void SetInput(IParserInput <char> input) { Input = input; position = 0; ParsingResults.Clear(); }
/// <summary> /// Parses an input, or throws an ParseError exception, if the parse fails. /// </summary> public static T Parse <T, S> (this Parser <T, S> parser, IParserInput <S> input) { return(TryParse(parser, input).Match( value => value, error => { throw error; })); }