coffeescript.js 6.4 KB

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