Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
// CodeMirror, copyright (c) by Marijn Haverbeke and others
2
// Distributed under an MIT license: http://codemirror.net/LICENSE
3
4
(function(mod) {
5
if (typeof exports == "object" && typeof module == "object") // CommonJS
6
mod(require("codemirror"));
7
else if (typeof define == "function" && define.amd) // AMD
8
define(["codemirror"], mod);
9
else // Plain browser env
10
mod(CodeMirror);
11
})(function(CodeMirror) {
12
"use strict";
13
14
function wordRegexp(words) {
15
return new RegExp("^((" + words.join(")|(") + "))\\b");
16
}
17
18
var wordOperators = wordRegexp(["and", "or", "not", "is"]);
19
var commonKeywords = ["as", "assert", "break", "class", "continue",
20
"def", "del", "elif", "else", "except", "finally",
21
"for", "from", "global", "if", "import",
22
"lambda", "pass", "raise", "return",
23
"try", "while", "with", "yield", "in"];
24
var commonBuiltins = ["abs", "all", "any", "bin", "bool", "bytearray", "callable", "chr",
25
"classmethod", "compile", "complex", "delattr", "dict", "dir", "divmod",
26
"enumerate", "eval", "filter", "float", "format", "frozenset",
27
"getattr", "globals", "hasattr", "hash", "help", "hex", "id",
28
"input", "int", "isinstance", "issubclass", "iter", "len",
29
"list", "locals", "map", "max", "memoryview", "min", "next",
30
"object", "oct", "open", "ord", "pow", "property", "range",
31
"repr", "reversed", "round", "set", "setattr", "slice",
32
"sorted", "staticmethod", "str", "sum", "super", "tuple",
33
"type", "vars", "zip", "__import__", "NotImplemented",
34
"Ellipsis", "__debug__"];
35
var py2 = {builtins: ["apply", "basestring", "buffer", "cmp", "coerce", "execfile",
36
"file", "intern", "long", "raw_input", "reduce", "reload",
37
"unichr", "unicode", "xrange", "False", "True", "None"],
38
keywords: ["exec", "print"]};
39
var py3 = {builtins: ["ascii", "bytes", "exec", "print"],
40
keywords: ["nonlocal", "False", "True", "None"]};
41
42
CodeMirror.registerHelper("hintWords", "python", commonKeywords.concat(commonBuiltins));
43
44
function top(state) {
45
if (state.scopes.length == 0) {
46
return {type:"undefined", offset:0}; /* better than totally crashing */
47
}
48
return state.scopes[state.scopes.length - 1];
49
}
50
51
CodeMirror.defineMode("python", function(conf, parserConf) {
52
var ERRORCLASS = "error";
53
54
var singleDelimiters = parserConf.singleDelimiters || new RegExp("^[\\(\\)\\[\\]\\{\\}@,:`=;\\.]");
55
var doubleOperators = parserConf.doubleOperators || new RegExp("^((==)|(!=)|(<=)|(>=)|(<>)|(<<)|(>>)|(//)|(\\*\\*))");
56
var doubleDelimiters = parserConf.doubleDelimiters || new RegExp("^((\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&=)|(\\|=)|(\\^=))");
57
var tripleDelimiters = parserConf.tripleDelimiters || new RegExp("^((//=)|(>>=)|(<<=)|(\\*\\*=))");
58
59
if (parserConf.version && parseInt(parserConf.version, 10) == 3){
60
// since http://legacy.python.org/dev/peps/pep-0465/ @ is also an operator
61
var singleOperators = parserConf.singleOperators || new RegExp("^[\\+\\-\\*/%&|\\^~<>!@]");
62
var identifiers = parserConf.identifiers|| new RegExp("^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*");
63
} else {
64
var singleOperators = parserConf.singleOperators || new RegExp("^[\\+\\-\\*/%&|\\^~<>!]");
65
var identifiers = parserConf.identifiers|| new RegExp("^[_A-Za-z][_A-Za-z0-9]*");
66
}
67
68
var hangingIndent = parserConf.hangingIndent || conf.indentUnit;
69
70
var myKeywords = commonKeywords, myBuiltins = commonBuiltins;
71
if(parserConf.extra_keywords != undefined){
72
myKeywords = myKeywords.concat(parserConf.extra_keywords);
73
}
74
if(parserConf.extra_builtins != undefined){
75
myBuiltins = myBuiltins.concat(parserConf.extra_builtins);
76
}
77
if (parserConf.version && parseInt(parserConf.version, 10) == 3) {
78
myKeywords = myKeywords.concat(py3.keywords);
79
myBuiltins = myBuiltins.concat(py3.builtins);
80
var stringPrefixes = new RegExp("^(([rb]|(br))?('{3}|\"{3}|['\"]))", "i");
81
} else {
82
myKeywords = myKeywords.concat(py2.keywords);
83
myBuiltins = myBuiltins.concat(py2.builtins);
84
var stringPrefixes = new RegExp("^(([rub]|(ur)|(br))?('{3}|\"{3}|['\"]))", "i");
85
}
86
var keywords = wordRegexp(myKeywords);
87
var builtins = wordRegexp(myBuiltins);
88
89
// tokenizers
90
function tokenBase(stream, state) {
91
// Handle scope changes
92
if (stream.sol() && top(state).type == "py") {
93
var scopeOffset = top(state).offset;
94
if (stream.eatSpace()) {
95
var lineOffset = stream.indentation();
96
if (lineOffset > scopeOffset)
97
pushScope(stream, state, "py");
98
else if (lineOffset < scopeOffset && dedent(stream, state))
99
state.errorToken = true;
100
return null;
101
} else {
102
var style = tokenBaseInner(stream, state);
103
if (scopeOffset > 0 && dedent(stream, state))
104
style += " " + ERRORCLASS;
105
return style;
106
}
107
}
108
return tokenBaseInner(stream, state);
109
}
110
111
function tokenBaseInner(stream, state) {
112
if (stream.eatSpace()) return null;
113
114
var ch = stream.peek();
115
116
// Handle Comments
117
if (ch == "#" || ch == "\uFE21") {
118
stream.skipToEnd();
119
return "comment";
120
}
121
122
// Handle Number Literals
123
if (stream.match(/^[0-9\.]/, false)) {
124
var floatLiteral = false;
125
// Floats
126
if (stream.match(/^\d*\.\d+(e[\+\-]?\d+)?/i)) { floatLiteral = true; }
127
if (stream.match(/^\d+\.\d*/)) { floatLiteral = true; }
128
if (stream.match(/^\.\d+/)) { floatLiteral = true; }
129
if (floatLiteral) {
130
// Float literals may be "imaginary"
131
stream.eat(/J/i);
132
return "number";
133
}
134
// Integers
135
var intLiteral = false;
136
// Hex
137
if (stream.match(/^0x[0-9a-f]+/i)) intLiteral = true;
138
// Binary
139
if (stream.match(/^0b[01]+/i)) intLiteral = true;
140
// Octal
141
if (stream.match(/^0o[0-7]+/i)) intLiteral = true;
142
// Decimal
143
if (stream.match(/^[1-9]\d*(e[\+\-]?\d+)?/)) {
144
// Decimal literals may be "imaginary"
145
stream.eat(/J/i);
146
// TODO - Can you have imaginary longs?
147
intLiteral = true;
148
}
149
// Zero by itself with no other piece of number.
150
if (stream.match(/^0(?![\dx])/i)) intLiteral = true;
151
if (intLiteral) {
152
// Integer literals may be "long"
153
stream.eat(/L/i);
154
return "number";
155
}
156
}
157
158
// Handle Strings
159
if (stream.match(stringPrefixes)) {
160
state.tokenize = tokenStringFactory(stream.current());
161
return state.tokenize(stream, state);
162
}
163
164
// Handle operators and Delimiters
165
if (stream.match(tripleDelimiters) || stream.match(doubleDelimiters))
166
return null;
167
168
if (stream.match(doubleOperators) || stream.match(singleOperators))
169
return "operator";
170
171
if (stream.match(singleDelimiters))
172
return null;
173
174
if (stream.match(keywords) || stream.match(wordOperators))
175
return "keyword";
176
177
if (stream.match(builtins))
178
return "builtin";
179
180
if (stream.match(/^(self|cls)\b/))
181
return "variable-2";
182
183
if (stream.match(identifiers)) {
184
if (state.lastToken == "def" || state.lastToken == "class")
185
return "def";
186
return "variable";
187
}
188
189
// Handle non-detected items
190
stream.next();
191
return ERRORCLASS;
192
}
193
194
function tokenStringFactory(delimiter) {
195
while ("rub".indexOf(delimiter.charAt(0).toLowerCase()) >= 0)
196
delimiter = delimiter.substr(1);
197
198
var singleline = delimiter.length == 1;
199
var OUTCLASS = "string";
200
201
function tokenString(stream, state) {
202
while (!stream.eol()) {
203
stream.eatWhile(/[^'"\\]/);
204
if (stream.eat("\\")) {
205
stream.next();
206
if (singleline && stream.eol())
207
return OUTCLASS;
208
} else if (stream.match(delimiter)) {
209
state.tokenize = tokenBase;
210
return OUTCLASS;
211
} else {
212
stream.eat(/['"]/);
213
}
214
}
215
if (singleline) {
216
if (parserConf.singleLineStringErrors)
217
return ERRORCLASS;
218
else
219
state.tokenize = tokenBase;
220
}
221
return OUTCLASS;
222
}
223
tokenString.isString = true;
224
return tokenString;
225
}
226
227
function pushScope(stream, state, type) {
228
var offset = 0, align = null;
229
if (type == "py") {
230
while (top(state).type != "py")
231
state.scopes.pop();
232
}
233
offset = top(state).offset + (type == "py" ? conf.indentUnit : hangingIndent);
234
if (type != "py" && !stream.match(/^(\s|#.*)*$/, false))
235
align = stream.column() + 1;
236
state.scopes.push({offset: offset, type: type, align: align});
237
}
238
239
function dedent(stream, state) {
240
var indented = stream.indentation();
241
while (top(state).offset > indented) {
242
if (top(state).type != "py") return true;
243
state.scopes.pop();
244
if (state.scopes.length == 0) return false;
245
}
246
return top(state).offset != indented;
247
}
248
249
function tokenLexer(stream, state) {
250
var style = state.tokenize(stream, state);
251
var current = stream.current();
252
253
// Handle '.' connected identifiers
254
if (current == ".") {
255
style = stream.match(identifiers, false) ? null : ERRORCLASS;
256
if (style == null && state.lastStyle == "meta") {
257
// Apply 'meta' style to '.' connected identifiers when
258
// appropriate.
259
style = "meta";
260
}
261
return style;
262
}
263
264
// Handle decorators
265
if (current == "@"){
266
if(parserConf.version && parseInt(parserConf.version, 10) == 3){
267
return stream.match(identifiers, false) ? "meta" : "operator";
268
} else {
269
return stream.match(identifiers, false) ? "meta" : ERRORCLASS;
270
}
271
}
272
273
if ((style == "variable" || style == "builtin")
274
&& state.lastStyle == "meta")
275
style = "meta";
276
277
// Handle scope changes.
278
if (current == "pass" || current == "return")
279
state.dedent += 1;
280
281
if (current == "lambda") state.lambda = true;
282
if (current == ":" && !state.lambda && top(state).type == "py")
283
pushScope(stream, state, "py");
284
285
var delimiter_index = current.length == 1 ? "[({".indexOf(current) : -1;
286
if (delimiter_index != -1)
287
pushScope(stream, state, "])}".slice(delimiter_index, delimiter_index+1));
288
289
delimiter_index = "])}".indexOf(current);
290
if (delimiter_index != -1) {
291
if (top(state).type == current) state.scopes.pop();
292
else return ERRORCLASS;
293
}
294
if (state.dedent > 0 && stream.eol() && top(state).type == "py") {
295
if (state.scopes.length > 1) state.scopes.pop();
296
state.dedent -= 1;
297
}
298
299
return style;
300
}
301
302
var external = {
303
startState: function(basecolumn) {
304
return {
305
tokenize: tokenBase,
306
scopes: [{offset: basecolumn || 0, type: "py", align: null}],
307
lastStyle: null,
308
lastToken: null,
309
lambda: false,
310
dedent: 0
311
};
312
},
313
314
token: function(stream, state) {
315
var addErr = state.errorToken;
316
if (addErr) state.errorToken = false;
317
var style = tokenLexer(stream, state);
318
319
state.lastStyle = style;
320
321
var current = stream.current();
322
if (current && style)
323
state.lastToken = current;
324
325
if (stream.eol() && state.lambda)
326
state.lambda = false;
327
return addErr ? style + " " + ERRORCLASS : style;
328
},
329
330
indent: function(state, textAfter) {
331
if (state.tokenize != tokenBase)
332
return state.tokenize.isString ? CodeMirror.Pass : 0;
333
334
var scope = top(state);
335
var closing = textAfter && textAfter.charAt(0) == scope.type;
336
if (scope.align != null)
337
return scope.align - (closing ? 1 : 0);
338
else if (closing && state.scopes.length > 1)
339
return state.scopes[state.scopes.length - 2].offset;
340
else
341
return scope.offset;
342
},
343
344
closeBrackets: {triples: "'\""},
345
lineComment: "#",
346
fold: "indent"
347
};
348
return external;
349
});
350
351
CodeMirror.defineMIME("text/x-python", "python");
352
353
var words = function(str) { return str.split(" "); };
354
355
CodeMirror.defineMIME("text/x-cython", {
356
name: "python",
357
extra_keywords: words("by cdef cimport cpdef ctypedef enum except"+
358
"extern gil include nogil property public"+
359
"readonly struct union DEF IF ELIF ELSE")
360
});
361
362
});
363
364