livescript.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. const LIVESCRIPT_BUILT_INS = [
  141. 'npm',
  142. 'print'
  143. ];
  144. const LIVESCRIPT_LITERALS = [
  145. 'yes',
  146. 'no',
  147. 'on',
  148. 'off',
  149. 'it',
  150. 'that',
  151. 'void'
  152. ];
  153. const 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. const 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. const JS_IDENT_RE = '[A-Za-z$_](?:-[0-9A-Za-z$_]|[0-9A-Za-z$_])*';
  190. const TITLE = hljs.inherit(hljs.TITLE_MODE, {
  191. begin: JS_IDENT_RE
  192. });
  193. const SUBST = {
  194. className: 'subst',
  195. begin: /#\{/,
  196. end: /\}/,
  197. keywords: KEYWORDS$1
  198. };
  199. const SUBST_SIMPLE = {
  200. className: 'subst',
  201. begin: /#[A-Za-z$_]/,
  202. end: /(?:-[0-9A-Za-z$_]|[0-9A-Za-z$_])*/,
  203. keywords: KEYWORDS$1
  204. };
  205. const EXPRESSIONS = [
  206. hljs.BINARY_NUMBER_MODE,
  207. {
  208. className: 'number',
  209. begin: '(\\b0[xX][a-fA-F0-9_]+)|(\\b\\d(\\d|_\\d)*(\\.(\\d(\\d|_\\d)*)?)?(_*[eE]([-+]\\d(_\\d|\\d)*)?)?[_a-z]*)',
  210. relevance: 0,
  211. starts: {
  212. end: '(\\s*/)?',
  213. relevance: 0
  214. } // a number tries to eat the following slash to prevent treating it as a regexp
  215. },
  216. {
  217. className: 'string',
  218. variants: [
  219. {
  220. begin: /'''/,
  221. end: /'''/,
  222. contains: [hljs.BACKSLASH_ESCAPE]
  223. },
  224. {
  225. begin: /'/,
  226. end: /'/,
  227. contains: [hljs.BACKSLASH_ESCAPE]
  228. },
  229. {
  230. begin: /"""/,
  231. end: /"""/,
  232. contains: [
  233. hljs.BACKSLASH_ESCAPE,
  234. SUBST,
  235. SUBST_SIMPLE
  236. ]
  237. },
  238. {
  239. begin: /"/,
  240. end: /"/,
  241. contains: [
  242. hljs.BACKSLASH_ESCAPE,
  243. SUBST,
  244. SUBST_SIMPLE
  245. ]
  246. },
  247. {
  248. begin: /\\/,
  249. end: /(\s|$)/,
  250. excludeEnd: true
  251. }
  252. ]
  253. },
  254. {
  255. className: 'regexp',
  256. variants: [
  257. {
  258. begin: '//',
  259. end: '//[gim]*',
  260. contains: [
  261. SUBST,
  262. hljs.HASH_COMMENT_MODE
  263. ]
  264. },
  265. {
  266. // regex can't start with space to parse x / 2 / 3 as two divisions
  267. // regex can't start with *, and it supports an "illegal" in the main mode
  268. begin: /\/(?![ *])(\\.|[^\\\n])*?\/[gim]*(?=\W)/
  269. }
  270. ]
  271. },
  272. {
  273. begin: '@' + JS_IDENT_RE
  274. },
  275. {
  276. begin: '``',
  277. end: '``',
  278. excludeBegin: true,
  279. excludeEnd: true,
  280. subLanguage: 'javascript'
  281. }
  282. ];
  283. SUBST.contains = EXPRESSIONS;
  284. const PARAMS = {
  285. className: 'params',
  286. begin: '\\(',
  287. returnBegin: true,
  288. /* We need another contained nameless mode to not have every nested
  289. pair of parens to be called "params" */
  290. contains: [
  291. {
  292. begin: /\(/,
  293. end: /\)/,
  294. keywords: KEYWORDS$1,
  295. contains: ['self'].concat(EXPRESSIONS)
  296. }
  297. ]
  298. };
  299. const SYMBOLS = {
  300. begin: '(#=>|=>|\\|>>|-?->|!->)'
  301. };
  302. return {
  303. name: 'LiveScript',
  304. aliases: ['ls'],
  305. keywords: KEYWORDS$1,
  306. illegal: /\/\*/,
  307. contains: EXPRESSIONS.concat([
  308. hljs.COMMENT('\\/\\*', '\\*\\/'),
  309. hljs.HASH_COMMENT_MODE,
  310. SYMBOLS, // relevance booster
  311. {
  312. className: 'function',
  313. contains: [
  314. TITLE,
  315. PARAMS
  316. ],
  317. returnBegin: true,
  318. variants: [
  319. {
  320. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\)\\s*)?\\B->\\*?',
  321. end: '->\\*?'
  322. },
  323. {
  324. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?!?(\\(.*\\)\\s*)?\\B[-~]{1,2}>\\*?',
  325. end: '[-~]{1,2}>\\*?'
  326. },
  327. {
  328. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\)\\s*)?\\B!?[-~]{1,2}>\\*?',
  329. end: '!?[-~]{1,2}>\\*?'
  330. }
  331. ]
  332. },
  333. {
  334. className: 'class',
  335. beginKeywords: 'class',
  336. end: '$',
  337. illegal: /[:="\[\]]/,
  338. contains: [
  339. {
  340. beginKeywords: 'extends',
  341. endsWithParent: true,
  342. illegal: /[:="\[\]]/,
  343. contains: [TITLE]
  344. },
  345. TITLE
  346. ]
  347. },
  348. {
  349. begin: JS_IDENT_RE + ':',
  350. end: ':',
  351. returnBegin: true,
  352. returnEnd: true,
  353. relevance: 0
  354. }
  355. ])
  356. };
  357. }
  358. module.exports = livescript;