Skip to content

Commit 9156986

Browse files
committed
use method instead
1 parent 93ad50b commit 9156986

File tree

5 files changed

+30
-38
lines changed

5 files changed

+30
-38
lines changed

dictionary/parser.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -490,24 +490,22 @@ const dictionaryParser = spaces
490490
words.map((word) => [word, definition])
491491
),
492492
)
493-
)
494-
.generateParser();
493+
);
495494

496495
const definitionExtractor = spaces
497496
.with(all(optionalAll(lex(head)).with(lex(match(/[^;]*;/, "definition")))))
498-
.skip(end)
499-
.generateParser();
500-
const definitionParser = spaces.with(definition).skip(end).generateParser();
497+
.skip(end);
498+
const definitionParser = spaces.with(definition).skip(end);
501499

502500
export function parseDictionary(sourceText: string): Dictionary {
503-
const arrayResult = dictionaryParser(sourceText);
501+
const arrayResult = dictionaryParser.parse(sourceText);
504502
if (!arrayResult.isError()) {
505503
return arrayResult.array[0];
506504
} else {
507-
const definitions = definitionExtractor(sourceText);
505+
const definitions = definitionExtractor.parse(sourceText);
508506
const errors = !definitions.isError()
509507
? definitions.array[0].flatMap((definition) =>
510-
definitionParser(definition).errors.map((error) =>
508+
definitionParser.parse(definition).errors.map((error) =>
511509
new ArrayResultError(
512510
`${error.message} at ${definition.trim()}`,
513511
{ cause: error },

src/parser/parser.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ const sentence = choice<Sentence>(
713713
})),
714714
)
715715
.filter(filter(SENTENCE_RULE));
716-
export const parse = spaces
716+
export const parser = spaces
717717
.with(
718718
lookAhead(everything.filter((source) =>
719719
source.trimEnd().length <= 500 ||
@@ -728,5 +728,4 @@ export const parse = spaces
728728
.skip(end)
729729
.filter(filter(MULTIPLE_SENTENCES_RULE))
730730
.map((sentences) => ({ type: "sentences", sentences })),
731-
))
732-
.generateParser();
731+
));

src/parser/parser_lib.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,17 @@ export class Parser<T> {
1919
{ cache },
2020
);
2121
}
22-
generateParser(): (source: string) => ArrayResult<T> {
23-
return (source) => {
24-
currentSource = source;
25-
for (const memo of allMemo) {
26-
const ref = memo.deref();
27-
if (ref == null) {
28-
allMemo.delete(memo);
29-
} else {
30-
ref.clear();
31-
}
22+
parse(source: string): ArrayResult<T> {
23+
currentSource = source;
24+
for (const memo of allMemo) {
25+
const ref = memo.deref();
26+
if (ref == null) {
27+
allMemo.delete(memo);
28+
} else {
29+
ref.clear();
3230
}
33-
return this.rawParser(0)
34-
.map(({ value }) => value);
35-
};
31+
}
32+
return this.rawParser(0).map(({ value }) => value);
3633
}
3734
map<U>(mapper: (value: T) => U): Parser<U> {
3835
return new Parser((input) =>

src/parser/test.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ import { assertLess } from "@std/assert/less";
55
import { assertNotEquals } from "@std/assert/not-equals";
66
import { assertThrows } from "@std/assert/throws";
77
import { EXAMPLE_SENTENCES, MALFORMED_SENTENCES } from "../examples.ts";
8-
import { parse } from "./parser.ts";
8+
import { parser } from "./parser.ts";
99
import { all, end, many, match, matchString, sequence } from "./parser_lib.ts";
1010
import { KU_LILI, KU_SULI, PU } from "./ucsur.ts";
1111

1212
Deno.test("AST all distinct", () => {
1313
for (const sentence of EXAMPLE_SENTENCES) {
14-
const pairs = uniquePairs(parse(sentence).unwrap());
14+
const pairs = uniquePairs(parser.parse(sentence).unwrap());
1515
for (const [a, b] of pairs) {
1616
assertNotEquals(a, b, `Error at "${sentence}"`);
1717
}
@@ -20,7 +20,7 @@ Deno.test("AST all distinct", () => {
2020

2121
Deno.test("parser all error", () => {
2222
for (const sentence of MALFORMED_SENTENCES) {
23-
assertThrows(() => parse(sentence).unwrap());
23+
assertThrows(() => parser.parse(sentence).unwrap());
2424
}
2525
});
2626

@@ -57,21 +57,18 @@ Deno.test("small parser", () => {
5757
match(/toki/, '"toki"').skip(space),
5858
matchString("pona").skip(space),
5959
match(/a/, '"a"').skip(end),
60-
)
61-
.generateParser();
62-
assertEquals(parser("toki pona a").unwrap(), [["toki", "pona", "a"]]);
60+
);
61+
assertEquals(parser.parse("toki pona a").unwrap(), [["toki", "pona", "a"]]);
6362
});
6463

6564
Deno.test("many", () => {
6665
const space = match(/\s*/, "space");
67-
const parser = many(matchString("a").skip(space)).skip(end)
68-
.generateParser();
69-
assertEquals(parser("a a a").unwrap(), [["a", "a", "a"]]);
66+
const parser = many(matchString("a").skip(space)).skip(end);
67+
assertEquals(parser.parse("a a a").unwrap(), [["a", "a", "a"]]);
7068
});
7169

7270
Deno.test("all", () => {
7371
const space = match(/\s*/, "space");
74-
const parser = all(matchString("a").skip(space)).skip(end)
75-
.generateParser();
76-
assertEquals(parser("a a a").unwrap(), [["a", "a", "a"]]);
72+
const parser = all(matchString("a").skip(space)).skip(end);
73+
assertEquals(parser.parse("a a a").unwrap(), [["a", "a", "a"]]);
7774
});

src/translator/translator.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { ArrayResult } from "../array_result.ts";
2-
import { parse } from "../parser/parser.ts";
2+
import { parser } from "../parser/parser.ts";
33
import * as EnglishComposer from "./composer.ts";
44
import { multipleSentences } from "./sentence.ts";
55

66
export function translate(text: string): ArrayResult<string> {
7-
return parse(text)
7+
return parser
8+
.parse(text)
89
.flatMap(multipleSentences)
910
.map(EnglishComposer.multipleSentences);
1011
}

0 commit comments

Comments
 (0)