forked from int2str/jssyntaxtree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.js
406 lines (341 loc) · 11.1 KB
/
tree.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// jsSyntaxTree - A syntax tree graph generator
// (c)2019 Andre Eisenbach <[email protected]>
'use strict';
const NODE_PADDING = 20;
import Canvas from './canvas.js';
import * as Parser from './parser.js';
export default class Tree {
constructor(canvas) {
this.nodecolor = true;
this.fontsize = 16;
this.triangles = true;
this.subscript = true;
this.alignment = 0;
this.canvas = null;
this.vscaler = 1;
}
resizeCanvas(w, h) {
this.canvas.resize(w, h);
this.canvas.translate(0, canvas.fontsize / 2);
}
draw(syntax_tree) {
if (this.canvas == null)
throw 'Canvas must be set first.';
const drawables = drawableFromNode(this.canvas, syntax_tree);
const max_depth = getMaxDepth(drawables);
if (this.alignment > 0)
moveLeafsToBottom(drawables, max_depth);
if (this.alignment > 1)
moveParentsDown(drawables);
if (this.subscript)
calculateAutoSubscript(drawables);
const has_arrow =
calculateDrawablePositions(this.canvas, drawables, this.vscaler);
const arrowSet = makeArrowSet(drawables, this.fontsize);
const arrowScaler =
Math.pow((Math.sqrt(arrowSet.maxBottom) / arrowSet.maxBottom), 1 / 50);
this.resizeCanvas(
drawables.width + 1,
Math.max((max_depth + 1) * (this.fontsize * this.vscaler * 3),
has_arrow ? arrowSet.maxBottom * arrowScaler : 0));
drawables.children.forEach(child => this.drawNode(child));
this.drawArrows(arrowSet.arrows);
}
drawNode(drawable) {
this.drawLabel(drawable);
this.drawSubscript(drawable);
drawable.children.forEach(child => {
this.drawNode(child);
this.drawConnector(drawable, child);
});
}
drawLabel(drawable) {
this.canvas.setFontSize(this.fontsize);
if (this.nodecolor) {
this.canvas.setFillStyle(drawable.is_leaf ? '#CC0000' : '#0000CC');
} else {
this.canvas.setFillStyle('black');
}
this.canvas.text(drawable.label, getDrawableCenter(drawable),
drawable.top + 2);
}
drawSubscript(drawable) {
if (!drawable.subscript && !drawable.superscript)
return;
let offset = 1 + getDrawableCenter(drawable) +
this.canvas.textWidth(drawable.label) / 2;
this.canvas.setFontSize(this.fontsize * 3 / 4);
if (drawable.subscript) {
offset += this.canvas.textWidth(drawable.subscript) / 2;
this.canvas.text(drawable.subscript, offset,
drawable.top + this.fontsize / 2);
} else {
offset += this.canvas.textWidth(drawable.superscript) / 2;
this.canvas.text(drawable.superscript, offset, drawable.top);
}
this.canvas.setFontSize(this.fontsize); // Reset font
}
drawConnector(parent, child) {
if (this.triangles && child.is_leaf && child.label.includes(' ')) {
const text_width = this.canvas.textWidth(child.label);
this.canvas.triangle(
getDrawableCenter(parent), parent.top + this.fontsize + 2,
getDrawableCenter(child) + (text_width / 2) - 4, child.top - 3,
getDrawableCenter(child) - (text_width / 2) + 4, child.top - 3);
} else {
this.canvas.line(getDrawableCenter(parent),
parent.top + this.fontsize + 2, getDrawableCenter(child),
child.top - 3);
}
}
drawArrows(arrows) {
const arrow_color = this.nodecolor ? '#909' : '#999';
this.canvas.setFillStyle(arrow_color);
this.canvas.setStrokeStyle(arrow_color);
this.canvas.setLineWidth(2);
for (const arrow of arrows) {
this.canvas.curve(arrow.from_x, arrow.from_y, arrow.to_x, arrow.to_y,
arrow.from_x, arrow.bottom, arrow.to_x, arrow.bottom);
if (arrow.ends_to)
this.drawArrowHead(arrow.to_x, arrow.to_y);
if (arrow.ends_from)
this.drawArrowHead(arrow.from_x, arrow.from_y);
}
}
drawArrowHead(x, y) {
const cx = this.fontsize / 4;
const cy = this.fontsize / 2;
this.canvas.triangle(x, y, x - cx, y + cy, x + cx, y + cy, true);
}
setCanvas(c) { this.canvas = new Canvas(c); }
setColor(e) { this.nodecolor = e; }
setFont(f) { this.canvas.setFont(f); }
setFontsize(s) {
this.fontsize = parseInt(s, 10);
this.canvas.setFontSize(this.fontsize);
}
setTriangles(t) { this.triangles = t; }
setSubscript(s) { this.subscript = s; }
setAlignment(a) { this.alignment = a; }
setSpacing(s) { this.vscaler = s; }
download() { this.canvas.download('syntax_tree.png'); }
}
class Arrow {
constructor(from_x, from_y, to_x, to_y, bottom, ends_to, ends_from) {
this.from_x = from_x;
this.from_y = from_y;
this.to_x = to_x;
this.to_y = to_y;
this.bottom = bottom;
this.ends_to = ends_to;
this.ends_from = ends_from;
}
}
class ArrowSet {
constructor() {
this.arrows = [];
this.maxBottom = 0;
}
add(arrow) {
this.arrows.push(arrow);
this.maxBottom = Math.max(this.maxBottom, arrow.bottom);
}
concatenate(arrowSet) {
this.arrows = this.arrows.concat(arrowSet.arrows);
this.maxBottom = Math.max(this.maxBottom, arrowSet.maxBottom);
}
}
function drawableFromNode(canvas, node, depth = -1) {
const drawable = {
label : node.label,
subscript : node.subscript,
superscript : node.superscript,
width : getNodeWidth(canvas, node),
depth : depth,
is_leaf : node.type == Parser.NodeType.VALUE,
arrow : 'arrow' in node ? node.arrow : null,
children : []
};
if (node.type != Parser.NodeType.VALUE) {
node.values.forEach(child => {
drawable.children.push(drawableFromNode(canvas, child, depth + 1));
});
}
return drawable;
}
function getNodeWidth(canvas, node) {
let label_width = node.type != Parser.NodeType.ROOT
? canvas.textWidth(node.label) + NODE_PADDING
: 0;
if (node.subscript)
label_width += canvas.textWidth(node.subscript) * 3 / 4 * 2;
else if (node.superscript)
label_width += canvas.textWidth(node.superscript) * 3 / 4 * 2;
if (node.type != Parser.NodeType.VALUE) {
return Math.max(label_width, getChildWidth(canvas, node));
} else {
return label_width;
}
}
function calculateDrawablePositions(canvas, drawable, vscaler,
parent_offset = 0) {
let offset = 0;
let scale = 1;
let hasArrow = drawable.arrow;
if (drawable.depth >= 0) {
const child_width = getDrawableChildWidth(canvas, drawable);
if (drawable.width > child_width)
scale = drawable.width / child_width;
}
drawable.children.forEach(child => {
child.top =
child.depth * (canvas.fontsize * 3 * vscaler) + NODE_PADDING / 2;
child.left = offset + parent_offset;
child.width *= scale;
const child_has_arrow =
calculateDrawablePositions(canvas, child, vscaler, child.left);
if (child_has_arrow)
hasArrow = true;
offset += child.width;
});
return hasArrow;
}
function getChildWidth(canvas, node) {
if (node.type == Parser.NodeType.VALUE)
return 0;
let child_width = 0;
node.values.forEach(child => { child_width += getNodeWidth(canvas, child); });
return child_width;
}
function getDrawableChildWidth(canvas, drawable) {
if (drawable.children.length == 0)
return drawable.width;
let child_width = 0;
drawable.children.forEach(child => { child_width += child.width; });
return child_width;
}
function getMaxDepth(drawable) {
let max_depth = drawable.depth;
drawable.children.forEach(child => {
const child_depth = getMaxDepth(child);
if (child_depth > max_depth)
max_depth = child_depth;
});
return max_depth;
}
function moveLeafsToBottom(drawable, bottom) {
if (drawable.is_leaf)
drawable.depth = bottom;
drawable.children.forEach(child => moveLeafsToBottom(child, bottom));
}
function moveParentsDown(drawable) {
if (drawable.is_leaf)
return;
drawable.children.forEach(child => moveParentsDown(child));
if (drawable.depth != 0) {
let depth = 999999;
for (let child of drawable.children) {
if (child.depth - 1 < depth)
depth = child.depth - 1;
}
drawable.depth = depth;
}
}
function calculateAutoSubscript(drawables) {
const map = countNodes(drawables);
map.forEach((value, key, map) => {
if (value === 1)
map.delete(key);
});
assignSubscripts(drawables, Array.from(map.keys()), new Map());
}
function assignSubscripts(drawable, keys, tally) {
if (!drawable.is_leaf && !drawable.subscript && !drawable.superscript &&
keys.includes(drawable.label)) {
mapInc(tally, drawable.label);
drawable.subscript = '' + tally.get(drawable.label);
}
drawable.children.forEach(child => assignSubscripts(child, keys, tally));
}
function countNodes(drawable) {
let map = new Map();
if (drawable.is_leaf)
return map;
if (!drawable.subscript)
mapInc(map, drawable.label);
drawable.children.forEach(child => {
const child_map = countNodes(child);
map = mapMerge(map, child_map);
});
return map;
}
function findTarget(drawable, arrow_idx) {
const [count, target] = findTargetLeaf(drawable, arrow_idx, 0);
return target;
}
function findTargetLeaf(drawable, arrow_idx, count) {
if (drawable.is_leaf && (++count == arrow_idx))
return [ count, drawable ];
for (const child of drawable.children) {
let target = null;
[count, target] = findTargetLeaf(child, arrow_idx, count);
if (target != null)
return [ count, target ];
}
return [ count, null ];
}
function mapInc(map, key) {
if (!map.has(key))
map.set(key, 1);
else
map.set(key, map.get(key) + 1);
}
function mapMerge(one, two) {
two.forEach((value, key) => {
if (one.has(key))
one.set(key, one.get(key) + value);
else
one.set(key, value);
});
return one;
}
function getDrawableCenter(drawable) {
return drawable.left + drawable.width / 2;
}
function findMaxDepthBetween(drawable, left, right, max_y = 0) {
drawable.children.forEach(child => {
const child_low = findMaxDepthBetween(child, left, right, max_y);
max_y = Math.max(child_low, max_y);
});
if (drawable.is_leaf && drawable.left >= left && drawable.left <= right) {
max_y = Math.max(drawable.top, max_y);
}
return max_y;
}
function makeArrowSet(root, fontsize) {
return makeArrowSetOn(root, root, fontsize);
}
function makeArrowSetOn(root, drawable, fontsize) {
const arrowSet = new ArrowSet();
drawable.children.forEach(child => {
arrowSet.concatenate(makeArrowSetOn(root, child, fontsize));
});
if (!drawable.is_leaf || !drawable.arrow)
return arrowSet;
const target = findTarget(root, drawable.arrow.target);
if (!target)
return arrowSet;
const from = {
x : getDrawableCenter(drawable),
y : drawable.top + (fontsize * 1.2)
};
const to = {x : getDrawableCenter(target), y : target.top + (fontsize * 1.2)};
const bottom =
1.4 * findMaxDepthBetween(root, Math.min(drawable.left, target.left),
Math.max(drawable.left, target.left));
const ends_to = drawable.arrow.ends.to;
const ends_from = drawable.arrow.ends.from;
arrowSet.add(
new Arrow(from.x, from.y, to.x, to.y, bottom, ends_to, ends_from));
return arrowSet;
}