Make End patterns instanced so expects() can be used on them

This commit is contained in:
Tangent Wantwight 2023-08-04 01:33:40 -04:00
parent 50df27c50e
commit 5c5884a39b
2 changed files with 15 additions and 11 deletions

View file

@ -44,7 +44,7 @@ var Notcl = (() => {
const CommandTerminator = Sequence(
PreWordWhitespace,
Choose(/** @type {Peg.Pattern<unknown>} */ (Regex(/[\n;]/y)), End)
Choose(/** @type {Peg.Pattern<unknown>} */ (Regex(/[\n;]/y)), End())
);
/** @type {Peg.Pattern<Notcl.Command>} */
@ -53,7 +53,7 @@ var Notcl = (() => {
);
/** @type {Peg.Pattern<Notcl.Script>} */
const Script = Sequence(AtLeast(0, Command), End).map(
const Script = Sequence(AtLeast(0, Command), End()).map(
([commands, _eof]) => commands
);

22
peg.js
View file

@ -165,13 +165,17 @@ Peg.AtLeast = function (min, pattern) {
};
/**
* Pattern that matches the end of input
* @type {Peg.Pattern<true>}
* Creates a pattern that matches the end of input
* @return {Peg.Pattern<true>}
*/
Peg.End = Peg.WrapPattern(function End(source, index) {
if (source.length == index) {
return [true, /** @type {true} */ (true), index];
} else {
return [false, index, "<eof>"];
}
}).expects("<eof>");
Peg.End = () => {
/** @type {Peg.Pattern<true>} */
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("<eof>");
return end;
};