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