|
1 | | -import { Context } from "./context"; |
| 1 | +class Context { |
| 2 | + code = ""; |
| 3 | + scopes = [["vars"]]; |
| 4 | + bitFields: Parser[] = []; |
| 5 | + tmpVariableCount = 0; |
| 6 | + references: { [key: string]: { resolved: boolean; requested: boolean } } = {}; |
| 7 | + importPath: string; |
| 8 | + imports: any[] = []; |
| 9 | + reverseImports = new Map<any, number>(); |
| 10 | + useContextVariables: boolean = false; |
| 11 | + |
| 12 | + constructor(importPath?: string, useContextVariables?: boolean) { |
| 13 | + this.importPath = importPath; |
| 14 | + this.useContextVariables = useContextVariables; |
| 15 | + } |
| 16 | + |
| 17 | + generateVariable(name?: string) { |
| 18 | + const arr = []; |
| 19 | + |
| 20 | + const scopes = this.scopes[this.scopes.length - 1]; |
| 21 | + arr.push(...scopes); |
| 22 | + if (name) { |
| 23 | + arr.push(name); |
| 24 | + } |
| 25 | + |
| 26 | + return arr.join("."); |
| 27 | + } |
| 28 | + |
| 29 | + generateOption(val: number | string | Function) { |
| 30 | + switch (typeof val) { |
| 31 | + case "number": |
| 32 | + return val.toString(); |
| 33 | + case "string": |
| 34 | + return this.generateVariable(val); |
| 35 | + case "function": |
| 36 | + return `${this.addImport(val)}.call(${this.generateVariable()}, vars)`; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + generateError(err: string) { |
| 41 | + this.pushCode("throw new Error(" + err + ");"); |
| 42 | + } |
| 43 | + |
| 44 | + generateTmpVariable() { |
| 45 | + return "$tmp" + this.tmpVariableCount++; |
| 46 | + } |
| 47 | + |
| 48 | + pushCode(code: string) { |
| 49 | + this.code += code + "\n"; |
| 50 | + } |
| 51 | + |
| 52 | + pushPath(name: string) { |
| 53 | + if (name) { |
| 54 | + this.scopes[this.scopes.length - 1].push(name); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + popPath(name: string) { |
| 59 | + if (name) { |
| 60 | + this.scopes[this.scopes.length - 1].pop(); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + pushScope(name: string) { |
| 65 | + this.scopes.push([name]); |
| 66 | + } |
| 67 | + |
| 68 | + popScope() { |
| 69 | + this.scopes.pop(); |
| 70 | + } |
| 71 | + |
| 72 | + addImport(im: any) { |
| 73 | + if (!this.importPath) return `(${im})`; |
| 74 | + let id = this.reverseImports.get(im); |
| 75 | + if (!id) { |
| 76 | + id = this.imports.push(im) - 1; |
| 77 | + this.reverseImports.set(im, id); |
| 78 | + } |
| 79 | + return `${this.importPath}[${id}]`; |
| 80 | + } |
| 81 | + |
| 82 | + addReference(alias: string) { |
| 83 | + if (this.references[alias]) return; |
| 84 | + this.references[alias] = { resolved: false, requested: false }; |
| 85 | + } |
| 86 | + |
| 87 | + markResolved(alias: string) { |
| 88 | + this.references[alias].resolved = true; |
| 89 | + } |
| 90 | + |
| 91 | + markRequested(aliasList: string[]) { |
| 92 | + aliasList.forEach((alias) => { |
| 93 | + this.references[alias].requested = true; |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + getUnresolvedReferences() { |
| 98 | + const references = this.references; |
| 99 | + return Object.keys(this.references).filter( |
| 100 | + (alias) => !references[alias].resolved && !references[alias].requested |
| 101 | + ); |
| 102 | + } |
| 103 | +} |
2 | 104 |
|
3 | 105 | const aliasRegistry: { [key: string]: Parser } = {}; |
4 | 106 | const FUNCTION_PREFIX = "___parser_"; |
|
0 commit comments