fix MatchMultiple parsing

This commit is contained in:
spiral 2021-12-04 18:15:26 -05:00
parent d28b67cb81
commit 0110a0b53f
No known key found for this signature in database
GPG Key ID: A6059F0CA0E1BD31
2 changed files with 29 additions and 8 deletions

View File

@ -24,7 +24,7 @@ public static class ContextArgumentsExt
ctx.Parameters.FullCommand; ctx.Parameters.FullCommand;
/// <summary> /// <summary>
/// Checks if the next parameter is equal to one of the given keywords. Case-insensitive. /// Checks if the next parameter is equal to one of the given keywords and pops it from the stack. Case-insensitive.
/// </summary> /// </summary>
public static bool Match(this Context ctx, ref string used, params string[] potentialMatches) public static bool Match(this Context ctx, ref string used, params string[] potentialMatches)
{ {
@ -48,6 +48,19 @@ public static class ContextArgumentsExt
return ctx.Match(ref used, potentialMatches); return ctx.Match(ref used, potentialMatches);
} }
/// <summary>
/// Checks if the next parameter (starting from `ptr`) is equal to one of the given keywords, and leaves it on the stack. Case-insensitive.
/// </summary>
public static bool PeekMatch(this Context ctx, ref int ptr, string[] potentialMatches)
{
var arg = ctx.Parameters.PeekWithPtr(ref ptr);
foreach (var match in potentialMatches)
if (arg.Equals(match, StringComparison.InvariantCultureIgnoreCase))
return true;
return false;
}
/// <summary> /// <summary>
/// Matches the next *n* parameters against each parameter consecutively. /// Matches the next *n* parameters against each parameter consecutively.
/// <br /> /// <br />
@ -56,11 +69,12 @@ public static class ContextArgumentsExt
/// </summary> /// </summary>
public static bool MatchMultiple(this Context ctx, params string[][] potentialParametersMatches) public static bool MatchMultiple(this Context ctx, params string[][] potentialParametersMatches)
{ {
foreach (var param in potentialParametersMatches) int ptr = ctx.Parameters._ptr;
if (!ctx.Match(param)) return false;
for (var i = 0; i < potentialParametersMatches.Length; i++) foreach (var param in potentialParametersMatches)
ctx.PopArgument(); if (!ctx.PeekMatch(ref ptr, param)) return false;
ctx.Parameters._ptr = ptr;
return true; return true;
} }

View File

@ -27,7 +27,7 @@ public class Parameters
}; };
private ISet<string> _flags; // Only parsed when requested first time private ISet<string> _flags; // Only parsed when requested first time
private int _ptr; public int _ptr;
public string FullCommand { get; } public string FullCommand { get; }
@ -100,8 +100,15 @@ public class Parameters
public string Peek() public string Peek()
{ {
// Loop to ignore and skip past flags, temp ptr so we don't move the real ptr // temp ptr so we don't move the real ptr
var ptr = _ptr; int ptr = _ptr;
return PeekWithPtr(ref ptr);
}
public string PeekWithPtr(ref int ptr)
{
// Loop to ignore and skip past flags
while (NextWordPosition(ptr) is { } pos) while (NextWordPosition(ptr) is { } pos)
{ {
ptr = pos.endPos + pos.advanceAfterWord; ptr = pos.endPos + pos.advanceAfterWord;