-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLazyQuantifier.cs
More file actions
102 lines (94 loc) · 4.75 KB
/
LazyQuantifier.cs
File metadata and controls
102 lines (94 loc) · 4.75 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
using System;
namespace ReadableRex.Patterns
{
/// <summary>
/// Represents a lazy quantifier that modifies the behavior of quantifiers to be non-greedy in pattern matching.
/// </summary>
/// <remarks>The <see cref="LazyQuantifier"/> class provides lazy versions of common quantifiers, such as
/// zero or more, one or more, and optional. Lazy quantifiers match as few characters as possible, as opposed to
/// greedy quantifiers which match as many as possible.</remarks>
public class LazyQuantifier : Quantifier
{
/// <summary>
/// Initializes a new instance of the <see cref="LazyQuantifier"/> class with the specified pattern to be
/// quantified.
/// </summary>
/// <param name="quantifiedExpression">The pattern to which the lazy quantifier will be applied. This parameter cannot be null.</param>
public LazyQuantifier(Pattern quantifiedExpression) : base(quantifiedExpression) {}
/// <summary>
/// Gets a pattern that matches zero or more occurrences of the current pattern.
/// </summary>
/// <remarks>This property returns a new pattern that represents the current pattern repeated zero
/// or more times. It is equivalent to applying the Kleene star operation in regular expressions.</remarks>
public override Pattern ZeroOrMore
{
get
{
return base.ZeroOrMore.RegEx("?");
}
}
/// <summary>
/// Gets a pattern that matches one or more occurrences of the preceding element.
/// </summary>
/// <remarks>This property returns a pattern that modifies the current pattern to match one or
/// more occurrences of the preceding element. It is equivalent to appending a "+" quantifier in regular
/// expressions.</remarks>
public override Pattern OneOrMore
{
get
{
return base.OneOrMore.RegEx("?");
}
}
/// <summary>
/// Creates a pattern that matches the preceding element exactly the specified number of times.
/// </summary>
/// <param name="timesToRepeat">The exact number of times the preceding element must occur. Must be a non-negative integer.</param>
/// <returns>A <see cref="Pattern"/> that matches the preceding element exactly <paramref name="timesToRepeat"/> times.</returns>
public override Pattern Exactly(int timesToRepeat)
{
return base.Exactly(timesToRepeat).RegEx("?");
}
/// <summary>
/// Repeats the current pattern at least a specified number of times.
/// </summary>
/// <param name="timesToRepeat">The minimum number of times the pattern should be repeated. Must be a non-negative integer.</param>
/// <returns>A new <see cref="Pattern"/> that matches the current pattern repeated at least the specified number of
/// times.</returns>
public override Pattern AtLeast(int timesToRepeat)
{
return base.AtLeast(timesToRepeat).RegEx("?");
}
/// <summary>
/// Gets a pattern that matches an optional occurrence of the current pattern.
/// </summary>
public override Pattern Optional
{
get
{
return base.Optional.RegEx("?");
}
}
/// <summary>
/// Creates a pattern that matches a specified number of occurrences within a given range.
/// </summary>
/// <param name="minimum">The minimum number of occurrences to match. Must be non-negative.</param>
/// <param name="maximum">The maximum number of occurrences to match. Must be greater than or equal to <paramref name="minimum"/>.</param>
/// <returns>A <see cref="Pattern"/> that matches between <paramref name="minimum"/> and <paramref name="maximum"/>
/// occurrences.</returns>
public override Pattern InRange(int minimum, int maximum)
{
return base.InRange(minimum, maximum).RegEx("?");
}
/// <summary>
/// Throws an exception indicating that lazy evaluation of the AtMost quantifier is not supported.
/// </summary>
/// <param name="timesToRepeat">The maximum number of times the pattern is allowed to repeat.</param>
/// <returns>This method does not return a value as it always throws an exception.</returns>
/// <exception cref="InvalidOperationException">Thrown to indicate that lazy evaluation of the AtMost quantifier is not supported.</exception>
public override Pattern AtMost(int timesToRepeat)
{
throw new InvalidOperationException("You cannot perform lazy evaluation of the AtMost {,n} quantifier.");
}
}
}