livescript.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
  53. const TYPES = [
  54. // Fundamental objects
  55. "Object",
  56. "Function",
  57. "Boolean",
  58. "Symbol",
  59. // numbers and dates
  60. "Math",
  61. "Date",
  62. "Number",
  63. "BigInt",
  64. // text
  65. "String",
  66. "RegExp",
  67. // Indexed collections
  68. "Array",
  69. "Float32Array",
  70. "Float64Array",
  71. "Int8Array",
  72. "Uint8Array",
  73. "Uint8ClampedArray",
  74. "Int16Array",
  75. "Int32Array",
  76. "Uint16Array",
  77. "Uint32Array",
  78. "BigInt64Array",
  79. "BigUint64Array",
  80. // Keyed collections
  81. "Set",
  82. "Map",
  83. "WeakSet",
  84. "WeakMap",
  85. // Structured data
  86. "ArrayBuffer",
  87. "SharedArrayBuffer",
  88. "Atomics",
  89. "DataView",
  90. "JSON",
  91. // Control abstraction objects
  92. "Promise",
  93. "Generator",
  94. "GeneratorFunction",
  95. "AsyncFunction",
  96. // Reflection
  97. "Reflect",
  98. "Proxy",
  99. // Internationalization
  100. "Intl",
  101. // WebAssembly
  102. "WebAssembly"
  103. ];
  104. const ERROR_TYPES = [
  105. "Error",
  106. "EvalError",
  107. "InternalError",
  108. "RangeError",
  109. "ReferenceError",
  110. "SyntaxError",
  111. "TypeError",
  112. "URIError"
  113. ];
  114. const BUILT_IN_GLOBALS = [
  115. "setInterval",
  116. "setTimeout",
  117. "clearInterval",
  118. "clearTimeout",
  119. "require",
  120. "exports",
  121. "eval",
  122. "isFinite",
  123. "isNaN",
  124. "parseFloat",
  125. "parseInt",
  126. "decodeURI",
  127. "decodeURIComponent",
  128. "encodeURI",
  129. "encodeURIComponent",
  130. "escape",
  131. "unescape"
  132. ];
  133. const BUILT_INS = [].concat(
  134. BUILT_IN_GLOBALS,
  135. TYPES,
  136. ERROR_TYPES
  137. );
  138. /*
  139. Language: LiveScript
  140. Author: Taneli Vatanen <taneli.vatanen@gmail.com>
  141. Contributors: Jen Evers-Corvina <jen@sevvie.net>
  142. Origin: coffeescript.js
  143. Description: LiveScript is a programming language that transcompiles to JavaScript. For info about language see http://livescript.net/
  144. Website: https://livescript.net
  145. Category: scripting
  146. */
  147. function livescript(hljs) {
  148. const LIVESCRIPT_BUILT_INS = [
  149. 'npm',
  150. 'print'
  151. ];
  152. const LIVESCRIPT_LITERALS = [
  153. 'yes',
  154. 'no',
  155. 'on',
  156. 'off',
  157. 'it',
  158. 'that',
  159. 'void'
  160. ];
  161. const LIVESCRIPT_KEYWORDS = [
  162. 'then',
  163. 'unless',
  164. 'until',
  165. 'loop',
  166. 'of',
  167. 'by',
  168. 'when',
  169. 'and',
  170. 'or',
  171. 'is',
  172. 'isnt',
  173. 'not',
  174. 'it',
  175. 'that',
  176. 'otherwise',
  177. 'from',
  178. 'to',
  179. 'til',
  180. 'fallthrough',
  181. 'case',
  182. 'enum',
  183. 'native',
  184. 'list',
  185. 'map',
  186. '__hasProp',
  187. '__extends',
  188. '__slice',
  189. '__bind',
  190. '__indexOf'
  191. ];
  192. const KEYWORDS$1 = {
  193. keyword: KEYWORDS.concat(LIVESCRIPT_KEYWORDS),
  194. literal: LITERALS.concat(LIVESCRIPT_LITERALS),
  195. built_in: BUILT_INS.concat(LIVESCRIPT_BUILT_INS)
  196. };
  197. const JS_IDENT_RE = '[A-Za-z$_](?:-[0-9A-Za-z$_]|[0-9A-Za-z$_])*';
  198. const TITLE = hljs.inherit(hljs.TITLE_MODE, { begin: JS_IDENT_RE });
  199. const SUBST = {
  200. className: 'subst',
  201. begin: /#\{/,
  202. end: /\}/,
  203. keywords: KEYWORDS$1
  204. };
  205. const SUBST_SIMPLE = {
  206. className: 'subst',
  207. begin: /#[A-Za-z$_]/,
  208. end: /(?:-[0-9A-Za-z$_]|[0-9A-Za-z$_])*/,
  209. keywords: KEYWORDS$1
  210. };
  211. const EXPRESSIONS = [
  212. hljs.BINARY_NUMBER_MODE,
  213. {
  214. className: 'number',
  215. begin: '(\\b0[xX][a-fA-F0-9_]+)|(\\b\\d(\\d|_\\d)*(\\.(\\d(\\d|_\\d)*)?)?(_*[eE]([-+]\\d(_\\d|\\d)*)?)?[_a-z]*)',
  216. relevance: 0,
  217. starts: {
  218. end: '(\\s*/)?',
  219. relevance: 0
  220. } // a number tries to eat the following slash to prevent treating it as a regexp
  221. },
  222. {
  223. className: 'string',
  224. variants: [
  225. {
  226. begin: /'''/,
  227. end: /'''/,
  228. contains: [ hljs.BACKSLASH_ESCAPE ]
  229. },
  230. {
  231. begin: /'/,
  232. end: /'/,
  233. contains: [ hljs.BACKSLASH_ESCAPE ]
  234. },
  235. {
  236. begin: /"""/,
  237. end: /"""/,
  238. contains: [
  239. hljs.BACKSLASH_ESCAPE,
  240. SUBST,
  241. SUBST_SIMPLE
  242. ]
  243. },
  244. {
  245. begin: /"/,
  246. end: /"/,
  247. contains: [
  248. hljs.BACKSLASH_ESCAPE,
  249. SUBST,
  250. SUBST_SIMPLE
  251. ]
  252. },
  253. {
  254. begin: /\\/,
  255. end: /(\s|$)/,
  256. excludeEnd: true
  257. }
  258. ]
  259. },
  260. {
  261. className: 'regexp',
  262. variants: [
  263. {
  264. begin: '//',
  265. end: '//[gim]*',
  266. contains: [
  267. SUBST,
  268. hljs.HASH_COMMENT_MODE
  269. ]
  270. },
  271. {
  272. // regex can't start with space to parse x / 2 / 3 as two divisions
  273. // regex can't start with *, and it supports an "illegal" in the main mode
  274. begin: /\/(?![ *])(\\.|[^\\\n])*?\/[gim]*(?=\W)/ }
  275. ]
  276. },
  277. { begin: '@' + JS_IDENT_RE },
  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 = { begin: '(#=>|=>|\\|>>|-?->|!->)' };
  303. const CLASS_DEFINITION = {
  304. variants: [
  305. { match: [
  306. /class\s+/,
  307. JS_IDENT_RE,
  308. /\s+extends\s+/,
  309. JS_IDENT_RE
  310. ] },
  311. { match: [
  312. /class\s+/,
  313. JS_IDENT_RE
  314. ] }
  315. ],
  316. scope: {
  317. 2: "title.class",
  318. 4: "title.class.inherited"
  319. },
  320. keywords: KEYWORDS$1
  321. };
  322. return {
  323. name: 'LiveScript',
  324. aliases: [ 'ls' ],
  325. keywords: KEYWORDS$1,
  326. illegal: /\/\*/,
  327. contains: EXPRESSIONS.concat([
  328. hljs.COMMENT('\\/\\*', '\\*\\/'),
  329. hljs.HASH_COMMENT_MODE,
  330. SYMBOLS, // relevance booster
  331. {
  332. className: 'function',
  333. contains: [
  334. TITLE,
  335. PARAMS
  336. ],
  337. returnBegin: true,
  338. variants: [
  339. {
  340. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\)\\s*)?\\B->\\*?',
  341. end: '->\\*?'
  342. },
  343. {
  344. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?!?(\\(.*\\)\\s*)?\\B[-~]{1,2}>\\*?',
  345. end: '[-~]{1,2}>\\*?'
  346. },
  347. {
  348. begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\)\\s*)?\\B!?[-~]{1,2}>\\*?',
  349. end: '!?[-~]{1,2}>\\*?'
  350. }
  351. ]
  352. },
  353. CLASS_DEFINITION,
  354. {
  355. begin: JS_IDENT_RE + ':',
  356. end: ':',
  357. returnBegin: true,
  358. returnEnd: true,
  359. relevance: 0
  360. }
  361. ])
  362. };
  363. }
  364. export { livescript as default };