Repository navigation
Expand file tree
/
Copy pathast.js
More file actions
101 lines (98 loc) · 3.16 KB
/
Copy pathast.js
File metadata and controls
101 lines (98 loc) · 3.16 KB
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
import { build_automata, initSync } from "./pkg/automata.js";
function build_tree(ast) {
function structure(expression) {
let exp;
if((exp = expression.concatenation) != undefined) {
return {
text: { name: "." },
children: exp.map(structure),
};
} else if((exp = expression.union) != undefined) {
return {
text: { name: "|" },
children: exp.map(structure),
};
} else if((exp = expression.l) != undefined) {
return {
text: { name: exp.atom },
};
} else if((exp = expression.one_or_more) != undefined) {
return {
text: { name: '+' },
children: [structure(exp)],
};
} else if((exp = expression.zero_or_more) != undefined) {
return {
text: { name: '*' },
children: [structure(exp)],
};
} else if((exp = expression.optional) != undefined) {
return {
text: { name: '?' },
children: [structure(exp)],
};
} else if((exp = expression.group) != undefined) {
return structure(exp);
} else if(expression === "empty") {
return {
text: { name: "empty" },
children: []
};
} else if((exp = expression.any) != undefined || (exp = expression.anyBut) != undefined) {
console.log('any: ', exp);
return {
text: { name: expression.any != undefined ? "[]": '[^]' },
children: exp.map(x => {
if (x.atom != undefined) return { text: { name: x.atom } };
return { text: { name: `${x.range.start}-${x.range.end}` } };
})
};
}
}
return {
chart: { container: "#OrganiseChart-simple" },
nodeStructure: structure(ast)
};
}
var simple_chart_config = {
chart: {
container: "#OrganiseChart-simple",
},
nodeStructure: {
text: { name: "|" },
children: [
{
HTMLclass: "the-parent",
text: { name: "|" },
children: [
{ text: { name: "a" } },
{ text: { name: "b" } },
]
},
{
HTMLclass: "the-parent",
text: { name: "." },
children: [
{ text: { name: "a-z" } },
{ text: { name: "0-9" } },
]
}
]
}
};
export default function initAst() {
let input = document.getElementById('input');
let form = document.getElementById('form');
initSync();
function print_ast() {
let div = document.getElementById('OrganiseChart-simple');
div.innerHTML = "";
let automata = build_automata(input.value);
let result = build_tree(automata);
console.log('automata: ', automata);
console.log(result);
new Treant(result);
}
form.addEventListener('submit', print_ast);
print_ast();
}