Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50672 views
1
CodeMirror.defineMode("julia", function(_conf, parserConf) {
2
var ERRORCLASS = 'error';
3
4
function wordRegexp(words) {
5
return new RegExp("^((" + words.join(")|(") + "))\\b");
6
}
7
8
var operators = parserConf.operators || /^\.?[|&^\\%*+\-<>!=\/]=?|\?|~|:|\$|\.[<>]|<<=?|>>>?=?|\.[<>=]=|->?|\/\/|\bin\b/;
9
var delimiters = parserConf.delimiters || /^[;,()[\]{}]/;
10
var identifiers = parserConf.identifiers|| /^[_A-Za-z][_A-Za-z0-9]*!*/;
11
var blockOpeners = ["begin", "function", "type", "immutable", "let", "macro", "for", "while", "quote", "if", "else", "elseif", "try", "finally", "catch", "do"];
12
var blockClosers = ["end", "else", "elseif", "catch", "finally"];
13
var keywordList = ['if', 'else', 'elseif', 'while', 'for', 'begin', 'let', 'end', 'do', 'try', 'catch', 'finally', 'return', 'break', 'continue', 'global', 'local', 'const', 'export', 'import', 'importall', 'using', 'function', 'macro', 'module', 'baremodule', 'type', 'immutable', 'quote', 'typealias', 'abstract', 'bitstype', 'ccall'];
14
var builtinList = ['true', 'false', 'enumerate', 'open', 'close', 'nothing', 'NaN', 'Inf', 'print', 'println', 'Int', 'Int8', 'Uint8', 'Int16', 'Uint16', 'Int32', 'Uint32', 'Int64', 'Uint64', 'Int128', 'Uint128', 'Bool', 'Char', 'Float16', 'Float32', 'Float64', 'Array', 'Vector', 'Matrix', 'String', 'UTF8String', 'ASCIIString', 'error', 'warn', 'info', '@printf'];
15
16
//var stringPrefixes = new RegExp("^[br]?('|\")")
17
var stringPrefixes = /^(`|'|"{3}|([br]?"))/;
18
var keywords = wordRegexp(keywordList);
19
var builtins = wordRegexp(builtinList);
20
var openers = wordRegexp(blockOpeners);
21
var closers = wordRegexp(blockClosers);
22
var macro = /^@[_A-Za-z][_A-Za-z0-9]*/;
23
var symbol = /^:[_A-Za-z][_A-Za-z0-9]*/;
24
var indentInfo = null;
25
26
function in_array(state) {
27
var ch = cur_scope(state);
28
if(ch=="[" || ch=="{") {
29
return true;
30
}
31
else {
32
return false;
33
}
34
}
35
36
function cur_scope(state) {
37
if(state.scopes.length==0) {
38
return null;
39
}
40
return state.scopes[state.scopes.length - 1];
41
}
42
43
// tokenizers
44
function tokenBase(stream, state) {
45
// Handle scope changes
46
var leaving_expr = state.leaving_expr;
47
if(stream.sol()) {
48
leaving_expr = false;
49
}
50
state.leaving_expr = false;
51
if(leaving_expr) {
52
if(stream.match(/^'+/)) {
53
return 'operator';
54
}
55
56
}
57
58
if(stream.match(/^\.{2,3}/)) {
59
return 'operator';
60
}
61
62
if (stream.eatSpace()) {
63
return null;
64
}
65
66
var ch = stream.peek();
67
// Handle Comments
68
if (ch === '#') {
69
stream.skipToEnd();
70
return 'comment';
71
}
72
if(ch==='[') {
73
state.scopes.push("[");
74
}
75
76
if(ch==='{') {
77
state.scopes.push("{");
78
}
79
80
var scope=cur_scope(state);
81
82
if(scope==='[' && ch===']') {
83
state.scopes.pop();
84
state.leaving_expr=true;
85
}
86
87
if(scope==='{' && ch==='}') {
88
state.scopes.pop();
89
state.leaving_expr=true;
90
}
91
92
if(ch===')') {
93
state.leaving_expr = true;
94
}
95
96
var match;
97
if(!in_array(state) && (match=stream.match(openers, false))) {
98
state.scopes.push(match);
99
}
100
101
if(!in_array(state) && stream.match(closers, false)) {
102
state.scopes.pop();
103
}
104
105
if(in_array(state)) {
106
if(stream.match(/^end/)) {
107
return 'number';
108
}
109
110
}
111
112
if(stream.match(/^=>/)) {
113
return 'operator';
114
}
115
116
117
// Handle Number Literals
118
if (stream.match(/^[0-9\.]/, false)) {
119
var imMatcher = RegExp(/^im\b/);
120
var floatLiteral = false;
121
// Floats
122
if (stream.match(/^\d*\.(?!\.)\d+([ef][\+\-]?\d+)?/i)) { floatLiteral = true; }
123
if (stream.match(/^\d+\.(?!\.)\d*/)) { floatLiteral = true; }
124
if (stream.match(/^\.\d+/)) { floatLiteral = true; }
125
if (floatLiteral) {
126
// Float literals may be "imaginary"
127
stream.match(imMatcher);
128
state.leaving_expr = true;
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
intLiteral = true;
142
}
143
// Zero by itself with no other piece of number.
144
if (stream.match(/^0(?![\dx])/i)) { intLiteral = true; }
145
if (intLiteral) {
146
// Integer literals may be "long"
147
stream.match(imMatcher);
148
state.leaving_expr = true;
149
return 'number';
150
}
151
}
152
153
if(stream.match(/^(::)|(<:)/)) {
154
return 'operator';
155
}
156
157
// Handle symbols
158
if(!leaving_expr && stream.match(symbol)) {
159
return 'string';
160
}
161
162
// Handle operators and Delimiters
163
if (stream.match(operators)) {
164
return 'operator';
165
}
166
167
168
// Handle Strings
169
if (stream.match(stringPrefixes)) {
170
state.tokenize = tokenStringFactory(stream.current());
171
return state.tokenize(stream, state);
172
}
173
174
if (stream.match(macro)) {
175
return 'meta';
176
}
177
178
179
if (stream.match(delimiters)) {
180
return null;
181
}
182
183
if (stream.match(keywords)) {
184
return 'keyword';
185
}
186
187
if (stream.match(builtins)) {
188
return 'builtin';
189
}
190
191
192
if (stream.match(identifiers)) {
193
state.leaving_expr=true;
194
return 'variable';
195
}
196
// Handle non-detected items
197
stream.next();
198
return ERRORCLASS;
199
}
200
201
function tokenStringFactory(delimiter) {
202
while ('rub'.indexOf(delimiter.charAt(0).toLowerCase()) >= 0) {
203
delimiter = delimiter.substr(1);
204
}
205
var singleline = delimiter.length == 1;
206
var OUTCLASS = 'string';
207
208
function tokenString(stream, state) {
209
while (!stream.eol()) {
210
stream.eatWhile(/[^'"\\]/);
211
if (stream.eat('\\')) {
212
stream.next();
213
if (singleline && stream.eol()) {
214
return OUTCLASS;
215
}
216
} else if (stream.match(delimiter)) {
217
state.tokenize = tokenBase;
218
return OUTCLASS;
219
} else {
220
stream.eat(/['"]/);
221
}
222
}
223
if (singleline) {
224
if (parserConf.singleLineStringErrors) {
225
return ERRORCLASS;
226
} else {
227
state.tokenize = tokenBase;
228
}
229
}
230
return OUTCLASS;
231
}
232
tokenString.isString = true;
233
return tokenString;
234
}
235
236
function tokenLexer(stream, state) {
237
indentInfo = null;
238
var style = state.tokenize(stream, state);
239
var current = stream.current();
240
241
// Handle '.' connected identifiers
242
if (current === '.') {
243
style = stream.match(identifiers, false) ? null : ERRORCLASS;
244
if (style === null && state.lastStyle === 'meta') {
245
// Apply 'meta' style to '.' connected identifiers when
246
// appropriate.
247
style = 'meta';
248
}
249
return style;
250
}
251
252
return style;
253
}
254
255
var external = {
256
startState: function() {
257
return {
258
tokenize: tokenBase,
259
scopes: [],
260
leaving_expr: false
261
};
262
},
263
264
token: function(stream, state) {
265
var style = tokenLexer(stream, state);
266
state.lastStyle = style;
267
return style;
268
},
269
270
indent: function(state, textAfter) {
271
var delta = 0;
272
if(textAfter=="end" || textAfter=="]" || textAfter=="}" || textAfter=="else" || textAfter=="elseif" || textAfter=="catch" || textAfter=="finally") {
273
delta = -1;
274
}
275
return (state.scopes.length + delta) * 4;
276
},
277
278
lineComment: "#",
279
fold: "indent",
280
electricChars: "edlsifyh]}"
281
};
282
return external;
283
});
284
285
286
CodeMirror.defineMIME("text/x-julia", "julia");
287
288