-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
token_tree.js
92 lines (77 loc) · 1.75 KB
/
token_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
export default class TokenTree {
constructor() {
this.rootNode = { root: true, children: [] };
this.stack = [ this.rootNode ];
}
get top() {
return this.stack[this.stack.length - 1];
}
add(node) {
this.top.children.push(node);
}
addKeyword(text, kind) {
if (text === "") { return; }
this.top.children.push({
kind,
children: [ text ]
});
}
addText(text) {
if (text === "") { return; }
this.add(text);
}
addSublanguage({rootNode}, name) {
let node = rootNode;
delete node.root; // no longer a root node
node.kind = name;
node.sublanguage = true;
this.add(node);
}
openNode(kind) {
var node = { kind, children: [] };
this.add(node);
this.stack.push(node);
}
closeNode() {
if (this.stack.length > 1)
this.stack.pop();
}
closeAllNodes() {
while (this.stack.length > 1)
this.closeNode();
}
toJSON() {
return JSON.stringify(this.rootNode, null, 4);
}
finalize() {
return;
TokenTree._collapse(this.rootNode);
}
walk(builder) {
return TokenTree._walk(builder, this.rootNode);
}
static _walk(builder, node) {
if (typeof node === "string") {
builder.addText(node);
} else if (node.children) {
builder.openNode(node);
node.children.forEach((child) => this._walk(builder, child))
builder.closeNode(node);
}
return builder;
}
static _collapse(node) {
if (!node.children) {
return
}
if (node.children.every(el => typeof el === "string")) {
node.text = node.children.join("")
delete node["children"]
} else {
node.children.forEach((child) => {
if (typeof child === "string") return;
TokenTree._collapse(child)
})
}
}
}