static bool CaptureGroup(ref int c, ref int p, VList<LNode> cArgs, VList<LNode> pArgs, ref MMap<Symbol, LNode> captures, ref VList<LNode> attrs)
{
Debug.Assert(IsParamsCapture(pArgs[p]));
// The goal now is to find a sequence of nodes in cArgs that matches
// the sequence pArgs[p+1 .. p+x] where x is the maximum value such
// that none of the nodes in the sequence are $(params caps).
int saved_p = p, saved_c = c;
var savedCaptures = captures.AsImmutable();
var savedAttrs = attrs;
int captureSize = 0;
for (;; captureSize++) {
for (p++, c += captureSize; ; c++, p++) {
// If we run out of pArgs, great, we're done; if we run out
// of cArgs, the match fails, unless all remaining pArgs are
// $(params caps).
if (p >= pArgs.Count || IsParamsCapture(pArgs[p])) {
goto done_group;
} else {
if (c >= cArgs.Count)
return false;
if (!MatchesPatternNested(cArgs[c], pArgs[p], ref captures, ref attrs))
goto continue_group;
}
}
continue_group:;
p = saved_p;
c = saved_c;
attrs = savedAttrs;
captures = savedCaptures.AsMutable();
}
done_group:
AddCapture(captures, pArgs[saved_p], cArgs.Slice(saved_c, captureSize));
return true;
}