l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

moonscript.js (3383B)


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