-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathmy-worker.js
More file actions
125 lines (118 loc) · 4.45 KB
/
my-worker.js
File metadata and controls
125 lines (118 loc) · 4.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
if (!importScripts) {
var importScripts = (function (globalEval) {
var xhr = new XMLHttpRequest;
return function importScripts() {
var
args = Array.prototype.slice.call(arguments)
, len = args.length
, i = 0
, meta
, data
, content
;
for (; i < len; i++) {
if (args[i].substr(0, 5).toLowerCase() === "data:") {
data = args[i];
content = data.indexOf(",");
meta = data.substr(5, content).toLowerCase();
data = decodeURIComponent(data.substr(content + 1));
if (/;\s*base64\s*[;,]/.test(meta)) {
data = atob(data);
}
if (/;\s*charset=[uU][tT][fF]-?8\s*[;,]/.test(meta)) {
data = decodeURIComponent(escape(data));
}
} else {
xhr.open("GET", location.origin + "/_/inputControl" + args[i], false);
xhr.send(null);
data = xhr.responseText;
}
globalEval(data);
}
};
}(eval));
}
importScripts("/ace-ext/worker.js");
importScripts("/ace/ace.js");
importScripts("/ace-ext/mirror.js");
importScripts("/toolgood.algorithm.js");
ace.define('ace/worker/my-worker', ["require", "exports", "module", "ace/lib/oop", "ace/worker/mirror"], function (require, exports, module) {
"use strict";
var oop = require("ace/lib/oop");
var Mirror = require("ace/worker/mirror").Mirror;
var MyWorker = function (sender) {
Mirror.call(this, sender);
this.setTimeout(500);
this.$dialect = null;
};
oop.inherits(MyWorker, Mirror);
antlr4 = algorithm.antlr4;
// class for gathering errors and posting them to ACE editor
var AnnotatingErrorListener = function (annotations) {
// antlr4.error.ErrorListener.call(this);
this.annotations = annotations;
return this;
};
AnnotatingErrorListener.prototype = Object.create(antlr4.error.ErrorListener.prototype);
AnnotatingErrorListener.prototype.constructor = AnnotatingErrorListener;
AnnotatingErrorListener.prototype.syntaxError = function (recognizer, offendingSymbol, line, column, msg, e) {
this.annotations.push({
row: line - 1,
column: column,
text: msg,
type: "error"
});
};
function validate(input) {
function StandardChar(o) {
if (o == '‘') return '\'';
if (o == '’') return '\'';
if (o == '“') return '"';
if (o == '”') return '"';
if (o == '〔') return '(';
if (o == '〕') return ')';
if (o == '=') return '=';
if (o == '+') return '+';
if (o == '-') return '-';
if (o == '×') return '*';
if (o == '÷') return '/';
if (o == '/') return '/';
if (o == ',') return ',';
let c = o.charAt(0);
if (c == 12288) {
o = String.fromCharCode(32)
} else if (c > 65280 && c < 65375) {
o = String.fromCharCode(c - 65248)
}
return o.toUpperCase();
}
try {
var str = "";
for (let i = 0; i < input.length; i++) {
let char = input.charAt(i);
let normalized_char = StandardChar(char);
str += normalized_char;
}
var stream = new antlr4.InputStream(str);
var lexer = new algorithm.mathLexer(stream);
var tokens = new antlr4.CommonTokenStream(lexer);
var parser = new algorithm.mathParser(tokens);
var annotations = [];
var listener = new AnnotatingErrorListener(annotations);
parser.removeErrorListeners();
parser.addErrorListener(listener);
parser.prog();
return annotations;
} catch (e) {
return [{ column: 0, row: 0, text: e.message, type: "error" }];
}
}
(function () {
this.onUpdate = function () {
var value = this.doc.getValue();
var annotations = validate(value);
this.sender.emit("annotate", annotations);
};
}).call(MyWorker.prototype);
exports.MyWorker = MyWorker;
});