UNPKG

6.82 kBJavaScriptView Raw
1const KEYWORDS = [
2 "as", // for exports
3 "in",
4 "of",
5 "if",
6 "for",
7 "while",
8 "finally",
9 "var",
10 "new",
11 "function",
12 "do",
13 "return",
14 "void",
15 "else",
16 "break",
17 "catch",
18 "instanceof",
19 "with",
20 "throw",
21 "case",
22 "default",
23 "try",
24 "switch",
25 "continue",
26 "typeof",
27 "delete",
28 "let",
29 "yield",
30 "const",
31 "class",
32 // JS handles these with a special rule
33 // "get",
34 // "set",
35 "debugger",
36 "async",
37 "await",
38 "static",
39 "import",
40 "from",
41 "export",
42 "extends"
43];
44const LITERALS = [
45 "true",
46 "false",
47 "null",
48 "undefined",
49 "NaN",
50 "Infinity"
51];
52
53// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
54const TYPES = [
55 // Fundamental objects
56 "Object",
57 "Function",
58 "Boolean",
59 "Symbol",
60 // numbers and dates
61 "Math",
62 "Date",
63 "Number",
64 "BigInt",
65 // text
66 "String",
67 "RegExp",
68 // Indexed collections
69 "Array",
70 "Float32Array",
71 "Float64Array",
72 "Int8Array",
73 "Uint8Array",
74 "Uint8ClampedArray",
75 "Int16Array",
76 "Int32Array",
77 "Uint16Array",
78 "Uint32Array",
79 "BigInt64Array",
80 "BigUint64Array",
81 // Keyed collections
82 "Set",
83 "Map",
84 "WeakSet",
85 "WeakMap",
86 // Structured data
87 "ArrayBuffer",
88 "SharedArrayBuffer",
89 "Atomics",
90 "DataView",
91 "JSON",
92 // Control abstraction objects
93 "Promise",
94 "Generator",
95 "GeneratorFunction",
96 "AsyncFunction",
97 // Reflection
98 "Reflect",
99 "Proxy",
100 // Internationalization
101 "Intl",
102 // WebAssembly
103 "WebAssembly"
104];
105
106const ERROR_TYPES = [
107 "Error",
108 "EvalError",
109 "InternalError",
110 "RangeError",
111 "ReferenceError",
112 "SyntaxError",
113 "TypeError",
114 "URIError"
115];
116
117const BUILT_IN_GLOBALS = [
118 "setInterval",
119 "setTimeout",
120 "clearInterval",
121 "clearTimeout",
122
123 "require",
124 "exports",
125
126 "eval",
127 "isFinite",
128 "isNaN",
129 "parseFloat",
130 "parseInt",
131 "decodeURI",
132 "decodeURIComponent",
133 "encodeURI",
134 "encodeURIComponent",
135 "escape",
136 "unescape"
137];
138
139const BUILT_INS = [].concat(
140 BUILT_IN_GLOBALS,
141 TYPES,
142 ERROR_TYPES
143);
144
145/*
146Language: CoffeeScript
147Author: Dmytrii Nagirniak <dnagir@gmail.com>
148Contributors: Oleg Efimov <efimovov@gmail.com>, Cédric Néhémie <cedric.nehemie@gmail.com>
149Description: CoffeeScript is a programming language that transcompiles to JavaScript. For info about language see http://coffeescript.org/
150Category: scripting
151Website: https://coffeescript.org
152*/
153
154/** @type LanguageFn */
155function coffeescript(hljs) {
156 const COFFEE_BUILT_INS = [
157 'npm',
158 'print'
159 ];
160 const COFFEE_LITERALS = [
161 'yes',
162 'no',
163 'on',
164 'off'
165 ];
166 const COFFEE_KEYWORDS = [
167 'then',
168 'unless',
169 'until',
170 'loop',
171 'by',
172 'when',
173 'and',
174 'or',
175 'is',
176 'isnt',
177 'not'
178 ];
179 const NOT_VALID_KEYWORDS = [
180 "var",
181 "const",
182 "let",
183 "function",
184 "static"
185 ];
186 const excluding = (list) =>
187 (kw) => !list.includes(kw);
188 const KEYWORDS$1 = {
189 keyword: KEYWORDS.concat(COFFEE_KEYWORDS).filter(excluding(NOT_VALID_KEYWORDS)),
190 literal: LITERALS.concat(COFFEE_LITERALS),
191 built_in: BUILT_INS.concat(COFFEE_BUILT_INS)
192 };
193 const JS_IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
194 const SUBST = {
195 className: 'subst',
196 begin: /#\{/,
197 end: /\}/,
198 keywords: KEYWORDS$1
199 };
200 const EXPRESSIONS = [
201 hljs.BINARY_NUMBER_MODE,
202 hljs.inherit(hljs.C_NUMBER_MODE, { starts: {
203 end: '(\\s*/)?',
204 relevance: 0
205 } }), // a number tries to eat the following slash to prevent treating it as a regexp
206 {
207 className: 'string',
208 variants: [
209 {
210 begin: /'''/,
211 end: /'''/,
212 contains: [ hljs.BACKSLASH_ESCAPE ]
213 },
214 {
215 begin: /'/,
216 end: /'/,
217 contains: [ hljs.BACKSLASH_ESCAPE ]
218 },
219 {
220 begin: /"""/,
221 end: /"""/,
222 contains: [
223 hljs.BACKSLASH_ESCAPE,
224 SUBST
225 ]
226 },
227 {
228 begin: /"/,
229 end: /"/,
230 contains: [
231 hljs.BACKSLASH_ESCAPE,
232 SUBST
233 ]
234 }
235 ]
236 },
237 {
238 className: 'regexp',
239 variants: [
240 {
241 begin: '///',
242 end: '///',
243 contains: [
244 SUBST,
245 hljs.HASH_COMMENT_MODE
246 ]
247 },
248 {
249 begin: '//[gim]{0,3}(?=\\W)',
250 relevance: 0
251 },
252 {
253 // regex can't start with space to parse x / 2 / 3 as two divisions
254 // regex can't start with *, and it supports an "illegal" in the main mode
255 begin: /\/(?![ *]).*?(?![\\]).\/[gim]{0,3}(?=\W)/ }
256 ]
257 },
258 { begin: '@' + JS_IDENT_RE // relevance booster
259 },
260 {
261 subLanguage: 'javascript',
262 excludeBegin: true,
263 excludeEnd: true,
264 variants: [
265 {
266 begin: '```',
267 end: '```'
268 },
269 {
270 begin: '`',
271 end: '`'
272 }
273 ]
274 }
275 ];
276 SUBST.contains = EXPRESSIONS;
277
278 const TITLE = hljs.inherit(hljs.TITLE_MODE, { begin: JS_IDENT_RE });
279 const POSSIBLE_PARAMS_RE = '(\\(.*\\)\\s*)?\\B[-=]>';
280 const PARAMS = {
281 className: 'params',
282 begin: '\\([^\\(]',
283 returnBegin: true,
284 /* We need another contained nameless mode to not have every nested
285 pair of parens to be called "params" */
286 contains: [
287 {
288 begin: /\(/,
289 end: /\)/,
290 keywords: KEYWORDS$1,
291 contains: [ 'self' ].concat(EXPRESSIONS)
292 }
293 ]
294 };
295
296 const CLASS_DEFINITION = {
297 variants: [
298 { match: [
299 /class\s+/,
300 JS_IDENT_RE,
301 /\s+extends\s+/,
302 JS_IDENT_RE
303 ] },
304 { match: [
305 /class\s+/,
306 JS_IDENT_RE
307 ] }
308 ],
309 scope: {
310 2: "title.class",
311 4: "title.class.inherited"
312 },
313 keywords: KEYWORDS$1
314 };
315
316 return {
317 name: 'CoffeeScript',
318 aliases: [
319 'coffee',
320 'cson',
321 'iced'
322 ],
323 keywords: KEYWORDS$1,
324 illegal: /\/\*/,
325 contains: [
326 ...EXPRESSIONS,
327 hljs.COMMENT('###', '###'),
328 hljs.HASH_COMMENT_MODE,
329 {
330 className: 'function',
331 begin: '^\\s*' + JS_IDENT_RE + '\\s*=\\s*' + POSSIBLE_PARAMS_RE,
332 end: '[-=]>',
333 returnBegin: true,
334 contains: [
335 TITLE,
336 PARAMS
337 ]
338 },
339 {
340 // anonymous function start
341 begin: /[:\(,=]\s*/,
342 relevance: 0,
343 contains: [
344 {
345 className: 'function',
346 begin: POSSIBLE_PARAMS_RE,
347 end: '[-=]>',
348 returnBegin: true,
349 contains: [ PARAMS ]
350 }
351 ]
352 },
353 CLASS_DEFINITION,
354 {
355 begin: JS_IDENT_RE + ':',
356 end: ':',
357 returnBegin: true,
358 returnEnd: true,
359 relevance: 0
360 }
361 ]
362 };
363}
364
365module.exports = coffeescript;