|
@@ -0,0 +1,340 @@
|
|
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("../../lib/codemirror"));
|
|
7
|
+ else if (typeof define == "function" && define.amd) // AMD
|
|
8
|
+ define(["../../lib/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
|
+ CodeMirror.registerHelper("hintWords", "python", commonKeywords.concat(commonBuiltins));
|
|
36
|
+
|
|
37
|
+ function top(state) {
|
|
38
|
+ return state.scopes[state.scopes.length - 1];
|
|
39
|
+ }
|
|
40
|
+
|
|
41
|
+ CodeMirror.defineMode("python", function(conf, parserConf) {
|
|
42
|
+ var ERRORCLASS = "error";
|
|
43
|
+
|
|
44
|
+ var singleDelimiters = parserConf.singleDelimiters || /^[\(\)\[\]\{\}@,:`=;\.]/;
|
|
45
|
+ var doubleOperators = parserConf.doubleOperators || /^([!<>]==|<>|<<|>>|\/\/|\*\*)/;
|
|
46
|
+ var doubleDelimiters = parserConf.doubleDelimiters || /^(\+=|\-=|\*=|%=|\/=|&=|\|=|\^=)/;
|
|
47
|
+ var tripleDelimiters = parserConf.tripleDelimiters || /^(\/\/=|>>=|<<=|\*\*=)/;
|
|
48
|
+
|
|
49
|
+ var hangingIndent = parserConf.hangingIndent || conf.indentUnit;
|
|
50
|
+
|
|
51
|
+ var myKeywords = commonKeywords, myBuiltins = commonBuiltins;
|
|
52
|
+ if (parserConf.extra_keywords != undefined)
|
|
53
|
+ myKeywords = myKeywords.concat(parserConf.extra_keywords);
|
|
54
|
+
|
|
55
|
+ if (parserConf.extra_builtins != undefined)
|
|
56
|
+ myBuiltins = myBuiltins.concat(parserConf.extra_builtins);
|
|
57
|
+
|
|
58
|
+ var py3 = !(parserConf.version && Number(parserConf.version) < 3)
|
|
59
|
+ if (py3) {
|
|
60
|
+ // since http://legacy.python.org/dev/peps/pep-0465/ @ is also an operator
|
|
61
|
+ var singleOperators = parserConf.singleOperators || /^[\+\-\*\/%&|\^~<>!@]/;
|
|
62
|
+ var identifiers = parserConf.identifiers|| /^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*/;
|
|
63
|
+ myKeywords = myKeywords.concat(["nonlocal", "False", "True", "None", "async", "await"]);
|
|
64
|
+ myBuiltins = myBuiltins.concat(["ascii", "bytes", "exec", "print"]);
|
|
65
|
+ var stringPrefixes = new RegExp("^(([rbuf]|(br))?('{3}|\"{3}|['\"]))", "i");
|
|
66
|
+ } else {
|
|
67
|
+ var singleOperators = parserConf.singleOperators || /^[\+\-\*\/%&|\^~<>!]/;
|
|
68
|
+ var identifiers = parserConf.identifiers|| /^[_A-Za-z][_A-Za-z0-9]*/;
|
|
69
|
+ myKeywords = myKeywords.concat(["exec", "print"]);
|
|
70
|
+ myBuiltins = myBuiltins.concat(["apply", "basestring", "buffer", "cmp", "coerce", "execfile",
|
|
71
|
+ "file", "intern", "long", "raw_input", "reduce", "reload",
|
|
72
|
+ "unichr", "unicode", "xrange", "False", "True", "None"]);
|
|
73
|
+ var stringPrefixes = new RegExp("^(([rubf]|(ur)|(br))?('{3}|\"{3}|['\"]))", "i");
|
|
74
|
+ }
|
|
75
|
+ var keywords = wordRegexp(myKeywords);
|
|
76
|
+ var builtins = wordRegexp(myBuiltins);
|
|
77
|
+
|
|
78
|
+ // tokenizers
|
|
79
|
+ function tokenBase(stream, state) {
|
|
80
|
+ if (stream.sol()) state.indent = stream.indentation()
|
|
81
|
+ // Handle scope changes
|
|
82
|
+ if (stream.sol() && top(state).type == "py") {
|
|
83
|
+ var scopeOffset = top(state).offset;
|
|
84
|
+ if (stream.eatSpace()) {
|
|
85
|
+ var lineOffset = stream.indentation();
|
|
86
|
+ if (lineOffset > scopeOffset)
|
|
87
|
+ pushPyScope(state);
|
|
88
|
+ else if (lineOffset < scopeOffset && dedent(stream, state) && stream.peek() != "#")
|
|
89
|
+ state.errorToken = true;
|
|
90
|
+ return null;
|
|
91
|
+ } else {
|
|
92
|
+ var style = tokenBaseInner(stream, state);
|
|
93
|
+ if (scopeOffset > 0 && dedent(stream, state))
|
|
94
|
+ style += " " + ERRORCLASS;
|
|
95
|
+ return style;
|
|
96
|
+ }
|
|
97
|
+ }
|
|
98
|
+ return tokenBaseInner(stream, state);
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ function tokenBaseInner(stream, state) {
|
|
102
|
+ if (stream.eatSpace()) return null;
|
|
103
|
+
|
|
104
|
+ var ch = stream.peek();
|
|
105
|
+
|
|
106
|
+ // Handle Comments
|
|
107
|
+ if (ch == "#") {
|
|
108
|
+ stream.skipToEnd();
|
|
109
|
+ return "comment";
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ // Handle Number Literals
|
|
113
|
+ if (stream.match(/^[0-9\.]/, false)) {
|
|
114
|
+ var floatLiteral = false;
|
|
115
|
+ // Floats
|
|
116
|
+ if (stream.match(/^[\d_]*\.\d+(e[\+\-]?\d+)?/i)) { floatLiteral = true; }
|
|
117
|
+ if (stream.match(/^[\d_]+\.\d*/)) { floatLiteral = true; }
|
|
118
|
+ if (stream.match(/^\.\d+/)) { floatLiteral = true; }
|
|
119
|
+ if (floatLiteral) {
|
|
120
|
+ // Float literals may be "imaginary"
|
|
121
|
+ stream.eat(/J/i);
|
|
122
|
+ return "number";
|
|
123
|
+ }
|
|
124
|
+ // Integers
|
|
125
|
+ var intLiteral = false;
|
|
126
|
+ // Hex
|
|
127
|
+ if (stream.match(/^0x[0-9a-f_]+/i)) intLiteral = true;
|
|
128
|
+ // Binary
|
|
129
|
+ if (stream.match(/^0b[01_]+/i)) intLiteral = true;
|
|
130
|
+ // Octal
|
|
131
|
+ if (stream.match(/^0o[0-7_]+/i)) intLiteral = true;
|
|
132
|
+ // Decimal
|
|
133
|
+ if (stream.match(/^[1-9][\d_]*(e[\+\-]?[\d_]+)?/)) {
|
|
134
|
+ // Decimal literals may be "imaginary"
|
|
135
|
+ stream.eat(/J/i);
|
|
136
|
+ // TODO - Can you have imaginary longs?
|
|
137
|
+ intLiteral = true;
|
|
138
|
+ }
|
|
139
|
+ // Zero by itself with no other piece of number.
|
|
140
|
+ if (stream.match(/^0(?![\dx])/i)) intLiteral = true;
|
|
141
|
+ if (intLiteral) {
|
|
142
|
+ // Integer literals may be "long"
|
|
143
|
+ stream.eat(/L/i);
|
|
144
|
+ return "number";
|
|
145
|
+ }
|
|
146
|
+ }
|
|
147
|
+
|
|
148
|
+ // Handle Strings
|
|
149
|
+ if (stream.match(stringPrefixes)) {
|
|
150
|
+ state.tokenize = tokenStringFactory(stream.current());
|
|
151
|
+ return state.tokenize(stream, state);
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+ // Handle operators and Delimiters
|
|
155
|
+ if (stream.match(tripleDelimiters) || stream.match(doubleDelimiters))
|
|
156
|
+ return "punctuation";
|
|
157
|
+
|
|
158
|
+ if (stream.match(doubleOperators) || stream.match(singleOperators))
|
|
159
|
+ return "operator";
|
|
160
|
+
|
|
161
|
+ if (stream.match(singleDelimiters))
|
|
162
|
+ return "punctuation";
|
|
163
|
+
|
|
164
|
+ if (state.lastToken == "." && stream.match(identifiers))
|
|
165
|
+ return "property";
|
|
166
|
+
|
|
167
|
+ if (stream.match(keywords) || stream.match(wordOperators))
|
|
168
|
+ return "keyword";
|
|
169
|
+
|
|
170
|
+ if (stream.match(builtins))
|
|
171
|
+ return "builtin";
|
|
172
|
+
|
|
173
|
+ if (stream.match(/^(self|cls)\b/))
|
|
174
|
+ return "variable-2";
|
|
175
|
+
|
|
176
|
+ if (stream.match(identifiers)) {
|
|
177
|
+ if (state.lastToken == "def" || state.lastToken == "class")
|
|
178
|
+ return "def";
|
|
179
|
+ return "variable";
|
|
180
|
+ }
|
|
181
|
+
|
|
182
|
+ // Handle non-detected items
|
|
183
|
+ stream.next();
|
|
184
|
+ return ERRORCLASS;
|
|
185
|
+ }
|
|
186
|
+
|
|
187
|
+ function tokenStringFactory(delimiter) {
|
|
188
|
+ while ("rubf".indexOf(delimiter.charAt(0).toLowerCase()) >= 0)
|
|
189
|
+ delimiter = delimiter.substr(1);
|
|
190
|
+
|
|
191
|
+ var singleline = delimiter.length == 1;
|
|
192
|
+ var OUTCLASS = "string";
|
|
193
|
+
|
|
194
|
+ function tokenString(stream, state) {
|
|
195
|
+ while (!stream.eol()) {
|
|
196
|
+ stream.eatWhile(/[^'"\\]/);
|
|
197
|
+ if (stream.eat("\\")) {
|
|
198
|
+ stream.next();
|
|
199
|
+ if (singleline && stream.eol())
|
|
200
|
+ return OUTCLASS;
|
|
201
|
+ } else if (stream.match(delimiter)) {
|
|
202
|
+ state.tokenize = tokenBase;
|
|
203
|
+ return OUTCLASS;
|
|
204
|
+ } else {
|
|
205
|
+ stream.eat(/['"]/);
|
|
206
|
+ }
|
|
207
|
+ }
|
|
208
|
+ if (singleline) {
|
|
209
|
+ if (parserConf.singleLineStringErrors)
|
|
210
|
+ return ERRORCLASS;
|
|
211
|
+ else
|
|
212
|
+ state.tokenize = tokenBase;
|
|
213
|
+ }
|
|
214
|
+ return OUTCLASS;
|
|
215
|
+ }
|
|
216
|
+ tokenString.isString = true;
|
|
217
|
+ return tokenString;
|
|
218
|
+ }
|
|
219
|
+
|
|
220
|
+ function pushPyScope(state) {
|
|
221
|
+ while (top(state).type != "py") state.scopes.pop()
|
|
222
|
+ state.scopes.push({offset: top(state).offset + conf.indentUnit,
|
|
223
|
+ type: "py",
|
|
224
|
+ align: null})
|
|
225
|
+ }
|
|
226
|
+
|
|
227
|
+ function pushBracketScope(stream, state, type) {
|
|
228
|
+ var align = stream.match(/^([\s\[\{\(]|#.*)*$/, false) ? null : stream.column() + 1
|
|
229
|
+ state.scopes.push({offset: state.indent + hangingIndent,
|
|
230
|
+ type: type,
|
|
231
|
+ align: align})
|
|
232
|
+ }
|
|
233
|
+
|
|
234
|
+ function dedent(stream, state) {
|
|
235
|
+ var indented = stream.indentation();
|
|
236
|
+ while (state.scopes.length > 1 && top(state).offset > indented) {
|
|
237
|
+ if (top(state).type != "py") return true;
|
|
238
|
+ state.scopes.pop();
|
|
239
|
+ }
|
|
240
|
+ return top(state).offset != indented;
|
|
241
|
+ }
|
|
242
|
+
|
|
243
|
+ function tokenLexer(stream, state) {
|
|
244
|
+ if (stream.sol()) state.beginningOfLine = true;
|
|
245
|
+
|
|
246
|
+ var style = state.tokenize(stream, state);
|
|
247
|
+ var current = stream.current();
|
|
248
|
+
|
|
249
|
+ // Handle decorators
|
|
250
|
+ if (state.beginningOfLine && current == "@")
|
|
251
|
+ return stream.match(identifiers, false) ? "meta" : py3 ? "operator" : ERRORCLASS;
|
|
252
|
+
|
|
253
|
+ if (/\S/.test(current)) state.beginningOfLine = false;
|
|
254
|
+
|
|
255
|
+ if ((style == "variable" || style == "builtin")
|
|
256
|
+ && state.lastToken == "meta")
|
|
257
|
+ style = "meta";
|
|
258
|
+
|
|
259
|
+ // Handle scope changes.
|
|
260
|
+ if (current == "pass" || current == "return")
|
|
261
|
+ state.dedent += 1;
|
|
262
|
+
|
|
263
|
+ if (current == "lambda") state.lambda = true;
|
|
264
|
+ if (current == ":" && !state.lambda && top(state).type == "py")
|
|
265
|
+ pushPyScope(state);
|
|
266
|
+
|
|
267
|
+ var delimiter_index = current.length == 1 ? "[({".indexOf(current) : -1;
|
|
268
|
+ if (delimiter_index != -1)
|
|
269
|
+ pushBracketScope(stream, state, "])}".slice(delimiter_index, delimiter_index+1));
|
|
270
|
+
|
|
271
|
+ delimiter_index = "])}".indexOf(current);
|
|
272
|
+ if (delimiter_index != -1) {
|
|
273
|
+ if (top(state).type == current) state.indent = state.scopes.pop().offset - hangingIndent
|
|
274
|
+ else return ERRORCLASS;
|
|
275
|
+ }
|
|
276
|
+ if (state.dedent > 0 && stream.eol() && top(state).type == "py") {
|
|
277
|
+ if (state.scopes.length > 1) state.scopes.pop();
|
|
278
|
+ state.dedent -= 1;
|
|
279
|
+ }
|
|
280
|
+
|
|
281
|
+ return style;
|
|
282
|
+ }
|
|
283
|
+
|
|
284
|
+ var external = {
|
|
285
|
+ startState: function(basecolumn) {
|
|
286
|
+ return {
|
|
287
|
+ tokenize: tokenBase,
|
|
288
|
+ scopes: [{offset: basecolumn || 0, type: "py", align: null}],
|
|
289
|
+ indent: basecolumn || 0,
|
|
290
|
+ lastToken: null,
|
|
291
|
+ lambda: false,
|
|
292
|
+ dedent: 0
|
|
293
|
+ };
|
|
294
|
+ },
|
|
295
|
+
|
|
296
|
+ token: function(stream, state) {
|
|
297
|
+ var addErr = state.errorToken;
|
|
298
|
+ if (addErr) state.errorToken = false;
|
|
299
|
+ var style = tokenLexer(stream, state);
|
|
300
|
+
|
|
301
|
+ if (style && style != "comment")
|
|
302
|
+ state.lastToken = (style == "keyword" || style == "punctuation") ? stream.current() : style;
|
|
303
|
+ if (style == "punctuation") style = null;
|
|
304
|
+
|
|
305
|
+ if (stream.eol() && state.lambda)
|
|
306
|
+ state.lambda = false;
|
|
307
|
+ return addErr ? style + " " + ERRORCLASS : style;
|
|
308
|
+ },
|
|
309
|
+
|
|
310
|
+ indent: function(state, textAfter) {
|
|
311
|
+ if (state.tokenize != tokenBase)
|
|
312
|
+ return state.tokenize.isString ? CodeMirror.Pass : 0;
|
|
313
|
+
|
|
314
|
+ var scope = top(state), closing = scope.type == textAfter.charAt(0)
|
|
315
|
+ if (scope.align != null)
|
|
316
|
+ return scope.align - (closing ? 1 : 0)
|
|
317
|
+ else
|
|
318
|
+ return scope.offset - (closing ? hangingIndent : 0)
|
|
319
|
+ },
|
|
320
|
+
|
|
321
|
+ electricInput: /^\s*[\}\]\)]$/,
|
|
322
|
+ closeBrackets: {triples: "'\""},
|
|
323
|
+ lineComment: "#",
|
|
324
|
+ fold: "indent"
|
|
325
|
+ };
|
|
326
|
+ return external;
|
|
327
|
+ });
|
|
328
|
+
|
|
329
|
+ CodeMirror.defineMIME("text/x-python", "python");
|
|
330
|
+
|
|
331
|
+ var words = function(str) { return str.split(" "); };
|
|
332
|
+
|
|
333
|
+ CodeMirror.defineMIME("text/x-cython", {
|
|
334
|
+ name: "python",
|
|
335
|
+ extra_keywords: words("by cdef cimport cpdef ctypedef enum except"+
|
|
336
|
+ "extern gil include nogil property public"+
|
|
337
|
+ "readonly struct union DEF IF ELIF ELSE")
|
|
338
|
+ });
|
|
339
|
+
|
|
340
|
+});
|