Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/parser/lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -131,8 +129,6 @@ const xAlaX = lazy(() => {
})
.map<Token>((word) => ({ type: "x ala x", word }));

Parser.endCache();

/** Parses a punctuation. */
const punctuation = choiceOnlyOne(
match(/[.,:;?!…·。。︒\u{F199C}\u{F199D}]+/u, "punctuation")
Expand Down
20 changes: 14 additions & 6 deletions src/parser/parser-lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ export class Parser<T> {
readonly #parser: (src: string) => ParserResult<T>;
static cache: null | Cache = null;
constructor(parser: (src: string) => ParserResult<T>) {
const cache = new Map<string, ParserResult<T>>();
Parser.addToCache(cache);
this.#parser = memoize(parser, { cache });
if (Parser.cache != null) {
const cache = new Map<string, ParserResult<T>>();
Parser.addToCache(cache);
this.#parser = memoize(parser, { cache });
} else {
this.#parser = parser;
}
}
parser(src: string): ParserResult<T> {
return ArrayResult.from(() => this.#parser(src));
Expand Down Expand Up @@ -140,9 +144,13 @@ export function lookAhead<T>(parser: Parser<T>): Parser<T> {
*/
export function lazy<T>(parser: () => Parser<T>): Parser<T> {
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
Expand Down