forked from gAmUssA/ksql-antlr4-ace-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
my-worker.js
89 lines (75 loc) · 3.57 KB
/
my-worker.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
importScripts("bower_components/ace-worker/worker.js");
importScripts("bower_components/ace-builds/src-noconflict/ace.js");
importScripts("bower_components/ace-worker/mirror.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(200);
this.$dialect = null;
};
oop.inherits(MyWorker, Mirror);
// load nodejs compatible require
var ace_require = require;
window.require = undefined; // prevent error: "Honey: 'require' already defined in
// global scope"
var Honey = {'requirePath': ['..']}; // walk up to js folder, see Honey docs
importScripts("./require.js");
var antlr4_require = window.require;
window.require = require = ace_require;
// load antlr4 and myLanguage
var antlr4, SqlBaseLexer, SqlBaseParser;
try {
window.require = antlr4_require;
antlr4 = antlr4_require('antlr4/index');
SqlBaseLexer = antlr4_require('./parser/SqlBaseLexer').SqlBaseLexer;
SqlBaseParser = antlr4_require('./parser/SqlBaseParser').SqlBaseParser;
} finally {
window.require = ace_require;
}
// 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"
});
};
var validate = function (input) {
var stream = new antlr4.InputStream(input);
var lexer = new SqlBaseLexer(stream);
var tokens = new antlr4.CommonTokenStream(lexer);
var parser = new SqlBaseParser(tokens);
var annotations = [];
var listener = new AnnotatingErrorListener(annotations);
parser.removeErrorListeners();
parser.addErrorListener(listener);
parser.statement();
return annotations;
};
(function () {
this.onUpdate = function () {
var value = this.doc.getValue();
var annotations = validate(value);
this.sender.emit("annotate", annotations);
};
}).call(MyWorker.prototype);
exports.MyWorker = MyWorker;
});
var validate = function (input) {
return [{row: 0, column: 0, text: "MyMode says Hello!", type: "error"}];
};