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