forked from o0101/bepis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
165 lines (139 loc) · 3.85 KB
/
parser.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
const DEBUG = false;
export function w(code, ...slots) {
const root = buildTree(code, ...slots);
const rootElement = treeToDOM(root);
return point => {
say("Insert at", point, rootElement);
if ( point ) {
point.insertAdjacentElement('beforeEnd', rootElement);
}
return rootElement;
};
}
function buildTree(code, ...slots) {
code = Array.from(code).join("$");
say(code);
let slice = {tag: '', params: [], children: []};
const stack = [slice];
for( const char of code ) {
switch(char) {
case ' ':
case '\t':
case '\n':
case '\r': {
if ( ! slice.finished && slice.tag.length ) {
slice.finished = true;
say("Got", slice.tag);
}
}; break;
case '$': {
slice.params.push(slots.shift());
}; break;
case ',': {
slice.finished = true;
stack.push(slice);
say("Saved", slice.tag);
const newSlice = {tag: '', params: [], children: []};
slice.children.push(newSlice);
slice = newSlice;
}; break;
case '.': {
if ( slice.tag.length ) {
slice.finished = true;
// this can create an empty item that we remove after loop
const newSlice = {tag: '', params: [], children: []};
const oldSlice = stack.pop();
oldSlice.children.push(newSlice);
say("Reset to", oldSlice.tag);
stack.push(oldSlice);
slice = newSlice;
} else {
let oldSlice = stack.pop();
const idx = oldSlice.children.indexOf(slice);
oldSlice.children.splice(idx,1);
oldSlice = stack.pop();
oldSlice.children.push(slice);
say("Reset to", oldSlice.tag);
stack.push(oldSlice);
}
}; break;
default: {
if ( slice.finished ) {
const newSlice = {tag: '', params: [], children: []};
slice.children.push(newSlice);
slice = newSlice;
}
slice.tag += char;
}; break;
}
}
// there could be an empty item
if (! slice.tag.length ) {
const parent = stack[0];
if ( parent ) {
const idx = parent.children.indexOf(slice);
parent.children.splice(idx,1);
}
}
while ( stack.length ) {
slice = stack.pop();
}
return slice;
}
function treeToDOM(root) {
say("Root", root);
const stack = [root];
let parentElement;
while( stack.length ) {
const item = stack.pop();
if ( item instanceof Element ) {
if ( item.parentElement ) {
parentElement = item.parentElement;
} else break;
} else if ( item.tag.length ) {
const element = document.createElement(item.tag);
say("Making", element);
specify(element, ...item.params);
if ( parentElement ) {
parentElement.append(element);
}
parentElement = element;
stack.push(element);
if ( item.children.length ) {
stack.push(...item.children.reverse());
}
} else {
say("Empty item", item);
}
}
while( parentElement.parentElement ) {
parentElement = parentElement.parentElement;
}
say("Stack", stack, parentElement);
return parentElement;
}
function specify(element, content, style) {
// insert local content at stack top
if ( content == null || content == undefined ) {
// do nothing
} else if ( typeof content == "string" ) {
element.innerText = content;
} else if ( typeof content == "object" ) {
Object.keys(content).forEach(attrName => {
const attrValue = content[attrName];
try {
element.setAttribute(attrName, attrValue);
} catch(e) {}
try {
element[attrName] = attrValue;
} catch(e) {}
});
}
// apply style
Object.assign(element.style, style);
}
function say(...args) {
if ( DEBUG ) {
console.log(...args);
}
}