-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
165 lines (154 loc) · 5.45 KB
/
index.html
File metadata and controls
165 lines (154 loc) · 5.45 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
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
<!DOCTYPE html>
<html>
<head>
<title>Expression Parser/Evaluator</title>
<script src="https://cdn.tailwindcss.com"></script>
<script>
tailwind.config = {
theme: {
extend: {
colors: {
stone: {
50: '#f4f3f1',
100: '#e7e5e0',
200: '#d2cec4',
300: '#b6b1a0',
400: '#9b9580',
500: '#817b65',
600: '#635f4d',
700: '#4e4b3d',
800: '#403e34',
900: '#38372f',
950: '#1d1c16'
}
}
}
}
}
</script>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link
rel="stylesheet"
data-name="vs/editor/editor.main"
href="../node_modules/monaco-editor/min/vs/editor/editor.main.css"
/>
</head>
<body class="flex flex-col p-20 gap-8 bg-stone-900 text-stone-200">
<div class="grid lg:grid-cols-2 gap-8">
<div class="flex flex-col gap-8 mr-8">
<h1 class="text-2xl font-bold">Evaluation Context</h1>
<div id="context" style="width: 600px; height: 500px"></div>
</div>
<div class="flex flex-col gap-8">
<h1 class="text-2xl font-bold">Expression</h1>
<div id="expression" style="width: 600px; height: 500px"></div>
</div>
</div>
<h1 class="text-2xl font-bold">Result</h1>
<pre class="font-mono" id="result"></pre>
<script>
var require = {
paths: { vs: '../node_modules/monaco-editor/min/vs' }
}
</script>
<script src="../node_modules/monaco-editor/min/vs/loader.js"></script>
<script src="../node_modules/monaco-editor/min/vs/editor/editor.main.nls.js"></script>
<script src="../node_modules/monaco-editor/min/vs/editor/editor.main.js"></script>
<script type="module">
import parse from './src/index.js'
const result = document.getElementById('result')
const context = monaco.editor.create(
document.getElementById('context'),
{
value: `return {
context: {
a: 10, b: 15, c: "text"
},
functions: {
now: () => Date.now()
}
}`,
language: 'javascript',
theme: 'vs-dark',
minimap: {
enabled: false
}
}
)
monaco.languages.register({ id: 'formula-expression' })
monaco.languages.setMonarchTokensProvider('formula-expression', {
keywords: ['true', 'false'],
operators: [
':',
',',
'+',
'-',
'*',
'/',
'%',
'=',
'<',
'<=',
'>',
'>=',
'&',
'|'
],
tokenizer: {
root: [
[
/@?[a-zA-Z][\w$]*/,
{
cases: {
'@keywords': 'keyword',
'@default': 'variable'
}
}
],
[/\d+/, 'number'],
[/".*?"/, 'string']
]
}
})
const expression = monaco.editor.create(
document.getElementById('expression'),
{
value: `a + ( b + now() ) + " hello world"`,
language: 'formula-expression',
theme: 'vs-dark',
minimap: {
enabled: false
}
}
)
let context_data
const run = () => {
try {
const expression_text = expression.getValue()
context_data = new Function(context.getValue())()
const parsed = parse(expression_text)
result.innerText = JSON.stringify(
parsed.evaluate(
context_data.context,
context_data.functions
),
null,
2
)
} catch (e) {
result.innerText = e.message
}
}
run()
let timer
expression.getModel().onDidChangeContent(() => {
clearTimeout(timer)
timer = setTimeout(() => run(), 250)
})
context.getModel().onDidChangeContent(() => {
clearTimeout(timer)
timer = setTimeout(() => run(), 250)
})
</script>
</body>
</html>