-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathParser-Or.cs
More file actions
106 lines (91 loc) · 3.65 KB
/
Copy pathParser-Or.cs
File metadata and controls
106 lines (91 loc) · 3.65 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
namespace Faithlife.Parsing;
public static partial class Parser
{
/// <summary>
/// Succeeds with a successful parser, if any.
/// </summary>
/// <remarks>The first successful parser that advances the text position is returned.
/// Otherwise, the first successful parser that does not advance the text position is returned.
/// Otherwise, the failure that advanced the text position farthest is returned.</remarks>
public static IParser<T> Or<T>(IEnumerable<IParser<T>> parsers) => new OrParser<T>(parsers);
private sealed class OrParser<T> : Parser<T>
{
public OrParser(IEnumerable<IParser<T>> parsers) => m_parsers = parsers.ToArray();
public override T TryParse(bool skip, ref TextPosition position, out bool success)
{
var hasEmptySuccess = false;
T firstEmptySuccessValue = default!;
TextPosition? bestFailurePosition = null;
foreach (var parser in m_parsers)
{
var currentPosition = position;
var currentValue = parser.TryParse(skip, ref currentPosition, out var currentSuccess);
if (currentSuccess)
{
if (currentPosition == position)
{
if (!hasEmptySuccess)
{
hasEmptySuccess = true;
firstEmptySuccessValue = currentValue;
}
}
else
{
position = currentPosition;
success = true;
return currentValue;
}
}
else if (bestFailurePosition is null || currentPosition.Index > bestFailurePosition.Value.Index)
{
bestFailurePosition = currentPosition;
}
}
if (hasEmptySuccess)
{
success = true;
return firstEmptySuccessValue;
}
if (bestFailurePosition != null)
position = bestFailurePosition.Value;
success = false;
return default!;
}
private readonly IParser<T>[] m_parsers;
}
/// <summary>
/// Succeeds with a successful parser, if any.
/// </summary>
/// <remarks>The first successful parser that advances the text position is returned.
/// Otherwise, the first successful parser that does not advance the text position is returned.
/// Otherwise, the failure that advanced the text position farthest is returned.</remarks>
public static IParser<T> Or<T>(params IParser<T>[] parsers) => Or((IEnumerable<IParser<T>>) parsers);
/// <summary>
/// Succeeds with a successful parser, if any.
/// </summary>
/// <remarks>The first successful parser that advances the text position is returned.
/// Otherwise, the first successful parser that does not advance the text position is returned.
/// Otherwise, the failure that advanced the text position farthest is returned.</remarks>
public static IParser<T> Or<T>(this IParser<T> first, IParser<T> second) => Or(new[] { first, second });
/// <summary>
/// Succeeds with the default value if the parser fails.
/// </summary>
public static IParser<T> OrDefault<T>(this IParser<T> parser) => parser.OrDefault(default!);
/// <summary>
/// Succeeds with the specified value if the parser fails.
/// </summary>
public static IParser<T> OrDefault<T>(this IParser<T> parser, T value) => parser.Or(Success(value));
/// <summary>
/// Succeeds with an empty collection if the parser fails.
/// </summary>
public static IParser<IEnumerable<T>> OrEmpty<T>(this IParser<IEnumerable<T>> parser) => parser.OrDefault(Array.Empty<T>());
/// <summary>
/// Succeeds with an empty collection if the parser fails.
/// </summary>
public static IParser<IReadOnlyCollection<T>> OrEmpty<T>(this IParser<IReadOnlyCollection<T>> parser) => parser.OrDefault(Array.Empty<T>());
/// <summary>
/// Succeeds with an empty collection if the parser fails.
/// </summary>
public static IParser<IReadOnlyList<T>> OrEmpty<T>(this IParser<IReadOnlyList<T>> parser) => parser.OrDefault(Array.Empty<T>());
}