tweak error messages a little
This commit is contained in:
parent
d6eacc886b
commit
050c0568a8
2 changed files with 12 additions and 25 deletions
|
@ -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<ScriptPiece> = Sequence(
|
|||
|
||||
function bareWordTmpl(charRegex: RegExp) {
|
||||
return Sequence(
|
||||
Regex(/(?!["\{])/y),
|
||||
Regex(/(?!["{])/y),
|
||||
AtLeast(
|
||||
1,
|
||||
Choose<InterpolatedPiece>(
|
||||
|
@ -65,10 +66,10 @@ const Brace: Pattern<string> = Sequence(
|
|||
.expects("{")
|
||||
.map((text) => `{${text}}`),
|
||||
Regex(/\\./y)
|
||||
.expects("\\")
|
||||
.expects("BACKSLASH")
|
||||
.map(([escape]) => escape),
|
||||
Regex(/[^\\{}]+/y)
|
||||
.expects("text")
|
||||
.expects("CHAR")
|
||||
.map(([text]) => text)
|
||||
)
|
||||
),
|
||||
|
|
24
src/peg.ts
24
src/peg.ts
|
@ -84,20 +84,14 @@ export function Choose<T>(...patterns: Pattern<T>[]): Pattern<T> {
|
|||
.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<T extends unknown[]>(
|
|||
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<T>(min: number, pattern: Pattern<T>): Pattern<T[]> {
|
|||
let furthestFound = index;
|
||||
let furthestExpected = this.expectLabel;
|
||||
do {
|
||||
const [value, furthest, expected] = pattern.match.call(
|
||||
this as Pattern<T>,
|
||||
source,
|
||||
index
|
||||
);
|
||||
const [value, furthest, expected] = pattern.match(source, index);
|
||||
if (furthest > furthestFound) {
|
||||
furthestFound = furthest;
|
||||
furthestExpected = expected;
|
||||
|
@ -188,7 +174,7 @@ export function AtLeast<T>(min: number, pattern: Pattern<T>): Pattern<T[]> {
|
|||
*/
|
||||
export function Peek<T>(pattern: Pattern<T>): Pattern<T> {
|
||||
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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue