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"]}; |
---|
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) |
---|
166 | || stream.match(singleOperators) |
---|
167 | || stream.match(wordOperators)) |
---|
168 | return "operator"; |
---|
169 | |
---|
170 | if (stream.match(singleDelimiters)) |
---|
171 | return null; |
---|
172 | |
---|
173 | if (stream.match(keywords)) |
---|
174 | return "keyword"; |
---|
175 | |
---|
176 | if (stream.match(builtins)) |
---|
177 | return "builtin"; |
---|
178 | |
---|
179 | if (stream.match(/^(self|cls)\b/)) |
---|
180 | return "variable-2"; |
---|
181 | |
---|
182 | if (stream.match(identifiers)) { |
---|
183 | if (state.lastToken == "def" || state.lastToken == "class") |
---|
184 | return "def"; |
---|
185 | return "variable"; |
---|
186 | } |
---|
187 | |
---|
188 | // Handle non-detected items |
---|
189 | stream.next(); |
---|
190 | return ERRORCLASS; |
---|
191 | } |
---|
192 | |
---|
193 | function tokenStringFactory(delimiter) { |
---|
194 | while ("rub".indexOf(delimiter.charAt(0).toLowerCase()) >= 0) |
---|
195 | delimiter = delimiter.substr(1); |
---|
196 | |
---|
197 | var singleline = delimiter.length == 1; |
---|
198 | var OUTCLASS = "string"; |
---|
199 | |
---|
200 | function tokenString(stream, state) { |
---|
201 | while (!stream.eol()) { |
---|
202 | stream.eatWhile(/[^'"\\]/); |
---|
203 | if (stream.eat("\\")) { |
---|
204 | stream.next(); |
---|
205 | if (singleline && stream.eol()) |
---|
206 | return OUTCLASS; |
---|
207 | } else if (stream.match(delimiter)) { |
---|
208 | state.tokenize = tokenBase; |
---|
209 | return OUTCLASS; |
---|
210 | } else { |
---|
211 | stream.eat(/['"]/); |
---|
212 | } |
---|
213 | } |
---|
214 | if (singleline) { |
---|
215 | if (parserConf.singleLineStringErrors) |
---|
216 | return ERRORCLASS; |
---|
217 | else |
---|
218 | state.tokenize = tokenBase; |
---|
219 | } |
---|
220 | return OUTCLASS; |
---|
221 | } |
---|
222 | tokenString.isString = true; |
---|
223 | return tokenString; |
---|
224 | } |
---|
225 | |
---|
226 | function pushScope(stream, state, type) { |
---|
227 | var offset = 0, align = null; |
---|
228 | if (type == "py") { |
---|
229 | while (top(state).type != "py") |
---|
230 | state.scopes.pop(); |
---|
231 | } |
---|
232 | offset = top(state).offset + (type == "py" ? conf.indentUnit : hangingIndent); |
---|
233 | if (type != "py" && !stream.match(/^(\s|#.*)*$/, false)) |
---|
234 | align = stream.column() + 1; |
---|
235 | state.scopes.push({offset: offset, type: type, align: align}); |
---|
236 | } |
---|
237 | |
---|
238 | function dedent(stream, state) { |
---|
239 | var indented = stream.indentation(); |
---|
240 | while (top(state).offset > indented) { |
---|
241 | if (top(state).type != "py") return true; |
---|
242 | state.scopes.pop(); |
---|
243 | } |
---|
244 | return top(state).offset != indented; |
---|
245 | } |
---|
246 | |
---|
247 | function tokenLexer(stream, state) { |
---|
248 | var style = state.tokenize(stream, state); |
---|
249 | var current = stream.current(); |
---|
250 | |
---|
251 | // Handle '.' connected identifiers |
---|
252 | if (current == ".") { |
---|
253 | style = stream.match(identifiers, false) ? null : ERRORCLASS; |
---|
254 | if (style == null && state.lastStyle == "meta") { |
---|
255 | // Apply 'meta' style to '.' connected identifiers when |
---|
256 | // appropriate. |
---|
257 | style = "meta"; |
---|
258 | } |
---|
259 | return style; |
---|
260 | } |
---|
261 | |
---|
262 | // Handle decorators |
---|
263 | if (current == "@"){ |
---|
264 | if(parserConf.version && parseInt(parserConf.version, 10) == 3){ |
---|
265 | return stream.match(identifiers, false) ? "meta" : "operator"; |
---|
266 | } else { |
---|
267 | return stream.match(identifiers, false) ? "meta" : ERRORCLASS; |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | if ((style == "variable" || style == "builtin") |
---|
272 | && state.lastStyle == "meta") |
---|
273 | style = "meta"; |
---|
274 | |
---|
275 | // Handle scope changes. |
---|
276 | if (current == "pass" || current == "return") |
---|
277 | state.dedent += 1; |
---|
278 | |
---|
279 | if (current == "lambda") state.lambda = true; |
---|
280 | if (current == ":" && !state.lambda && top(state).type == "py") |
---|
281 | pushScope(stream, state, "py"); |
---|
282 | |
---|
283 | var delimiter_index = current.length == 1 ? "[({".indexOf(current) : -1; |
---|
284 | if (delimiter_index != -1) |
---|
285 | pushScope(stream, state, "])}".slice(delimiter_index, delimiter_index+1)); |
---|
286 | |
---|
287 | delimiter_index = "])}".indexOf(current); |
---|
288 | if (delimiter_index != -1) { |
---|
289 | if (top(state).type == current) state.scopes.pop(); |
---|
290 | else return ERRORCLASS; |
---|
291 | } |
---|
292 | if (state.dedent > 0 && stream.eol() && top(state).type == "py") { |
---|
293 | if (state.scopes.length > 1) state.scopes.pop(); |
---|
294 | state.dedent -= 1; |
---|
295 | } |
---|
296 | |
---|
297 | return style; |
---|
298 | } |
---|
299 | |
---|
300 | var external = { |
---|
301 | startState: function(basecolumn) { |
---|
302 | return { |
---|
303 | tokenize: tokenBase, |
---|
304 | scopes: [{offset: basecolumn || 0, type: "py", align: null}], |
---|
305 | lastStyle: null, |
---|
306 | lastToken: null, |
---|
307 | lambda: false, |
---|
308 | dedent: 0 |
---|
309 | }; |
---|
310 | }, |
---|
311 | |
---|
312 | token: function(stream, state) { |
---|
313 | var addErr = state.errorToken; |
---|
314 | if (addErr) state.errorToken = false; |
---|
315 | var style = tokenLexer(stream, state); |
---|
316 | |
---|
317 | state.lastStyle = style; |
---|
318 | |
---|
319 | var current = stream.current(); |
---|
320 | if (current && style) |
---|
321 | state.lastToken = current; |
---|
322 | |
---|
323 | if (stream.eol() && state.lambda) |
---|
324 | state.lambda = false; |
---|
325 | return addErr ? style + " " + ERRORCLASS : style; |
---|
326 | }, |
---|
327 | |
---|
328 | indent: function(state, textAfter) { |
---|
329 | if (state.tokenize != tokenBase) |
---|
330 | return state.tokenize.isString ? CodeMirror.Pass : 0; |
---|
331 | |
---|
332 | var scope = top(state); |
---|
333 | var closing = textAfter && textAfter.charAt(0) == scope.type; |
---|
334 | if (scope.align != null) |
---|
335 | return scope.align - (closing ? 1 : 0); |
---|
336 | else if (closing && state.scopes.length > 1) |
---|
337 | return state.scopes[state.scopes.length - 2].offset; |
---|
338 | else |
---|
339 | return scope.offset; |
---|
340 | }, |
---|
341 | |
---|
342 | lineComment: "#", |
---|
343 | fold: "indent" |
---|
344 | }; |
---|
345 | return external; |
---|
346 | }); |
---|
347 | |
---|
348 | CodeMirror.defineMIME("text/x-python", "python"); |
---|
349 | |
---|
350 | var words = function(str) { return str.split(" "); }; |
---|
351 | |
---|
352 | CodeMirror.defineMIME("text/x-cython", { |
---|
353 | name: "python", |
---|
354 | extra_keywords: words("by cdef cimport cpdef ctypedef enum except"+ |
---|
355 | "extern gil include nogil property public"+ |
---|
356 | "readonly struct union DEF IF ELIF ELSE") |
---|
357 | }); |
---|
358 | |
---|
359 | }); |
---|