coffeescript.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. module.exports = function(hljs) {
  2. var KEYWORDS = {
  3. keyword:
  4. // JS keywords
  5. 'in if for while finally new do return else break catch instanceof throw try this ' +
  6. 'switch continue typeof delete debugger super yield import export from as default await ' +
  7. // Coffee keywords
  8. 'then unless until loop of by when and or is isnt not',
  9. literal:
  10. // JS literals
  11. 'true false null undefined ' +
  12. // Coffee literals
  13. 'yes no on off',
  14. built_in:
  15. 'npm require console print module global window document'
  16. };
  17. var JS_IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
  18. var SUBST = {
  19. className: 'subst',
  20. begin: /#\{/, end: /}/,
  21. keywords: KEYWORDS
  22. };
  23. var EXPRESSIONS = [
  24. hljs.BINARY_NUMBER_MODE,
  25. 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
  26. {
  27. className: 'string',
  28. variants: [
  29. {
  30. begin: /'''/, end: /'''/,
  31. contains: [hljs.BACKSLASH_ESCAPE]
  32. },
  33. {
  34. begin: /'/, end: /'/,
  35. contains: [hljs.BACKSLASH_ESCAPE]
  36. },
  37. {
  38. begin: /"""/, end: /"""/,
  39. contains: [hljs.BACKSLASH_ESCAPE, SUBST]
  40. },
  41. {
  42. begin: /"/, end: /"/,
  43. contains: [hljs.BACKSLASH_ESCAPE, SUBST]
  44. }
  45. ]
  46. },
  47. {
  48. className: 'regexp',
  49. variants: [
  50. {
  51. begin: '///', end: '///',
  52. contains: [SUBST, hljs.HASH_COMMENT_MODE]
  53. },
  54. {
  55. begin: '//[gim]{0,3}(?=\\W)',
  56. relevance: 0
  57. },
  58. {
  59. // regex can't start with space to parse x / 2 / 3 as two divisions
  60. // regex can't start with *, and it supports an "illegal" in the main mode
  61. begin: /\/(?![ *]).*?(?![\\]).\/[gim]{0,3}(?=\W)/
  62. }
  63. ]
  64. },
  65. {
  66. begin: '@' + JS_IDENT_RE // relevance booster
  67. },
  68. {
  69. subLanguage: 'javascript',
  70. excludeBegin: true, excludeEnd: true,
  71. variants: [
  72. {
  73. begin: '```', end: '```',
  74. },
  75. {
  76. begin: '`', end: '`',
  77. }
  78. ]
  79. }
  80. ];
  81. SUBST.contains = EXPRESSIONS;
  82. var TITLE = hljs.inherit(hljs.TITLE_MODE, {begin: JS_IDENT_RE});
  83. var PARAMS_RE = '(\\(.*\\))?\\s*\\B[-=]>';
  84. var PARAMS = {
  85. className: 'params',
  86. begin: '\\([^\\(]', returnBegin: true,
  87. /* We need another contained nameless mode to not have every nested
  88. pair of parens to be called "params" */
  89. contains: [{
  90. begin: /\(/, end: /\)/,
  91. keywords: KEYWORDS,
  92. contains: ['self'].concat(EXPRESSIONS)
  93. }]
  94. };
  95. return {
  96. aliases: ['coffee', 'cson', 'iced'],
  97. keywords: KEYWORDS,
  98. illegal: /\/\*/,
  99. contains: EXPRESSIONS.concat([
  100. hljs.COMMENT('###', '###'),
  101. hljs.HASH_COMMENT_MODE,
  102. {
  103. className: 'function',
  104. begin: '^\\s*' + JS_IDENT_RE + '\\s*=\\s*' + PARAMS_RE, end: '[-=]>',
  105. returnBegin: true,
  106. contains: [TITLE, PARAMS]
  107. },
  108. {
  109. // anonymous function start
  110. begin: /[:\(,=]\s*/,
  111. relevance: 0,
  112. contains: [
  113. {
  114. className: 'function',
  115. begin: PARAMS_RE, end: '[-=]>',
  116. returnBegin: true,
  117. contains: [PARAMS]
  118. }
  119. ]
  120. },
  121. {
  122. className: 'class',
  123. beginKeywords: 'class',
  124. end: '$',
  125. illegal: /[:="\[\]]/,
  126. contains: [
  127. {
  128. beginKeywords: 'extends',
  129. endsWithParent: true,
  130. illegal: /[:="\[\]]/,
  131. contains: [TITLE]
  132. },
  133. TITLE
  134. ]
  135. },
  136. {
  137. begin: JS_IDENT_RE + ':', end: ':',
  138. returnBegin: true, returnEnd: true,
  139. relevance: 0
  140. }
  141. ])
  142. };
  143. };