forked from sguise/BlockMirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestWorkingAntlr.html
512 lines (481 loc) · 13.8 KB
/
TestWorkingAntlr.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<html>
<head>
<script src="../node_modules/codemirror/lib/codemirror.js"></script>
<link rel="stylesheet" href="../node_modules/codemirror/lib/codemirror.css">
<script src="../node_modules/codemirror/mode/css/css.js"></script>
<script src="../node_modules/codemirror/mode/python/python.js"></script>
<script src="../node_modules/codemirror/addon/hint/show-hint.js"></script>
<script src="../node_modules/codemirror/addon/hint/css-hint.js"></script>
<script src="../node_modules/blockly/blockly_compressed.js"></script>
<script src="../node_modules/blockly/python_compressed.js"></script>
<script src="../node_modules/blockly/blocks_compressed.js"></script>
<script src="../node_modules/blockly/msg/js/en.js"></script>
<link rel="stylesheet" href="../node_modules/codemirror/addon/hint/show-hint.css">
<link rel="stylesheet" href="../node_modules/codemirror/theme/ambiance.css">
<script src="build/python_bundle.js"></script>
<style>
body {
background-color: #eee;
}
.CodeMirror {
height: auto;
}
</style>
</head>
<table style="height: 100%; width: 100%">
<tbody>
<tr>
<td id="blocklyArea" style="height: 99%; background: #fc9; text-align: center"></td>
<td id="editorArea" style="height: 99%; background: #fc9; text-align: center"></td>
</tr>
</tbody>
</table>
<div id="blocklyDiv" style="position: absolute"></div>
<div id="editorDiv" style="position: absolute"></div>
<xml id="toolbox" style="display: none">
<category name="Logic" colour="%{BKY_LOGIC_HUE}">
<block type="controls_if"></block>
<block type="logic_compare"></block>
<block type="logic_operation"></block>
<block type="logic_negate"></block>
<block type="logic_boolean"></block>
<block type="logic_null"></block>
<block type="logic_ternary"></block>
</category>
<category name="Loops" colour="%{BKY_LOOPS_HUE}">
<block type="controls_repeat_ext">
<value name="TIMES">
<shadow type="math_number">
<field name="NUM">10</field>
</shadow>
</value>
</block>
<block type="controls_whileUntil"></block>
<block type="controls_for">
<value name="FROM">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
<value name="TO">
<shadow type="math_number">
<field name="NUM">10</field>
</shadow>
</value>
<value name="BY">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
</block>
<block type="controls_forEach"></block>
<block type="controls_flow_statements"></block>
</category>
<category name="Math" colour="%{BKY_MATH_HUE}">
<block type="math_number">
<field name="NUM">123</field>
</block>
<block type="math_arithmetic">
<value name="A">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
<value name="B">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
</block>
<block type="math_single">
<value name="NUM">
<shadow type="math_number">
<field name="NUM">9</field>
</shadow>
</value>
</block>
<block type="math_trig">
<value name="NUM">
<shadow type="math_number">
<field name="NUM">45</field>
</shadow>
</value>
</block>
<block type="math_constant"></block>
<block type="math_number_property">
<value name="NUMBER_TO_CHECK">
<shadow type="math_number">
<field name="NUM">0</field>
</shadow>
</value>
</block>
<block type="math_round">
<value name="NUM">
<shadow type="math_number">
<field name="NUM">3.1</field>
</shadow>
</value>
</block>
<block type="math_on_list"></block>
<block type="math_modulo">
<value name="DIVIDEND">
<shadow type="math_number">
<field name="NUM">64</field>
</shadow>
</value>
<value name="DIVISOR">
<shadow type="math_number">
<field name="NUM">10</field>
</shadow>
</value>
</block>
<block type="math_constrain">
<value name="VALUE">
<shadow type="math_number">
<field name="NUM">50</field>
</shadow>
</value>
<value name="LOW">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
<value name="HIGH">
<shadow type="math_number">
<field name="NUM">100</field>
</shadow>
</value>
</block>
<block type="math_random_int">
<value name="FROM">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
<value name="TO">
<shadow type="math_number">
<field name="NUM">100</field>
</shadow>
</value>
</block>
<block type="math_random_float"></block>
<block type="math_atan2">
<value name="X">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
<value name="Y">
<shadow type="math_number">
<field name="NUM">1</field>
</shadow>
</value>
</block>
</category>
<category name="Text" colour="%{BKY_TEXTS_HUE}">
<block type="text"></block>
<block type="text_join"></block>
<block type="text_append">
<value name="TEXT">
<shadow type="text"></shadow>
</value>
</block>
<block type="text_length">
<value name="VALUE">
<shadow type="text">
<field name="TEXT">abc</field>
</shadow>
</value>
</block>
<block type="text_isEmpty">
<value name="VALUE">
<shadow type="text">
<field name="TEXT"></field>
</shadow>
</value>
</block>
<block type="text_indexOf">
<value name="VALUE">
<block type="variables_get">
<field name="VAR">{textVariable}</field>
</block>
</value>
<value name="FIND">
<shadow type="text">
<field name="TEXT">abc</field>
</shadow>
</value>
</block>
<block type="text_charAt">
<value name="VALUE">
<block type="variables_get">
<field name="VAR">{textVariable}</field>
</block>
</value>
</block>
<block type="text_getSubstring">
<value name="STRING">
<block type="variables_get">
<field name="VAR">{textVariable}</field>
</block>
</value>
</block>
<block type="text_changeCase">
<value name="TEXT">
<shadow type="text">
<field name="TEXT">abc</field>
</shadow>
</value>
</block>
<block type="text_trim">
<value name="TEXT">
<shadow type="text">
<field name="TEXT">abc</field>
</shadow>
</value>
</block>
<block type="text_print">
<value name="TEXT">
<shadow type="text">
<field name="TEXT">abc</field>
</shadow>
</value>
</block>
<block type="text_prompt_ext">
<value name="TEXT">
<shadow type="text">
<field name="TEXT">abc</field>
</shadow>
</value>
</block>
</category>
<category name="Lists" colour="%{BKY_LISTS_HUE}">
<block type="lists_create_with">
<mutation items="0"></mutation>
</block>
<block type="lists_create_with"></block>
<block type="lists_repeat">
<value name="NUM">
<shadow type="math_number">
<field name="NUM">5</field>
</shadow>
</value>
</block>
<block type="lists_length"></block>
<block type="lists_isEmpty"></block>
<block type="lists_indexOf">
<value name="VALUE">
<block type="variables_get">
<field name="VAR">{listVariable}</field>
</block>
</value>
</block>
<block type="lists_getIndex">
<value name="VALUE">
<block type="variables_get">
<field name="VAR">{listVariable}</field>
</block>
</value>
</block>
<block type="lists_setIndex">
<value name="LIST">
<block type="variables_get">
<field name="VAR">{listVariable}</field>
</block>
</value>
</block>
<block type="lists_getSublist">
<value name="LIST">
<block type="variables_get">
<field name="VAR">{listVariable}</field>
</block>
</value>
</block>
<block type="lists_split">
<value name="DELIM">
<shadow type="text">
<field name="TEXT">,</field>
</shadow>
</value>
</block>
<block type="lists_sort"></block>
</category>
<category name="Colour" colour="%{BKY_COLOUR_HUE}">
<block type="colour_picker"></block>
<block type="colour_random"></block>
<block type="colour_rgb">
<value name="RED">
<shadow type="math_number">
<field name="NUM">100</field>
</shadow>
</value>
<value name="GREEN">
<shadow type="math_number">
<field name="NUM">50</field>
</shadow>
</value>
<value name="BLUE">
<shadow type="math_number">
<field name="NUM">0</field>
</shadow>
</value>
</block>
<block type="colour_blend">
<value name="COLOUR1">
<shadow type="colour_picker">
<field name="COLOUR">#ff0000</field>
</shadow>
</value>
<value name="COLOUR2">
<shadow type="colour_picker">
<field name="COLOUR">#3333ff</field>
</shadow>
</value>
<value name="RATIO">
<shadow type="math_number">
<field name="NUM">0.5</field>
</shadow>
</value>
</block>
</category>
<sep></sep>
<category name="Variables" colour="%{BKY_VARIABLES_HUE}" custom="VARIABLE"></category>
<category name="Functions" colour="%{BKY_PROCEDURES_HUE}" custom="PROCEDURE"></category>
</xml>
<script>
var blocklyArea = document.getElementById('blocklyArea');
var blocklyDiv = document.getElementById('blocklyDiv');
var dragLoc = 1;
var updating = 2;
// References the Blockly workspace area
var workspace = Blockly.inject(blocklyDiv,
{ toolbox: document.getElementById('toolbox') });
// Resizes the Blockly area if the window changes size
var onresize = function (e) {
// Compute the absolute coordinates and dimensions of blocklyArea.
var element = blocklyArea;
var x = 0;
var y = 0;
do {
x += element.offsetLeft;
y += element.offsetTop;
element = element.offsetParent;
} while (element);
// Position blocklyDiv over blocklyArea.
blocklyDiv.style.left = x + 'px';
blocklyDiv.style.top = y + 'px';
blocklyDiv.style.width = (blocklyArea.offsetWidth + 3) * dragLoc + 'px';
blocklyDiv.style.height = blocklyArea.offsetHeight + 'px';
Blockly.svgResize(workspace);
};
window.addEventListener('resize', onresize, false);
onresize();
Blockly.svgResize(workspace);
// Resizes the CodeMirror area if the window changes size
var editorArea = document.getElementById('editorArea');
var editorDiv = document.getElementById('editorDiv');
var dividerX;
var editonresize = function (e) {
// Compute the absolute coordinates and dimensions of blocklyArea.
var editelement = editorArea;
var editx = 0;
var edity = 0;
var xshift = editelement.offsetLeft * dragLoc;
editx += xshift;
edity += editelement.offsetTop;
editelement = editelement.offsetParent;
do {
editx += editelement.offsetLeft;
edity += editelement.offsetTop;
editelement = editelement.offsetParent;
} while (editelement);
// Position editorDiv over editorArea.
dividerX = editx;
editorDiv.style.left = editx + 'px';
editorDiv.style.top = edity + 'px';
editorDiv.style.width = window.innerWidth - xshift + 'px';
editorDiv.style.height = editorArea.offsetHeight + 'px';
};
window.addEventListener('resize', editonresize, false);
editonresize();
// The function converts the workspace blocks into Python code, store in variable 'code'
var code = Blockly.Python.workspaceToCode(workspace);
// Displays the Python code in the CodeMirror text editor
var editor = CodeMirror(document.getElementById("editorDiv"), {
value: code,
mode: "python",
tabSize: 5,
lineNumbers: true,
firstLineNumber: 1,
viewportMargin: Infinity,
extraKeys: { "Ctrl-Space": "autocomplete" }
});
// Function for resizing Blockly and CodeMirror areas with the draggable bar down the center
var isClicked;
editorDiv.onmousedown = function (e) {
if (e.clientX - dividerX < 30) {
isClicked = true;
}
}
onmouseup = function (e) {
isClicked = false;
}
onmousemove = function (e) {
var margin_error = 2.03;
if (isClicked) {
dragLoc = (e.clientX - 30) / window.innerWidth * margin_error;
onresize();
editonresize();
}
}
window.addEventListener('moveDivider', onmousemove, false);
editor.on('change', function (editor) {
if (updating == 0) {
updating = 1;
// Adds a new block to the workspace.
// "controls_if" can be replaced by any other block name found in \node_modules\blockly\blocks
console.log(editor.getValue());
/* Antlr, invoking the parser
* InputStream - Vacuum all input from a Reader/InputStream and then treat it like a char[] buffer. Can also pass in a String or char[] to use.
* Lexer — Recognizer that draws input symbols from a character stream, and performs lexical analysis.
* Token — Sequence of characters in the source program that matches the pattern for a token and is identified by the lexical analyser as an instance of that token.
* Parser — An element of a compiler that takes input data and builds a parse tree.
* Parse tree — an tree that produced by parser, which is ordered and rooted . It reflects the syntax of the input and contains all relationships between elements.
*/
editor.getDoc().setValue(code);
const chars = new window.antlr4.InputStream(editor.getValue());
const lexer = new window.Python3Lexer.Python3Lexer(chars);
lexer.strictMode = false; // do not use strictMode
const tokens = new window.antlr4.CommonTokenStream(lexer);
const parser = new window.Python3Parser.Python3Parser(tokens);
try {
tree = parser.file_input();
console.log("THIS IS THE TREE: ", tree.toStringTree(this, parser));
} catch (error) {
console.log(error);
}
var bl = workspace.newBlock("controls_if");
//Display new block visually on the Blocky area
bl.initSvg();
bl.render();
}
else {
updating = 0;
}
});
//Update function to output the parse tree after the blockly workspace has changed
function myUpdateFunction(event) {
if (updating == 0) {
updating = 1;
code = Blockly.Python.workspaceToCode(workspace);
}
else if (updating == 2) {
updating = 0;
}
else {
updating = 0;
}
}
workspace.addChangeListener(myUpdateFunction);
</script>
</html>