/
Parser.WithResult.cs
45 lines (43 loc) · 2.16 KB
/
Parser.WithResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
namespace Pidgin
{
public partial class Parser<TToken, T>
{
/// <summary>
/// Creates a parser which behaves like the current parser but returns <paramref name="result"/> after a successful parse.
/// This is a synonym for <see cref="ThenReturn"/>.
/// </summary>
/// <example>
/// Equivalent to using <see cref="Select"/> with a function that returns a fixed value,
/// or <see cref="Then{U}(Parser{TToken, U})"/> with <see cref="Parser{TToken}.Return{T}(T)"/>.
/// <code>
/// p.WithResult(x) == p.Select(_ => x) == p.Then(Return(x));
/// </code>
/// </example>
/// <param name="result">The result</param>
/// <returns>A parser which behaves like the current parser but returns <paramref name="result"/>.</returns>
public Parser<TToken, U> WithResult<U>(U result)
=> this.Select(_ => result);
/// <summary>
/// Creates a parser which behaves like the current parser but returns <paramref name="result"/> after a successful parse.
/// This is a synonym for <see cref="WithResult"/>.
/// </summary>
/// <example>
/// Equivalent to using <see cref="Select"/> with a function that returns a fixed value,
/// or <see cref="Then{U}(Parser{TToken, U})"/> with <see cref="Parser{TToken}.Return{T}(T)"/>.
/// <code>
/// p.ThenReturn(x) == p.Select(_ => x) == p.Then(Return(x));
/// </code>
/// </example>
/// <param name="result">The result</param>
/// <returns>A parser which behaves like the current parser but returns <paramref name="result"/>.</returns>
public Parser<TToken, U> ThenReturn<U>(U result)
=> this.Select(_ => result);
/// <summary>
/// Creates a parser which behaves like the current parser but returns <see cref="Unit.Value"/>.
/// Equivalent to <c>p.WithResult(Unit.Value)</c>.
/// </summary>
/// <returns>A parser which behaves like the current parser but returns <see cref="Unit.Value"/>.</returns>
public Parser<TToken, Unit> IgnoreResult()
=> this.WithResult(Unit.Value);
}
}