l0bsterssg

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

kotlin.js (6397B)


      1 /*
      2  Language: Kotlin
      3  Description: Kotlin is an OSS statically typed programming language that targets the JVM, Android, JavaScript and Native.
      4  Author: Sergey Mashkov <cy6erGn0m@gmail.com>
      5  Website: https://kotlinlang.org
      6  Category: common
      7  */
      8 
      9 
     10 function kotlin(hljs) {
     11   var KEYWORDS = {
     12     keyword:
     13       'abstract as val var vararg get set class object open private protected public noinline ' +
     14       'crossinline dynamic final enum if else do while for when throw try catch finally ' +
     15       'import package is in fun override companion reified inline lateinit init ' +
     16       'interface annotation data sealed internal infix operator out by constructor super ' +
     17       'tailrec where const inner suspend typealias external expect actual',
     18     built_in:
     19       'Byte Short Char Int Long Boolean Float Double Void Unit Nothing',
     20     literal:
     21       'true false null'
     22   };
     23   var KEYWORDS_WITH_LABEL = {
     24     className: 'keyword',
     25     begin: /\b(break|continue|return|this)\b/,
     26     starts: {
     27       contains: [
     28         {
     29           className: 'symbol',
     30           begin: /@\w+/
     31         }
     32       ]
     33     }
     34   };
     35   var LABEL = {
     36     className: 'symbol', begin: hljs.UNDERSCORE_IDENT_RE + '@'
     37   };
     38 
     39   // for string templates
     40   var SUBST = {
     41     className: 'subst',
     42     begin: '\\${', end: '}', contains: [hljs.C_NUMBER_MODE]
     43   };
     44   var VARIABLE = {
     45     className: 'variable', begin: '\\$' + hljs.UNDERSCORE_IDENT_RE
     46   };
     47   var STRING = {
     48     className: 'string',
     49     variants: [
     50       {
     51         begin: '"""', end: '"""(?=[^"])',
     52         contains: [VARIABLE, SUBST]
     53       },
     54       // Can't use built-in modes easily, as we want to use STRING in the meta
     55       // context as 'meta-string' and there's no syntax to remove explicitly set
     56       // classNames in built-in modes.
     57       {
     58         begin: '\'', end: '\'',
     59         illegal: /\n/,
     60         contains: [hljs.BACKSLASH_ESCAPE]
     61       },
     62       {
     63         begin: '"', end: '"',
     64         illegal: /\n/,
     65         contains: [hljs.BACKSLASH_ESCAPE, VARIABLE, SUBST]
     66       }
     67     ]
     68   };
     69   SUBST.contains.push(STRING);
     70 
     71   var ANNOTATION_USE_SITE = {
     72     className: 'meta', begin: '@(?:file|property|field|get|set|receiver|param|setparam|delegate)\\s*:(?:\\s*' + hljs.UNDERSCORE_IDENT_RE + ')?'
     73   };
     74   var ANNOTATION = {
     75     className: 'meta', begin: '@' + hljs.UNDERSCORE_IDENT_RE,
     76     contains: [
     77       {
     78         begin: /\(/, end: /\)/,
     79         contains: [
     80           hljs.inherit(STRING, {className: 'meta-string'})
     81         ]
     82       }
     83     ]
     84   };
     85 
     86   // https://kotlinlang.org/docs/reference/whatsnew11.html#underscores-in-numeric-literals
     87   // According to the doc above, the number mode of kotlin is the same as java 8,
     88   // so the code below is copied from java.js
     89   var KOTLIN_NUMBER_RE = '\\b' +
     90     '(' +
     91       '0[bB]([01]+[01_]+[01]+|[01]+)' + // 0b...
     92       '|' +
     93       '0[xX]([a-fA-F0-9]+[a-fA-F0-9_]+[a-fA-F0-9]+|[a-fA-F0-9]+)' + // 0x...
     94       '|' +
     95       '(' +
     96         '([\\d]+[\\d_]+[\\d]+|[\\d]+)(\\.([\\d]+[\\d_]+[\\d]+|[\\d]+))?' +
     97         '|' +
     98         '\\.([\\d]+[\\d_]+[\\d]+|[\\d]+)' +
     99       ')' +
    100       '([eE][-+]?\\d+)?' + // octal, decimal, float
    101     ')' +
    102     '[lLfF]?';
    103   var KOTLIN_NUMBER_MODE = {
    104     className: 'number',
    105     begin: KOTLIN_NUMBER_RE,
    106     relevance: 0
    107   };
    108   var KOTLIN_NESTED_COMMENT = hljs.COMMENT(
    109     '/\\*', '\\*/',
    110     { contains: [ hljs.C_BLOCK_COMMENT_MODE ] }
    111   );
    112   var KOTLIN_PAREN_TYPE = {
    113     variants: [
    114 	  { className: 'type',
    115 	    begin: hljs.UNDERSCORE_IDENT_RE
    116 	  },
    117 	  { begin: /\(/, end: /\)/,
    118 	    contains: [] //defined later
    119 	  }
    120 	]
    121   };
    122   var KOTLIN_PAREN_TYPE2 = KOTLIN_PAREN_TYPE;
    123   KOTLIN_PAREN_TYPE2.variants[1].contains = [ KOTLIN_PAREN_TYPE ];
    124   KOTLIN_PAREN_TYPE.variants[1].contains = [ KOTLIN_PAREN_TYPE2 ];
    125 
    126   return {
    127     name: 'Kotlin',
    128     aliases: ['kt'],
    129     keywords: KEYWORDS,
    130     contains : [
    131       hljs.COMMENT(
    132         '/\\*\\*',
    133         '\\*/',
    134         {
    135           relevance : 0,
    136           contains : [{
    137             className : 'doctag',
    138             begin : '@[A-Za-z]+'
    139           }]
    140         }
    141       ),
    142       hljs.C_LINE_COMMENT_MODE,
    143       KOTLIN_NESTED_COMMENT,
    144       KEYWORDS_WITH_LABEL,
    145       LABEL,
    146       ANNOTATION_USE_SITE,
    147       ANNOTATION,
    148       {
    149         className: 'function',
    150         beginKeywords: 'fun', end: '[(]|$',
    151         returnBegin: true,
    152         excludeEnd: true,
    153         keywords: KEYWORDS,
    154         illegal: /fun\s+(<.*>)?[^\s\(]+(\s+[^\s\(]+)\s*=/,
    155         relevance: 5,
    156         contains: [
    157           {
    158             begin: hljs.UNDERSCORE_IDENT_RE + '\\s*\\(', returnBegin: true,
    159             relevance: 0,
    160             contains: [hljs.UNDERSCORE_TITLE_MODE]
    161           },
    162           {
    163             className: 'type',
    164             begin: /</, end: />/, keywords: 'reified',
    165             relevance: 0
    166           },
    167           {
    168             className: 'params',
    169             begin: /\(/, end: /\)/,
    170             endsParent: true,
    171             keywords: KEYWORDS,
    172             relevance: 0,
    173             contains: [
    174               {
    175                 begin: /:/, end: /[=,\/]/, endsWithParent: true,
    176                 contains: [
    177                   KOTLIN_PAREN_TYPE,
    178                   hljs.C_LINE_COMMENT_MODE,
    179                   KOTLIN_NESTED_COMMENT
    180                 ],
    181                 relevance: 0
    182               },
    183               hljs.C_LINE_COMMENT_MODE,
    184               KOTLIN_NESTED_COMMENT,
    185               ANNOTATION_USE_SITE,
    186               ANNOTATION,
    187               STRING,
    188               hljs.C_NUMBER_MODE
    189             ]
    190           },
    191           KOTLIN_NESTED_COMMENT
    192         ]
    193       },
    194       {
    195         className: 'class',
    196         beginKeywords: 'class interface trait', end: /[:\{(]|$/, // remove 'trait' when removed from KEYWORDS
    197         excludeEnd: true,
    198         illegal: 'extends implements',
    199         contains: [
    200           {beginKeywords: 'public protected internal private constructor'},
    201           hljs.UNDERSCORE_TITLE_MODE,
    202           {
    203             className: 'type',
    204             begin: /</, end: />/, excludeBegin: true, excludeEnd: true,
    205             relevance: 0
    206           },
    207           {
    208             className: 'type',
    209             begin: /[,:]\s*/, end: /[<\(,]|$/, excludeBegin: true, returnEnd: true
    210           },
    211           ANNOTATION_USE_SITE,
    212           ANNOTATION
    213         ]
    214       },
    215       STRING,
    216       {
    217         className: 'meta',
    218         begin: "^#!/usr/bin/env", end: '$',
    219         illegal: '\n'
    220       },
    221       KOTLIN_NUMBER_MODE
    222     ]
    223   };
    224 }
    225 
    226 module.exports = kotlin;