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