You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
6.4 KiB

6 months ago
  1. const 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. ];
  44. const LITERALS = [
  45. "true",
  46. "false",
  47. "null",
  48. "undefined",
  49. "NaN",
  50. "Infinity"
  51. ];
  52. const TYPES = [
  53. "Intl",
  54. "DataView",
  55. "Number",
  56. "Math",
  57. "Date",
  58. "String",
  59. "RegExp",
  60. "Object",
  61. "Function",
  62. "Boolean",
  63. "Error",
  64. "Symbol",
  65. "Set",
  66. "Map",
  67. "WeakSet",
  68. "WeakMap",
  69. "Proxy",
  70. "Reflect",
  71. "JSON",
  72. "Promise",
  73. "Float64Array",
  74. "Int16Array",
  75. "Int32Array",
  76. "Int8Array",
  77. "Uint16Array",
  78. "Uint32Array",
  79. "Float32Array",
  80. "Array",
  81. "Uint8Array",
  82. "Uint8ClampedArray",
  83. "ArrayBuffer",
  84. "BigInt64Array",
  85. "BigUint64Array",
  86. "BigInt"
  87. ];
  88. const ERROR_TYPES = [
  89. "EvalError",
  90. "InternalError",
  91. "RangeError",
  92. "ReferenceError",
  93. "SyntaxError",
  94. "TypeError",
  95. "URIError"
  96. ];
  97. const BUILT_IN_GLOBALS = [
  98. "setInterval",
  99. "setTimeout",
  100. "clearInterval",
  101. "clearTimeout",
  102. "require",
  103. "exports",
  104. "eval",
  105. "isFinite",
  106. "isNaN",
  107. "parseFloat",
  108. "parseInt",
  109. "decodeURI",
  110. "decodeURIComponent",
  111. "encodeURI",
  112. "encodeURIComponent",
  113. "escape",
  114. "unescape"
  115. ];
  116. const BUILT_IN_VARIABLES = [
  117. "arguments",
  118. "this",
  119. "super",
  120. "console",
  121. "window",
  122. "document",
  123. "localStorage",
  124. "module",
  125. "global" // Node.js
  126. ];
  127. const BUILT_INS = [].concat(
  128. BUILT_IN_GLOBALS,
  129. BUILT_IN_VARIABLES,
  130. TYPES,
  131. ERROR_TYPES
  132. );
  133. /*
  134. Language: CoffeeScript
  135. Author: Dmytrii Nagirniak <dnagir@gmail.com>
  136. Contributors: Oleg Efimov <efimovov@gmail.com>, Cédric Néhémie <cedric.nehemie@gmail.com>
  137. Description: CoffeeScript is a programming language that transcompiles to JavaScript. For info about language see http://coffeescript.org/
  138. Category: common, scripting
  139. Website: https://coffeescript.org
  140. */
  141. /** @type LanguageFn */
  142. function coffeescript(hljs) {
  143. const COFFEE_BUILT_INS = [
  144. 'npm',
  145. 'print'
  146. ];
  147. const COFFEE_LITERALS = [
  148. 'yes',
  149. 'no',
  150. 'on',
  151. 'off'
  152. ];
  153. const COFFEE_KEYWORDS = [
  154. 'then',
  155. 'unless',
  156. 'until',
  157. 'loop',
  158. 'by',
  159. 'when',
  160. 'and',
  161. 'or',
  162. 'is',
  163. 'isnt',
  164. 'not'
  165. ];
  166. const NOT_VALID_KEYWORDS = [
  167. "var",
  168. "const",
  169. "let",
  170. "function",
  171. "static"
  172. ];
  173. const excluding = (list) =>
  174. (kw) => !list.includes(kw);
  175. const KEYWORDS$1 = {
  176. keyword: KEYWORDS.concat(COFFEE_KEYWORDS).filter(excluding(NOT_VALID_KEYWORDS)),
  177. literal: LITERALS.concat(COFFEE_LITERALS),
  178. built_in: BUILT_INS.concat(COFFEE_BUILT_INS)
  179. };
  180. const JS_IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
  181. const SUBST = {
  182. className: 'subst',
  183. begin: /#\{/,
  184. end: /\}/,
  185. keywords: KEYWORDS$1
  186. };
  187. const EXPRESSIONS = [
  188. hljs.BINARY_NUMBER_MODE,
  189. hljs.inherit(hljs.C_NUMBER_MODE, {
  190. starts: {
  191. end: '(\\s*/)?',
  192. relevance: 0
  193. }
  194. }), // a number tries to eat the following slash to prevent treating it as a regexp
  195. {
  196. className: 'string',
  197. variants: [
  198. {
  199. begin: /'''/,
  200. end: /'''/,
  201. contains: [hljs.BACKSLASH_ESCAPE]
  202. },
  203. {
  204. begin: /'/,
  205. end: /'/,
  206. contains: [hljs.BACKSLASH_ESCAPE]
  207. },
  208. {
  209. begin: /"""/,
  210. end: /"""/,
  211. contains: [
  212. hljs.BACKSLASH_ESCAPE,
  213. SUBST
  214. ]
  215. },
  216. {
  217. begin: /"/,
  218. end: /"/,
  219. contains: [
  220. hljs.BACKSLASH_ESCAPE,
  221. SUBST
  222. ]
  223. }
  224. ]
  225. },
  226. {
  227. className: 'regexp',
  228. variants: [
  229. {
  230. begin: '///',
  231. end: '///',
  232. contains: [
  233. SUBST,
  234. hljs.HASH_COMMENT_MODE
  235. ]
  236. },
  237. {
  238. begin: '//[gim]{0,3}(?=\\W)',
  239. relevance: 0
  240. },
  241. {
  242. // regex can't start with space to parse x / 2 / 3 as two divisions
  243. // regex can't start with *, and it supports an "illegal" in the main mode
  244. begin: /\/(?![ *]).*?(?![\\]).\/[gim]{0,3}(?=\W)/
  245. }
  246. ]
  247. },
  248. {
  249. begin: '@' + JS_IDENT_RE // relevance booster
  250. },
  251. {
  252. subLanguage: 'javascript',
  253. excludeBegin: true,
  254. excludeEnd: true,
  255. variants: [
  256. {
  257. begin: '```',
  258. end: '```'
  259. },
  260. {
  261. begin: '`',
  262. end: '`'
  263. }
  264. ]
  265. }
  266. ];
  267. SUBST.contains = EXPRESSIONS;
  268. const TITLE = hljs.inherit(hljs.TITLE_MODE, {
  269. begin: JS_IDENT_RE
  270. });
  271. const POSSIBLE_PARAMS_RE = '(\\(.*\\)\\s*)?\\B[-=]>';
  272. const PARAMS = {
  273. className: 'params',
  274. begin: '\\([^\\(]',
  275. returnBegin: true,
  276. /* We need another contained nameless mode to not have every nested
  277. pair of parens to be called "params" */
  278. contains: [{
  279. begin: /\(/,
  280. end: /\)/,
  281. keywords: KEYWORDS$1,
  282. contains: ['self'].concat(EXPRESSIONS)
  283. }]
  284. };
  285. return {
  286. name: 'CoffeeScript',
  287. aliases: [
  288. 'coffee',
  289. 'cson',
  290. 'iced'
  291. ],
  292. keywords: KEYWORDS$1,
  293. illegal: /\/\*/,
  294. contains: EXPRESSIONS.concat([
  295. hljs.COMMENT('###', '###'),
  296. hljs.HASH_COMMENT_MODE,
  297. {
  298. className: 'function',
  299. begin: '^\\s*' + JS_IDENT_RE + '\\s*=\\s*' + POSSIBLE_PARAMS_RE,
  300. end: '[-=]>',
  301. returnBegin: true,
  302. contains: [
  303. TITLE,
  304. PARAMS
  305. ]
  306. },
  307. {
  308. // anonymous function start
  309. begin: /[:\(,=]\s*/,
  310. relevance: 0,
  311. contains: [{
  312. className: 'function',
  313. begin: POSSIBLE_PARAMS_RE,
  314. end: '[-=]>',
  315. returnBegin: true,
  316. contains: [PARAMS]
  317. }]
  318. },
  319. {
  320. className: 'class',
  321. beginKeywords: 'class',
  322. end: '$',
  323. illegal: /[:="\[\]]/,
  324. contains: [
  325. {
  326. beginKeywords: 'extends',
  327. endsWithParent: true,
  328. illegal: /[:="\[\]]/,
  329. contains: [TITLE]
  330. },
  331. TITLE
  332. ]
  333. },
  334. {
  335. begin: JS_IDENT_RE + ':',
  336. end: ':',
  337. returnBegin: true,
  338. returnEnd: true,
  339. relevance: 0
  340. }
  341. ])
  342. };
  343. }
  344. module.exports = coffeescript;