/** * A Pattern is a function that matches against a string starting at a given index. * * If it matches successfully, it returns some captured value, and the index following the match. * * @template T * @callback Peg.PatternCall * @param {string} source - the string being parsed * @param {number} index - the index in the string to begin matching from * @returns {[true, T, number] | [false, number]} - if successful, true, the captured value, and the index to start parsing following symbols from. Else, false, and the furthest index that could be understood. */ /** * @template T * @typedef {object} Peg.PatternExt * @property {(map: (value: T) => U) => Peg.Pattern} map Creates a pattern that wraps another pattern, transforming the returned value on a match */ /** * @template T * @typedef {Peg.PatternCall & Peg.PatternExt} Peg.Pattern */ var Peg = window.Peg ?? {}; /** * Makes a pattern from a function, adding helper methods. * * @template T * @param {(source: string, index: number) => ([true, T, number] | [false, number])} pattern * @returns {Peg.Pattern} */ Peg.WrapPattern = function (pattern) { /** @type {Peg.Pattern} */ (pattern).map = function (map) { return Peg.WrapPattern(function (source, index) { const match = pattern(source, index); return match[0] ? [true, map(match[1]), match[2]] : [false, match[1]]; }); }; return /** @type {Peg.Pattern} */ (pattern); }; /** * Proxies to a pattern retrieved from an accessor function. * * Allows using a pattern recursively in its own definition, by returning the value of the const assigned to. * * @template T * @param {() => Peg.Pattern} getPattern * @returns {Peg.Pattern} */ Peg.Use = function (getPattern) { return Peg.WrapPattern(function (source, index) { return getPattern()(source, index); }); }; /** * Creates a pattern matching a regex & returning any captures. The regex needs to be sticky (using the //y modifier) * @param {RegExp} regex * @return {Peg.Pattern} */ Peg.Regex = function (regex) { return Peg.WrapPattern(function (source, index) { regex.lastIndex = index; const matches = regex.exec(source); return matches ? [true, matches, regex.lastIndex] : [false, index]; }); }; /** * Creates a pattern that tries the given patterns, in order, until it finds one that matches at the current index. * @template T * @param {...Peg.Pattern} patterns * @return {Peg.Pattern} */ Peg.Choose = function (...patterns) { return Peg.WrapPattern(function (source, index) { let furthest = index; for (const pattern of patterns) { const match = pattern(source, index); if (match[0]) { return match; } else if (match[1] > furthest) { furthest = match[1]; } } return [false, furthest]; }); }; /** * Creates a pattern that concatenates the given patterns, returning a tuple of their captured values. * * For example, if A matches "a" and captures 1, while B matches "b" and captures null, * then `Sequence(A,B)` will match "ab" and capture [1, null] * @template {unknown[]} T * @param {{[K in keyof T]: Peg.Pattern}} patterns * @return {Peg.Pattern} */ Peg.Sequence = function (...patterns) { return Peg.WrapPattern(function (source, index) { const values = /** @type {T} */ (/** @type {unknown} */ ([])); for (const pattern of patterns) { const match = pattern(source, index); if (match[0] == false) { return match; } values.push(match[1]); index = match[2]; } return [true, values, index]; }); }; /** * Creates a pattern that matches consecutive runs of the given pattern, returning an array of all captures. * * The match only succeeds if the run is at least {@link min} instances long. * * If the given pattern does not consume input, the matching will be terminated to prevent an eternal loop. * * Note that if the minimum run is zero, this pattern will always succeed, but might not consume any input. * @template {unknown} T * @param {number} min * @param {Peg.Pattern} pattern * @return {Peg.Pattern} */ Peg.AtLeast = function (min, pattern) { return Peg.WrapPattern(function (source, index) { const values = /** @type {T[]} */ ([]); let furthest = index; do { const match = pattern(source, index); if (match[0] == false) { furthest = match[1]; break; } values.push(match[1]); if (index == match[2]) { furthest = match[2]; break; } index = match[2]; } while (true); if (values.length >= min) { return [true, values, index]; } else { return [false, furthest]; } }); }; /** * Pattern that matches the end of input * @type {Peg.Pattern} */ Peg.End = Peg.WrapPattern(function End(source, index) { if (source.length == index) { return [true, true, index]; } else { return [false, index]; } });