2023-07-29 04:26:29 +00:00
|
|
|
/**
|
|
|
|
* A Pattern is a function that matches against a string starting at a given index.
|
|
|
|
*
|
2023-07-29 20:26:41 +00:00
|
|
|
*/
|
2023-08-05 05:09:33 +00:00
|
|
|
export type Pattern<T> = PatternFunc<T> & {
|
|
|
|
/**
|
|
|
|
* Creates a pattern that wraps another pattern, transforming the returned value on a match.
|
|
|
|
*
|
|
|
|
* @param map - Mapping function
|
|
|
|
*/
|
|
|
|
map<U>(map: (value: T) => U): Pattern<U>;
|
|
|
|
|
|
|
|
/** A human-readable annotation describing the pattern for error messages */
|
|
|
|
expectLabel: string;
|
|
|
|
|
|
|
|
/** Adds a human-readable annotation describing the pattern */
|
|
|
|
expects(label: string): Pattern<T>;
|
|
|
|
};
|
|
|
|
type PatternFunc<T> = {
|
|
|
|
/**
|
|
|
|
* If the pattern matches successfully, it returns some captured value, and the index following the match.
|
|
|
|
*
|
|
|
|
* It may also return an error, if that error may have prevented the pattern from matching more than it did.
|
|
|
|
*
|
|
|
|
* Some more complex patterns might succeed, but consume less input than they would have been able to if some
|
|
|
|
* other expected symbol was found. Reporting the furthest a pattern could hypothetically have gotten can help generate
|
|
|
|
* better error messages if no valid parse tree is found.
|
|
|
|
*
|
|
|
|
* @param source - the string being parsed
|
|
|
|
* @param index - the index in the string to begin matching from
|
|
|
|
* @returns - [successValue, furthest symbol attempted, expected pattern]
|
|
|
|
*/
|
|
|
|
(source: string, index: number): [[T, number] | null, number, string];
|
|
|
|
};
|
2023-07-29 04:26:29 +00:00
|
|
|
|
2023-07-29 18:39:11 +00:00
|
|
|
/**
|
|
|
|
* Makes a pattern from a function, adding helper methods.
|
|
|
|
*
|
2023-08-04 22:31:21 +00:00
|
|
|
* @param {(source: string, index: number) => ([[T, number] | null, number, string])} matchFunc
|
2023-07-29 18:39:11 +00:00
|
|
|
* @returns {Peg.Pattern<T>}
|
|
|
|
*/
|
2023-08-05 05:09:33 +00:00
|
|
|
function WrapPattern<T>(matchFunc: PatternFunc<T>) {
|
|
|
|
const pattern = matchFunc as Pattern<T>;
|
|
|
|
pattern.map = (map) => {
|
|
|
|
return WrapPattern((source, index) => {
|
2023-08-04 22:31:21 +00:00
|
|
|
const [value, furthest, expected] = pattern(source, index);
|
|
|
|
return [value ? [map(value[0]), value[1]] : null, furthest, expected];
|
2023-08-04 05:24:02 +00:00
|
|
|
}).expects(pattern.expectLabel);
|
2023-07-29 20:26:41 +00:00
|
|
|
};
|
|
|
|
|
2023-08-04 05:24:02 +00:00
|
|
|
pattern.expectLabel = pattern.name;
|
|
|
|
pattern.expects = (label) => {
|
|
|
|
pattern.expectLabel = label;
|
|
|
|
return pattern;
|
|
|
|
};
|
|
|
|
|
|
|
|
return pattern;
|
2023-08-05 05:09:33 +00:00
|
|
|
}
|
2023-07-29 04:26:29 +00:00
|
|
|
|
2023-07-29 18:50:49 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
2023-08-05 05:09:33 +00:00
|
|
|
* @param getPattern
|
2023-07-29 18:50:49 +00:00
|
|
|
*/
|
2023-08-05 05:09:33 +00:00
|
|
|
export function Use<T>(getPattern: () => Pattern<T>): Pattern<T> {
|
|
|
|
return WrapPattern((source, index) => getPattern()(source, index)).expects(
|
|
|
|
String(getPattern)
|
|
|
|
);
|
|
|
|
}
|
2023-07-29 18:50:49 +00:00
|
|
|
|
2023-07-29 18:39:11 +00:00
|
|
|
/**
|
|
|
|
* Creates a pattern matching a regex & returning any captures. The regex needs to be sticky (using the //y modifier)
|
|
|
|
*/
|
2023-08-05 05:09:33 +00:00
|
|
|
export function Regex(regex: RegExp): Pattern<RegExpExecArray> {
|
2023-08-06 06:09:30 +00:00
|
|
|
const pattern: Pattern<RegExpExecArray> = WrapPattern((source, index) => {
|
2023-07-29 18:39:11 +00:00
|
|
|
regex.lastIndex = index;
|
|
|
|
const matches = regex.exec(source);
|
2023-08-04 23:25:47 +00:00
|
|
|
return matches
|
2023-08-05 00:28:26 +00:00
|
|
|
? [[matches, regex.lastIndex], -1, pattern.expectLabel]
|
2023-08-04 23:25:47 +00:00
|
|
|
: [null, index, pattern.expectLabel];
|
2023-08-04 05:24:02 +00:00
|
|
|
}).expects(regex.source);
|
2023-08-04 06:17:16 +00:00
|
|
|
return pattern;
|
2023-08-05 05:09:33 +00:00
|
|
|
}
|
2023-07-29 04:26:29 +00:00
|
|
|
|
2023-07-29 18:39:11 +00:00
|
|
|
/**
|
|
|
|
* Creates a pattern that tries the given patterns, in order, until it finds one that matches at the current index.
|
|
|
|
* @param {...Peg.Pattern<T>} patterns
|
2023-08-05 05:09:33 +00:00
|
|
|
* @return {}
|
2023-07-29 18:39:11 +00:00
|
|
|
*/
|
2023-08-05 05:09:33 +00:00
|
|
|
export function Choose<T>(...patterns: Pattern<T>[]): Pattern<T> {
|
2023-08-04 22:31:21 +00:00
|
|
|
const genericExpected = patterns
|
|
|
|
.map((pattern) => pattern.expectLabel)
|
|
|
|
.join(" | ");
|
2023-08-05 05:09:33 +00:00
|
|
|
return WrapPattern((source, index) => {
|
2023-08-04 22:31:21 +00:00
|
|
|
let furthestFound = index;
|
|
|
|
let furthestExpected = genericExpected;
|
2023-07-29 18:39:11 +00:00
|
|
|
for (const pattern of patterns) {
|
2023-08-04 22:31:21 +00:00
|
|
|
const [value, furthest, expected] = pattern(source, index);
|
|
|
|
if (value) {
|
|
|
|
return [value, furthest, expected];
|
|
|
|
} else if (furthest > furthestFound) {
|
|
|
|
furthestFound = furthest;
|
|
|
|
furthestExpected = expected;
|
2023-08-05 00:28:26 +00:00
|
|
|
} else if (furthest == furthestFound) {
|
|
|
|
furthestExpected = furthestExpected + " | " + expected;
|
2023-07-29 04:26:29 +00:00
|
|
|
}
|
2023-07-29 18:39:11 +00:00
|
|
|
}
|
2023-08-04 22:31:21 +00:00
|
|
|
return [null, furthestFound, furthestExpected];
|
|
|
|
}).expects(genericExpected);
|
2023-08-05 05:09:33 +00:00
|
|
|
}
|
2023-07-29 05:13:38 +00:00
|
|
|
|
2023-07-29 18:39:11 +00:00
|
|
|
/**
|
|
|
|
* 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]
|
|
|
|
*/
|
2023-08-05 05:09:33 +00:00
|
|
|
export function Sequence<T extends unknown[]>(
|
|
|
|
...patterns: { [K in keyof T]: Pattern<T[K]> }
|
|
|
|
): Pattern<T> {
|
2023-08-04 22:31:21 +00:00
|
|
|
const genericExpected = patterns[0]?.expectLabel ?? "(nothing)";
|
2023-08-05 05:09:33 +00:00
|
|
|
return WrapPattern((source, index) => {
|
|
|
|
const values: unknown[] = [];
|
2023-08-04 22:31:21 +00:00
|
|
|
let furthestFound = index;
|
|
|
|
let furthestExpected = genericExpected;
|
2023-07-29 18:39:11 +00:00
|
|
|
for (const pattern of patterns) {
|
2023-08-04 22:31:21 +00:00
|
|
|
const [value, furthest, expected] = pattern(source, index);
|
2023-08-05 00:28:26 +00:00
|
|
|
if (furthest > furthestFound) {
|
2023-08-04 23:25:47 +00:00
|
|
|
furthestFound = furthest;
|
|
|
|
furthestExpected = expected;
|
2023-08-05 00:28:26 +00:00
|
|
|
} else if (furthest == furthestFound) {
|
|
|
|
furthestExpected = furthestExpected + " | " + expected;
|
2023-08-04 23:25:47 +00:00
|
|
|
}
|
2023-08-04 22:31:21 +00:00
|
|
|
if (value == null) {
|
2023-08-04 23:25:47 +00:00
|
|
|
return [null, furthestFound, furthestExpected];
|
2023-07-29 05:13:38 +00:00
|
|
|
}
|
2023-08-04 22:31:21 +00:00
|
|
|
values.push(value[0]);
|
|
|
|
index = value[1];
|
2023-07-29 18:39:11 +00:00
|
|
|
}
|
2023-08-05 05:09:33 +00:00
|
|
|
return [[values as T, index], furthestFound, furthestExpected];
|
2023-08-04 22:31:21 +00:00
|
|
|
}).expects(genericExpected);
|
2023-08-05 05:09:33 +00:00
|
|
|
}
|
2023-07-29 05:45:34 +00:00
|
|
|
|
2023-07-29 18:39:11 +00:00
|
|
|
/**
|
|
|
|
* Creates a pattern that matches consecutive runs of the given pattern, returning an array of all captures.
|
|
|
|
*
|
2023-07-29 20:26:41 +00:00
|
|
|
* The match only succeeds if the run is at least {@link min} instances long.
|
2023-07-29 18:39:11 +00:00
|
|
|
*
|
|
|
|
* 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.
|
2023-08-06 06:09:30 +00:00
|
|
|
* @param min
|
2023-08-05 05:09:33 +00:00
|
|
|
|
2023-07-29 18:39:11 +00:00
|
|
|
*/
|
2023-08-06 06:09:30 +00:00
|
|
|
export function AtLeast<T>(min: number, pattern: Pattern<T>): Pattern<T[]> {
|
2023-08-05 05:09:33 +00:00
|
|
|
return WrapPattern(function (source, index) {
|
|
|
|
const values: T[] = [];
|
2023-08-04 22:31:21 +00:00
|
|
|
let furthestFound = index;
|
|
|
|
let furthestExpected = pattern.expectLabel;
|
2023-07-29 18:39:11 +00:00
|
|
|
do {
|
2023-08-04 22:31:21 +00:00
|
|
|
const [value, furthest, expected] = pattern(source, index);
|
2023-08-04 23:25:47 +00:00
|
|
|
if (furthest > furthestFound) {
|
2023-08-04 22:31:21 +00:00
|
|
|
furthestFound = furthest;
|
|
|
|
furthestExpected = expected;
|
2023-08-04 23:25:47 +00:00
|
|
|
}
|
|
|
|
if (value == null) {
|
2023-08-04 04:17:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-08-04 22:31:21 +00:00
|
|
|
values.push(value[0]);
|
|
|
|
if (index == value[1]) {
|
2023-08-04 04:17:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-08-04 22:31:21 +00:00
|
|
|
index = value[1];
|
2023-07-29 18:39:11 +00:00
|
|
|
} while (true);
|
|
|
|
if (values.length >= min) {
|
2023-08-04 22:31:21 +00:00
|
|
|
return [[values, index], furthestFound, furthestExpected];
|
2023-07-29 05:45:34 +00:00
|
|
|
} else {
|
2023-08-04 22:31:21 +00:00
|
|
|
return [null, furthestFound, furthestExpected];
|
2023-07-29 05:45:34 +00:00
|
|
|
}
|
2023-08-04 05:24:02 +00:00
|
|
|
}).expects(pattern.expectLabel);
|
2023-08-05 05:09:33 +00:00
|
|
|
}
|
2023-07-29 18:39:11 +00:00
|
|
|
|
2023-08-25 20:44:50 +00:00
|
|
|
/**
|
|
|
|
* Creates a pattern that matches the given pattern, but consumes no input.
|
|
|
|
*/
|
|
|
|
export function Peek<T>(pattern: Pattern<T>): Pattern<T> {
|
|
|
|
return WrapPattern(function (source, index) {
|
|
|
|
const [value, furthest, expected] = pattern(source, index);
|
|
|
|
return [value ? [value[0], index] : null, furthest, expected];
|
|
|
|
}).expects(pattern.expectLabel);
|
|
|
|
}
|
|
|
|
|
2023-07-29 18:39:11 +00:00
|
|
|
/**
|
2023-08-04 05:33:40 +00:00
|
|
|
* Creates a pattern that matches the end of input
|
2023-07-29 18:39:11 +00:00
|
|
|
*/
|
2023-08-05 05:09:33 +00:00
|
|
|
export function End(): Pattern<true> {
|
|
|
|
const end = WrapPattern(function End(source, index) {
|
2023-08-04 22:31:21 +00:00
|
|
|
return [
|
2023-08-05 05:09:33 +00:00
|
|
|
source.length == index ? [true, index] : null,
|
2023-08-04 22:31:21 +00:00
|
|
|
index,
|
|
|
|
end.expectLabel,
|
|
|
|
];
|
2023-08-06 06:09:30 +00:00
|
|
|
}).expects("<eof>") as Pattern<true>;
|
2023-08-04 05:33:40 +00:00
|
|
|
return end;
|
2023-08-05 05:09:33 +00:00
|
|
|
}
|