moonscript.js 3.3 KB

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