1
0

moonscript.js 3.3 KB

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