coffeescript.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. Language: CoffeeScript
  3. Author: Dmytrii Nagirniak <[email protected]>
  4. Contributors: Oleg Efimov <[email protected]>, Cédric Néhémie <[email protected]>
  5. Description: CoffeeScript is a programming language that transcompiles to JavaScript. For info about language see http://coffeescript.org/
  6. Category: common, scripting
  7. */
  8. function(hljs) {
  9. var KEYWORDS = {
  10. keyword:
  11. // JS keywords
  12. 'in if for while finally new do return else break catch instanceof throw try this ' +
  13. 'switch continue typeof delete debugger super ' +
  14. // Coffee keywords
  15. 'then unless until loop of by when and or is isnt not',
  16. literal:
  17. // JS literals
  18. 'true false null undefined ' +
  19. // Coffee literals
  20. 'yes no on off',
  21. built_in:
  22. 'npm require console print module global window document'
  23. };
  24. var JS_IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
  25. var SUBST = {
  26. className: 'subst',
  27. begin: /#\{/, end: /}/,
  28. keywords: KEYWORDS
  29. };
  30. var EXPRESSIONS = [
  31. hljs.BINARY_NUMBER_MODE,
  32. 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
  33. {
  34. className: 'string',
  35. variants: [
  36. {
  37. begin: /'''/, end: /'''/,
  38. contains: [hljs.BACKSLASH_ESCAPE]
  39. },
  40. {
  41. begin: /'/, end: /'/,
  42. contains: [hljs.BACKSLASH_ESCAPE]
  43. },
  44. {
  45. begin: /"""/, end: /"""/,
  46. contains: [hljs.BACKSLASH_ESCAPE, SUBST]
  47. },
  48. {
  49. begin: /"/, end: /"/,
  50. contains: [hljs.BACKSLASH_ESCAPE, SUBST]
  51. }
  52. ]
  53. },
  54. {
  55. className: 'regexp',
  56. variants: [
  57. {
  58. begin: '///', end: '///',
  59. contains: [SUBST, hljs.HASH_COMMENT_MODE]
  60. },
  61. {
  62. begin: '//[gim]*',
  63. relevance: 0
  64. },
  65. {
  66. // regex can't start with space to parse x / 2 / 3 as two divisions
  67. // regex can't start with *, and it supports an "illegal" in the main mode
  68. begin: /\/(?![ *])(\\\/|.)*?\/[gim]*(?=\W|$)/
  69. }
  70. ]
  71. },
  72. {
  73. begin: '@' + JS_IDENT_RE // relevance booster
  74. },
  75. {
  76. begin: '`', end: '`',
  77. excludeBegin: true, excludeEnd: true,
  78. subLanguage: 'javascript'
  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. }