-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.ts
375 lines (291 loc) · 9.06 KB
/
syntax.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import {
basename,
dirname,
relative,
} from "https://deno.land/[email protected]/path/posix/mod.ts";
import type {
APIDependency,
APIGroupIdentifier,
APIGroupVersion,
} from "https://kure.sh/lib/[email protected]/mod.ts";
export interface Generator {
apply(module: Module): void;
render(ctx: Context): string;
}
export class Context {
public readonly group: APIGroupIdentifier;
public readonly version: string;
public readonly dependencies: Map<string, APIDependency>;
public readonly module: Module;
public readonly engine: Engine;
constructor(
{ group, version, dependencies }: APIGroupVersion,
module: Module,
engine: Engine
) {
this.group = group;
this.version = version;
this.dependencies = new Map(dependencies.map((dep) => [dep.package, dep]));
this.module = module;
this.engine = engine;
}
get apiVersion(): string {
const { group, version } = this;
return group.name ? `${group.name}/${version}` : version;
}
}
export class Module implements Pick<Generator, "render"> {
public readonly path: string;
public readonly names: Names;
public readonly imports: Imports;
private readonly members: Generator[];
constructor(path: string) {
this.path = path;
this.names = new Names();
this.imports = new Imports();
this.members = [];
}
add(member: Generator | ((module: Module) => (ctx: Context) => string)) {
const generator =
typeof member === "function" ? new ClosureGenerator(member) : member;
this.members.push(generator);
}
name(name: string, usage: NameUsage = "declaration"): Binding {
return this.names.add(name, usage).binding;
}
get(name: string): Binding {
return () => this.names.getDeclared(name).token();
}
import(src: ImportSource, name: string, options?: ImportOptions): Binding {
return this.imports.use(src, name, options).binding;
}
render(ctx: Context): string {
for (const generator of [...this.members, this.imports]) {
generator.apply(this);
}
this.names.resolve();
const source: string[] = [];
for (const generator of [this.imports, ...this.members]) {
source.push(generator.render(ctx));
}
return source.filter(Boolean).join("\n\n");
}
}
export type Engine = "deno" | "node";
export type ModuleURN = `kure:${"api" | "lib"}:${string}` | `/${string}`;
export type ImportSource =
| [type: "api", name: string, path?: string]
| [type: "lib", name: string, path?: string]
| [type: "local", path: string];
class Imports implements Generator {
private modules: Map<ModuleURN, ImportedModule> = new Map();
use(src: ImportSource, name: string, options?: ImportOptions): Import {
const { members } = this.module(src);
let member = members.get(name);
if (member == null) {
member = new Import(name, options);
members.set(name, member);
} else if (options != null) {
if (member.type && !options.type) member.type = false;
if (!member.as && options.as) member.as = options.as;
}
return member;
}
apply(module: Module): void {
for (const imported of this.modules.values()) {
for (const imp of imported.members.values()) {
imp.apply(module);
}
}
}
render(ctx: Context): string {
const paths: Map<ModuleURN, string> = new Map();
const remote: ModuleURN[] = [];
const local: ModuleURN[] = [];
for (const [urn, { src }] of this.modules) {
paths.set(urn, this.path(src, ctx));
if (src[0] === "local") {
local.push(urn);
} else {
remote.push(urn);
}
}
const groups = [remote, local]
.filter((group) => group.length > 0)
.map((group) =>
group.sort((a, b) => paths.get(a)!.localeCompare(paths.get(b)!))
);
const renderGroup = (urns: ModuleURN[]) =>
urns.map(renderImport).join("\n");
const renderImport = (urn: ModuleURN) => {
const path = paths.get(urn)!;
const imports = [...this.modules.get(urn)!.members.values()].sort(
(a, b) => a.member.localeCompare(b.member)
);
const statement = imports.every((i) => i.type)
? `import type { ${imports.map((i) => i.render(true)).join(", ")} }`
: `import { ${imports.map((i) => i.render()).join(", ")} }`;
return `${statement} from ${JSON.stringify(path)};`;
};
return groups.map(renderGroup).join("\n\n");
}
private module(src: ImportSource): ImportedModule {
const urn = Import.urn(src);
let mod = this.modules.get(urn);
if (mod == null) {
mod = { src, members: new Map() };
this.modules.set(urn, mod);
}
return mod;
}
private path(
src: ImportSource,
{ module, engine, dependencies }: Context
): string {
const local = `/src/${dirname(module.path)}`;
if (src[0] === "local") {
const dest = `/src/${src[1]}`;
let dir = relative(local, dirname(dest)) || ".";
if (!dir.startsWith(".")) dir = `./${dir}`;
return `${dir}/${basename(dest)}`;
} else {
let [type, pkg, path] = src;
if (engine === "deno") {
const version = type === "api" ? dependencies.get(pkg)?.version : "0.1";
if (pkg === "kubernetes" && version) pkg = "";
const target = `${pkg}${version ? `@${version}` : ""}`;
return `https://kure.sh/${type}/${target}/${path || "mod.ts"}`;
} else if (engine === "node") {
return (
`@kure-${type}/${pkg.replaceAll("/", "-")}` + (path ? `/${path}` : "")
);
}
}
throw new Error("unreachable");
}
}
interface ImportedModule {
src: ImportSource;
members: Map<string, Import>;
}
class Import {
public readonly member: string;
public as?: string;
public type: boolean;
private name?: Name;
constructor(member: string, options?: ImportOptions) {
this.member = member;
this.as = options?.as || undefined;
this.type = options?.type ?? false;
}
apply(module: Module): void {
this.name = module.names.add(this.as ?? this.member, "import");
}
token(): string {
if (this.name == null)
throw new Error(`import of ${this.member} not yet named`);
return this.name.token();
}
render(inTypeImport = false) {
if (inTypeImport && !this.type)
throw new Error(`cannot import value ${this.member} via \`import type\``);
const name = this.token();
const aliased = name !== this.member;
const type = this.type && !inTypeImport;
return (
(type ? "type " : "") + (aliased ? `${this.member} as ${name}` : name)
);
}
get binding(): Binding {
return this.token.bind(this);
}
static urn(source: ImportSource): ModuleURN {
if (source[0] === "api" || source[0] === "lib") {
return ["kure", ...source].join(":") as ModuleURN;
} else if (source[0] === "local") {
return `/${source[1]}`;
}
throw new Error("unreachable");
}
}
class Names {
private readonly bound: Map<string, Name[]> = new Map();
add(token: string, usage: NameUsage): Name {
const names = this.token(token);
if (names.some((name) => name.isConflict(usage))) {
throw new Error(`Duplicate ${usage} of ${token}`);
}
const name = new Name(token, usage);
names.push(name);
return name;
}
getDeclared(token: string): Name {
const names = this.bound.get(token);
const name = names?.find((name) => name.usage === "declaration");
if (name == null) throw new Error(`name ${name} not declared in module`);
return name;
}
resolve() {
for (const [identifier, names] of this.bound) {
names.sort((a, b) => a.weight - b.weight);
for (const [i, name] of names.entries()) {
name.resolve(
i === 0 ? null : i === 1 ? `${identifier}_` : `${identifier}_${i}`
);
}
}
}
private token(name: string): Name[] {
let names = this.bound.get(name);
if (names == null) {
names = [];
this.bound.set(name, names);
}
return names;
}
}
class Name {
public readonly declared: string;
public readonly usage: NameUsage;
private resolved: string | null;
constructor(declared: string, usage: NameUsage) {
this.declared = declared;
this.usage = usage;
this.resolved = null;
}
get binding(): Binding {
return this.token.bind(this);
}
get weight(): number {
return Name.usages[this.usage];
}
isConflict(usage: NameUsage) {
return this.usage === "declaration" && usage === "declaration";
}
resolve(as: string | null) {
this.resolved = as ?? this.declared;
}
token(): string {
if (this.resolved == null)
throw new Error(`Name ${this.declared} not yet resolved`);
return this.resolved;
}
private static readonly usages = {
declaration: 0,
import: 1,
};
}
type NameUsage = keyof (typeof Name)["usages"];
type Binding = () => string;
type ImportOptions = Partial<Pick<Import, "as" | "type">>;
class ClosureGenerator implements Generator {
constructor(
private readonly impl: (module: Module) => (ctx: Context) => string
) {}
apply(module: Module): void {
this.render = this.impl(module);
}
render(_ctx: Context): string {
throw new Error("apply() was not called");
}
}