/// <summary> /// Creates a new instance of <see cref="UnorderedParser{TValue}"/>. /// </summary> /// <param name="separator">Item separator. Not null.</param> /// <param name="mode">Parsing mode.</param> /// <param name="items">The item parsers. Not null. Not empty.</param> public UnorderedParser(IGeneralParser separator, UnorderedParsingMode mode, params IParser <TValue>[] items) { ArgumentCheck.NotNullOrEmptyOrWithNulls(items, nameof(items)); _separator = separator ?? throw new ArgumentNullException(nameof(separator)); _mode = mode; _items = items; }
public static IGeneralReply FailsToParse(this IGeneralParser parser, IEnumerable <Token> tokens) { var reply = parser.ParseGenerally(new TokenStream(tokens)); if (reply.Success) { throw new AssertionException("parser failure", "parser completed successfully"); } return(reply); }
/// <summary> /// Creates a new instance of <see cref="QuantifiedParser{TValue}"/>. /// </summary> /// <param name="item">The ~item parser. Not null.</param> /// <param name="quantificationRule">Quantification rule.</param> /// <param name="n">N parameter of the quantification rule. Non-negative.</param> /// <param name="m">M parameter of the quantification rule. If used by the <paramref name="quantificationRule"/>, /// should be not less than N, othervise should be set to -1. ></param> /// <param name="separator">Optional item separator parser. Is null by default.</param> public QuantifiedParser(IParser <TValue> item, QuantificationRule quantificationRule, int n, int m = -1, IGeneralParser separator = null) { _item = item ?? throw new ArgumentNullException(nameof(item)); if (n < 0) { throw new ArgumentOutOfRangeException(nameof(n), "should be non-negative"); } switch (quantificationRule) { case QuantificationRule.ExactlyN: case QuantificationRule.NOrMore: if (m != -1) { throw new ArgumentOutOfRangeException(nameof(m), "this value is not used in this mode and should be left -1"); } break; case QuantificationRule.NtoM: if (n > m) { throw new ArgumentOutOfRangeException(nameof(m), "should not be less than nTimes"); } break; } if (item == separator) { throw new ArgumentException("parser for the item and the separator cannot be the same one", nameof(separator)); } _quantificationRule = quantificationRule; _n = n; _m = m; _separator = separator; }
/// <summary> /// Skips the `skip and then takes the `item. /// </summary> /// <typeparam name="TValue">The type of the parsed result.</typeparam> /// <param name="skip">The parser of the part to be skipped. Not null.</param> /// <param name="item">The item parser. Not null.</param> public static SkipTakeParser <TValue> SkipAndThenTake <TValue>(this IGeneralParser skip, IParser <TValue> item) => new SkipTakeParser <TValue>(skip, item);
public static IParser <TValue> Take <TValue>(this IGeneralParser preceding, IParser <TValue> item) => new SkipTakeParser <TValue>(preceding, item);
/// <summary> /// Takes the `item and then skips the `skip. /// </summary> /// <typeparam name="TValue">The type of the parsed result.</typeparam> /// <param name="item">The item parser. Not null.</param> /// <param name="skip">The parser of the part to be skipped. Not null.</param> public static IParser <TValue> TakeAndThenSkip <TValue>(this IParser <TValue> item, IGeneralParser skip) => new TakeSkipParser <TValue>(item, skip);
public static IParser <TValue> ThenSkip <TValue>(this IParser <TValue> item, IGeneralParser following) => new TakeSkipParser <TValue>(item, following);
/// <summary> /// Creates a new instance of <see cref="SkipTakeParser{TValue}"/>. /// </summary> /// <param name="skip">The `skip. Not null.</param> /// <param name="take">The `take. Not null.</param> public SkipTakeParser(IGeneralParser skip, IParser <TValue> take) { _take = take ?? throw new ArgumentNullException(nameof(take)); _skip = skip ?? throw new ArgumentNullException(nameof(skip)); }
public static IGeneralReply Parses(this IGeneralParser parser, IEnumerable <Token> tokens) { return(parser.ParseGenerally(new TokenStream(tokens)).Succeeds().AtEndOfInput()); }
/// <summary> /// Creates a new instance of <see cref="QuantifiedParser{TValue}"/> configured to <see cref="QuantificationRule.NtoM"/>. /// </summary> /// <typeparam name="TValue">The type of the parsed value.</typeparam> /// <param name="n">The minimum number of occurrences of the `item. Non-negative.</param> /// <param name="m">The maximum number of occurrences of the `item. Non-negative.</param> /// <param name="item">The `item parser. Not null.</param> /// <param name="itemSeparator">Optional item separator parser. Is null by default.</param> /// <returns>The new instance of <see cref="QuantifiedParser{TValue}"/>. Not null.</returns> public static QuantifiedParser <TValue> NToM <TValue>(int n, int m, IParser <TValue> item, IGeneralParser separator = null) => new QuantifiedParser <TValue>(item, QuantificationRule.NtoM, n, m, separator);
/// <summary> /// Creates a new instance of <see cref="QuantifiedParser{TValue}"/> configured to <see cref="QuantificationRule.NOrLess"/>. /// </summary> /// <typeparam name="TValue">The type of the parsed value.</typeparam> /// <param name="n">The maximum number of occurrences of the `item. Non-negative.</param> /// <param name="item">The `item parser. Not null.</param> /// <param name="itemSeparator">Optional item separator parser. Is null by default.</param> /// <returns>The new instance of <see cref="QuantifiedParser{TValue}"/>. Not null.</returns> public static QuantifiedParser <TValue> NOrLess <TValue>(int n, IParser <TValue> item, IGeneralParser separator = null) => new QuantifiedParser <TValue>(item, QuantificationRule.NOrLess, n, -1, separator);
/// <summary> /// Creates a new instance of <see cref="QuantifiedParser{TValue}"/> configured to <see cref="QuantificationRule.NOrMore"/> with N being 1. /// </summary> /// <typeparam name="TValue">The type of the parsed value.</typeparam> /// <param name="item">The item parser. Not null.</param> /// <param name="itemSeparator">Optional item separator parser. Is null by default.</param> /// <returns>The new instance of <see cref="QuantifiedParser{TValue}"/>. Not null.</returns> public static QuantifiedParser <TValue> OneOrMore <TValue>(IParser <TValue> item, IGeneralParser separator = null) => new QuantifiedParser <TValue>(item, QuantificationRule.NOrMore, 1, -1, separator);
/// <summary> /// Creates a new instance of <see cref="UnorderedParser{TValue}"/>. /// </summary> /// <param name="separator">Item separator. Not null.</param> /// <param name="items">The item parsers. Not null. Not empty.</param> /// <returns>The new instance of <see cref="UnorderedParser{TValue}"/>. Not null.</returns> public static UnorderedParser <TValue> Unordered <TValue>(IGeneralParser separator, UnorderedParsingMode mode, params IParser <TValue>[] items) => new UnorderedParser <TValue>(separator, mode, items);
/// <summary> /// Creates a new instance of <see cref="NameValuePairParser{TName, TValue}"/>. /// </summary> /// <typeparam name="TName">The type of the parsed name.</typeparam> /// <typeparam name="TValue">The type of the parsed value.</typeparam> /// <param name="name">The `name parser. Not null.</param> /// <param name="delimiter">The `delimiter parser. Not null.</param> /// <param name="value">The `value parser. Not null.</param> /// <returns></returns> public static NameValuePairParser <TName, TValue> NameValuePair <TName, TValue>(IParser <TName> name, IGeneralParser delimiter, IParser <TValue> value) => new NameValuePairParser <TName, TValue>(name, delimiter, value);
/// <summary> /// Creates a new instance of <see cref="BetweenParser{TValue}"/>. /// </summary> /// <typeparam name="TValue">The type of the parsed value.</typeparam> /// <param name="left">General parser of the left part. Not null.</param> /// <param name="item">Item parser. Not null.</param> /// <param name="right">General parser of the right part. Not null.</param> /// <returns>The new instance of <see cref="BetweenParser{TValue}"/>. Not null.</returns> public static BetweenParser <TValue> Between <TValue>(IGeneralParser left, IParser <TValue> item, IGeneralParser right) => new BetweenParser <TValue>(left, item, right);
/// <summary> /// Creates a new instance of <see cref="QuantifiedParser{TValue}"/> configured to <see cref="QuantificationRule.ExactlyN"/>. /// </summary> /// <typeparam name="TValue">The type of the parsed value.</typeparam> /// <param name="n">The exact number of occurrences of the `item. Non-negative.</param> /// <param name="item">The `item parser. Not null.</param> /// <param name="itemSeparator">Optional item separator parser. Is null by default.</param> /// <returns>The new instance of <see cref="QuantifiedParser{TValue}"/>. Not null.</returns> public static QuantifiedParser <TValue> ExactlyN <TValue>(int n, IParser <TValue> item, IGeneralParser separator = null) => new QuantifiedParser <TValue>(item, QuantificationRule.ExactlyN, n, -1, separator);
/// <summary> /// Creates a new instance of <see cref="BetweenParser{TValue}"/>. /// </summary> /// <param name="left">General parser of the left part. Not null.</param> /// <param name="item">Item parser. Not null.</param> /// <param name="right">General parser of the right part. Not null.</param> public BetweenParser(IGeneralParser left, IParser <TValue> item, IGeneralParser right) { _left = left ?? throw new ArgumentNullException(nameof(left)); _item = item ?? throw new ArgumentNullException(nameof(item)); _right = right ?? throw new ArgumentNullException(nameof(right)); }
/// <summary> /// Creates a new instance of <see cref="NameValuePairParser{TName, TValue}"/>. /// </summary> /// <param name="name">Name parser.</param> /// <param name="delimiter">Delimiter parser.</param> /// <param name="value">Value parser.</param> public NameValuePairParser(IParser <TName> name, IGeneralParser delimiter, IParser <TValue> value) { _name = name ?? throw new ArgumentNullException(nameof(name)); _delimiter = delimiter ?? throw new ArgumentNullException(nameof(delimiter)); _value = value ?? throw new ArgumentNullException(nameof(value)); }
/// <summary> /// ZeroOrMore(p, s) parses zero or more occurrences of p separated by occurrences of s, /// returning the list of values returned by successful applications of p. /// </summary> private static IParser <IEnumerable <T> > ClassicZeroOrMore <T>(IParser <T> item, IGeneralParser separator) { return(Grammar.Choice(Grammar.OneOrMore(item, separator), new MonadicUnitParser <IEnumerable <T> >(Enumerable.Empty <T>()))); }
public static IGeneralReply PartiallyParses(this IGeneralParser parser, IEnumerable <Token> tokens) { var stream = new TokenStream(tokens); return(parser.ParseGenerally(stream).Succeeds()); }