Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50665 views
1
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2
// Distributed under an MIT license: http://codemirror.net/LICENSE
3
4
/* Just enough of CodeMirror to run runMode under node.js */
5
6
// declare global: StringStream
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
exports.StringStream = StringStream;
70
71
exports.startState = function(mode, a1, a2) {
72
return mode.startState ? mode.startState(a1, a2) : true;
73
};
74
75
var modes = exports.modes = {}, mimeModes = exports.mimeModes = {};
76
exports.defineMode = function(name, mode) {
77
if (arguments.length > 2)
78
mode.dependencies = Array.prototype.slice.call(arguments, 2);
79
modes[name] = mode;
80
};
81
exports.defineMIME = function(mime, spec) { mimeModes[mime] = spec; };
82
83
exports.defineMode("null", function() {
84
return {token: function(stream) {stream.skipToEnd();}};
85
});
86
exports.defineMIME("text/plain", "null");
87
88
exports.resolveMode = function(spec) {
89
if (typeof spec == "string" && mimeModes.hasOwnProperty(spec)) {
90
spec = mimeModes[spec];
91
} else if (spec && typeof spec.name == "string" && mimeModes.hasOwnProperty(spec.name)) {
92
spec = mimeModes[spec.name];
93
}
94
if (typeof spec == "string") return {name: spec};
95
else return spec || {name: "null"};
96
};
97
exports.getMode = function(options, spec) {
98
spec = exports.resolveMode(spec);
99
var mfactory = modes[spec.name];
100
if (!mfactory) throw new Error("Unknown mode: " + spec);
101
return mfactory(options, spec);
102
};
103
exports.registerHelper = exports.registerGlobalHelper = Math.min;
104
105
exports.runMode = function(string, modespec, callback, options) {
106
var mode = exports.getMode({indentUnit: 2}, modespec);
107
var lines = splitLines(string), state = (options && options.state) || exports.startState(mode);
108
for (var i = 0, e = lines.length; i < e; ++i) {
109
if (i) callback("\n");
110
var stream = new exports.StringStream(lines[i]);
111
if (!stream.string && mode.blankLine) mode.blankLine(state);
112
while (!stream.eol()) {
113
var style = mode.token(stream, state);
114
callback(stream.current(), style, i, stream.start, state);
115
stream.start = stream.pos;
116
}
117
}
118
};
119
120
require.cache[require.resolve("../../lib/codemirror")] = require.cache[require.resolve("./runmode.node")];
121
122