-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathassembler.js
387 lines (345 loc) · 11.1 KB
/
assembler.js
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
376
377
378
379
380
381
382
383
384
385
386
387
// [TYPE] is used to set metadata on array types
// this lets us use regular JS arrays, but tag them as "freeform" or whatever
var TYPE = Symbol("TYPE");
// [NAME] tags objects with their keys, for easy backtracking of {/name} syntax
var NAME = Symbol("NAME");
// [PARENT] lets us navigate back up the tree
var PARENT = Symbol("PARENT");
var assignSymbol = (a, symbol, value) =>
Object.defineProperty(a, symbol, {
value,
enumerable: false,
configurable: false
});
/*
The assembler takes a series of instructions from the parser and uses those to build
an output object. For example, it might use "objectOpen: nested" and "singleValue: key,value"
instructions to output { nested: { key: "value" }}
*/
class Assembler {
constructor(options) {
this.options = options;
this.root = {};
this.stack = [this.root];
this.instructions = [];
this.index = 0;
}
log(...args) {
if (!this.options.verbose) return;
console.log(
args
.map(a => typeof a == "object" ? JSON.stringify(a) : a)
.join(" ")
.replace(/\n/g, "\\n")
.replace(/\t/g, "\\t")
);
}
// stack manipulation methods
// the assembler maintains a context stack of references for "where" it is
// in the output object tree - e.g., in a nested object inside an array
get top() {
return this.stack[this.stack.length - 1];
}
set top(value) {
this.stack[this.stack.length - 1] = value;
}
pushContext(scope) {
this.stack.push(scope);
}
popContext() {
var scope = this.stack.pop();
if (!this.stack.length) this.stack = [this.root];
return scope || this.root;
}
// given a key, sets the place in the object where the key should be placed
getTarget(key) {
if (key[0] == ".") {
return this.top;
}
this.reset();
return this.root;
}
// on many new keys (outside of lists), jump back to the object root
reset(scope) {
this.stack = [this.root];
if (scope) this.stack.push(scope);
}
// turn deep keypaths ("nested.key.string") into a path array
normalizeKeypath(keypath) {
if (typeof keypath == "string") keypath = keypath.split(".");
keypath = keypath.filter(Boolean);
keypath = keypath.map(this.options.onFieldName);
return keypath;
}
// given a keypath, traverse to that location in the output object
// returns undefined if any step along the keypath fails to exist
getPath(object, keypath) {
keypath = this.normalizeKeypath(keypath);
var terminal = keypath.pop();
var branch = object;
for (var k of keypath) {
if (!(k in branch)) {
return undefined;
}
branch = branch[k];
}
return branch && branch[terminal];
}
// given a keypath, set the value at that final location
// creates objects along the way for missing keypath segments
setPath(object, keypath, value) {
keypath = this.normalizeKeypath(keypath);
var terminal = keypath.pop();
var branch = object;
for (var k of keypath) {
if (!k) continue;
if (!(k in branch) || typeof branch[k] != "object") {
branch[k] = {};
assignSymbol(branch[k], PARENT, branch);
assignSymbol(branch[k], NAME, k);
}
branch = branch[k];
}
if (typeof value == "string") value = value.trim();
branch[terminal] = this.options.onValue(value, terminal);
if (typeof branch[terminal] == "object") {
assignSymbol(branch[terminal], PARENT, branch);
}
return branch;
}
// following the instructions, assemble the final object
assemble(instructions) {
this.log("Raw instructions stream:");
instructions.forEach(i => this.log(" ", i));
// pre-process to combine sequential buffered values into a single instruction
// this initial pass makes it easier to handle blocks of arbitrary text
var processed = [];
var interrupts = new Set(["skipped"]);
var lastValue = null;
var buffer = [];
var mergeBuffer = () => buffer.join("").replace(/^\\/m, "");
this.log("Preprocessing...");
for (var i = 0; i < instructions.length; i++) {
var instruction = instructions[i];
var { type, key, value } = instruction;
switch (type) {
case "simple":
// simple buffers if any other value has been set
if (!lastValue || lastValue.type == "simple") {
buffer = [];
processed.push(instruction);
lastValue = instruction;
break;
} else {
this.log(" Simple item being ignored");
value = key + value;
}
case "buffer":
this.log(" Merging buffer instructions...");
buffer.push(value);
var next = instructions[i + 1];
while (next && next.type == "buffer") {
i++;
buffer.push(next.value);
next = instructions[i + 1];
}
break;
case "value":
if (lastValue && lastValue.type == "simple") {
// buffer this inside of a simple value
buffer.push(key + ":" + value);
break;
}
this.log(` Value encountered: ${key}`);
lastValue = instruction;
var merged = mergeBuffer();
if (merged.trim()) {
processed.push({ type: "buffer", value: merged });
}
processed.push(instruction);
buffer = [];
break;
case "flush":
if (buffer.length) {
var merged = mergeBuffer();
if (lastValue && merged.trim()) {
lastValue.value += merged;
} else {
processed.push({ type: "buffer", value: merged });
}
}
buffer = [];
break;
case "object":
case "array":
case "closeObject":
case "closeArray":
var merged = mergeBuffer();
if (merged.trim()) {
processed.push({ type: "buffer", value: merged });
}
this.log(` Encountered ${type}${key ? ` (${key})` : ""}, clearing buffer`);
buffer = [];
default:
lastValue = null;
processed.push(instruction);
}
}
// handle leftover garbage in freeform arrays
var merged = mergeBuffer();
this.log(`Clearing out dangling buffer items...`);
if (merged.trim()) {
processed.push({ type: "buffer", value: merged });
}
this.log("Post-process instructions:");
processed.forEach(i => this.log(" ", i));
// now we actually process the final instruction stream
this.log("Assembling result object...")
for (var instruction of processed) {
var { type, key, value } = instruction;
this.log(` ${[type,key,value].filter(d => d).join("/")}`);
// each instruction has a matching method
this[type](key, value);
}
return this.root;
}
// methods for adding values to arbitrary targets (objects or arrays)
append(target, key, value) {
if (target instanceof Array) {
return this.addToArray(target, key, value);
} else {
if (typeof value == "string") value = value.trim();
this.setPath(target, key, value);
}
}
// returns true if the addition was ignored
addToArray(target, key, value) {
switch (target[TYPE]) {
case "freeform":
if (typeof value == "string") value = value.trim();
key = key.replace(/^\./, "");
var obj = { type: key, value };
assignSymbol(obj, PARENT, target);
target.push(obj);
break;
case "simple":
// simple arrays can't contain keyed values
break;
default:
if (!target[TYPE]) assignSymbol(target, TYPE, "standard");
// add to the last object in the array
var last = target[target.length - 1];
if (!last || this.getPath(last, key)) {
last = {};
target.push(last);
assignSymbol(last, PARENT, target);
}
if (typeof value == "string") value = value.trim();
this.setPath(last, key, value);
}
}
// methods to handle each instruction
// there are relatively few of these, because after parsing, an ArchieML
// document basically just enters object/arrays and adds properties to them
value(key, value) {
var target = this.top;
value = value.trim();
if (target instanceof Array) {
switch (target[TYPE]) {
case "simple":
// key value pairs are ignored
break;
case "freeform":
// you can't add non-dot composite objects to a freeform array
// so we'll exit the array and re-call this
if (typeof value == "object" && key[0] != ".") {
this.closeArray();
return this.value(key, value);
}
target.push({ type: key, value });
break;
default:
this.append(target, key, value);
}
} else {
this.append(target, key, value);
}
}
simple(key, value) {
if (this.top instanceof Array) {
if (this.top[TYPE] == "simple" || !this.top[TYPE]) {
assignSymbol(this.top, TYPE, "simple");
this.top.push(value.trim());
}
if (this.top[TYPE] == "freeform") {
var obj = { type: "text", value: (key + value).trim() };
assignSymbol(obj, PARENT, this.top);
this.top.push(obj);
}
}
}
object(key) {
var target = this.getTarget(key);
var object = this.getPath(target, key);
var path = this.normalizeKeypath(key);
if (typeof object != "object") {
object = {};
var name = path.at(-1);
assignSymbol(object, NAME, name);
}
this.append(target, key, object);
this.pushContext(object);
}
closeObject(key) {
// remove the current top scope
var top = this.popContext();
// navigate via parent up to the named object and push it onto the stack
if (key) {
while (top != this.root && top[NAME] != key) {
top = top[PARENT];
}
this.pushContext(top[PARENT]);
}
}
array(key, type) {
var array = [];
var target = this.getTarget(key);
var path = this.normalizeKeypath(key);
var name = path.at(-1);
var [head] = path;
assignSymbol(array, NAME, name);
if (type) {
assignSymbol(array, TYPE, type);
}
this.append(target, key, array);
this.pushContext(array);
}
closeArray(key) {
if (key) {
// remove the current top scope
var top = this.popContext();
// navigate via parent up to the named object and push it onto the stack
if (key) {
while (top != this.root && top[NAME] != key) {
top = top[PARENT];
}
this.pushContext(top[PARENT]);
}
} else {
while (!(this.top instanceof Array) && this.top != this.root) this.popContext();
if (this.top instanceof Array) this.popContext();
}
}
buffer(key, value) {
var target = this.top;
if (target[TYPE] == "freeform") {
var split = value.split("\n").filter(s => s.trim());
split.forEach(v => target.push({ type: "text", value: v.trim() }));
}
}
// no-op instructions
// flush and skip are technically handled during pre-processing
flush() {}
skipped() {}
}
module.exports = Assembler;