livescript.js 7.1 KB

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