l0bsterssg

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

java.js (5287B)


      1 /**
      2  * @param {string} value
      3  * @returns {RegExp}
      4  * */
      5 
      6 /**
      7  * @param {RegExp | string } re
      8  * @returns {string}
      9  */
     10 function source(re) {
     11   if (!re) return null;
     12   if (typeof re === "string") return re;
     13 
     14   return re.source;
     15 }
     16 
     17 /**
     18  * @param {RegExp | string } re
     19  * @returns {string}
     20  */
     21 function optional(re) {
     22   return concat('(', re, ')?');
     23 }
     24 
     25 /**
     26  * @param {...(RegExp | string) } args
     27  * @returns {string}
     28  */
     29 function concat(...args) {
     30   const joined = args.map((x) => source(x)).join("");
     31   return joined;
     32 }
     33 
     34 /**
     35  * Any of the passed expresssions may match
     36  *
     37  * Creates a huge this | this | that | that match
     38  * @param {(RegExp | string)[] } args
     39  * @returns {string}
     40  */
     41 function either(...args) {
     42   const joined = '(' + args.map((x) => source(x)).join("|") + ")";
     43   return joined;
     44 }
     45 
     46 /*
     47 Language: Java
     48 Author: Vsevolod Solovyov <vsevolod.solovyov@gmail.com>
     49 Category: common, enterprise
     50 Website: https://www.java.com/
     51 */
     52 
     53 function java(hljs) {
     54   var JAVA_IDENT_RE = '[\u00C0-\u02B8a-zA-Z_$][\u00C0-\u02B8a-zA-Z_$0-9]*';
     55   var GENERIC_IDENT_RE = JAVA_IDENT_RE + '(<' + JAVA_IDENT_RE + '(\\s*,\\s*' + JAVA_IDENT_RE + ')*>)?';
     56   var KEYWORDS = 'false synchronized int abstract float private char boolean var static null if const ' +
     57     'for true while long strictfp finally protected import native final void ' +
     58     'enum else break transient catch instanceof byte super volatile case assert short ' +
     59     'package default double public try this switch continue throws protected public private ' +
     60     'module requires exports do';
     61 
     62   var ANNOTATION = {
     63     className: 'meta',
     64     begin: '@' + JAVA_IDENT_RE,
     65     contains: [
     66       {
     67         begin: /\(/,
     68         end: /\)/,
     69         contains: ["self"] // allow nested () inside our annotation
     70       },
     71     ]
     72   };
     73   /**
     74    * A given sequence, possibly with underscores
     75    * @type {(s: string | RegExp) => string}  */
     76   var SEQUENCE_ALLOWING_UNDERSCORES = (seq) => concat('[', seq, ']+([', seq, '_]*[', seq, ']+)?');
     77   var JAVA_NUMBER_MODE = {
     78     className: 'number',
     79     variants: [
     80       { begin: `\\b(0[bB]${SEQUENCE_ALLOWING_UNDERSCORES('01')})[lL]?` }, // binary
     81       { begin: `\\b(0${SEQUENCE_ALLOWING_UNDERSCORES('0-7')})[dDfFlL]?` }, // octal
     82       {
     83         begin: concat(
     84           /\b0[xX]/,
     85           either(
     86             concat(SEQUENCE_ALLOWING_UNDERSCORES('a-fA-F0-9'), /\./, SEQUENCE_ALLOWING_UNDERSCORES('a-fA-F0-9')),
     87             concat(SEQUENCE_ALLOWING_UNDERSCORES('a-fA-F0-9'), /\.?/),
     88             concat(/\./, SEQUENCE_ALLOWING_UNDERSCORES('a-fA-F0-9'))
     89           ),
     90           /([pP][+-]?(\d+))?/,
     91           /[fFdDlL]?/ // decimal & fp mixed for simplicity
     92         )
     93       },
     94       // scientific notation
     95       { begin: concat(
     96         /\b/,
     97         either(
     98           concat(/\d*\./, SEQUENCE_ALLOWING_UNDERSCORES("\\d")), // .3, 3.3, 3.3_3
     99           SEQUENCE_ALLOWING_UNDERSCORES("\\d") // 3, 3_3
    100         ),
    101         /[eE][+-]?[\d]+[dDfF]?/)
    102       },
    103       // decimal & fp mixed for simplicity
    104       { begin: concat(
    105         /\b/,
    106         SEQUENCE_ALLOWING_UNDERSCORES(/\d/),
    107         optional(/\.?/),
    108         optional(SEQUENCE_ALLOWING_UNDERSCORES(/\d/)),
    109         /[dDfFlL]?/)
    110       }
    111     ],
    112     relevance: 0
    113   };
    114 
    115   return {
    116     name: 'Java',
    117     aliases: ['jsp'],
    118     keywords: KEYWORDS,
    119     illegal: /<\/|#/,
    120     contains: [
    121       hljs.COMMENT(
    122         '/\\*\\*',
    123         '\\*/',
    124         {
    125           relevance: 0,
    126           contains: [
    127             {
    128               // eat up @'s in emails to prevent them to be recognized as doctags
    129               begin: /\w+@/, relevance: 0
    130             },
    131             {
    132               className: 'doctag',
    133               begin: '@[A-Za-z]+'
    134             }
    135           ]
    136         }
    137       ),
    138       hljs.C_LINE_COMMENT_MODE,
    139       hljs.C_BLOCK_COMMENT_MODE,
    140       hljs.APOS_STRING_MODE,
    141       hljs.QUOTE_STRING_MODE,
    142       {
    143         className: 'class',
    144         beginKeywords: 'class interface enum', end: /[{;=]/, excludeEnd: true,
    145         keywords: 'class interface enum',
    146         illegal: /[:"\[\]]/,
    147         contains: [
    148           { beginKeywords: 'extends implements' },
    149           hljs.UNDERSCORE_TITLE_MODE
    150         ]
    151       },
    152       {
    153         // Expression keywords prevent 'keyword Name(...)' from being
    154         // recognized as a function definition
    155         beginKeywords: 'new throw return else',
    156         relevance: 0
    157       },
    158       {
    159         className: 'function',
    160         begin: '(' + GENERIC_IDENT_RE + '\\s+)+' + hljs.UNDERSCORE_IDENT_RE + '\\s*\\(', returnBegin: true, end: /[{;=]/,
    161         excludeEnd: true,
    162         keywords: KEYWORDS,
    163         contains: [
    164           {
    165             begin: hljs.UNDERSCORE_IDENT_RE + '\\s*\\(', returnBegin: true,
    166             relevance: 0,
    167             contains: [hljs.UNDERSCORE_TITLE_MODE]
    168           },
    169           {
    170             className: 'params',
    171             begin: /\(/, end: /\)/,
    172             keywords: KEYWORDS,
    173             relevance: 0,
    174             contains: [
    175               ANNOTATION,
    176               hljs.APOS_STRING_MODE,
    177               hljs.QUOTE_STRING_MODE,
    178               hljs.C_NUMBER_MODE,
    179               hljs.C_BLOCK_COMMENT_MODE
    180             ]
    181           },
    182           hljs.C_LINE_COMMENT_MODE,
    183           hljs.C_BLOCK_COMMENT_MODE
    184         ]
    185       },
    186       JAVA_NUMBER_MODE,
    187       ANNOTATION
    188     ]
    189   };
    190 }
    191 
    192 module.exports = java;