livescript.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 case default function var with ' +
  7. // LiveScript keywords
  8. 'then unless until loop of by when and or is isnt not it that otherwise from to til fallthrough super ' +
  9. 'case default function var void const let enum export import native list map ' +
  10. '__hasProp __extends __slice __bind __indexOf',
  11. literal:
  12. // JS literals
  13. 'true false null undefined ' +
  14. // LiveScript literals
  15. 'yes no on off it that void',
  16. built_in:
  17. 'npm require console print module global window document'
  18. };
  19. var JS_IDENT_RE = '[A-Za-z$_](?:\-[0-9A-Za-z$_]|[0-9A-Za-z$_])*';
  20. var TITLE = hljs.inherit(hljs.TITLE_MODE, {begin: JS_IDENT_RE});
  21. var SUBST = {
  22. className: 'subst',
  23. begin: /#\{/, end: /}/,
  24. keywords: KEYWORDS
  25. };
  26. var SUBST_SIMPLE = {
  27. className: 'subst',
  28. begin: /#[A-Za-z$_]/, end: /(?:\-[0-9A-Za-z$_]|[0-9A-Za-z$_])*/,
  29. keywords: KEYWORDS
  30. };
  31. var EXPRESSIONS = [
  32. hljs.BINARY_NUMBER_MODE,
  33. {
  34. className: 'number',
  35. begin: '(\\b0[xX][a-fA-F0-9_]+)|(\\b\\d(\\d|_\\d)*(\\.(\\d(\\d|_\\d)*)?)?(_*[eE]([-+]\\d(_\\d|\\d)*)?)?[_a-z]*)',
  36. relevance: 0,
  37. starts: {end: '(\\s*/)?', relevance: 0} // a number tries to eat the following slash to prevent treating it as a regexp
  38. },
  39. {
  40. className: 'string',
  41. variants: [
  42. {
  43. begin: /'''/, end: /'''/,
  44. contains: [hljs.BACKSLASH_ESCAPE]
  45. },
  46. {
  47. begin: /'/, end: /'/,
  48. contains: [hljs.BACKSLASH_ESCAPE]
  49. },
  50. {
  51. begin: /"""/, end: /"""/,
  52. contains: [hljs.BACKSLASH_ESCAPE, SUBST, SUBST_SIMPLE]
  53. },
  54. {
  55. begin: /"/, end: /"/,
  56. contains: [hljs.BACKSLASH_ESCAPE, SUBST, SUBST_SIMPLE]
  57. },
  58. {
  59. begin: /\\/, end: /(\s|$)/,
  60. excludeEnd: true
  61. }
  62. ]
  63. },
  64. {
  65. className: 'regexp',
  66. variants: [
  67. {
  68. begin: '//', end: '//[gim]*',
  69. contains: [SUBST, hljs.HASH_COMMENT_MODE]
  70. },
  71. {
  72. // regex can't start with space to parse x / 2 / 3 as two divisions
  73. // regex can't start with *, and it supports an "illegal" in the main mode
  74. begin: /\/(?![ *])(\\.|[^\\\n])*?\/[gim]*(?=\W)/
  75. }
  76. ]
  77. },
  78. {
  79. begin: '@' + JS_IDENT_RE
  80. },
  81. {
  82. begin: '``', end: '``',
  83. excludeBegin: true, excludeEnd: true,
  84. subLanguage: 'javascript'
  85. }
  86. ];
  87. SUBST.contains = EXPRESSIONS;
  88. var PARAMS = {
  89. className: 'params',
  90. begin: '\\(', returnBegin: true,
  91. /* We need another contained nameless mode to not have every nested
  92. pair of parens to be called "params" */
  93. contains: [
  94. {
  95. begin: /\(/, end: /\)/,
  96. keywords: KEYWORDS,
  97. contains: ['self'].concat(EXPRESSIONS)
  98. }
  99. ]
  100. };
  101. var SYMBOLS = {
  102. begin: '(#=>|=>|\\|>>|-?->|\\!->)'
  103. };
  104. return {
  105. aliases: ['ls'],
  106. keywords: KEYWORDS,
  107. illegal: /\/\*/,
  108. contains: EXPRESSIONS.concat([
  109. hljs.COMMENT('\\/\\*', '\\*\\/'),
  110. hljs.HASH_COMMENT_MODE,
  111. SYMBOLS, // relevance booster
  112. {
  113. className: 'function',
  114. contains: [TITLE, PARAMS],
  115. returnBegin: true,
  116. variants: [
  117. {
  118. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\))?\\s*\\B\\->\\*?', end: '\\->\\*?'
  119. },
  120. {
  121. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?!?(\\(.*\\))?\\s*\\B[-~]{1,2}>\\*?', end: '[-~]{1,2}>\\*?'
  122. },
  123. {
  124. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\))?\\s*\\B!?[-~]{1,2}>\\*?', end: '!?[-~]{1,2}>\\*?'
  125. }
  126. ]
  127. },
  128. {
  129. className: 'class',
  130. beginKeywords: 'class',
  131. end: '$',
  132. illegal: /[:="\[\]]/,
  133. contains: [
  134. {
  135. beginKeywords: 'extends',
  136. endsWithParent: true,
  137. illegal: /[:="\[\]]/,
  138. contains: [TITLE]
  139. },
  140. TITLE
  141. ]
  142. },
  143. {
  144. begin: JS_IDENT_RE + ':', end: ':',
  145. returnBegin: true, returnEnd: true,
  146. relevance: 0
  147. }
  148. ])
  149. };
  150. };