livescript.js 4.4 KB

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