1 | const __init = Symbol();const __init2 = Symbol();import {TokenType as tt} from "./parser/tokenizer/types";
|
2 |
|
3 |
|
4 | export default class NameManager {
|
5 | [__init]() {this.usedNames = new Set()}
|
6 | [__init2]() {this.symbolNames = []}
|
7 |
|
8 | constructor( tokens) {;this.tokens = tokens;this[__init]();this[__init2]();}
|
9 |
|
10 | preprocessNames() {
|
11 | for (let i = 0; i < this.tokens.tokens.length; i++) {
|
12 | if (this.tokens.matches1AtIndex(i, tt.name)) {
|
13 | this.usedNames.add(this.tokens.identifierNameAtIndex(i));
|
14 | }
|
15 | }
|
16 | }
|
17 |
|
18 | claimFreeName(name) {
|
19 | const newName = this.findFreeName(name);
|
20 | this.usedNames.add(newName);
|
21 | return newName;
|
22 | }
|
23 |
|
24 | findFreeName(name) {
|
25 | if (!this.usedNames.has(name)) {
|
26 | return name;
|
27 | }
|
28 | let suffixNum = 2;
|
29 | while (this.usedNames.has(name + suffixNum)) {
|
30 | suffixNum++;
|
31 | }
|
32 | return name + suffixNum;
|
33 | }
|
34 |
|
35 | |
36 |
|
37 |
|
38 | claimSymbol(name) {
|
39 | const newName = this.claimFreeName(name);
|
40 | this.symbolNames.push(newName);
|
41 | return newName;
|
42 | }
|
43 |
|
44 | getInjectedSymbolCode() {
|
45 | return this.symbolNames.map((name) => `const ${name} = Symbol();`).join("");
|
46 | }
|
47 | }
|