Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50654 views
1
// Based on https://github.com/giovannicalo/brackets-coffeescript/blob/master/main.js
2
// modified by William Stein.
3
4
(function(mod) {
5
if (typeof exports == "object" && typeof module == "object") // CommonJS
6
mod(require("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
CodeMirror.defineMode("coffeescript2", function(config, parser_config) {
14
var constant_list = [
15
"false",
16
"no",
17
"null",
18
"off",
19
"on",
20
"true",
21
"undefined",
22
"Infinity",
23
"NaN"
24
];
25
var keyword_list = [
26
"and",
27
"break",
28
"by",
29
"catch",
30
"class",
31
"continue",
32
"debugger",
33
"delete",
34
"do",
35
"else",
36
"extends",
37
"finally",
38
"for",
39
"if",
40
"in",
41
"instanceof",
42
"is",
43
"isnt",
44
"loop",
45
"new",
46
"not",
47
"of",
48
"or",
49
"return",
50
"super",
51
"switch",
52
"then",
53
"this",
54
"throw",
55
"try",
56
"typeof",
57
"unless",
58
"until",
59
"when",
60
"while",
61
"yield"
62
];
63
var constant = constant_list.join("|");
64
var identifier = "[a-zA-Z\\$_][\\w\\$]*";
65
var keyword = keyword_list.join("|");
66
var number = "((?:0(?:(?:[bB][01]+)|(?:[oO][0-7]+)|(?:[xX][0-9a-fA-F]+)))|(?:[\\d]*\\.?[\\d]+(?:e[\\+\\-]\\d+)?))";
67
var regexp = "\\/((?![*+?\\s])(?:[^\\r\\n\\[/\\\\]|\\\\.|\\[(?:[^\\r\\n\\]\\\\]|\\\\.)*\\])+)\\/";
68
var regexp_flag = "\\b(([gimuy])(?![gimuy]*\\2))+\\b";
69
var not_identifier = "[^\\w\\$]";
70
var not_keyword = "[^a-z]";
71
var not_number = "([^0-9a-fA-FoxOX\\+\\-\\.]|\\.{2,})";
72
var whitespace = "[\\t ]*";
73
var xml_identifier = "[a-zA-Z:_][a-zA-Z0-9:_\\-\\.]*";
74
var xml_string = "(?:\"(?:(?:\\\")|[^\"])*\")|(?:'(?:(?:\\\')|[^'])*')";
75
var xml_value = "(?:\\{[\\s\\S]*?\\})";
76
var xml_element = "<\\/?(" + xml_identifier + ")(?: (?:" + xml_string + "|" + xml_value + "|[^<>\"'])*?)?(?:\\/)?" + whitespace + ">";
77
return {
78
token: function(stream, state) {
79
var highlight = "";
80
if (!state.isolated) {
81
if (stream.sol()) {
82
state.isolated = true;
83
} else {
84
stream.backUp(1);
85
if (stream.match(new RegExp("^" + not_identifier), false)) {
86
state.isolated = true;
87
}
88
stream.next();
89
}
90
} else if (!stream.sol()) {
91
stream.backUp(1);
92
if (!stream.match(new RegExp("^" + not_identifier), false)) {
93
state.isolated = false;
94
}
95
stream.next();
96
}
97
if (parser_config.cjsx) {
98
if (state.xml_element) {
99
if (stream.match(/^\/?>/)) {
100
state.xml_attribute = false;
101
state.xml_element = false;
102
state.xml_string = false;
103
state.xml_value = false;
104
highlight = "keyword";
105
}
106
} else if ((!state.string_interpolated) && (!state.string_literal) && (!state.regexp) && (!state.regexp_block) && (stream.match(new RegExp("^" + xml_element), false))) {
107
state.xml_element = true;
108
stream.match(new RegExp("<\\/?" + xml_identifier));
109
return "keyword";
110
}
111
if (state.xml_element) {
112
if (state.xml_attribute) {
113
if (stream.match(/^(?:\/>)|[\t=> ]/)) {
114
state.xml_attribute = false;
115
return highlight;
116
} else {
117
highlight = "number";
118
}
119
} else if ((state.isolated) && (!state.xml_string) && (!state.xml_value) && (stream.match(new RegExp("^" + identifier), false))) {
120
state.xml_attribute = true;
121
highlight = "number";
122
}
123
if (stream.match(new RegExp("^" + xml_string))) {
124
return "string";
125
}
126
if (state.xml_value) {
127
if (stream.match(/^\}/)) {
128
state.xml_value = false;
129
return "minus";
130
}
131
} else if (stream.match(new RegExp("^" + xml_value), false)) {
132
state.xml_value = true;
133
highlight = "minus";
134
}
135
if (!state.xml_value) {
136
stream.next();
137
return highlight;
138
}
139
}
140
}
141
if (state.parameter_list) {
142
if (stream.match(/^\)/, false)) {
143
state.parameter_list = false;
144
}
145
} else if (stream.match(/^\([^\n\r\(\)]*\)[\t ]*(->|=>)/, false)) {
146
state.parameter_list = true;
147
}
148
if (state.parameter) {
149
if ((stream.sol()) || (stream.match(new RegExp("^" + not_identifier), false))) {
150
state.parameter = false;
151
} else {
152
highlight = "def";
153
}
154
}
155
if ((state.parameter_list) && (stream.match(new RegExp("^" + identifier), false))) {
156
state.parameter = true;
157
highlight = "def";
158
}
159
if ((state.isolated) && (!state.string_interpolated) && (!state.string_literal) && (!state.comment_block) && (!state.comment_line) && (stream.match(new RegExp("^@")))) {
160
state.method = true;
161
return "keyword";
162
}
163
if (state.keyword) {
164
if ((stream.sol()) || (stream.match(new RegExp("^" + not_keyword), false))) {
165
state.keyword = false;
166
} else {
167
highlight = "keyword";
168
}
169
}
170
if ((state.isolated) && (stream.match(new RegExp("^(" + keyword + ")(" + not_identifier + "|$)"), false))) {
171
state.keyword = true;
172
highlight = "keyword";
173
}
174
if (state.constant) {
175
if ((stream.sol()) || (stream.match(new RegExp("^" + not_keyword), false))) {
176
state.constant = false;
177
} else {
178
highlight = "string";
179
}
180
}
181
if ((state.isolated) && (stream.match(new RegExp("^(" + constant + ")(" + not_identifier + "|$)"), false))) {
182
state.constant = true;
183
highlight = "string";
184
}
185
if (state.function) {
186
if ((stream.sol()) || (stream.match(/^(:|=)/, false))) {
187
state.function = false;
188
} else {
189
highlight = "def";
190
}
191
}
192
if (stream.match(new RegExp("^" + identifier + whitespace + "(:|=)" + whitespace + "(\\([^\\n\\r]+\\))?" + whitespace + "(->|=>)"), false)) {
193
state.function = true;
194
highlight = "def";
195
}
196
if (state.property) {
197
if ((stream.sol()) || (stream.match(/^:/, false))) {
198
state.property = false;
199
} else {
200
highlight = "def";
201
}
202
} else if ((!state.regexp) && (!state.regexp_block) && (!state.string_interpolated) && (!state.string_literal) && (stream.match(new RegExp("^(" + identifier + "|((\"|')?(?:(?:(?!\\3).)|\\\\\\3)*\\3))" + whitespace + ":"), false))) {
203
state.property = true;
204
highlight = "def";
205
}
206
if (state.variable) {
207
if ((stream.sol()) || (stream.match(/^[=\[]/, false))) {
208
state.variable = false;
209
} else {
210
highlight = "def";
211
}
212
}
213
if (stream.match(new RegExp("^" + identifier + "(\\[.*\\])*" + whitespace + "=([^=]|$)"), false)) {
214
state.variable = true;
215
highlight = "def";
216
}
217
if (state.method) {
218
if ((stream.sol()) || (stream.match(new RegExp("^" + not_identifier), false))) {
219
state.method = false;
220
} else {
221
highlight = "def";
222
}
223
}
224
if ((stream.match(new RegExp("^\\." + identifier), false))) {
225
state.method = true;
226
}
227
if (state.number) {
228
if ((stream.sol()) || (stream.match(new RegExp("^" + not_number), false))) {
229
state.number = false;
230
} else {
231
highlight = "number";
232
}
233
}
234
if ((state.isolated) && (stream.match(new RegExp("^" + number + "(" + not_identifier + "|$)"), false))) {
235
stream.backUp(1);
236
if (!stream.match(/^\.{2,}/, false)) {
237
state.number = true;
238
highlight = "number";
239
}
240
stream.next();
241
}
242
if (state.string_interpolated) {
243
if ((stream.match(/^\\{2}/, false)) || (stream.match(/^\\"/, false))) {
244
highlight = "string";
245
stream.next();
246
} else if (stream.match(/^"/, false)) {
247
state.string_interpolated = false;
248
highlight = "string";
249
} else {
250
highlight = "string";
251
}
252
} else if ((!state.comment_block) && (!state.comment_line) && (!state.regexp) && (!state.regexp_block) && (!state.property) && (!state.string_literal) && (stream.match(/^"/, false))) {
253
state.string_interpolated = true;
254
highlight = "string";
255
}
256
if (state.string_literal) {
257
if ((stream.match(/^\\{2}/, false)) || (stream.match(/^\\'/, false))) {
258
highlight = "string";
259
stream.next();
260
} else if (stream.match(/^'/, false)) {
261
state.string_literal = false;
262
highlight = "string";
263
} else {
264
highlight = "string";
265
}
266
} else if ((!state.comment_block) && (!state.comment_line) && (!state.regexp) && (!state.regexp_block) && (!state.property) && (!state.string_interpolated) && (stream.match(/^'/, false))) {
267
state.string_literal = true;
268
highlight = "string";
269
}
270
if (state.regexp_block) {
271
if (stream.match(/^\/{3}/, false)) {
272
state.regexp_block = false;
273
highlight = "string";
274
stream.next();
275
stream.next();
276
stream.next();
277
stream.match(new RegExp("^" + regexp_flag));
278
stream.backUp(1);
279
} else {
280
highlight = "string";
281
}
282
} else if ((!state.string_interpolated) && (!state.string_literal) && (stream.match(/^\/{3}/, false))) {
283
state.regexp_block = true;
284
highlight = "string";
285
}
286
if (state.regexp) {
287
if (stream.match(/^\\\\\//, false)) {
288
state.regexp = false;
289
highlight = "string";
290
stream.next();
291
stream.next();
292
stream.next();
293
stream.match(new RegExp("^" + regexp_flag));
294
stream.backUp(1);
295
} else if (stream.match(/^\\\//, false)) {
296
highlight = "string";
297
stream.next();
298
} else if ((stream.sol()) || (stream.match(/^\//, false))) {
299
state.regexp = false;
300
highlight = "string";
301
stream.next();
302
stream.match(new RegExp("^" + regexp_flag));
303
stream.backUp(1);
304
} else {
305
highlight = "string";
306
}
307
} else if ((!state.regexp_block) && (!state.string_interpolated) && (!state.string_literal) && (stream.match(new RegExp("^" + regexp), false))) {
308
state.regexp = true;
309
highlight = "string";
310
}
311
if (state.comment_block) {
312
if (stream.match(/^#{3}/, false)) {
313
state.comment_block = false;
314
highlight = "comment";
315
stream.next();
316
stream.next();
317
} else {
318
highlight = "comment";
319
}
320
} else if ((!state.regexp) && (!state.regexp_block) && (!state.string_interpolated) && (!state.string_literal) && (stream.match(/^#{3}/, false))) {
321
state.comment_block = true;
322
highlight = "comment";
323
stream.next();
324
stream.next();
325
}
326
if (stream.sol()) {
327
state.comment_line = false;
328
}
329
if (state.comment_line) {
330
highlight = "comment";
331
} else if ((!state.comment_block) && (!state.regexp) && (!state.regexp_block) && (!state.string_interpolated) && (!state.string_literal) && (stream.match(/^#/, false))) {
332
if (stream.column() > 1) {
333
stream.backUp(2);
334
if (!stream.match(/^#{3}/, false)) {
335
state.comment_line = true;
336
highlight = "comment";
337
}
338
stream.next();
339
stream.next();
340
} else {
341
state.comment_line = true;
342
highlight = "comment";
343
}
344
} else if ((state.regexp_block) && (stream.match(/^[\t ]+#/, false))) {
345
state.comment_line = true;
346
highlight = "comment";
347
}
348
if (state.string_interpolation) {
349
if ((!state.comment_block) && (!state.regexp) && (!state.regexp_block) && (!state.string_interpolated) && (!state.string_literal) && (stream.match(/^\}/, false))) {
350
state.string_interpolation = false;
351
state.string_interpolated = true;
352
highlight = "minus";
353
}
354
} else if ((state.string_interpolated) && (stream.match(/^#\{/, false))) {
355
state.string_interpolation = true;
356
state.string_interpolated = false;
357
highlight = "minus";
358
stream.next();
359
}
360
stream.next();
361
return highlight;
362
},
363
startState: function() {
364
return {
365
comment_block: false,
366
comment_line: false,
367
constant: false,
368
function: false,
369
isolated: false,
370
keyword: false,
371
method: false,
372
number: false,
373
parameter: false,
374
parameter_list: false,
375
property: false,
376
regexp: false,
377
regexp_block: false,
378
string_interpolated: false,
379
string_interpolation: false,
380
string_literal: false,
381
variable: false,
382
xml_attribute: false,
383
xml_element: false,
384
xml_string: false,
385
xml_value: false
386
};
387
},
388
lineComment: "#",
389
fold: "indent"
390
};
391
});
392
CodeMirror.defineMIME("text/cjsx", {
393
cjsx: true,
394
name: "coffeescript2"
395
});
396
CodeMirror.defineMIME("text/coffeescript2", {
397
cjsx: false,
398
name: "coffeescript2"
399
});
400
});
401
402