l0bsterssg

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

coffeescript.js (6163B)


      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: CoffeeScript
    140 Author: Dmytrii Nagirniak <dnagir@gmail.com>
    141 Contributors: Oleg Efimov <efimovov@gmail.com>, Cédric Néhémie <cedric.nehemie@gmail.com>
    142 Description: CoffeeScript is a programming language that transcompiles to JavaScript. For info about language see http://coffeescript.org/
    143 Category: common, scripting
    144 Website: https://coffeescript.org
    145 */
    146 
    147 /** @type LanguageFn */
    148 function coffeescript(hljs) {
    149   var COFFEE_BUILT_INS = [
    150     'npm',
    151     'print'
    152   ];
    153   var COFFEE_LITERALS = [
    154     'yes',
    155     'no',
    156     'on',
    157     'off'
    158   ];
    159   var COFFEE_KEYWORDS = [
    160     'then',
    161     'unless',
    162     'until',
    163     'loop',
    164     'by',
    165     'when',
    166     'and',
    167     'or',
    168     'is',
    169     'isnt',
    170     'not'
    171   ];
    172   var NOT_VALID_KEYWORDS = [
    173     "var",
    174     "const",
    175     "let",
    176     "function",
    177     "static"
    178   ];
    179   var excluding = (list) =>
    180     (kw) => !list.includes(kw);
    181   var KEYWORDS$1 = {
    182     keyword: KEYWORDS.concat(COFFEE_KEYWORDS).filter(excluding(NOT_VALID_KEYWORDS)).join(" "),
    183     literal: LITERALS.concat(COFFEE_LITERALS).join(" "),
    184     built_in: BUILT_INS.concat(COFFEE_BUILT_INS).join(" ")
    185   };
    186   var JS_IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
    187   var SUBST = {
    188     className: 'subst',
    189     begin: /#\{/, end: /}/,
    190     keywords: KEYWORDS$1
    191   };
    192   var EXPRESSIONS = [
    193     hljs.BINARY_NUMBER_MODE,
    194     hljs.inherit(hljs.C_NUMBER_MODE, {starts: {end: '(\\s*/)?', relevance: 0}}), // a number tries to eat the following slash to prevent treating it as a regexp
    195     {
    196       className: 'string',
    197       variants: [
    198         {
    199           begin: /'''/, end: /'''/,
    200           contains: [hljs.BACKSLASH_ESCAPE]
    201         },
    202         {
    203           begin: /'/, end: /'/,
    204           contains: [hljs.BACKSLASH_ESCAPE]
    205         },
    206         {
    207           begin: /"""/, end: /"""/,
    208           contains: [hljs.BACKSLASH_ESCAPE, SUBST]
    209         },
    210         {
    211           begin: /"/, end: /"/,
    212           contains: [hljs.BACKSLASH_ESCAPE, SUBST]
    213         }
    214       ]
    215     },
    216     {
    217       className: 'regexp',
    218       variants: [
    219         {
    220           begin: '///', end: '///',
    221           contains: [SUBST, hljs.HASH_COMMENT_MODE]
    222         },
    223         {
    224           begin: '//[gim]{0,3}(?=\\W)',
    225           relevance: 0
    226         },
    227         {
    228           // regex can't start with space to parse x / 2 / 3 as two divisions
    229           // regex can't start with *, and it supports an "illegal" in the main mode
    230           begin: /\/(?![ *]).*?(?![\\]).\/[gim]{0,3}(?=\W)/
    231         }
    232       ]
    233     },
    234     {
    235       begin: '@' + JS_IDENT_RE // relevance booster
    236     },
    237     {
    238       subLanguage: 'javascript',
    239       excludeBegin: true, excludeEnd: true,
    240       variants: [
    241         {
    242           begin: '```', end: '```',
    243         },
    244         {
    245           begin: '`', end: '`',
    246         }
    247       ]
    248     }
    249   ];
    250   SUBST.contains = EXPRESSIONS;
    251 
    252   var TITLE = hljs.inherit(hljs.TITLE_MODE, {begin: JS_IDENT_RE});
    253   var PARAMS_RE = '(\\(.*\\))?\\s*\\B[-=]>';
    254   var PARAMS = {
    255     className: 'params',
    256     begin: '\\([^\\(]', returnBegin: true,
    257     /* We need another contained nameless mode to not have every nested
    258     pair of parens to be called "params" */
    259     contains: [{
    260       begin: /\(/, end: /\)/,
    261       keywords: KEYWORDS$1,
    262       contains: ['self'].concat(EXPRESSIONS)
    263     }]
    264   };
    265 
    266   return {
    267     name: 'CoffeeScript',
    268     aliases: ['coffee', 'cson', 'iced'],
    269     keywords: KEYWORDS$1,
    270     illegal: /\/\*/,
    271     contains: EXPRESSIONS.concat([
    272       hljs.COMMENT('###', '###'),
    273       hljs.HASH_COMMENT_MODE,
    274       {
    275         className: 'function',
    276         begin: '^\\s*' + JS_IDENT_RE + '\\s*=\\s*' + PARAMS_RE, end: '[-=]>',
    277         returnBegin: true,
    278         contains: [TITLE, PARAMS]
    279       },
    280       {
    281         // anonymous function start
    282         begin: /[:\(,=]\s*/,
    283         relevance: 0,
    284         contains: [
    285           {
    286             className: 'function',
    287             begin: PARAMS_RE, end: '[-=]>',
    288             returnBegin: true,
    289             contains: [PARAMS]
    290           }
    291         ]
    292       },
    293       {
    294         className: 'class',
    295         beginKeywords: 'class',
    296         end: '$',
    297         illegal: /[:="\[\]]/,
    298         contains: [
    299           {
    300             beginKeywords: 'extends',
    301             endsWithParent: true,
    302             illegal: /[:="\[\]]/,
    303             contains: [TITLE]
    304           },
    305           TITLE
    306         ]
    307       },
    308       {
    309         begin: JS_IDENT_RE + ':', end: ':',
    310         returnBegin: true, returnEnd: true,
    311         relevance: 0
    312       }
    313     ])
    314   };
    315 }
    316 
    317 module.exports = coffeescript;