提交 | 用户 | 时间
|
722af2
|
1 |
// CodeMirror, copyright (c) by Marijn Haverbeke and others |
X |
2 |
// Distributed under an MIT license: https://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 delimiters = parserConf.delimiters || parserConf.singleDelimiters || /^[\(\)\[\]\{\}@,:`=;\.\\]/; |
|
45 |
// (Backwards-compatiblity with old, cumbersome config system) |
|
46 |
var operators = [parserConf.singleOperators, parserConf.doubleOperators, parserConf.doubleDelimiters, parserConf.tripleDelimiters, |
|
47 |
parserConf.operators || /^([-+*/%\/&|^]=?|[<>=]+|\/\/=?|\*\*=?|!=|[~!@])/] |
|
48 |
for (var i = 0; i < operators.length; i++) if (!operators[i]) operators.splice(i--, 1) |
|
49 |
|
|
50 |
var hangingIndent = parserConf.hangingIndent || conf.indentUnit; |
|
51 |
|
|
52 |
var myKeywords = commonKeywords, myBuiltins = commonBuiltins; |
|
53 |
if (parserConf.extra_keywords != undefined) |
|
54 |
myKeywords = myKeywords.concat(parserConf.extra_keywords); |
|
55 |
|
|
56 |
if (parserConf.extra_builtins != undefined) |
|
57 |
myBuiltins = myBuiltins.concat(parserConf.extra_builtins); |
|
58 |
|
|
59 |
var py3 = !(parserConf.version && Number(parserConf.version) < 3) |
|
60 |
if (py3) { |
|
61 |
// since http://legacy.python.org/dev/peps/pep-0465/ @ is also an operator |
|
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)|(fr))?('{3}|\"{3}|['\"]))", "i"); |
|
66 |
} else { |
|
67 |
var identifiers = parserConf.identifiers|| /^[_A-Za-z][_A-Za-z0-9]*/; |
|
68 |
myKeywords = myKeywords.concat(["exec", "print"]); |
|
69 |
myBuiltins = myBuiltins.concat(["apply", "basestring", "buffer", "cmp", "coerce", "execfile", |
|
70 |
"file", "intern", "long", "raw_input", "reduce", "reload", |
|
71 |
"unichr", "unicode", "xrange", "False", "True", "None"]); |
|
72 |
var stringPrefixes = new RegExp("^(([rubf]|(ur)|(br))?('{3}|\"{3}|['\"]))", "i"); |
|
73 |
} |
|
74 |
var keywords = wordRegexp(myKeywords); |
|
75 |
var builtins = wordRegexp(myBuiltins); |
|
76 |
|
|
77 |
// tokenizers |
|
78 |
function tokenBase(stream, state) { |
|
79 |
var sol = stream.sol() && state.lastToken != "\\" |
|
80 |
if (sol) state.indent = stream.indentation() |
|
81 |
// Handle scope changes |
|
82 |
if (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 |
// Handle Comments |
|
105 |
if (stream.match(/^#.*/)) return "comment"; |
|
106 |
|
|
107 |
// Handle Number Literals |
|
108 |
if (stream.match(/^[0-9\.]/, false)) { |
|
109 |
var floatLiteral = false; |
|
110 |
// Floats |
|
111 |
if (stream.match(/^[\d_]*\.\d+(e[\+\-]?\d+)?/i)) { floatLiteral = true; } |
|
112 |
if (stream.match(/^[\d_]+\.\d*/)) { floatLiteral = true; } |
|
113 |
if (stream.match(/^\.\d+/)) { floatLiteral = true; } |
|
114 |
if (floatLiteral) { |
|
115 |
// Float literals may be "imaginary" |
|
116 |
stream.eat(/J/i); |
|
117 |
return "number"; |
|
118 |
} |
|
119 |
// Integers |
|
120 |
var intLiteral = false; |
|
121 |
// Hex |
|
122 |
if (stream.match(/^0x[0-9a-f_]+/i)) intLiteral = true; |
|
123 |
// Binary |
|
124 |
if (stream.match(/^0b[01_]+/i)) intLiteral = true; |
|
125 |
// Octal |
|
126 |
if (stream.match(/^0o[0-7_]+/i)) intLiteral = true; |
|
127 |
// Decimal |
|
128 |
if (stream.match(/^[1-9][\d_]*(e[\+\-]?[\d_]+)?/)) { |
|
129 |
// Decimal literals may be "imaginary" |
|
130 |
stream.eat(/J/i); |
|
131 |
// TODO - Can you have imaginary longs? |
|
132 |
intLiteral = true; |
|
133 |
} |
|
134 |
// Zero by itself with no other piece of number. |
|
135 |
if (stream.match(/^0(?![\dx])/i)) intLiteral = true; |
|
136 |
if (intLiteral) { |
|
137 |
// Integer literals may be "long" |
|
138 |
stream.eat(/L/i); |
|
139 |
return "number"; |
|
140 |
} |
|
141 |
} |
|
142 |
|
|
143 |
// Handle Strings |
|
144 |
if (stream.match(stringPrefixes)) { |
|
145 |
var isFmtString = stream.current().toLowerCase().indexOf('f') !== -1; |
|
146 |
if (!isFmtString) { |
|
147 |
state.tokenize = tokenStringFactory(stream.current()); |
|
148 |
return state.tokenize(stream, state); |
|
149 |
} else { |
|
150 |
state.tokenize = formatStringFactory(stream.current(), state.tokenize); |
|
151 |
return state.tokenize(stream, state); |
|
152 |
} |
|
153 |
} |
|
154 |
|
|
155 |
for (var i = 0; i < operators.length; i++) |
|
156 |
if (stream.match(operators[i])) return "operator" |
|
157 |
|
|
158 |
if (stream.match(delimiters)) return "punctuation"; |
|
159 |
|
|
160 |
if (state.lastToken == "." && stream.match(identifiers)) |
|
161 |
return "property"; |
|
162 |
|
|
163 |
if (stream.match(keywords) || stream.match(wordOperators)) |
|
164 |
return "keyword"; |
|
165 |
|
|
166 |
if (stream.match(builtins)) |
|
167 |
return "builtin"; |
|
168 |
|
|
169 |
if (stream.match(/^(self|cls)\b/)) |
|
170 |
return "variable-2"; |
|
171 |
|
|
172 |
if (stream.match(identifiers)) { |
|
173 |
if (state.lastToken == "def" || state.lastToken == "class") |
|
174 |
return "def"; |
|
175 |
return "variable"; |
|
176 |
} |
|
177 |
|
|
178 |
// Handle non-detected items |
|
179 |
stream.next(); |
|
180 |
return ERRORCLASS; |
|
181 |
} |
|
182 |
|
|
183 |
function formatStringFactory(delimiter, tokenOuter) { |
|
184 |
while ("rubf".indexOf(delimiter.charAt(0).toLowerCase()) >= 0) |
|
185 |
delimiter = delimiter.substr(1); |
|
186 |
|
|
187 |
var singleline = delimiter.length == 1; |
|
188 |
var OUTCLASS = "string"; |
|
189 |
|
|
190 |
function tokenFString(stream, state) { |
|
191 |
// inside f-str Expression |
|
192 |
if (stream.match(delimiter)) { |
|
193 |
// expression ends pre-maturally, but very common in editing |
|
194 |
// Could show error to remind users to close brace here |
|
195 |
state.tokenize = tokenString |
|
196 |
return OUTCLASS; |
|
197 |
} else if (stream.match('{')) { |
|
198 |
// starting brace, if not eaten below |
|
199 |
return "punctuation"; |
|
200 |
} else if (stream.match('}')) { |
|
201 |
// return to regular inside string state |
|
202 |
state.tokenize = tokenString |
|
203 |
return "punctuation"; |
|
204 |
} else { |
|
205 |
// use tokenBaseInner to parse the expression |
|
206 |
return tokenBaseInner(stream, state); |
|
207 |
} |
|
208 |
} |
|
209 |
|
|
210 |
function tokenString(stream, state) { |
|
211 |
while (!stream.eol()) { |
|
212 |
stream.eatWhile(/[^'"\{\}\\]/); |
|
213 |
if (stream.eat("\\")) { |
|
214 |
stream.next(); |
|
215 |
if (singleline && stream.eol()) |
|
216 |
return OUTCLASS; |
|
217 |
} else if (stream.match(delimiter)) { |
|
218 |
state.tokenize = tokenOuter; |
|
219 |
return OUTCLASS; |
|
220 |
} else if (stream.match('{{')) { |
|
221 |
// ignore {{ in f-str |
|
222 |
return OUTCLASS; |
|
223 |
} else if (stream.match('{', false)) { |
|
224 |
// switch to nested mode |
|
225 |
state.tokenize = tokenFString |
|
226 |
if (stream.current()) { |
|
227 |
return OUTCLASS; |
|
228 |
} else { |
|
229 |
// need to return something, so eat the starting { |
|
230 |
stream.next(); |
|
231 |
return "punctuation"; |
|
232 |
} |
|
233 |
} else if (stream.match('}}')) { |
|
234 |
return OUTCLASS; |
|
235 |
} else if (stream.match('}')) { |
|
236 |
// single } in f-string is an error |
|
237 |
return ERRORCLASS; |
|
238 |
} else { |
|
239 |
stream.eat(/['"]/); |
|
240 |
} |
|
241 |
} |
|
242 |
if (singleline) { |
|
243 |
if (parserConf.singleLineStringErrors) |
|
244 |
return ERRORCLASS; |
|
245 |
else |
|
246 |
state.tokenize = tokenOuter; |
|
247 |
} |
|
248 |
return OUTCLASS; |
|
249 |
} |
|
250 |
tokenString.isString = true; |
|
251 |
return tokenString; |
|
252 |
} |
|
253 |
|
|
254 |
function tokenStringFactory(delimiter) { |
|
255 |
while ("rubf".indexOf(delimiter.charAt(0).toLowerCase()) >= 0) |
|
256 |
delimiter = delimiter.substr(1); |
|
257 |
|
|
258 |
var singleline = delimiter.length == 1; |
|
259 |
var OUTCLASS = "string"; |
|
260 |
|
|
261 |
function tokenString(stream, state) { |
|
262 |
while (!stream.eol()) { |
|
263 |
stream.eatWhile(/[^'"\\]/); |
|
264 |
if (stream.eat("\\")) { |
|
265 |
stream.next(); |
|
266 |
if (singleline && stream.eol()) |
|
267 |
return OUTCLASS; |
|
268 |
} else if (stream.match(delimiter)) { |
|
269 |
state.tokenize = tokenBase; |
|
270 |
return OUTCLASS; |
|
271 |
} else { |
|
272 |
stream.eat(/['"]/); |
|
273 |
} |
|
274 |
} |
|
275 |
if (singleline) { |
|
276 |
if (parserConf.singleLineStringErrors) |
|
277 |
return ERRORCLASS; |
|
278 |
else |
|
279 |
state.tokenize = tokenBase; |
|
280 |
} |
|
281 |
return OUTCLASS; |
|
282 |
} |
|
283 |
tokenString.isString = true; |
|
284 |
return tokenString; |
|
285 |
} |
|
286 |
|
|
287 |
function pushPyScope(state) { |
|
288 |
while (top(state).type != "py") state.scopes.pop() |
|
289 |
state.scopes.push({offset: top(state).offset + conf.indentUnit, |
|
290 |
type: "py", |
|
291 |
align: null}) |
|
292 |
} |
|
293 |
|
|
294 |
function pushBracketScope(stream, state, type) { |
|
295 |
var align = stream.match(/^([\s\[\{\(]|#.*)*$/, false) ? null : stream.column() + 1 |
|
296 |
state.scopes.push({offset: state.indent + hangingIndent, |
|
297 |
type: type, |
|
298 |
align: align}) |
|
299 |
} |
|
300 |
|
|
301 |
function dedent(stream, state) { |
|
302 |
var indented = stream.indentation(); |
|
303 |
while (state.scopes.length > 1 && top(state).offset > indented) { |
|
304 |
if (top(state).type != "py") return true; |
|
305 |
state.scopes.pop(); |
|
306 |
} |
|
307 |
return top(state).offset != indented; |
|
308 |
} |
|
309 |
|
|
310 |
function tokenLexer(stream, state) { |
|
311 |
if (stream.sol()) state.beginningOfLine = true; |
|
312 |
|
|
313 |
var style = state.tokenize(stream, state); |
|
314 |
var current = stream.current(); |
|
315 |
|
|
316 |
// Handle decorators |
|
317 |
if (state.beginningOfLine && current == "@") |
|
318 |
return stream.match(identifiers, false) ? "meta" : py3 ? "operator" : ERRORCLASS; |
|
319 |
|
|
320 |
if (/\S/.test(current)) state.beginningOfLine = false; |
|
321 |
|
|
322 |
if ((style == "variable" || style == "builtin") |
|
323 |
&& state.lastToken == "meta") |
|
324 |
style = "meta"; |
|
325 |
|
|
326 |
// Handle scope changes. |
|
327 |
if (current == "pass" || current == "return") |
|
328 |
state.dedent += 1; |
|
329 |
|
|
330 |
if (current == "lambda") state.lambda = true; |
|
331 |
if (current == ":" && !state.lambda && top(state).type == "py") |
|
332 |
pushPyScope(state); |
|
333 |
|
|
334 |
if (current.length == 1 && !/string|comment/.test(style)) { |
|
335 |
var delimiter_index = "[({".indexOf(current); |
|
336 |
if (delimiter_index != -1) |
|
337 |
pushBracketScope(stream, state, "])}".slice(delimiter_index, delimiter_index+1)); |
|
338 |
|
|
339 |
delimiter_index = "])}".indexOf(current); |
|
340 |
if (delimiter_index != -1) { |
|
341 |
if (top(state).type == current) state.indent = state.scopes.pop().offset - hangingIndent |
|
342 |
else return ERRORCLASS; |
|
343 |
} |
|
344 |
} |
|
345 |
if (state.dedent > 0 && stream.eol() && top(state).type == "py") { |
|
346 |
if (state.scopes.length > 1) state.scopes.pop(); |
|
347 |
state.dedent -= 1; |
|
348 |
} |
|
349 |
|
|
350 |
return style; |
|
351 |
} |
|
352 |
|
|
353 |
var external = { |
|
354 |
startState: function(basecolumn) { |
|
355 |
return { |
|
356 |
tokenize: tokenBase, |
|
357 |
scopes: [{offset: basecolumn || 0, type: "py", align: null}], |
|
358 |
indent: basecolumn || 0, |
|
359 |
lastToken: null, |
|
360 |
lambda: false, |
|
361 |
dedent: 0 |
|
362 |
}; |
|
363 |
}, |
|
364 |
|
|
365 |
token: function(stream, state) { |
|
366 |
var addErr = state.errorToken; |
|
367 |
if (addErr) state.errorToken = false; |
|
368 |
var style = tokenLexer(stream, state); |
|
369 |
|
|
370 |
if (style && style != "comment") |
|
371 |
state.lastToken = (style == "keyword" || style == "punctuation") ? stream.current() : style; |
|
372 |
if (style == "punctuation") style = null; |
|
373 |
|
|
374 |
if (stream.eol() && state.lambda) |
|
375 |
state.lambda = false; |
|
376 |
return addErr ? style + " " + ERRORCLASS : style; |
|
377 |
}, |
|
378 |
|
|
379 |
indent: function(state, textAfter) { |
|
380 |
if (state.tokenize != tokenBase) |
|
381 |
return state.tokenize.isString ? CodeMirror.Pass : 0; |
|
382 |
|
|
383 |
var scope = top(state), closing = scope.type == textAfter.charAt(0) |
|
384 |
if (scope.align != null) |
|
385 |
return scope.align - (closing ? 1 : 0) |
|
386 |
else |
|
387 |
return scope.offset - (closing ? hangingIndent : 0) |
|
388 |
}, |
|
389 |
|
|
390 |
electricInput: /^\s*[\}\]\)]$/, |
|
391 |
closeBrackets: {triples: "'\""}, |
|
392 |
lineComment: "#", |
|
393 |
fold: "indent" |
|
394 |
}; |
|
395 |
return external; |
|
396 |
}); |
|
397 |
|
|
398 |
CodeMirror.defineMIME("text/x-python", "python"); |
|
399 |
|
|
400 |
var words = function(str) { return str.split(" "); }; |
|
401 |
|
|
402 |
CodeMirror.defineMIME("text/x-cython", { |
|
403 |
name: "python", |
|
404 |
extra_keywords: words("by cdef cimport cpdef ctypedef enum except "+ |
|
405 |
"extern gil include nogil property public "+ |
|
406 |
"readonly struct union DEF IF ELIF ELSE") |
|
407 |
}); |
|
408 |
|
|
409 |
}); |