-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
julia.js
390 lines (333 loc) · 11.1 KB
/
julia.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// CodeMirror, copyright (c) by Marijn Haverbeke and others
// Distributed under an MIT license: https://codemirror.net/5/LICENSE
(function(mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror"], mod);
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
"use strict";
CodeMirror.defineMode("julia", function(config, parserConf) {
function wordRegexp(words, end, pre) {
if (typeof pre === "undefined") { pre = ""; }
if (typeof end === "undefined") { end = "\\b"; }
return new RegExp("^" + pre + "((" + words.join(")|(") + "))" + end);
}
var octChar = "\\\\[0-7]{1,3}";
var hexChar = "\\\\x[A-Fa-f0-9]{1,2}";
var sChar = "\\\\[abefnrtv0%?'\"\\\\]";
var uChar = "([^\\u0027\\u005C\\uD800-\\uDFFF]|[\\uD800-\\uDFFF][\\uDC00-\\uDFFF])";
var asciiOperatorsList = [
"[<>]:", "[<>=]=", "<<=?", ">>>?=?", "=>", "--?>", "<--[->]?", "\\/\\/",
"\\.{2,3}", "[\\.\\\\%*+\\-<>!\\/^|&]=?", "\\?", "\\$", "~", ":"
];
var operators = parserConf.operators || wordRegexp([
"[<>]:", "[<>=]=", "[!=]==", "<<=?", ">>>?=?", "=>?", "--?>", "<--[->]?", "\\/\\/",
"[\\\\%*+\\-<>!\\/^|&\\u00F7\\u22BB]=?", "\\?", "\\$", "~", ":",
"\\u00D7", "\\u2208", "\\u2209", "\\u220B", "\\u220C", "\\u2218",
"\\u221A", "\\u221B", "\\u2229", "\\u222A", "\\u2260", "\\u2264",
"\\u2265", "\\u2286", "\\u2288", "\\u228A", "\\u22C5",
"\\b(in|isa)\\b(?!\.?\\()"
], "");
var delimiters = parserConf.delimiters || /^[;,()[\]{}]/;
var identifiers = parserConf.identifiers ||
/^[_A-Za-z\u00A1-\u2217\u2219-\uFFFF][\w\u00A1-\u2217\u2219-\uFFFF]*!*/;
var chars = wordRegexp([octChar, hexChar, sChar, uChar], "'");
var openersList = ["begin", "function", "type", "struct", "immutable", "let",
"macro", "for", "while", "quote", "if", "else", "elseif", "try",
"finally", "catch", "do"];
var closersList = ["end", "else", "elseif", "catch", "finally"];
var keywordsList = ["if", "else", "elseif", "while", "for", "begin", "let",
"end", "do", "try", "catch", "finally", "return", "break", "continue",
"global", "local", "const", "export", "import", "importall", "using",
"function", "where", "macro", "module", "baremodule", "struct", "type",
"mutable", "immutable", "quote", "typealias", "abstract", "primitive",
"bitstype"];
var builtinsList = ["true", "false", "nothing", "NaN", "Inf"];
CodeMirror.registerHelper("hintWords", "julia", keywordsList.concat(builtinsList));
var openers = wordRegexp(openersList);
var closers = wordRegexp(closersList);
var keywords = wordRegexp(keywordsList);
var builtins = wordRegexp(builtinsList);
var macro = /^@[_A-Za-z\u00A1-\uFFFF][\w\u00A1-\uFFFF]*!*/;
var symbol = /^:[_A-Za-z\u00A1-\uFFFF][\w\u00A1-\uFFFF]*!*/;
var stringPrefixes = /^(`|([_A-Za-z\u00A1-\uFFFF]*"("")?))/;
var macroOperators = wordRegexp(asciiOperatorsList, "", "@");
var symbolOperators = wordRegexp(asciiOperatorsList, "", ":");
function inArray(state) {
return (state.nestedArrays > 0);
}
function inGenerator(state) {
return (state.nestedGenerators > 0);
}
function currentScope(state, n) {
if (typeof(n) === "undefined") { n = 0; }
if (state.scopes.length <= n) {
return null;
}
return state.scopes[state.scopes.length - (n + 1)];
}
// tokenizers
function tokenBase(stream, state) {
// Handle multiline comments
if (stream.match('#=', false)) {
state.tokenize = tokenComment;
return state.tokenize(stream, state);
}
// Handle scope changes
var leavingExpr = state.leavingExpr;
if (stream.sol()) {
leavingExpr = false;
}
state.leavingExpr = false;
if (leavingExpr) {
if (stream.match(/^'+/)) {
return "operator";
}
}
if (stream.match(/\.{4,}/)) {
return "error";
} else if (stream.match(/\.{1,3}/)) {
return "operator";
}
if (stream.eatSpace()) {
return null;
}
var ch = stream.peek();
// Handle single line comments
if (ch === '#') {
stream.skipToEnd();
return "comment";
}
if (ch === '[') {
state.scopes.push('[');
state.nestedArrays++;
}
if (ch === '(') {
state.scopes.push('(');
state.nestedGenerators++;
}
if (inArray(state) && ch === ']') {
while (state.scopes.length && currentScope(state) !== "[") { state.scopes.pop(); }
state.scopes.pop();
state.nestedArrays--;
state.leavingExpr = true;
}
if (inGenerator(state) && ch === ')') {
while (state.scopes.length && currentScope(state) !== "(") { state.scopes.pop(); }
state.scopes.pop();
state.nestedGenerators--;
state.leavingExpr = true;
}
if (inArray(state)) {
if (state.lastToken == "end" && stream.match(':')) {
return "operator";
}
if (stream.match('end')) {
return "number";
}
}
var match;
if (match = stream.match(openers, false)) {
state.scopes.push(match[0]);
}
if (stream.match(closers, false)) {
state.scopes.pop();
}
// Handle type annotations
if (stream.match(/^::(?![:\$])/)) {
state.tokenize = tokenAnnotation;
return state.tokenize(stream, state);
}
// Handle symbols
if (!leavingExpr && (stream.match(symbol) || stream.match(symbolOperators))) {
return "builtin";
}
// Handle parametric types
//if (stream.match(/^{[^}]*}(?=\()/)) {
// return "builtin";
//}
// Handle operators and Delimiters
if (stream.match(operators)) {
return "operator";
}
// Handle Number Literals
if (stream.match(/^\.?\d/, false)) {
var imMatcher = RegExp(/^im\b/);
var numberLiteral = false;
if (stream.match(/^0x\.[0-9a-f_]+p[\+\-]?[_\d]+/i)) { numberLiteral = true; }
// Integers
if (stream.match(/^0x[0-9a-f_]+/i)) { numberLiteral = true; } // Hex
if (stream.match(/^0b[01_]+/i)) { numberLiteral = true; } // Binary
if (stream.match(/^0o[0-7_]+/i)) { numberLiteral = true; } // Octal
// Floats
if (stream.match(/^(?:(?:\d[_\d]*)?\.(?!\.)(?:\d[_\d]*)?|\d[_\d]*\.(?!\.)(?:\d[_\d]*))?([Eef][\+\-]?[_\d]+)?/i)) { numberLiteral = true; }
if (stream.match(/^\d[_\d]*(e[\+\-]?\d+)?/i)) { numberLiteral = true; } // Decimal
if (numberLiteral) {
// Integer literals may be "long"
stream.match(imMatcher);
state.leavingExpr = true;
return "number";
}
}
// Handle Chars
if (stream.match('\'')) {
state.tokenize = tokenChar;
return state.tokenize(stream, state);
}
// Handle Strings
if (stream.match(stringPrefixes)) {
state.tokenize = tokenStringFactory(stream.current());
return state.tokenize(stream, state);
}
if (stream.match(macro) || stream.match(macroOperators)) {
return "meta";
}
if (stream.match(delimiters)) {
return null;
}
if (stream.match(keywords)) {
return "keyword";
}
if (stream.match(builtins)) {
return "builtin";
}
var isDefinition = state.isDefinition || state.lastToken == "function" ||
state.lastToken == "macro" || state.lastToken == "type" ||
state.lastToken == "struct" || state.lastToken == "immutable";
if (stream.match(identifiers)) {
if (isDefinition) {
if (stream.peek() === '.') {
state.isDefinition = true;
return "variable";
}
state.isDefinition = false;
return "def";
}
state.leavingExpr = true;
return "variable";
}
// Handle non-detected items
stream.next();
return "error";
}
function tokenAnnotation(stream, state) {
stream.match(/.*?(?=[,;{}()=\s]|$)/);
if (stream.match('{')) {
state.nestedParameters++;
} else if (stream.match('}') && state.nestedParameters > 0) {
state.nestedParameters--;
}
if (state.nestedParameters > 0) {
stream.match(/.*?(?={|})/) || stream.next();
} else if (state.nestedParameters == 0) {
state.tokenize = tokenBase;
}
return "builtin";
}
function tokenComment(stream, state) {
if (stream.match('#=')) {
state.nestedComments++;
}
if (!stream.match(/.*?(?=(#=|=#))/)) {
stream.skipToEnd();
}
if (stream.match('=#')) {
state.nestedComments--;
if (state.nestedComments == 0)
state.tokenize = tokenBase;
}
return "comment";
}
function tokenChar(stream, state) {
var isChar = false, match;
if (stream.match(chars)) {
isChar = true;
} else if (match = stream.match(/\\u([a-f0-9]{1,4})(?=')/i)) {
var value = parseInt(match[1], 16);
if (value <= 55295 || value >= 57344) { // (U+0,U+D7FF), (U+E000,U+FFFF)
isChar = true;
stream.next();
}
} else if (match = stream.match(/\\U([A-Fa-f0-9]{5,8})(?=')/)) {
var value = parseInt(match[1], 16);
if (value <= 1114111) { // U+10FFFF
isChar = true;
stream.next();
}
}
if (isChar) {
state.leavingExpr = true;
state.tokenize = tokenBase;
return "string";
}
if (!stream.match(/^[^']+(?=')/)) { stream.skipToEnd(); }
if (stream.match('\'')) { state.tokenize = tokenBase; }
return "error";
}
function tokenStringFactory(delimiter) {
if (delimiter.substr(-3) === '"""') {
delimiter = '"""';
} else if (delimiter.substr(-1) === '"') {
delimiter = '"';
}
function tokenString(stream, state) {
if (stream.eat('\\')) {
stream.next();
} else if (stream.match(delimiter)) {
state.tokenize = tokenBase;
state.leavingExpr = true;
return "string";
} else {
stream.eat(/[`"]/);
}
stream.eatWhile(/[^\\`"]/);
return "string";
}
return tokenString;
}
var external = {
startState: function() {
return {
tokenize: tokenBase,
scopes: [],
lastToken: null,
leavingExpr: false,
isDefinition: false,
nestedArrays: 0,
nestedComments: 0,
nestedGenerators: 0,
nestedParameters: 0,
firstParenPos: -1
};
},
token: function(stream, state) {
var style = state.tokenize(stream, state);
var current = stream.current();
if (current && style) {
state.lastToken = current;
}
return style;
},
indent: function(state, textAfter) {
var delta = 0;
if ( textAfter === ']' || textAfter === ')' || /^end\b/.test(textAfter) ||
/^else/.test(textAfter) || /^catch\b/.test(textAfter) || /^elseif\b/.test(textAfter) ||
/^finally/.test(textAfter) ) {
delta = -1;
}
return (state.scopes.length + delta) * config.indentUnit;
},
electricInput: /\b(end|else|catch|finally)\b/,
blockCommentStart: "#=",
blockCommentEnd: "=#",
lineComment: "#",
closeBrackets: "()[]{}\"\"",
fold: "indent"
};
return external;
});
CodeMirror.defineMIME("text/x-julia", "julia");
});