livescript.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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: LiveScript
  132. Author: Taneli Vatanen <taneli.vatanen@gmail.com>
  133. Contributors: Jen Evers-Corvina <jen@sevvie.net>
  134. Origin: coffeescript.js
  135. Description: LiveScript is a programming language that transcompiles to JavaScript. For info about language see http://livescript.net/
  136. Website: https://livescript.net
  137. Category: scripting
  138. */
  139. function livescript(hljs) {
  140. var LIVESCRIPT_BUILT_INS = [
  141. 'npm',
  142. 'print'
  143. ];
  144. var LIVESCRIPT_LITERALS = [
  145. 'yes',
  146. 'no',
  147. 'on',
  148. 'off',
  149. 'it',
  150. 'that',
  151. 'void'
  152. ];
  153. var LIVESCRIPT_KEYWORDS = [
  154. 'then',
  155. 'unless',
  156. 'until',
  157. 'loop',
  158. 'of',
  159. 'by',
  160. 'when',
  161. 'and',
  162. 'or',
  163. 'is',
  164. 'isnt',
  165. 'not',
  166. 'it',
  167. 'that',
  168. 'otherwise',
  169. 'from',
  170. 'to',
  171. 'til',
  172. 'fallthrough',
  173. 'case',
  174. 'enum',
  175. 'native',
  176. 'list',
  177. 'map',
  178. '__hasProp',
  179. '__extends',
  180. '__slice',
  181. '__bind',
  182. '__indexOf'
  183. ];
  184. var KEYWORDS$1 = {
  185. keyword: KEYWORDS.concat(LIVESCRIPT_KEYWORDS).join(" "),
  186. literal: LITERALS.concat(LIVESCRIPT_LITERALS).join(" "),
  187. built_in: BUILT_INS.concat(LIVESCRIPT_BUILT_INS).join(" ")
  188. };
  189. var JS_IDENT_RE = '[A-Za-z$_](?:\-[0-9A-Za-z$_]|[0-9A-Za-z$_])*';
  190. var TITLE = hljs.inherit(hljs.TITLE_MODE, {begin: JS_IDENT_RE});
  191. var SUBST = {
  192. className: 'subst',
  193. begin: /#\{/, end: /}/,
  194. keywords: KEYWORDS$1
  195. };
  196. var SUBST_SIMPLE = {
  197. className: 'subst',
  198. begin: /#[A-Za-z$_]/, end: /(?:\-[0-9A-Za-z$_]|[0-9A-Za-z$_])*/,
  199. keywords: KEYWORDS$1
  200. };
  201. var EXPRESSIONS = [
  202. hljs.BINARY_NUMBER_MODE,
  203. {
  204. className: 'number',
  205. begin: '(\\b0[xX][a-fA-F0-9_]+)|(\\b\\d(\\d|_\\d)*(\\.(\\d(\\d|_\\d)*)?)?(_*[eE]([-+]\\d(_\\d|\\d)*)?)?[_a-z]*)',
  206. relevance: 0,
  207. starts: {end: '(\\s*/)?', relevance: 0} // a number tries to eat the following slash to prevent treating it as a regexp
  208. },
  209. {
  210. className: 'string',
  211. variants: [
  212. {
  213. begin: /'''/, end: /'''/,
  214. contains: [hljs.BACKSLASH_ESCAPE]
  215. },
  216. {
  217. begin: /'/, end: /'/,
  218. contains: [hljs.BACKSLASH_ESCAPE]
  219. },
  220. {
  221. begin: /"""/, end: /"""/,
  222. contains: [hljs.BACKSLASH_ESCAPE, SUBST, SUBST_SIMPLE]
  223. },
  224. {
  225. begin: /"/, end: /"/,
  226. contains: [hljs.BACKSLASH_ESCAPE, SUBST, SUBST_SIMPLE]
  227. },
  228. {
  229. begin: /\\/, end: /(\s|$)/,
  230. excludeEnd: true
  231. }
  232. ]
  233. },
  234. {
  235. className: 'regexp',
  236. variants: [
  237. {
  238. begin: '//', end: '//[gim]*',
  239. contains: [SUBST, hljs.HASH_COMMENT_MODE]
  240. },
  241. {
  242. // regex can't start with space to parse x / 2 / 3 as two divisions
  243. // regex can't start with *, and it supports an "illegal" in the main mode
  244. begin: /\/(?![ *])(\\\/|.)*?\/[gim]*(?=\W)/
  245. }
  246. ]
  247. },
  248. {
  249. begin: '@' + JS_IDENT_RE
  250. },
  251. {
  252. begin: '``', end: '``',
  253. excludeBegin: true, excludeEnd: true,
  254. subLanguage: 'javascript'
  255. }
  256. ];
  257. SUBST.contains = EXPRESSIONS;
  258. var PARAMS = {
  259. className: 'params',
  260. begin: '\\(', returnBegin: true,
  261. /* We need another contained nameless mode to not have every nested
  262. pair of parens to be called "params" */
  263. contains: [
  264. {
  265. begin: /\(/, end: /\)/,
  266. keywords: KEYWORDS$1,
  267. contains: ['self'].concat(EXPRESSIONS)
  268. }
  269. ]
  270. };
  271. var SYMBOLS = {
  272. begin: '(#=>|=>|\\|>>|-?->|\\!->)'
  273. };
  274. return {
  275. name: 'LiveScript',
  276. aliases: ['ls'],
  277. keywords: KEYWORDS$1,
  278. illegal: /\/\*/,
  279. contains: EXPRESSIONS.concat([
  280. hljs.COMMENT('\\/\\*', '\\*\\/'),
  281. hljs.HASH_COMMENT_MODE,
  282. SYMBOLS, // relevance booster
  283. {
  284. className: 'function',
  285. contains: [TITLE, PARAMS],
  286. returnBegin: true,
  287. variants: [
  288. {
  289. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\))?\\s*\\B\\->\\*?', end: '\\->\\*?'
  290. },
  291. {
  292. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?!?(\\(.*\\))?\\s*\\B[-~]{1,2}>\\*?', end: '[-~]{1,2}>\\*?'
  293. },
  294. {
  295. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\))?\\s*\\B!?[-~]{1,2}>\\*?', end: '!?[-~]{1,2}>\\*?'
  296. }
  297. ]
  298. },
  299. {
  300. className: 'class',
  301. beginKeywords: 'class',
  302. end: '$',
  303. illegal: /[:="\[\]]/,
  304. contains: [
  305. {
  306. beginKeywords: 'extends',
  307. endsWithParent: true,
  308. illegal: /[:="\[\]]/,
  309. contains: [TITLE]
  310. },
  311. TITLE
  312. ]
  313. },
  314. {
  315. begin: JS_IDENT_RE + ':', end: ':',
  316. returnBegin: true, returnEnd: true,
  317. relevance: 0
  318. }
  319. ])
  320. };
  321. }
  322. module.exports = livescript;