Skip to content

Commit 45d4c5e

Browse files
committed
better parameters
1 parent c4f2b7d commit 45d4c5e

4 files changed

Lines changed: 39 additions & 23 deletions

File tree

src/dictionary/parallel_parser.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import { HEADS } from "./parser.ts";
44
import { Dictionary } from "./type.ts";
55

66
type WorkerError =
7-
| Readonly<
8-
{
9-
type: "positioned error";
10-
errors: ReadonlyArray<Position & Readonly<{ message: string }>>;
11-
}
12-
>
7+
| Readonly<{
8+
type: "result error";
9+
errors: ReadonlyArray<
10+
Readonly<{ message: string; position: null | Position }>
11+
>;
12+
}>
1313
| Readonly<{ type: "other"; error: unknown }>;
1414

1515
function buildOffloaded(source: string): Promise<Dictionary> {
@@ -26,11 +26,14 @@ function buildOffloaded(source: string): Promise<Dictionary> {
2626
worker.onerror = (event) => {
2727
const error = event.error as WorkerError;
2828
switch (error.type) {
29-
case "positioned error":
29+
case "result error":
3030
reject(
3131
new AggregateError(
3232
error.errors.map((error) =>
33-
new PositionedError(error.message, error)
33+
new PositionedError(
34+
error.message,
35+
{ position: error.position ?? undefined },
36+
)
3437
),
3538
),
3639
);
@@ -74,8 +77,10 @@ export async function parseDictionary(source: string): Promise<Dictionary> {
7477
) {
7578
errors.push(
7679
new PositionedError(resultError.message, {
77-
position: job.index + resultError.position.position,
78-
length: resultError.position.length,
80+
position: {
81+
position: job.index + resultError.position.position,
82+
length: resultError.position.length,
83+
},
7984
cause: resultError,
8085
}),
8186
);

src/dictionary/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ const dictionaryParser: Parser<Dictionary> = ignore
656656
errors.push(
657657
new PositionedError(
658658
`duplicate Toki Pona word "${head.value}"`,
659-
head,
659+
{ position: head },
660660
),
661661
);
662662
} else {

src/dictionary/worker.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { extractResultError, ResultError } from "../compound.ts";
1+
import { extractResultError, type ResultError } from "../compound.ts";
2+
import { type PositionedError } from "../parser/parser_lib.ts";
23
import { parseDictionary } from "./parser.ts";
34

45
onmessage = (message) => {
@@ -12,8 +13,11 @@ onmessage = (message) => {
1213
throw { type: "other", error };
1314
}
1415
throw {
15-
type: "positioned error",
16-
error: errors.map((error) => ({ ...error })),
16+
type: "result error",
17+
error: errors.map((error) => ({
18+
message: error.message,
19+
position: (error as PositionedError).position,
20+
})),
1721
};
1822
}
1923
};

src/parser/parser_lib.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { assertGreater } from "@std/assert/greater";
22
import { MemoizationCacheResult, memoize } from "@std/cache/memoize";
33
import { IterableResult, ResultError } from "../compound.ts";
4-
import { ErrorOption, lazy as lazyEval } from "../misc/misc.ts";
4+
import { lazy as lazyEval } from "../misc/misc.ts";
55

66
type ValueLength<T> = Readonly<{ value: T; length: number }>;
77
type ParserResult<T> = IterableResult<ValueLength<T>>;
@@ -101,16 +101,19 @@ export class Parser<T> {
101101
}
102102
}
103103
export type Position = Readonly<{ position: number; length: number }>;
104+
export type PositionedErrorOption = Readonly<{
105+
position?: Position;
106+
cause?: unknown;
107+
}>;
104108
export class PositionedError extends ResultError {
105109
override name = "PositionedError";
106110
position: null | Position;
107111
constructor(
108112
message: string,
109-
// TODO: it's better to separate these two instead
110-
option?: Position & ErrorOption,
113+
option?: PositionedErrorOption,
111114
) {
112115
super(message, option);
113-
this.position = option ?? null;
116+
this.position = option?.position ?? null;
114117
}
115118
}
116119
function withPositionedError<T>(fn: () => T, position: Position) {
@@ -120,7 +123,7 @@ function withPositionedError<T>(fn: () => T, position: Position) {
120123
if (error instanceof PositionedError && error.position != null) {
121124
throw error;
122125
} else if (error instanceof ResultError) {
123-
throw new PositionedError(error.message, { ...position, cause: error });
126+
throw new PositionedError(error.message, { position, cause: error });
124127
} else {
125128
throw error;
126129
}
@@ -131,7 +134,7 @@ export class UnexpectedError extends PositionedError {
131134
constructor(
132135
unexpected: string,
133136
expected: string,
134-
option?: Position & ErrorOption,
137+
option?: PositionedErrorOption,
135138
) {
136139
super(
137140
`unexpected ${unexpected}. ${expected} were expected instead`,
@@ -141,7 +144,7 @@ export class UnexpectedError extends PositionedError {
141144
}
142145
export class UnrecognizedError extends PositionedError {
143146
override name = "UnrecognizedError";
144-
constructor(element: string, option?: Position & ErrorOption) {
147+
constructor(element: string, option?: PositionedErrorOption) {
145148
super(`${element} is unrecognized`, option);
146149
}
147150
}
@@ -279,7 +282,11 @@ function generateError(position: number, expected: string) {
279282
}
280283
}
281284
return IterableResult.errors([
282-
new UnexpectedError(unexpected, expected, { position, length }),
285+
new UnexpectedError(
286+
unexpected,
287+
expected,
288+
{ position: { position, length } },
289+
),
283290
]);
284291
}
285292
export function matchCapture(
@@ -338,7 +345,7 @@ export const notEnd: Parser<null> = new Parser((position) =>
338345
new UnexpectedError(
339346
"end of text",
340347
"not end of text",
341-
{ position, length: currentSource.length - position },
348+
{ position: { position, length: currentSource.length - position } },
342349
),
343350
])
344351
);

0 commit comments

Comments
 (0)