UNPKG

7.15 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: LiveScript
147Author: Taneli Vatanen <taneli.vatanen@gmail.com>
148Contributors: Jen Evers-Corvina <jen@sevvie.net>
149Origin: coffeescript.js
150Description: LiveScript is a programming language that transcompiles to JavaScript. For info about language see http://livescript.net/
151Website: https://livescript.net
152Category: scripting
153*/
154
155function livescript(hljs) {
156 const LIVESCRIPT_BUILT_INS = [
157 'npm',
158 'print'
159 ];
160 const LIVESCRIPT_LITERALS = [
161 'yes',
162 'no',
163 'on',
164 'off',
165 'it',
166 'that',
167 'void'
168 ];
169 const LIVESCRIPT_KEYWORDS = [
170 'then',
171 'unless',
172 'until',
173 'loop',
174 'of',
175 'by',
176 'when',
177 'and',
178 'or',
179 'is',
180 'isnt',
181 'not',
182 'it',
183 'that',
184 'otherwise',
185 'from',
186 'to',
187 'til',
188 'fallthrough',
189 'case',
190 'enum',
191 'native',
192 'list',
193 'map',
194 '__hasProp',
195 '__extends',
196 '__slice',
197 '__bind',
198 '__indexOf'
199 ];
200 const KEYWORDS$1 = {
201 keyword: KEYWORDS.concat(LIVESCRIPT_KEYWORDS),
202 literal: LITERALS.concat(LIVESCRIPT_LITERALS),
203 built_in: BUILT_INS.concat(LIVESCRIPT_BUILT_INS)
204 };
205 const JS_IDENT_RE = '[A-Za-z$_](?:-[0-9A-Za-z$_]|[0-9A-Za-z$_])*';
206 const TITLE = hljs.inherit(hljs.TITLE_MODE, {
207 begin: JS_IDENT_RE
208 });
209 const SUBST = {
210 className: 'subst',
211 begin: /#\{/,
212 end: /\}/,
213 keywords: KEYWORDS$1
214 };
215 const SUBST_SIMPLE = {
216 className: 'subst',
217 begin: /#[A-Za-z$_]/,
218 end: /(?:-[0-9A-Za-z$_]|[0-9A-Za-z$_])*/,
219 keywords: KEYWORDS$1
220 };
221 const EXPRESSIONS = [
222 hljs.BINARY_NUMBER_MODE,
223 {
224 className: 'number',
225 begin: '(\\b0[xX][a-fA-F0-9_]+)|(\\b\\d(\\d|_\\d)*(\\.(\\d(\\d|_\\d)*)?)?(_*[eE]([-+]\\d(_\\d|\\d)*)?)?[_a-z]*)',
226 relevance: 0,
227 starts: {
228 end: '(\\s*/)?',
229 relevance: 0
230 } // a number tries to eat the following slash to prevent treating it as a regexp
231 },
232 {
233 className: 'string',
234 variants: [
235 {
236 begin: /'''/,
237 end: /'''/,
238 contains: [hljs.BACKSLASH_ESCAPE]
239 },
240 {
241 begin: /'/,
242 end: /'/,
243 contains: [hljs.BACKSLASH_ESCAPE]
244 },
245 {
246 begin: /"""/,
247 end: /"""/,
248 contains: [
249 hljs.BACKSLASH_ESCAPE,
250 SUBST,
251 SUBST_SIMPLE
252 ]
253 },
254 {
255 begin: /"/,
256 end: /"/,
257 contains: [
258 hljs.BACKSLASH_ESCAPE,
259 SUBST,
260 SUBST_SIMPLE
261 ]
262 },
263 {
264 begin: /\\/,
265 end: /(\s|$)/,
266 excludeEnd: true
267 }
268 ]
269 },
270 {
271 className: 'regexp',
272 variants: [
273 {
274 begin: '//',
275 end: '//[gim]*',
276 contains: [
277 SUBST,
278 hljs.HASH_COMMENT_MODE
279 ]
280 },
281 {
282 // regex can't start with space to parse x / 2 / 3 as two divisions
283 // regex can't start with *, and it supports an "illegal" in the main mode
284 begin: /\/(?![ *])(\\.|[^\\\n])*?\/[gim]*(?=\W)/
285 }
286 ]
287 },
288 {
289 begin: '@' + JS_IDENT_RE
290 },
291 {
292 begin: '``',
293 end: '``',
294 excludeBegin: true,
295 excludeEnd: true,
296 subLanguage: 'javascript'
297 }
298 ];
299 SUBST.contains = EXPRESSIONS;
300
301 const PARAMS = {
302 className: 'params',
303 begin: '\\(',
304 returnBegin: true,
305 /* We need another contained nameless mode to not have every nested
306 pair of parens to be called "params" */
307 contains: [
308 {
309 begin: /\(/,
310 end: /\)/,
311 keywords: KEYWORDS$1,
312 contains: ['self'].concat(EXPRESSIONS)
313 }
314 ]
315 };
316
317 const SYMBOLS = {
318 begin: '(#=>|=>|\\|>>|-?->|!->)'
319 };
320
321 return {
322 name: 'LiveScript',
323 aliases: ['ls'],
324 keywords: KEYWORDS$1,
325 illegal: /\/\*/,
326 contains: EXPRESSIONS.concat([
327 hljs.COMMENT('\\/\\*', '\\*\\/'),
328 hljs.HASH_COMMENT_MODE,
329 SYMBOLS, // relevance booster
330 {
331 className: 'function',
332 contains: [
333 TITLE,
334 PARAMS
335 ],
336 returnBegin: true,
337 variants: [
338 {
339 begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\)\\s*)?\\B->\\*?',
340 end: '->\\*?'
341 },
342 {
343 begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?!?(\\(.*\\)\\s*)?\\B[-~]{1,2}>\\*?',
344 end: '[-~]{1,2}>\\*?'
345 },
346 {
347 begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\)\\s*)?\\B!?[-~]{1,2}>\\*?',
348 end: '!?[-~]{1,2}>\\*?'
349 }
350 ]
351 },
352 {
353 className: 'class',
354 beginKeywords: 'class',
355 end: '$',
356 illegal: /[:="\[\]]/,
357 contains: [
358 {
359 beginKeywords: 'extends',
360 endsWithParent: true,
361 illegal: /[:="\[\]]/,
362 contains: [TITLE]
363 },
364 TITLE
365 ]
366 },
367 {
368 begin: JS_IDENT_RE + ':',
369 end: ':',
370 returnBegin: true,
371 returnEnd: true,
372 relevance: 0
373 }
374 ])
375 };
376}
377
378export { livescript as default };