coffeescript.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. var COFFEE_BUILT_INS = [
  141. 'npm',
  142. 'print'
  143. ];
  144. var COFFEE_LITERALS = [
  145. 'yes',
  146. 'no',
  147. 'on',
  148. 'off'
  149. ];
  150. var 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. var NOT_VALID_KEYWORDS = [
  164. "var",
  165. "const",
  166. "let",
  167. "function",
  168. "static"
  169. ];
  170. var excluding = (list) =>
  171. (kw) => !list.includes(kw);
  172. var 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. var JS_IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
  178. var SUBST = {
  179. className: 'subst',
  180. begin: /#\{/, end: /}/,
  181. keywords: KEYWORDS$1
  182. };
  183. var EXPRESSIONS = [
  184. hljs.BINARY_NUMBER_MODE,
  185. hljs.inherit(hljs.C_NUMBER_MODE, {starts: {end: '(\\s*/)?', relevance: 0}}), // a number tries to eat the following slash to prevent treating it as a regexp
  186. {
  187. className: 'string',
  188. variants: [
  189. {
  190. begin: /'''/, end: /'''/,
  191. contains: [hljs.BACKSLASH_ESCAPE]
  192. },
  193. {
  194. begin: /'/, end: /'/,
  195. contains: [hljs.BACKSLASH_ESCAPE]
  196. },
  197. {
  198. begin: /"""/, end: /"""/,
  199. contains: [hljs.BACKSLASH_ESCAPE, SUBST]
  200. },
  201. {
  202. begin: /"/, end: /"/,
  203. contains: [hljs.BACKSLASH_ESCAPE, SUBST]
  204. }
  205. ]
  206. },
  207. {
  208. className: 'regexp',
  209. variants: [
  210. {
  211. begin: '///', end: '///',
  212. contains: [SUBST, hljs.HASH_COMMENT_MODE]
  213. },
  214. {
  215. begin: '//[gim]{0,3}(?=\\W)',
  216. relevance: 0
  217. },
  218. {
  219. // regex can't start with space to parse x / 2 / 3 as two divisions
  220. // regex can't start with *, and it supports an "illegal" in the main mode
  221. begin: /\/(?![ *]).*?(?![\\]).\/[gim]{0,3}(?=\W)/
  222. }
  223. ]
  224. },
  225. {
  226. begin: '@' + JS_IDENT_RE // relevance booster
  227. },
  228. {
  229. subLanguage: 'javascript',
  230. excludeBegin: true, excludeEnd: true,
  231. variants: [
  232. {
  233. begin: '```', end: '```',
  234. },
  235. {
  236. begin: '`', end: '`',
  237. }
  238. ]
  239. }
  240. ];
  241. SUBST.contains = EXPRESSIONS;
  242. var TITLE = hljs.inherit(hljs.TITLE_MODE, {begin: JS_IDENT_RE});
  243. var PARAMS_RE = '(\\(.*\\))?\\s*\\B[-=]>';
  244. var PARAMS = {
  245. className: 'params',
  246. begin: '\\([^\\(]', returnBegin: true,
  247. /* We need another contained nameless mode to not have every nested
  248. pair of parens to be called "params" */
  249. contains: [{
  250. begin: /\(/, end: /\)/,
  251. keywords: KEYWORDS$1,
  252. contains: ['self'].concat(EXPRESSIONS)
  253. }]
  254. };
  255. return {
  256. name: 'CoffeeScript',
  257. aliases: ['coffee', 'cson', 'iced'],
  258. keywords: KEYWORDS$1,
  259. illegal: /\/\*/,
  260. contains: EXPRESSIONS.concat([
  261. hljs.COMMENT('###', '###'),
  262. hljs.HASH_COMMENT_MODE,
  263. {
  264. className: 'function',
  265. begin: '^\\s*' + JS_IDENT_RE + '\\s*=\\s*' + PARAMS_RE, end: '[-=]>',
  266. returnBegin: true,
  267. contains: [TITLE, PARAMS]
  268. },
  269. {
  270. // anonymous function start
  271. begin: /[:\(,=]\s*/,
  272. relevance: 0,
  273. contains: [
  274. {
  275. className: 'function',
  276. begin: PARAMS_RE, end: '[-=]>',
  277. returnBegin: true,
  278. contains: [PARAMS]
  279. }
  280. ]
  281. },
  282. {
  283. className: 'class',
  284. beginKeywords: 'class',
  285. end: '$',
  286. illegal: /[:="\[\]]/,
  287. contains: [
  288. {
  289. beginKeywords: 'extends',
  290. endsWithParent: true,
  291. illegal: /[:="\[\]]/,
  292. contains: [TITLE]
  293. },
  294. TITLE
  295. ]
  296. },
  297. {
  298. begin: JS_IDENT_RE + ':', end: ':',
  299. returnBegin: true, returnEnd: true,
  300. relevance: 0
  301. }
  302. ])
  303. };
  304. }
  305. module.exports = coffeescript;