diff --git a/src/parser/lexer.ts b/src/parser/lexer.ts index b2698d07..464c2fef 100644 --- a/src/parser/lexer.ts +++ b/src/parser/lexer.ts @@ -116,8 +116,6 @@ const longWord = choiceOnlyOne(matchString("a"), matchString("n")) ) .skip(spaces); -Parser.startCache(cache); - /** Parses X ala X constructions if allowed by the settings. */ const xAlaX = lazy(() => { if (settings.xAlaXPartialParsing) { @@ -131,8 +129,6 @@ const xAlaX = lazy(() => { }) .map((word) => ({ type: "x ala x", word })); -Parser.endCache(); - /** Parses a punctuation. */ const punctuation = choiceOnlyOne( match(/[.,:;?!…·。。︒\u{F199C}\u{F199D}]+/u, "punctuation") diff --git a/src/parser/parser-lib.ts b/src/parser/parser-lib.ts index f263a162..350799d3 100644 --- a/src/parser/parser-lib.ts +++ b/src/parser/parser-lib.ts @@ -17,9 +17,13 @@ export class Parser { readonly #parser: (src: string) => ParserResult; static cache: null | Cache = null; constructor(parser: (src: string) => ParserResult) { - const cache = new Map>(); - Parser.addToCache(cache); - this.#parser = memoize(parser, { cache }); + if (Parser.cache != null) { + const cache = new Map>(); + Parser.addToCache(cache); + this.#parser = memoize(parser, { cache }); + } else { + this.#parser = parser; + } } parser(src: string): ParserResult { return ArrayResult.from(() => this.#parser(src)); @@ -140,9 +144,13 @@ export function lookAhead(parser: Parser): Parser { */ export function lazy(parser: () => Parser): Parser { const { cache } = Parser; - const cachedParser = new Lazy(() => Parser.inContext(parser, cache)); - Parser.addToCache(cachedParser); - return new Parser((src) => cachedParser.getValue().parser(src)); + if (Parser.cache != null) { + const cachedParser = new Lazy(() => Parser.inContext(parser, cache)); + Parser.addToCache(cachedParser); + return new Parser((src) => cachedParser.getValue().parser(src)); + } else { + return new Parser((src) => Parser.inContext(parser, cache).parser(src)); + } } /** * Evaluates all parsers on the same source string and sums it all on a single