-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.html
86 lines (68 loc) · 1.98 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script src="https://unpkg.com/[email protected]/treeify.js"></script>
<script src="./build/bundle.js"></script>
<pre id="tree"></pre>
<script>
function removeSpans(obj, seen = []) {
if (seen.includes(obj)) {
return;
}
seen.push(obj);
for (const prop in obj) {
if (obj[prop] instanceof syntek.Span) {
delete obj[prop];
} else if (typeof obj[prop] === 'object') {
removeSpans(obj[prop], seen);
}
}
}
function typeToName(obj, seen = []) {
if (seen.includes(obj)) {
return;
}
seen.push(obj);
for (const prop in obj) {
if (prop === 'type') {
if (obj instanceof syntek.Node) {
obj[prop] = syntek.SyntacticToken[obj[prop]];
} else if (obj instanceof syntek.Token) {
obj[prop] = syntek.LexicalToken[obj[prop]];
}
}
if (typeof obj[prop] === 'object') {
typeToName(obj[prop], seen);
}
}
}
function deepClone(obj) {
const clone = Object.assign(Object.create(Object.getPrototypeOf(obj)), obj);
for (const prop in clone) {
if (clone[prop] && typeof clone[prop] === 'object') {
clone[prop] = deepClone(clone[prop]);
}
}
return clone;
}
let ast;
let clone;
// Prevent this code from also throwing an error if the compiler throws an error
if (typeof syntek !== 'undefined') {
// Shorthand to the AST variable
ast = syntek.parseResult.ast;
// Clone and display the AST
clone = deepClone(ast);
typeToName(clone);
removeSpans(clone);
document.getElementById('tree').innerHTML = treeify.asTree(clone, true);
}
</script>
</body>
</html>