moonscript.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. { starts: {
  33. end: '(\\s*/)?',
  34. relevance: 0
  35. } }), // a number tries to eat the following slash to prevent treating it as a regexp
  36. {
  37. className: 'string',
  38. variants: [
  39. {
  40. begin: /'/,
  41. end: /'/,
  42. contains: [ hljs.BACKSLASH_ESCAPE ]
  43. },
  44. {
  45. begin: /"/,
  46. end: /"/,
  47. contains: [
  48. hljs.BACKSLASH_ESCAPE,
  49. SUBST
  50. ]
  51. }
  52. ]
  53. },
  54. {
  55. className: 'built_in',
  56. begin: '@__' + hljs.IDENT_RE
  57. },
  58. { begin: '@' + hljs.IDENT_RE // relevance booster on par with CoffeeScript
  59. },
  60. { begin: hljs.IDENT_RE + '\\\\' + hljs.IDENT_RE // inst\method
  61. }
  62. ];
  63. SUBST.contains = EXPRESSIONS;
  64. const TITLE = hljs.inherit(hljs.TITLE_MODE, { begin: JS_IDENT_RE });
  65. const POSSIBLE_PARAMS_RE = '(\\(.*\\)\\s*)?\\B[-=]>';
  66. const PARAMS = {
  67. className: 'params',
  68. begin: '\\([^\\(]',
  69. returnBegin: true,
  70. /* We need another contained nameless mode to not have every nested
  71. pair of parens to be called "params" */
  72. contains: [
  73. {
  74. begin: /\(/,
  75. end: /\)/,
  76. keywords: KEYWORDS,
  77. contains: [ 'self' ].concat(EXPRESSIONS)
  78. }
  79. ]
  80. };
  81. return {
  82. name: 'MoonScript',
  83. aliases: [ 'moon' ],
  84. keywords: KEYWORDS,
  85. illegal: /\/\*/,
  86. contains: EXPRESSIONS.concat([
  87. hljs.COMMENT('--', '$'),
  88. {
  89. className: 'function', // function: -> =>
  90. begin: '^\\s*' + JS_IDENT_RE + '\\s*=\\s*' + POSSIBLE_PARAMS_RE,
  91. end: '[-=]>',
  92. returnBegin: true,
  93. contains: [
  94. TITLE,
  95. PARAMS
  96. ]
  97. },
  98. {
  99. begin: /[\(,:=]\s*/, // anonymous function start
  100. relevance: 0,
  101. contains: [
  102. {
  103. className: 'function',
  104. begin: POSSIBLE_PARAMS_RE,
  105. end: '[-=]>',
  106. returnBegin: true,
  107. contains: [ PARAMS ]
  108. }
  109. ]
  110. },
  111. {
  112. className: 'class',
  113. beginKeywords: 'class',
  114. end: '$',
  115. illegal: /[:="\[\]]/,
  116. contains: [
  117. {
  118. beginKeywords: 'extends',
  119. endsWithParent: true,
  120. illegal: /[:="\[\]]/,
  121. contains: [ TITLE ]
  122. },
  123. TITLE
  124. ]
  125. },
  126. {
  127. className: 'name', // table
  128. begin: JS_IDENT_RE + ':',
  129. end: ':',
  130. returnBegin: true,
  131. returnEnd: true,
  132. relevance: 0
  133. }
  134. ])
  135. };
  136. }
  137. export { moonscript as default };