Skip to content
← Back

src/syntax/process/node_state_stores.ghul

1
namespace Syntax.Process is
2
use Collections;
3
4
// Contract for a pass-owned store of per-node derived state.
5
//
6
// A pass that computes state about tree nodes keeps it in a store it
7
// owns, keyed by the node, rather than in fields on the node itself.
8
// The tree then carries only what the source says; each store's
9
// contents have a single producer, and the state's lifetime is
10
// managed explicitly (see STATE_STORE_REGISTRY) instead of riding
11
// along with the tree object's lifetime.
12
trait NodeStateStore is
13
name: string;
14
entry_count: int;
15
16
// Forget everything recorded against nodes of one file. Called
17
// when a file's tree is replaced (re-parse), so entries keyed by
18
// the abandoned tree's nodes cannot accumulate.
19
drop_file(file_name: string);
20
21
clear_all();
22
si
23
24
// Every NodeStateStore registers here. The registry is the one place
25
// that lists all per-node derived state and gives file replacement
26
// and full-rebuild paths a single call to evict it.
27
class STATE_STORE_REGISTRY is
28
_stores: MutableList[NodeStateStore];
29
30
init() is
31
_stores = LIST[NodeStateStore]();
32
si
33
34
register(store: NodeStateStore) is
35
_stores.add(store);
36
si
37
38
drop_file(file_name: string) is
39
for store in _stores do
40
store.drop_file(file_name);
41
od
42
si
43
44
clear_all() is
45
for store in _stores do
46
store.clear_all();
47
od
48
si
49
50
describe() -> string is
51
let result = System.Text.StringBuilder();
52
53
for store in _stores do
54
result
55
.append(store.name)
56
.append(": ")
57
.append(store.entry_count)
58
.append(" entries\n");
59
od
60
61
return result.to_string();
62
si
63
si
64
si