From 8214f05fe34ff88775fe4b641175083fa97b9892 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Fri, 4 Aug 2023 02:18:07 -0400 Subject: [PATCH] Choose() returns the expected message from the alternative that made the furthest progress, if any did. --- peg.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/peg.js b/peg.js index 44226a9..95a8231 100644 --- a/peg.js +++ b/peg.js @@ -90,15 +90,17 @@ Peg.Choose = function (...patterns) { const expected = patterns.map((pattern) => pattern.expectLabel).join(" | "); return Peg.WrapPattern(function (source, index) { let furthest = index; + let furthestExpected = expected; for (const pattern of patterns) { const match = pattern(source, index); if (match[0]) { return match; } else if (match[1] > furthest) { furthest = match[1]; + furthestExpected = match[2]; } } - return [false, furthest, expected]; + return [false, furthest, furthestExpected]; }).expects(expected); };