@@ -6,49 +6,50 @@ type Source = Readonly<{ source: string; position: number }>;
6
6
type ParserResult < T > = ArrayResult < Readonly < { value : T ; size : number } > > ;
7
7
type InnerParser < T > = ( input : Source ) => ParserResult < T > ;
8
8
9
+ let source = "" ;
10
+ const allMemo : Set < WeakRef < SourceMemo < unknown > > > = new Set ( ) ;
11
+
12
+ export function clearCache ( ) : void {
13
+ for ( const memo of allMemo ) {
14
+ const ref = memo . deref ( ) ;
15
+ if ( ref == null ) {
16
+ allMemo . delete ( memo ) ;
17
+ } else {
18
+ ref . clear ( ) ;
19
+ }
20
+ }
21
+ }
9
22
class SourceMemo < T > {
10
- #source = "" ;
11
23
readonly #map: Map < number , T > = new Map ( ) ;
24
+ constructor ( ) {
25
+ allMemo . add ( new WeakRef ( this ) ) ;
26
+ }
12
27
set ( key : Source , value : T ) : void {
13
- if ( this . # source !== key . source ) {
14
- this . # source = key . source ;
15
- this . clear ( ) ;
28
+ if ( source !== key . source ) {
29
+ source = key . source ;
30
+ clearCache ( ) ;
16
31
}
17
32
this . #map. set ( key . position , value ) ;
18
33
}
19
34
get ( key : Source ) : undefined | T {
20
- if ( this . # source === key . source ) {
35
+ if ( source === key . source ) {
21
36
return this . #map. get ( key . position ) ;
22
37
} else {
23
38
return undefined ;
24
39
}
25
40
}
26
41
has ( key : Source ) : boolean {
27
- return this . # source === key . source && this . #map. has ( key . position ) ;
42
+ return source === key . source && this . #map. has ( key . position ) ;
28
43
}
29
44
delete ( key : Source ) : void {
30
- if ( this . # source === key . source ) {
45
+ if ( source === key . source ) {
31
46
this . #map. delete ( key . position ) ;
32
47
}
33
48
}
34
49
clear ( ) : void {
35
50
this . #map. clear ( ) ;
36
51
}
37
52
}
38
- type SourceMemoResult < T > = SourceMemo < MemoizationCacheResult < ParserResult < T > > > ;
39
-
40
- const caches : Set < WeakRef < SourceMemo < unknown > > > = new Set ( ) ;
41
-
42
- export function clearCache ( ) : void {
43
- for ( const memo of caches ) {
44
- const ref = memo . deref ( ) ;
45
- if ( ref == null ) {
46
- caches . delete ( memo ) ;
47
- } else {
48
- ref . clear ( ) ;
49
- }
50
- }
51
- }
52
53
export class Parser < T > {
53
54
readonly rawParser : InnerParser < T > ;
54
55
constructor ( parser : InnerParser < T > ) {
@@ -57,11 +58,13 @@ export class Parser<T> {
57
58
assert ( source . source . length >= source . position ) ;
58
59
return parser ( source ) ;
59
60
} ;
60
- const cache : SourceMemoResult < T > = new SourceMemo ( ) ;
61
- caches . add ( new WeakRef ( cache ) ) ;
62
- this . rawParser = memoize < InnerParser < T > , Source , SourceMemoResult < T > > (
61
+ this . rawParser = memoize <
62
+ InnerParser < T > ,
63
+ Source ,
64
+ SourceMemo < MemoizationCacheResult < ParserResult < T > > >
65
+ > (
63
66
ensureParser ,
64
- { cache } ,
67
+ { cache : new SourceMemo ( ) } ,
65
68
) ;
66
69
}
67
70
generateParser ( ) : ( source : string ) => ArrayResult < T > {
0 commit comments