From 9b4d16fbe9d5698c57f9da725a22b528a7d7cac2 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 31 May 2024 15:13:52 +0100 Subject: [PATCH] fix(parsely): Make Repeat parser work when no separator is given --- .../packages/parsely/parsers/combinators.js | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/packages/phoenix/packages/parsely/parsers/combinators.js b/packages/phoenix/packages/parsely/parsers/combinators.js index b597e8576..d65066d86 100644 --- a/packages/phoenix/packages/parsely/parsers/combinators.js +++ b/packages/phoenix/packages/parsely/parsers/combinators.js @@ -79,7 +79,7 @@ export class Optional extends Parser { export class Repeat extends Parser { _create (value_parser, separator_parser, { trailing = false } = {}) { this.value_parser = adapt_parser(value_parser); - this.separator_parser = adapt_parser(separator_parser); + this.separator_parser = separator_parser ? adapt_parser(separator_parser) : null; this.trailing = trailing; } @@ -99,22 +99,23 @@ export class Repeat extends Parser { // Repeatedly parse for (;;) { // Separator - if (!this.separator_parser) - continue; - - const separatorResult = this.separator_parser.parse(subStream); - if (separatorResult.status === UNRECOGNIZED) - break; - if (separatorResult.status === INVALID) - return { status: INVALID, value: separatorResult }; - stream.join(subStream); - if (!separatorResult.$discard) results.push(separatorResult); + let parsed_separator = false; + if (this.separator_parser) { + const separatorResult = this.separator_parser.parse(subStream); + if (separatorResult.status === UNRECOGNIZED) + break; + if (separatorResult.status === INVALID) + return { status: INVALID, value: separatorResult }; + stream.join(subStream); + if (!separatorResult.$discard) results.push(separatorResult); + parsed_separator = true; + } // Value const result = this.value_parser.parse(subStream); if (result.status === UNRECOGNIZED) { // If we failed to parse a value, we have a trailing separator - if (this.trailing === false) + if (parsed_separator && this.trailing === false) return { status: INVALID, value: result }; break; }