Regex pattern returns custom expects() if set

This commit is contained in:
Tangent Wantwight 2023-08-04 02:17:16 -04:00
parent 5c5884a39b
commit 3bb125dd8c

6
peg.js
View file

@ -69,13 +69,15 @@ Peg.Use = function (getPattern) {
* @return {Peg.Pattern<RegExpExecArray>}
*/
Peg.Regex = function (regex) {
return Peg.WrapPattern(function (source, index) {
/** @type {Peg.Pattern<RegExpExecArray>} */
const pattern = Peg.WrapPattern(function (source, index) {
regex.lastIndex = index;
const matches = regex.exec(source);
return matches
? [true, matches, regex.lastIndex]
: [false, index, regex.source];
: [false, index, pattern.expectLabel];
}).expects(regex.source);
return pattern;
};
/**