-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
177 lines (159 loc) · 4.21 KB
/
index.js
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
require('@construit/web');
const ScriptBox = require('@construit/scriptbox');
const esprima = require('esprima');
function ajax(options) {
var xhr = new XMLHttpRequest();
//xhr.withCredentials = true;
xhr.open(options.type.toUpperCase(), options.url);
//xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onload = function() {
if (xhr.status === 200 || xhr.status == 0) {
var data;
if (xhr.responseText.charAt(0) == "{" || xhr.responseText.charAt(0) == "[") data = JSON.parse(xhr.responseText);
else data = xhr.responseText;
options.success(data);
} else {
options.error(xhr, xhr.status, xhr.responseText);
}
};
xhr.send((options.data) ? JSON.stringify(options.data) : undefined);
}
function jsparser(src) {
var errors = [];
var ast;
// Filter out markdown comments
src = src.split("\n").map(function(x) {
if (x.startsWith("#")) return "";
else return x;
}); //.join("\n");
var lines = [];
lines[0] = 0;
for (var i=1; i<src.length; i++) {
lines[i] = lines[i-1] + src[i-1].length + 1;
}
var curline = 0;
src = src.join("\n");
var statements = {};
try {
ast = esprima.parse(src, {range: true});
//console.log(ast);
var lastend = 0;
for (var i=0; i<ast.body.length; i++) {
while (lines.length > curline+1 && ast.body[i].range[0] >= lines[curline+1]) {
curline++;
}
statements[curline] = ast.body[i];
if (ast.body[i].range[0]+1 > lastend) {
var dum = {type: "dummy", lines: src.substring(lastend, ast.body[i].range[0]).split("\n").length-1, getNumberOfLines: function() { return this.lines; }};
ast.body.splice(i, 0, dum);
i++;
}
lastend = ast.body[i].range[1]+1;
ast.body[i].source = src.substring(ast.body[i].range[0], ast.body[i].range[1]+1);
ast.body[i].getNumberOfLines = function() { return this.source.split("\n").length-1; };
}
console.log("STATEMENTS",statements);
} catch(e) {
console.log(e);
errors.push(e);
e.line = e.lineNumber;
e.messageText = function() { return e.description; };
}
return {
ast: ast,
errors: errors,
hasErrors: function() { return (errors.length > 0); },
getError: function() { return errors[0]; },
source: src,
statements: ast.body,
getStatementByLine: function(line) { console.log("GET BY LINE",line); return statements[line]; }
}
}
function jstranslator(ast) {
eval(ast.source);
}
var cwebload = window.onload;
window.onload = function() {
cwebload();
var input = document.getElementById("emtextin");
var inputjs = document.getElementById("jstextin");
rt = Construit.webrt;
var embox = new ScriptBox(input, rt);
var jsbox = new ScriptBox(inputjs, rt, {
parser: {parse: jsparser},
translator: { eval: jstranslator },
keywords: {
"do": "do",
"if": "if",
"in": "in",
"for": "for",
"let": "let",
"new": "new",
"try": "try",
"var": "var",
"case": "case",
"else": "else",
"enum": "enum",
"this": "this",
"with": "with",
"await": "await",
"break": "break",
"catch": "catch",
"class": "class",
"const": "const",
"super": "super",
"throw": "throw",
"while": "while",
"yield": "yield",
"delete": "delete",
"export": "export",
"import": "import",
"public": "public",
"return": "return",
"static": "static",
"switch": "switch",
"typeof": "typeof",
"default": "default",
"extends": "extends",
"finally": "finally",
"package": "package",
"private": "private",
"continue": "continue",
"debugger": "debugger",
"function": "function",
"interface": "interface",
"protected": "protected",
"prototype": "prototype",
"implements": "implements",
"instanceof": "instanceof"
}
});
var jsoptions = document.getElementById("jsoptions");
jsoptions.onchange = function(e) {
ajax({
url: jsoptions.value,
type: "get",
success: function(data){
jsbox.intextarea.value = data;
jsbox.updateEntireHighlight();
},
error: function(a){
}
});
}
jsoptions.onchange();
var emoptions = document.getElementById("emoptions");
emoptions.onchange = function(e) {
ajax({
url: emoptions.value,
type: "get",
success: function(data){
embox.intextarea.value = data;
embox.updateEntireHighlight();
},
error: function(a){
}
});
}
emoptions.onchange();
}