Skip to content

Commit

Permalink
Release 1.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
Virtlink committed Mar 17, 2017
1 parent b7df006 commit ec1c86f
Show file tree
Hide file tree
Showing 4 changed files with 295 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/Yargon.Parsing/Parser.Conversions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Yargon.Parsing
{
partial class Parser
{

}
}
283 changes: 282 additions & 1 deletion src/Yargon.Parsing/Parser.Linq.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;

namespace Yargon.Parsing
{
Expand Down Expand Up @@ -59,7 +62,7 @@ public static Parser<TResult, TToken> SelectMany<TSource, TCollection, TResult,
/// Creates a parser that succeeds only when the specified parser succeeds and the specified condition succeeds.
/// </summary>
/// <typeparam name="TResult">The type of result of the parser.</typeparam>
/// <typeparam name="TToken"></typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <param name="condition">The parser condition.</param>
/// <returns>The created parser.</returns>
Expand Down Expand Up @@ -94,5 +97,283 @@ IParseResult<TResult, TToken> Parser(ITokenStream<TToken> input)

return Parser;
}

/// <summary>
/// Creates a parser that returns the first result for which the condition succeeds,
/// or the specified default value when there are none.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <param name="condition">The condition.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> FirstOrDefault<T, TToken>(this Parser<IEnumerable<T>, TToken> parser, Predicate<T> condition,
[CanBeNull] T defaultValue)
{
#region Contract
if (parser == null)
throw new ArgumentNullException(nameof(parser));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
#endregion

return parser.Select(e => e.Where(v => condition(v)).DefaultIfEmpty(defaultValue).FirstOrDefault());
}

/// <summary>
/// Creates a parser that returns the first result for which the condition succeeds,
/// or the default value when there are none.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <param name="condition">The condition.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> FirstOrDefault<T, TToken>(this Parser<IEnumerable<T>, TToken> parser, Predicate<T> condition)
=> parser.FirstOrDefault(condition, default(T));

/// <summary>
/// Creates a parser that returns the first result,
/// or the default value when there are none.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> FirstOrDefault<T, TToken>(this Parser<IEnumerable<T>, TToken> parser)
=> parser.FirstOrDefault(v => true, default(T));

/// <summary>
/// Creates a parser that returns the first result for which the condition succeeds.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <param name="condition">The condition.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> First<T, TToken>(this Parser<IEnumerable<T>, TToken> parser, Predicate<T> condition)
{
#region Contract
if (parser == null)
throw new ArgumentNullException(nameof(parser));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
#endregion

return parser.Select(e => e.First(v => condition(v)));
}

/// <summary>
/// Creates a parser that returns the first result.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> First<T, TToken>(this Parser<IEnumerable<T>, TToken> parser)
=> parser.First(v => true);

/// <summary>
/// Creates a parser that returns the last result for which the condition succeeds,
/// or the specified default value when there are none.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <param name="condition">The condition.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> LastOrDefault<T, TToken>(this Parser<IEnumerable<T>, TToken> parser, Predicate<T> condition,
[CanBeNull] T defaultValue)
{
#region Contract
if (parser == null)
throw new ArgumentNullException(nameof(parser));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
#endregion

return parser.Select(e => e.Where(v => condition(v)).DefaultIfEmpty(defaultValue).LastOrDefault());
}

/// <summary>
/// Creates a parser that returns the last result for which the condition succeeds,
/// or the default value when there are none.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <param name="condition">The condition.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> LastOrDefault<T, TToken>(this Parser<IEnumerable<T>, TToken> parser, Predicate<T> condition)
=> parser.LastOrDefault(condition, default(T));

/// <summary>
/// Creates a parser that returns the last result,
/// or the default value when there are none.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> LastOrDefault<T, TToken>(this Parser<IEnumerable<T>, TToken> parser)
=> parser.LastOrDefault(v => true, default(T));

/// <summary>
/// Creates a parser that returns the last result for which the condition succeeds.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <param name="condition">The condition.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> Last<T, TToken>(this Parser<IEnumerable<T>, TToken> parser, Predicate<T> condition)
{
#region Contract
if (parser == null)
throw new ArgumentNullException(nameof(parser));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
#endregion

return parser.Select(e => e.Last(v => condition(v)));
}

/// <summary>
/// Creates a parser that returns the last result.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> Last<T, TToken>(this Parser<IEnumerable<T>, TToken> parser)
=> parser.Last(v => true);

/// <summary>
/// Creates a parser that returns the only result for which the condition succeeds,
/// or the specified default value when there are none.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <param name="condition">The condition.</param>
/// <param name="defaultValue">The default value.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> SingleOrDefault<T, TToken>(this Parser<IEnumerable<T>, TToken> parser, Predicate<T> condition,
[CanBeNull] T defaultValue)
{
#region Contract
if (parser == null)
throw new ArgumentNullException(nameof(parser));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
#endregion

return parser.Select(e => e.Where(v => condition(v)).DefaultIfEmpty(defaultValue).SingleOrDefault());
}

/// <summary>
/// Creates a parser that returns the only result for which the condition succeeds,
/// or the default value when there are none.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <param name="condition">The condition.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> SingleOrDefault<T, TToken>(this Parser<IEnumerable<T>, TToken> parser, Predicate<T> condition)
=> parser.SingleOrDefault(condition, default(T));

/// <summary>
/// Creates a parser that returns the only result,
/// or the default value when there are none.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> SingleOrDefault<T, TToken>(this Parser<IEnumerable<T>, TToken> parser)
=> parser.SingleOrDefault(v => true, default(T));

/// <summary>
/// Creates a parser that returns the only result for which the condition succeeds.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <param name="condition">The condition.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> Single<T, TToken>(this Parser<IEnumerable<T>, TToken> parser, Predicate<T> condition)
{
#region Contract
if (parser == null)
throw new ArgumentNullException(nameof(parser));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
#endregion

return parser.Select(e => e.Single(v => condition(v)));
}

/// <summary>
/// Creates a parser that returns the only result.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <returns>The created parser.</returns>
public static Parser<T, TToken> Single<T, TToken>(this Parser<IEnumerable<T>, TToken> parser)
=> parser.Single(v => true);

/// <summary>
/// Creates a parser that returns whether there is any result for which the condition succeeds.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <param name="condition">The condition.</param>
/// <returns>The created parser.</returns>
public static Parser<bool, TToken> Any<T, TToken>(this Parser<IEnumerable<T>, TToken> parser, Predicate<T> condition)
{
#region Contract
if (parser == null)
throw new ArgumentNullException(nameof(parser));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
#endregion

return parser.Select(e => e.Any(v => condition(v)));
}

/// <summary>
/// Creates a parser that returns whether there is any result.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <returns>The created parser.</returns>
public static Parser<bool, TToken> Any<T, TToken>(this Parser<IEnumerable<T>, TToken> parser)
=> parser.Any(v => true);

/// <summary>
/// Creates a parser that returns whether there the condition succeeds for all results.
/// </summary>
/// <typeparam name="T">The type of result.</typeparam>
/// <typeparam name="TToken">The type of tokens.</typeparam>
/// <param name="parser">The parser.</param>
/// <param name="condition">The condition.</param>
/// <returns>The created parser.</returns>
public static Parser<bool, TToken> All<T, TToken>(this Parser<IEnumerable<T>, TToken> parser, Predicate<T> condition)
{
#region Contract
if (parser == null)
throw new ArgumentNullException(nameof(parser));
if (condition == null)
throw new ArgumentNullException(nameof(condition));
#endregion

return parser.Select(e => e.All(v => condition(v)));
}
}
}
2 changes: 1 addition & 1 deletion src/Yargon.Parsing/Yargon.Parsing.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework>
<VersionPrefix>1.0.5</VersionPrefix>
<VersionPrefix>1.0.6</VersionPrefix>
<VersionSuffix></VersionSuffix>
<Authors>Daniel Pelsmaeker</Authors>
<Company>Daniel Pelsmaeker</Company>
Expand Down
2 changes: 1 addition & 1 deletion test/Yargon.Parsing.Tests/Yargon.Parsing.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<RootNamespace>Yargon.Parsing</RootNamespace>
<VersionPrefix>1.0.5</VersionPrefix>
<VersionPrefix>1.0.6</VersionPrefix>
<VersionSuffix></VersionSuffix>
</PropertyGroup>

Expand Down

0 comments on commit ec1c86f

Please sign in to comment.