tarsec 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -497,6 +497,12 @@ export type OperatorInfo<T> = {
497
497
  * @returns - a parser that handles the full expression grammar
498
498
  */
499
499
  export declare function buildExpressionParser<T>(atom: Parser<T>, operatorTable: OperatorInfo<T>[][], parenParser?: Parser<T>): Parser<T>;
500
+ /**
501
+ * Clear every cache created by `memo`. Call this at the start of each
502
+ * top-level parse if your memoized parsers produce results that depend
503
+ * on mutable global state (e.g. positional info from `setInputStr`).
504
+ */
505
+ export declare function resetMemos(): void;
500
506
  /**
501
507
  * Wraps a parser with a per-input cache. Useful for parsers that may be invoked
502
508
  * many times at the same position (e.g. recursive grammars where the same
@@ -933,8 +933,25 @@ function tryOps(ops, input) {
933
933
  return null;
934
934
  }
935
935
  const DEFAULT_MEMO_LIMIT = 10000;
936
+ // Registry of all caches created by `memo`, so callers can clear them
937
+ // between independent parses. This matters when memoized parsers produce
938
+ // results derived from globally-set state (e.g. `withSpan` reads the
939
+ // current input via `setInputStr`): the cached `loc` is only valid for
940
+ // the source it was computed against, so reusing the cache across parses
941
+ // with different sources would return stale positional info.
942
+ const memoCaches = [];
943
+ /**
944
+ * Clear every cache created by `memo`. Call this at the start of each
945
+ * top-level parse if your memoized parsers produce results that depend
946
+ * on mutable global state (e.g. positional info from `setInputStr`).
947
+ */
948
+ export function resetMemos() {
949
+ for (const cache of memoCaches)
950
+ cache.clear();
951
+ }
936
952
  export function memo(name, parser) {
937
953
  const cache = new Map();
954
+ memoCaches.push(cache);
938
955
  return trace(name ? `memo(${name})` : "memo", (input) => {
939
956
  const hit = cache.get(input);
940
957
  if (hit !== undefined)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tarsec",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "A parser combinator library for TypeScript, inspired by Parsec.",
5
5
  "homepage": "https://github.com/egonSchiele/tarsec",
6
6
  "scripts": {