l0bsterssg

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

livescript.js (6539B)


      1 const KEYWORDS = [
      2   "as", // for exports
      3   "in",
      4   "of",
      5   "if",
      6   "for",
      7   "while",
      8   "finally",
      9   "var",
     10   "new",
     11   "function",
     12   "do",
     13   "return",
     14   "void",
     15   "else",
     16   "break",
     17   "catch",
     18   "instanceof",
     19   "with",
     20   "throw",
     21   "case",
     22   "default",
     23   "try",
     24   "switch",
     25   "continue",
     26   "typeof",
     27   "delete",
     28   "let",
     29   "yield",
     30   "const",
     31   "class",
     32   // JS handles these with a special rule
     33   // "get",
     34   // "set",
     35   "debugger",
     36   "async",
     37   "await",
     38   "static",
     39   "import",
     40   "from",
     41   "export",
     42   "extends"
     43 ];
     44 const LITERALS = [
     45   "true",
     46   "false",
     47   "null",
     48   "undefined",
     49   "NaN",
     50   "Infinity"
     51 ];
     52 
     53 const TYPES = [
     54   "Intl",
     55   "DataView",
     56   "Number",
     57   "Math",
     58   "Date",
     59   "String",
     60   "RegExp",
     61   "Object",
     62   "Function",
     63   "Boolean",
     64   "Error",
     65   "Symbol",
     66   "Set",
     67   "Map",
     68   "WeakSet",
     69   "WeakMap",
     70   "Proxy",
     71   "Reflect",
     72   "JSON",
     73   "Promise",
     74   "Float64Array",
     75   "Int16Array",
     76   "Int32Array",
     77   "Int8Array",
     78   "Uint16Array",
     79   "Uint32Array",
     80   "Float32Array",
     81   "Array",
     82   "Uint8Array",
     83   "Uint8ClampedArray",
     84   "ArrayBuffer"
     85 ];
     86 
     87 const ERROR_TYPES = [
     88   "EvalError",
     89   "InternalError",
     90   "RangeError",
     91   "ReferenceError",
     92   "SyntaxError",
     93   "TypeError",
     94   "URIError"
     95 ];
     96 
     97 const BUILT_IN_GLOBALS = [
     98   "setInterval",
     99   "setTimeout",
    100   "clearInterval",
    101   "clearTimeout",
    102 
    103   "require",
    104   "exports",
    105 
    106   "eval",
    107   "isFinite",
    108   "isNaN",
    109   "parseFloat",
    110   "parseInt",
    111   "decodeURI",
    112   "decodeURIComponent",
    113   "encodeURI",
    114   "encodeURIComponent",
    115   "escape",
    116   "unescape"
    117 ];
    118 
    119 const BUILT_IN_VARIABLES = [
    120   "arguments",
    121   "this",
    122   "super",
    123   "console",
    124   "window",
    125   "document",
    126   "localStorage",
    127   "module",
    128   "global" // Node.js
    129 ];
    130 
    131 const BUILT_INS = [].concat(
    132   BUILT_IN_GLOBALS,
    133   BUILT_IN_VARIABLES,
    134   TYPES,
    135   ERROR_TYPES
    136 );
    137 
    138 /*
    139 Language: LiveScript
    140 Author: Taneli Vatanen <taneli.vatanen@gmail.com>
    141 Contributors: Jen Evers-Corvina <jen@sevvie.net>
    142 Origin: coffeescript.js
    143 Description: LiveScript is a programming language that transcompiles to JavaScript. For info about language see http://livescript.net/
    144 Website: https://livescript.net
    145 Category: scripting
    146 */
    147 
    148 function livescript(hljs) {
    149   var LIVESCRIPT_BUILT_INS = [
    150     'npm',
    151     'print'
    152   ];
    153   var LIVESCRIPT_LITERALS = [
    154     'yes',
    155     'no',
    156     'on',
    157     'off',
    158     'it',
    159     'that',
    160     'void'
    161   ];
    162   var LIVESCRIPT_KEYWORDS = [
    163     'then',
    164     'unless',
    165     'until',
    166     'loop',
    167     'of',
    168     'by',
    169     'when',
    170     'and',
    171     'or',
    172     'is',
    173     'isnt',
    174     'not',
    175     'it',
    176     'that',
    177     'otherwise',
    178     'from',
    179     'to',
    180     'til',
    181     'fallthrough',
    182     'case',
    183     'enum',
    184     'native',
    185     'list',
    186     'map',
    187     '__hasProp',
    188     '__extends',
    189     '__slice',
    190     '__bind',
    191     '__indexOf'
    192   ];
    193   var KEYWORDS$1 = {
    194     keyword: KEYWORDS.concat(LIVESCRIPT_KEYWORDS).join(" "),
    195     literal: LITERALS.concat(LIVESCRIPT_LITERALS).join(" "),
    196     built_in: BUILT_INS.concat(LIVESCRIPT_BUILT_INS).join(" ")
    197   };
    198   var JS_IDENT_RE = '[A-Za-z$_](?:\-[0-9A-Za-z$_]|[0-9A-Za-z$_])*';
    199   var TITLE = hljs.inherit(hljs.TITLE_MODE, {begin: JS_IDENT_RE});
    200   var SUBST = {
    201     className: 'subst',
    202     begin: /#\{/, end: /}/,
    203     keywords: KEYWORDS$1
    204   };
    205   var SUBST_SIMPLE = {
    206     className: 'subst',
    207     begin: /#[A-Za-z$_]/, end: /(?:\-[0-9A-Za-z$_]|[0-9A-Za-z$_])*/,
    208     keywords: KEYWORDS$1
    209   };
    210   var EXPRESSIONS = [
    211     hljs.BINARY_NUMBER_MODE,
    212     {
    213       className: 'number',
    214       begin: '(\\b0[xX][a-fA-F0-9_]+)|(\\b\\d(\\d|_\\d)*(\\.(\\d(\\d|_\\d)*)?)?(_*[eE]([-+]\\d(_\\d|\\d)*)?)?[_a-z]*)',
    215       relevance: 0,
    216       starts: {end: '(\\s*/)?', relevance: 0} // a number tries to eat the following slash to prevent treating it as a regexp
    217     },
    218     {
    219       className: 'string',
    220       variants: [
    221         {
    222           begin: /'''/, end: /'''/,
    223           contains: [hljs.BACKSLASH_ESCAPE]
    224         },
    225         {
    226           begin: /'/, end: /'/,
    227           contains: [hljs.BACKSLASH_ESCAPE]
    228         },
    229         {
    230           begin: /"""/, end: /"""/,
    231           contains: [hljs.BACKSLASH_ESCAPE, SUBST, SUBST_SIMPLE]
    232         },
    233         {
    234           begin: /"/, end: /"/,
    235           contains: [hljs.BACKSLASH_ESCAPE, SUBST, SUBST_SIMPLE]
    236         },
    237         {
    238           begin: /\\/, end: /(\s|$)/,
    239           excludeEnd: true
    240         }
    241       ]
    242     },
    243     {
    244       className: 'regexp',
    245       variants: [
    246         {
    247           begin: '//', end: '//[gim]*',
    248           contains: [SUBST, hljs.HASH_COMMENT_MODE]
    249         },
    250         {
    251           // regex can't start with space to parse x / 2 / 3 as two divisions
    252           // regex can't start with *, and it supports an "illegal" in the main mode
    253           begin: /\/(?![ *])(\\\/|.)*?\/[gim]*(?=\W)/
    254         }
    255       ]
    256     },
    257     {
    258       begin: '@' + JS_IDENT_RE
    259     },
    260     {
    261       begin: '``', end: '``',
    262       excludeBegin: true, excludeEnd: true,
    263       subLanguage: 'javascript'
    264     }
    265   ];
    266   SUBST.contains = EXPRESSIONS;
    267 
    268   var PARAMS = {
    269     className: 'params',
    270     begin: '\\(', returnBegin: true,
    271     /* We need another contained nameless mode to not have every nested
    272     pair of parens to be called "params" */
    273     contains: [
    274       {
    275         begin: /\(/, end: /\)/,
    276         keywords: KEYWORDS$1,
    277         contains: ['self'].concat(EXPRESSIONS)
    278       }
    279     ]
    280   };
    281 
    282   var SYMBOLS = {
    283     begin: '(#=>|=>|\\|>>|-?->|\\!->)'
    284   };
    285 
    286   return {
    287     name: 'LiveScript',
    288     aliases: ['ls'],
    289     keywords: KEYWORDS$1,
    290     illegal: /\/\*/,
    291     contains: EXPRESSIONS.concat([
    292       hljs.COMMENT('\\/\\*', '\\*\\/'),
    293       hljs.HASH_COMMENT_MODE,
    294       SYMBOLS, // relevance booster
    295       {
    296         className: 'function',
    297         contains: [TITLE, PARAMS],
    298         returnBegin: true,
    299         variants: [
    300           {
    301             begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\))?\\s*\\B\\->\\*?', end: '\\->\\*?'
    302           },
    303           {
    304             begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?!?(\\(.*\\))?\\s*\\B[-~]{1,2}>\\*?', end: '[-~]{1,2}>\\*?'
    305           },
    306           {
    307             begin: '(' + JS_IDENT_RE + '\\s*(?:=|:=)\\s*)?(\\(.*\\))?\\s*\\B!?[-~]{1,2}>\\*?', end: '!?[-~]{1,2}>\\*?'
    308           }
    309         ]
    310       },
    311       {
    312         className: 'class',
    313         beginKeywords: 'class',
    314         end: '$',
    315         illegal: /[:="\[\]]/,
    316         contains: [
    317           {
    318             beginKeywords: 'extends',
    319             endsWithParent: true,
    320             illegal: /[:="\[\]]/,
    321             contains: [TITLE]
    322           },
    323           TITLE
    324         ]
    325       },
    326       {
    327         begin: JS_IDENT_RE + ':', end: ':',
    328         returnBegin: true, returnEnd: true,
    329         relevance: 0
    330       }
    331     ])
    332   };
    333 }
    334 
    335 module.exports = livescript;