moonscript.js 3.0 KB

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