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-standalone.js
1293 views
1
/* Just enough of CodeMirror to run runMode under node.js */
2
3
window.CodeMirror = {};
4
5
(function() {
6
"use strict";
7
8
function splitLines(string){ return string.split(/\r?\n|\r/); };
9
10
function StringStream(string) {
11
this.pos = this.start = 0;
12
this.string = string;
13
this.lineStart = 0;
14
}
15
StringStream.prototype = {
16
eol: function() {return this.pos >= this.string.length;},
17
sol: function() {return this.pos == 0;},
18
peek: function() {return this.string.charAt(this.pos) || null;},
19
next: function() {
20
if (this.pos < this.string.length)
21
return this.string.charAt(this.pos++);
22
},
23
eat: function(match) {
24
var ch = this.string.charAt(this.pos);
25
if (typeof match == "string") var ok = ch == match;
26
else var ok = ch && (match.test ? match.test(ch) : match(ch));
27
if (ok) {++this.pos; return ch;}
28
},
29
eatWhile: function(match) {
30
var start = this.pos;
31
while (this.eat(match)){}
32
return this.pos > start;
33
},
34
eatSpace: function() {
35
var start = this.pos;
36
while (/[\s\u00a0]/.test(this.string.charAt(this.pos))) ++this.pos;
37
return this.pos > start;
38
},
39
skipToEnd: function() {this.pos = this.string.length;},
40
skipTo: function(ch) {
41
var found = this.string.indexOf(ch, this.pos);
42
if (found > -1) {this.pos = found; return true;}
43
},
44
backUp: function(n) {this.pos -= n;},
45
column: function() {return this.start - this.lineStart;},
46
indentation: function() {return 0;},
47
match: function(pattern, consume, caseInsensitive) {
48
if (typeof pattern == "string") {
49
var cased = function(str) {return caseInsensitive ? str.toLowerCase() : str;};
50
var substr = this.string.substr(this.pos, pattern.length);
51
if (cased(substr) == cased(pattern)) {
52
if (consume !== false) this.pos += pattern.length;
53
return true;
54
}
55
} else {
56
var match = this.string.slice(this.pos).match(pattern);
57
if (match && match.index > 0) return null;
58
if (match && consume !== false) this.pos += match[0].length;
59
return match;
60
}
61
},
62
current: function(){return this.string.slice(this.start, this.pos);},
63
hideFirstChars: function(n, inner) {
64
this.lineStart += n;
65
try { return inner(); }
66
finally { this.lineStart -= n; }
67
}
68
};
69
CodeMirror.StringStream = StringStream;
70
71
CodeMirror.startState = function (mode, a1, a2) {
72
return mode.startState ? mode.startState(a1, a2) : true;
73
};
74
75
var modes = CodeMirror.modes = {}, mimeModes = CodeMirror.mimeModes = {};
76
CodeMirror.defineMode = function (name, mode) { modes[name] = mode; };
77
CodeMirror.defineMIME = function (mime, spec) { mimeModes[mime] = spec; };
78
CodeMirror.resolveMode = function(spec) {
79
if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
80
spec = mimeModes[spec];
81
} else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
82
spec = mimeModes[spec.name];
83
}
84
if (typeof spec == "string") return {name: spec};
85
else return spec || {name: "null"};
86
};
87
CodeMirror.getMode = function (options, spec) {
88
spec = CodeMirror.resolveMode(spec);
89
var mfactory = modes[spec.name];
90
if (!mfactory) throw new Error("Unknown mode: " + spec);
91
return mfactory(options, spec);
92
};
93
CodeMirror.registerHelper = CodeMirror.registerGlobalHelper = Math.min;
94
CodeMirror.defineMode("null", function() {
95
return {token: function(stream) {stream.skipToEnd();}};
96
});
97
CodeMirror.defineMIME("text/plain", "null");
98
99
CodeMirror.runMode = function (string, modespec, callback, options) {
100
var mode = CodeMirror.getMode({ indentUnit: 2 }, modespec);
101
102
if (callback.nodeType == 1) {
103
var tabSize = (options && options.tabSize) || 4;
104
var node = callback, col = 0;
105
node.innerHTML = "";
106
callback = function (text, style) {
107
if (text == "\n") {
108
node.appendChild(document.createElement("br"));
109
col = 0;
110
return;
111
}
112
var content = "";
113
// replace tabs
114
for (var pos = 0; ;) {
115
var idx = text.indexOf("\t", pos);
116
if (idx == -1) {
117
content += text.slice(pos);
118
col += text.length - pos;
119
break;
120
} else {
121
col += idx - pos;
122
content += text.slice(pos, idx);
123
var size = tabSize - col % tabSize;
124
col += size;
125
for (var i = 0; i < size; ++i) content += " ";
126
pos = idx + 1;
127
}
128
}
129
130
if (style) {
131
var sp = node.appendChild(document.createElement("span"));
132
sp.className = "cm-" + style.replace(/ +/g, " cm-");
133
sp.appendChild(document.createTextNode(content));
134
} else {
135
node.appendChild(document.createTextNode(content));
136
}
137
};
138
}
139
140
var lines = splitLines(string), state = (options && options.state) || CodeMirror.startState(mode);
141
for (var i = 0, e = lines.length; i < e; ++i) {
142
if (i) callback("\n");
143
var stream = new CodeMirror.StringStream(lines[i]);
144
while (!stream.eol()) {
145
var style = mode.token(stream, state);
146
callback(stream.current(), style, i, stream.start, state);
147
stream.start = stream.pos;
148
}
149
}
150
};
151
})();
152
153