Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
malwaredllc
GitHub Repository: malwaredllc/byob
Path: blob/master/web-gui/buildyourownbotnet/assets/js/codemirror/addon/runmode/runmode.node.js
1293 views
1
/* Just enough of CodeMirror to run runMode under node.js */
2
3
function splitLines(string){ return string.split(/\r?\n|\r/); };
4
5
function StringStream(string) {
6
this.pos = this.start = 0;
7
this.string = string;
8
this.lineStart = 0;
9
}
10
StringStream.prototype = {
11
eol: function() {return this.pos >= this.string.length;},
12
sol: function() {return this.pos == 0;},
13
peek: function() {return this.string.charAt(this.pos) || null;},
14
next: function() {
15
if (this.pos < this.string.length)
16
return this.string.charAt(this.pos++);
17
},
18
eat: function(match) {
19
var ch = this.string.charAt(this.pos);
20
if (typeof match == "string") var ok = ch == match;
21
else var ok = ch && (match.test ? match.test(ch) : match(ch));
22
if (ok) {++this.pos; return ch;}
23
},
24
eatWhile: function(match) {
25
var start = this.pos;
26
while (this.eat(match)){}
27
return this.pos > start;
28
},
29
eatSpace: function() {
30
var start = this.pos;
31
while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
32
return this.pos > start;
33
},
34
skipToEnd: function() {this.pos = this.string.length;},
35
skipTo: function(ch) {
36
var found = this.string.indexOf(ch, this.pos);
37
if (found > -1) {this.pos = found; return true;}
38
},
39
backUp: function(n) {this.pos -= n;},
40
column: function() {return this.start - this.lineStart;},
41
indentation: function() {return 0;},
42
match: function(pattern, consume, caseInsensitive) {
43
if (typeof pattern == "string") {
44
var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
45
var substr = this.string.substr(this.pos, pattern.length);
46
if (cased(substr) == cased(pattern)) {
47
if (consume !== false) this.pos += pattern.length;
48
return true;
49
}
50
} else {
51
var match = this.string.slice(this.pos).match(pattern);
52
if (match && match.index > 0) return null;
53
if (match && consume !== false) this.pos += match[0].length;
54
return match;
55
}
56
},
57
current: function(){return this.string.slice(this.start, this.pos);},
58
hideFirstChars: function(n, inner) {
59
this.lineStart += n;
60
try { return inner(); }
61
finally { this.lineStart -= n; }
62
}
63
};
64
exports.StringStream = StringStream;
65
66
exports.startState = function(mode, a1, a2) {
67
return mode.startState ? mode.startState(a1, a2) : true;
68
};
69
70
var modes = exports.modes = {}, mimeModes = exports.mimeModes = {};
71
exports.defineMode = function(name, mode) {
72
if (arguments.length > 2) {
73
mode.dependencies = [];
74
for (var i = 2; i < arguments.length; ++i) mode.dependencies.push(arguments[i]);
75
}
76
modes[name] = mode;
77
};
78
exports.defineMIME = function(mime, spec) { mimeModes[mime] = spec; };
79
80
exports.defineMode("null", function() {
81
return {token: function(stream) {stream.skipToEnd();}};
82
});
83
exports.defineMIME("text/plain", "null");
84
85
exports.resolveMode = function(spec) {
86
if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
87
spec = mimeModes[spec];
88
} else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
89
spec = mimeModes[spec.name];
90
}
91
if (typeof spec == "string") return {name: spec};
92
else return spec || {name: "null"};
93
};
94
exports.getMode = function(options, spec) {
95
spec = exports.resolveMode(mimeModes[spec]);
96
var mfactory = modes[spec.name];
97
if (!mfactory) throw new Error("Unknown mode: " + spec);
98
return mfactory(options, spec);
99
};
100
exports.registerHelper = exports.registerGlobalHelper = Math.min;
101
102
exports.runMode = function(string, modespec, callback) {
103
var mode = exports.getMode({indentUnit: 2}, modespec);
104
var lines = splitLines(string), state = (options && options.state) || exports.startState(mode);
105
for (var i = 0, e = lines.length; i < e; ++i) {
106
if (i) callback("\n");
107
var stream = new exports.StringStream(lines[i]);
108
while (!stream.eol()) {
109
var style = mode.token(stream, state);
110
callback(stream.current(), style, i, stream.start, state);
111
stream.start = stream.pos;
112
}
113
}
114
};
115
116