livescript.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. "BigInt64Array",
  85. "BigUint64Array",
  86. "BigInt"
  87. ];
  88. const ERROR_TYPES = [
  89. "EvalError",
  90. "InternalError",
  91. "RangeError",
  92. "ReferenceError",
  93. "SyntaxError",
  94. "TypeError",
  95. "URIError"
  96. ];
  97. const BUILT_IN_GLOBALS = [
  98. "setInterval",
  99. "setTimeout",
  100. "clearInterval",
  101. "clearTimeout",
  102. "require",
  103. "exports",
  104. "eval",
  105. "isFinite",
  106. "isNaN",
  107. "parseFloat",
  108. "parseInt",
  109. "decodeURI",
  110. "decodeURIComponent",
  111. "encodeURI",
  112. "encodeURIComponent",
  113. "escape",
  114. "unescape"
  115. ];
  116. const BUILT_IN_VARIABLES = [
  117. "arguments",
  118. "this",
  119. "super",
  120. "console",
  121. "window",
  122. "document",
  123. "localStorage",
  124. "module",
  125. "global" // Node.js
  126. ];
  127. const BUILT_INS = [].concat(
  128. BUILT_IN_GLOBALS,
  129. BUILT_IN_VARIABLES,
  130. TYPES,
  131. ERROR_TYPES
  132. );
  133. /*
  134. Language: LiveScript
  135. Author: Taneli Vatanen <taneli.vatanen@gmail.com>
  136. Contributors: Jen Evers-Corvina <jen@sevvie.net>
  137. Origin: coffeescript.js
  138. Description: LiveScript is a programming language that transcompiles to JavaScript. For info about language see http://livescript.net/
  139. Website: https://livescript.net
  140. Category: scripting
  141. */
  142. function livescript(hljs) {
  143. const LIVESCRIPT_BUILT_INS = [
  144. 'npm',
  145. 'print'
  146. ];
  147. const LIVESCRIPT_LITERALS = [
  148. 'yes',
  149. 'no',
  150. 'on',
  151. 'off',
  152. 'it',
  153. 'that',
  154. 'void'
  155. ];
  156. const LIVESCRIPT_KEYWORDS = [
  157. 'then',
  158. 'unless',
  159. 'until',
  160. 'loop',
  161. 'of',
  162. 'by',
  163. 'when',
  164. 'and',
  165. 'or',
  166. 'is',
  167. 'isnt',
  168. 'not',
  169. 'it',
  170. 'that',
  171. 'otherwise',
  172. 'from',
  173. 'to',
  174. 'til',
  175. 'fallthrough',
  176. 'case',
  177. 'enum',
  178. 'native',
  179. 'list',
  180. 'map',
  181. '__hasProp',
  182. '__extends',
  183. '__slice',
  184. '__bind',
  185. '__indexOf'
  186. ];
  187. const KEYWORDS$1 = {
  188. keyword: KEYWORDS.concat(LIVESCRIPT_KEYWORDS),
  189. literal: LITERALS.concat(LIVESCRIPT_LITERALS),
  190. built_in: BUILT_INS.concat(LIVESCRIPT_BUILT_INS)
  191. };
  192. const JS_IDENT_RE = '[A-Za-z$_](?:-[0-9A-Za-z$_]|[0-9A-Za-z$_])*';
  193. const TITLE = hljs.inherit(hljs.TITLE_MODE, {
  194. begin: JS_IDENT_RE
  195. });
  196. const SUBST = {
  197. className: 'subst',
  198. begin: /#\{/,
  199. end: /\}/,
  200. keywords: KEYWORDS$1
  201. };
  202. const SUBST_SIMPLE = {
  203. className: 'subst',
  204. begin: /#[A-Za-z$_]/,
  205. end: /(?:-[0-9A-Za-z$_]|[0-9A-Za-z$_])*/,
  206. keywords: KEYWORDS$1
  207. };
  208. const EXPRESSIONS = [
  209. hljs.BINARY_NUMBER_MODE,
  210. {
  211. className: 'number',
  212. begin: '(\\b0[xX][a-fA-F0-9_]+)|(\\b\\d(\\d|_\\d)*(\\.(\\d(\\d|_\\d)*)?)?(_*[eE]([-+]\\d(_\\d|\\d)*)?)?[_a-z]*)',
  213. relevance: 0,
  214. starts: {
  215. end: '(\\s*/)?',
  216. relevance: 0
  217. } // a number tries to eat the following slash to prevent treating it as a regexp
  218. },
  219. {
  220. className: 'string',
  221. variants: [
  222. {
  223. begin: /'''/,
  224. end: /'''/,
  225. contains: [hljs.BACKSLASH_ESCAPE]
  226. },
  227. {
  228. begin: /'/,
  229. end: /'/,
  230. contains: [hljs.BACKSLASH_ESCAPE]
  231. },
  232. {
  233. begin: /"""/,
  234. end: /"""/,
  235. contains: [
  236. hljs.BACKSLASH_ESCAPE,
  237. SUBST,
  238. SUBST_SIMPLE
  239. ]
  240. },
  241. {
  242. begin: /"/,
  243. end: /"/,
  244. contains: [
  245. hljs.BACKSLASH_ESCAPE,
  246. SUBST,
  247. SUBST_SIMPLE
  248. ]
  249. },
  250. {
  251. begin: /\\/,
  252. end: /(\s|$)/,
  253. excludeEnd: true
  254. }
  255. ]
  256. },
  257. {
  258. className: 'regexp',
  259. variants: [
  260. {
  261. begin: '//',
  262. end: '//[gim]*',
  263. contains: [
  264. SUBST,
  265. hljs.HASH_COMMENT_MODE
  266. ]
  267. },
  268. {
  269. // regex can't start with space to parse x / 2 / 3 as two divisions
  270. // regex can't start with *, and it supports an "illegal" in the main mode
  271. begin: /\/(?![ *])(\\.|[^\\\n])*?\/[gim]*(?=\W)/
  272. }
  273. ]
  274. },
  275. {
  276. begin: '@' + JS_IDENT_RE
  277. },
  278. {
  279. begin: '``',
  280. end: '``',
  281. excludeBegin: true,
  282. excludeEnd: true,
  283. subLanguage: 'javascript'
  284. }
  285. ];
  286. SUBST.contains = EXPRESSIONS;
  287. const PARAMS = {
  288. className: 'params',
  289. begin: '\\(',
  290. returnBegin: true,
  291. /* We need another contained nameless mode to not have every nested
  292. pair of parens to be called "params" */
  293. contains: [
  294. {
  295. begin: /\(/,
  296. end: /\)/,
  297. keywords: KEYWORDS$1,
  298. contains: ['self'].concat(EXPRESSIONS)
  299. }
  300. ]
  301. };
  302. const SYMBOLS = {
  303. begin: '(#=>|=>|\\|>>|-?->|!->)'
  304. };
  305. return {
  306. name: 'LiveScript',
  307. aliases: ['ls'],
  308. keywords: KEYWORDS$1,
  309. illegal: /\/\*/,
  310. contains: EXPRESSIONS.concat([
  311. hljs.COMMENT('\\/\\*', '\\*\\/'),
  312. hljs.HASH_COMMENT_MODE,
  313. SYMBOLS, // relevance booster
  314. {
  315. className: 'function',
  316. contains: [
  317. TITLE,
  318. PARAMS
  319. ],
  320. returnBegin: true,
  321. variants: [
  322. {
  323. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\)\\s*)?\\B->\\*?',
  324. end: '->\\*?'
  325. },
  326. {
  327. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?!?(\\(.*\\)\\s*)?\\B[-~]{1,2}>\\*?',
  328. end: '[-~]{1,2}>\\*?'
  329. },
  330. {
  331. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\)\\s*)?\\B!?[-~]{1,2}>\\*?',
  332. end: '!?[-~]{1,2}>\\*?'
  333. }
  334. ]
  335. },
  336. {
  337. className: 'class',
  338. beginKeywords: 'class',
  339. end: '$',
  340. illegal: /[:="\[\]]/,
  341. contains: [
  342. {
  343. beginKeywords: 'extends',
  344. endsWithParent: true,
  345. illegal: /[:="\[\]]/,
  346. contains: [TITLE]
  347. },
  348. TITLE
  349. ]
  350. },
  351. {
  352. begin: JS_IDENT_RE + ':',
  353. end: ':',
  354. returnBegin: true,
  355. returnEnd: true,
  356. relevance: 0
  357. }
  358. ])
  359. };
  360. }
  361. module.exports = livescript;