diff --git a/src/parser.ts b/src/parser.ts index baabed9..354bef5 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -10,9 +10,10 @@ const Comment = Regex(/#[^\n]*/y) const PreWordWhitespace = Regex(/[^\S\n;]+/y).expects("whitespace"); -const BackslashEscape = Regex(/\\(.)/y) - .expects("\\") - .map(([, char]) => ({ text: char })); +const BackslashEscape = Sequence( + Regex(/\\/y).expects("BACKSLASH"), + Regex(/./y).expects("CHAR") +).map(([, [char]]) => ({ text: char })); const BARE_WORD_CHAR = /[^\s\\;\[]+/y; const BARE_BRACKET_WORD_CHAR = /[^\s\\;\[\]]+/y; @@ -27,7 +28,7 @@ const Bracket: Pattern = Sequence( function bareWordTmpl(charRegex: RegExp) { return Sequence( - Regex(/(?!["\{])/y), + Regex(/(?!["{])/y), AtLeast( 1, Choose( @@ -65,10 +66,10 @@ const Brace: Pattern = Sequence( .expects("{") .map((text) => `{${text}}`), Regex(/\\./y) - .expects("\\") + .expects("BACKSLASH") .map(([escape]) => escape), Regex(/[^\\{}]+/y) - .expects("text") + .expects("CHAR") .map(([text]) => text) ) ), diff --git a/src/peg.ts b/src/peg.ts index 0b4f999..0bc87f5 100644 --- a/src/peg.ts +++ b/src/peg.ts @@ -84,20 +84,14 @@ export function Choose(...patterns: Pattern[]): Pattern { .join(" | "); return new Pattern(function (source, index) { let furthestFound = index; - let furthestExpected = genericExpected; + let furthestExpected = this.expectLabel; for (const pattern of patterns) { - const [value, furthest, expected] = pattern.match.call( - this, - source, - index - ); + const [value, furthest, expected] = pattern.match(source, index); if (value) { return [value, furthest, expected]; } else if (furthest > furthestFound) { furthestFound = furthest; furthestExpected = expected; - } else if (furthest == furthestFound) { - furthestExpected = furthestExpected + " | " + expected; } } return [null, furthestFound, furthestExpected]; @@ -119,11 +113,7 @@ export function Sequence( let furthestFound = index; let furthestExpected = genericExpected; for (const pattern of patterns) { - const [value, furthest, expected] = pattern.match.call( - this, - source, - index - ); + const [value, furthest, expected] = pattern.match(source, index); if (furthest > furthestFound) { furthestFound = furthest; furthestExpected = expected; @@ -157,11 +147,7 @@ export function AtLeast(min: number, pattern: Pattern): Pattern { let furthestFound = index; let furthestExpected = this.expectLabel; do { - const [value, furthest, expected] = pattern.match.call( - this as Pattern, - source, - index - ); + const [value, furthest, expected] = pattern.match(source, index); if (furthest > furthestFound) { furthestFound = furthest; furthestExpected = expected; @@ -188,7 +174,7 @@ export function AtLeast(min: number, pattern: Pattern): Pattern { */ export function Peek(pattern: Pattern): Pattern { return new Pattern(function (source, index) { - const [value, furthest, expected] = pattern.match.call(this, source, index); + const [value, furthest, expected] = pattern.match(source, index); return [value ? [value[0], index] : null, furthest, expected]; }, pattern.expectLabel); }