moonscript.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Language: MoonScript
  3. Author: Billy Quith <chinbillybilbo@gmail.com>
  4. Description: MoonScript is a programming language that transcompiles to Lua.
  5. Origin: coffeescript.js
  6. Website: http://moonscript.org/
  7. Category: scripting
  8. */
  9. function moonscript(hljs) {
  10. const KEYWORDS = {
  11. keyword:
  12. // Moonscript keywords
  13. 'if then not for in while do return else elseif break continue switch and or ' +
  14. 'unless when class extends super local import export from using',
  15. literal:
  16. 'true false nil',
  17. built_in:
  18. '_G _VERSION assert collectgarbage dofile error getfenv getmetatable ipairs load ' +
  19. 'loadfile loadstring module next pairs pcall print rawequal rawget rawset require ' +
  20. 'select setfenv setmetatable tonumber tostring type unpack xpcall coroutine debug ' +
  21. 'io math os package string table'
  22. };
  23. const JS_IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
  24. const SUBST = {
  25. className: 'subst',
  26. begin: /#\{/,
  27. end: /\}/,
  28. keywords: KEYWORDS
  29. };
  30. const EXPRESSIONS = [
  31. hljs.inherit(hljs.C_NUMBER_MODE,
  32. {
  33. starts: {
  34. end: '(\\s*/)?',
  35. relevance: 0
  36. }
  37. }), // a number tries to eat the following slash to prevent treating it as a regexp
  38. {
  39. className: 'string',
  40. variants: [
  41. {
  42. begin: /'/,
  43. end: /'/,
  44. contains: [ hljs.BACKSLASH_ESCAPE ]
  45. },
  46. {
  47. begin: /"/,
  48. end: /"/,
  49. contains: [
  50. hljs.BACKSLASH_ESCAPE,
  51. SUBST
  52. ]
  53. }
  54. ]
  55. },
  56. {
  57. className: 'built_in',
  58. begin: '@__' + hljs.IDENT_RE
  59. },
  60. {
  61. begin: '@' + hljs.IDENT_RE // relevance booster on par with CoffeeScript
  62. },
  63. {
  64. begin: hljs.IDENT_RE + '\\\\' + hljs.IDENT_RE // inst\method
  65. }
  66. ];
  67. SUBST.contains = EXPRESSIONS;
  68. const TITLE = hljs.inherit(hljs.TITLE_MODE, {
  69. begin: JS_IDENT_RE
  70. });
  71. const POSSIBLE_PARAMS_RE = '(\\(.*\\)\\s*)?\\B[-=]>';
  72. const PARAMS = {
  73. className: 'params',
  74. begin: '\\([^\\(]',
  75. returnBegin: true,
  76. /* We need another contained nameless mode to not have every nested
  77. pair of parens to be called "params" */
  78. contains: [
  79. {
  80. begin: /\(/,
  81. end: /\)/,
  82. keywords: KEYWORDS,
  83. contains: [ 'self' ].concat(EXPRESSIONS)
  84. }
  85. ]
  86. };
  87. return {
  88. name: 'MoonScript',
  89. aliases: [ 'moon' ],
  90. keywords: KEYWORDS,
  91. illegal: /\/\*/,
  92. contains: EXPRESSIONS.concat([
  93. hljs.COMMENT('--', '$'),
  94. {
  95. className: 'function', // function: -> =>
  96. begin: '^\\s*' + JS_IDENT_RE + '\\s*=\\s*' + POSSIBLE_PARAMS_RE,
  97. end: '[-=]>',
  98. returnBegin: true,
  99. contains: [
  100. TITLE,
  101. PARAMS
  102. ]
  103. },
  104. {
  105. begin: /[\(,:=]\s*/, // anonymous function start
  106. relevance: 0,
  107. contains: [
  108. {
  109. className: 'function',
  110. begin: POSSIBLE_PARAMS_RE,
  111. end: '[-=]>',
  112. returnBegin: true,
  113. contains: [ PARAMS ]
  114. }
  115. ]
  116. },
  117. {
  118. className: 'class',
  119. beginKeywords: 'class',
  120. end: '$',
  121. illegal: /[:="\[\]]/,
  122. contains: [
  123. {
  124. beginKeywords: 'extends',
  125. endsWithParent: true,
  126. illegal: /[:="\[\]]/,
  127. contains: [ TITLE ]
  128. },
  129. TITLE
  130. ]
  131. },
  132. {
  133. className: 'name', // table
  134. begin: JS_IDENT_RE + ':',
  135. end: ':',
  136. returnBegin: true,
  137. returnEnd: true,
  138. relevance: 0
  139. }
  140. ])
  141. };
  142. }
  143. module.exports = moonscript;