generated from exercism/generic-codemirror-lang
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.html
240 lines (198 loc) · 4.5 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>JikiScript playground</title>
</head>
<body>
<div id="editor"></div>
<script type="module">
import { EditorView, basicSetup } from "codemirror";
import { jikiscript, jikiscriptStrict} from "./src/index.ts";
import { colorScheme } from "./src/create-theme/colorScheme";
import { syntaxTree } from "@codemirror/language";
const doc = `
// Instantiation
log new Foobar()
log new Foobar(1, true, ["a"])
// A comment
log 10 // Postfix comment
// Numbers
log 5
log 0
log 3.2
log -5
log -0
log -3.2
// Boolean
log true
log false
// Strings
log ""
log "Hello, world!"
log "Hello,\\sworld!"
log "Hello, \\"world\\"!"
// String Concatenation
log "Hello, " + "world!"
// parenthesis matching
log 5 + 3
log (5 + 3) * 2
log ((5 + 3))
log max( (e / f), 5 )
log max( (e / f), (g - h) )
log ( (a + b) * (5) )
log (( (a + b) * (c - d) ) + 5 )
log 2 * (a + b) * (c - d)
log 2 * ( (a + b) * (c - d) )
log 2 * (( (a + b) * (c - d) ) + 5 )
log 2 * (( (a + b) * (c - d) ) + max())
log 2 * (( (a + b) * (c - d) ) + max( (e / f), (g - h) ))
// Comparisons
log 5 > 3
log 5 < 3
log 5 >= 3
log 5 <= 3
log 5 == 3
log 5 != 3
// Logical operators
log true and false
log true or false
log not true
log !false
log not (5 > 3)
log !(3 == 2)
// Function calls
something()
something(1, true, "foo")
// Variable setting
set x to 5
change x to 3
set something1234 to "Hello, world!"
// Repeat block
repeat 10 times do
something()
end
repeat 10 times indexed by idx do
something()
next
break
continue
end
// Dictionaries
set x to { "foo": 1 }
set x to {
"foo": 1,
"bar": "bar",
"baz": true,
"qux": [1, 2, 3],
"quux": {"a": 1, "b": 2, "c": 3}
}
for each key, value in {"a": 1} do
something()
end
log {"a": 1, "b": 2, "c": 3}["a"]["b"]
// List things
log []
log [1, "foo", true]
set list to [1, "foo", true]
change list[4] to 3
log list[4]
for each item in [1, "a", true] do
something()
end
for each item in [1, "a", true] indexed by idx do
something()
end
// Repeat_until_game_over block
repeat_until_game_over do
something()
end
repeat_until_game_over indexed by idx do
something()
end
// Repeat_forever block
repeat_forever do
something()
end
// Repeat_until_game_over block
repeat_forever indexed by idx do
something()
end
// If statement
if something is true do
something()
end
// Else statement
if something is true do
else do
something()
end
// Else if statement
if something is true do
else if something is false do
something()
end
// Function definitions
function foobar do
return "foobar"
end
function has_underscore do
return something_with_underscore
end
function foobar with arg1 do
return 10
end
function foobar with arg1, arg2 do
return (10 + 20)
end
`;
const view = new EditorView({
doc: doc,
extensions: [
basicSetup,
jikiscript(),
colorScheme,
EditorView.updateListener.of((update) => {
if (update.docChanged) {
try {
const strictTree = jikiscriptStrict.parser.parse(
update.state.doc.toString()
);
console.log("Strict parsing successful:", strictTree);
} catch (error) {
console.error("Strict parsing error:", error);
}
// logSyntaxTree(view);
// printSyntaxTree(update.state);
// console.log(syntaxTree(update.state));
}
}),
],
parent: document.querySelector("#editor"),
});
function posToLineCol(state, pos) {
const line = state.doc.lineAt(pos);
return `L${line.number}:C${pos - line.from + 1}`;
}
function printSyntaxTree(state) {
const tree = syntaxTree(state);
let output = "";
tree.cursor().iterate((node) => {
const indent = " ".repeat(node.depth);
const from = posToLineCol(state, node.from);
const to = posToLineCol(state, node.to);
output += `${indent}- ${node.name} [${from} -> ${to}]\n`;
});
console.log(output);
}
function logSyntaxTree(view) {
const cursor = syntaxTree(view.state).cursor();
do {
console.log("Node:", cursor.name, "from:", cursor.from, "to:", cursor.to);
} while (cursor.next());
}
// logSyntaxTree(view);
</script>
</body>
</html>