From 5c5884a39b15406c8631ddb9d4e5bcbdb780ba83 Mon Sep 17 00:00:00 2001 From: Tangent Wantwight Date: Fri, 4 Aug 2023 01:33:40 -0400 Subject: [PATCH] Make End patterns instanced so expects() can be used on them --- notcl.js | 4 ++-- peg.js | 22 +++++++++++++--------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/notcl.js b/notcl.js index 5888cd7..70c3c3a 100644 --- a/notcl.js +++ b/notcl.js @@ -44,7 +44,7 @@ var Notcl = (() => { const CommandTerminator = Sequence( PreWordWhitespace, - Choose(/** @type {Peg.Pattern} */ (Regex(/[\n;]/y)), End) + Choose(/** @type {Peg.Pattern} */ (Regex(/[\n;]/y)), End()) ); /** @type {Peg.Pattern} */ @@ -53,7 +53,7 @@ var Notcl = (() => { ); /** @type {Peg.Pattern} */ - const Script = Sequence(AtLeast(0, Command), End).map( + const Script = Sequence(AtLeast(0, Command), End()).map( ([commands, _eof]) => commands ); diff --git a/peg.js b/peg.js index a9b765f..89e5341 100644 --- a/peg.js +++ b/peg.js @@ -165,13 +165,17 @@ Peg.AtLeast = function (min, pattern) { }; /** - * Pattern that matches the end of input - * @type {Peg.Pattern} + * Creates a pattern that matches the end of input + * @return {Peg.Pattern} */ -Peg.End = Peg.WrapPattern(function End(source, index) { - if (source.length == index) { - return [true, /** @type {true} */ (true), index]; - } else { - return [false, index, ""]; - } -}).expects(""); +Peg.End = () => { + /** @type {Peg.Pattern} */ + const end = Peg.WrapPattern(function End(source, index) { + if (source.length == index) { + return [true, /** @type {true} */ (true), index]; + } else { + return [false, index, end.expectLabel]; + } + }).expects(""); + return end; +};