l0bsterssg

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

d.js (6822B)


      1 /*
      2 Language: D
      3 Author: Aleksandar Ruzicic <aleksandar@ruzicic.info>
      4 Description: D is a language with C-like syntax and static typing. It pragmatically combines efficiency, control, and modeling power, with safety and programmer productivity.
      5 Version: 1.0a
      6 Website: https://dlang.org
      7 Date: 2012-04-08
      8 */
      9 
     10 /**
     11  * Known issues:
     12  *
     13  * - invalid hex string literals will be recognized as a double quoted strings
     14  *   but 'x' at the beginning of string will not be matched
     15  *
     16  * - delimited string literals are not checked for matching end delimiter
     17  *   (not possible to do with js regexp)
     18  *
     19  * - content of token string is colored as a string (i.e. no keyword coloring inside a token string)
     20  *   also, content of token string is not validated to contain only valid D tokens
     21  *
     22  * - special token sequence rule is not strictly following D grammar (anything following #line
     23  *   up to the end of line is matched as special token sequence)
     24  */
     25 
     26 /** @type LanguageFn */
     27 function d(hljs) {
     28   /**
     29    * Language keywords
     30    *
     31    * @type {Object}
     32    */
     33   var D_KEYWORDS = {
     34     $pattern: hljs.UNDERSCORE_IDENT_RE,
     35     keyword:
     36       'abstract alias align asm assert auto body break byte case cast catch class ' +
     37       'const continue debug default delete deprecated do else enum export extern final ' +
     38       'finally for foreach foreach_reverse|10 goto if immutable import in inout int ' +
     39       'interface invariant is lazy macro mixin module new nothrow out override package ' +
     40       'pragma private protected public pure ref return scope shared static struct ' +
     41       'super switch synchronized template this throw try typedef typeid typeof union ' +
     42       'unittest version void volatile while with __FILE__ __LINE__ __gshared|10 ' +
     43       '__thread __traits __DATE__ __EOF__ __TIME__ __TIMESTAMP__ __VENDOR__ __VERSION__',
     44     built_in:
     45       'bool cdouble cent cfloat char creal dchar delegate double dstring float function ' +
     46       'idouble ifloat ireal long real short string ubyte ucent uint ulong ushort wchar ' +
     47       'wstring',
     48     literal:
     49       'false null true'
     50   };
     51 
     52   /**
     53    * Number literal regexps
     54    *
     55    * @type {String}
     56    */
     57   var decimal_integer_re = '(0|[1-9][\\d_]*)',
     58     decimal_integer_nosus_re = '(0|[1-9][\\d_]*|\\d[\\d_]*|[\\d_]+?\\d)',
     59     binary_integer_re = '0[bB][01_]+',
     60     hexadecimal_digits_re = '([\\da-fA-F][\\da-fA-F_]*|_[\\da-fA-F][\\da-fA-F_]*)',
     61     hexadecimal_integer_re = '0[xX]' + hexadecimal_digits_re,
     62 
     63     decimal_exponent_re = '([eE][+-]?' + decimal_integer_nosus_re + ')',
     64     decimal_float_re = '(' + decimal_integer_nosus_re + '(\\.\\d*|' + decimal_exponent_re + ')|' +
     65                 '\\d+\\.' + decimal_integer_nosus_re + decimal_integer_nosus_re + '|' +
     66                 '\\.' + decimal_integer_re + decimal_exponent_re + '?' +
     67               ')',
     68     hexadecimal_float_re = '(0[xX](' +
     69                   hexadecimal_digits_re + '\\.' + hexadecimal_digits_re + '|'+
     70                   '\\.?' + hexadecimal_digits_re +
     71                  ')[pP][+-]?' + decimal_integer_nosus_re + ')',
     72 
     73     integer_re = '(' +
     74       decimal_integer_re + '|' +
     75       binary_integer_re  + '|' +
     76        hexadecimal_integer_re   +
     77     ')',
     78 
     79     float_re = '(' +
     80       hexadecimal_float_re + '|' +
     81       decimal_float_re  +
     82     ')';
     83 
     84   /**
     85    * Escape sequence supported in D string and character literals
     86    *
     87    * @type {String}
     88    */
     89   var escape_sequence_re = '\\\\(' +
     90               '[\'"\\?\\\\abfnrtv]|' +  // common escapes
     91               'u[\\dA-Fa-f]{4}|' +     // four hex digit unicode codepoint
     92               '[0-7]{1,3}|' +       // one to three octal digit ascii char code
     93               'x[\\dA-Fa-f]{2}|' +    // two hex digit ascii char code
     94               'U[\\dA-Fa-f]{8}' +      // eight hex digit unicode codepoint
     95               ')|' +
     96               '&[a-zA-Z\\d]{2,};';      // named character entity
     97 
     98   /**
     99    * D integer number literals
    100    *
    101    * @type {Object}
    102    */
    103   var D_INTEGER_MODE = {
    104     className: 'number',
    105       begin: '\\b' + integer_re + '(L|u|U|Lu|LU|uL|UL)?',
    106       relevance: 0
    107   };
    108 
    109   /**
    110    * [D_FLOAT_MODE description]
    111    * @type {Object}
    112    */
    113   var D_FLOAT_MODE = {
    114     className: 'number',
    115     begin: '\\b(' +
    116         float_re + '([fF]|L|i|[fF]i|Li)?|' +
    117         integer_re + '(i|[fF]i|Li)' +
    118       ')',
    119     relevance: 0
    120   };
    121 
    122   /**
    123    * D character literal
    124    *
    125    * @type {Object}
    126    */
    127   var D_CHARACTER_MODE = {
    128     className: 'string',
    129     begin: '\'(' + escape_sequence_re + '|.)', end: '\'',
    130     illegal: '.'
    131   };
    132 
    133   /**
    134    * D string escape sequence
    135    *
    136    * @type {Object}
    137    */
    138   var D_ESCAPE_SEQUENCE = {
    139     begin: escape_sequence_re,
    140     relevance: 0
    141   };
    142 
    143   /**
    144    * D double quoted string literal
    145    *
    146    * @type {Object}
    147    */
    148   var D_STRING_MODE = {
    149     className: 'string',
    150     begin: '"',
    151     contains: [D_ESCAPE_SEQUENCE],
    152     end: '"[cwd]?'
    153   };
    154 
    155   /**
    156    * D wysiwyg and delimited string literals
    157    *
    158    * @type {Object}
    159    */
    160   var D_WYSIWYG_DELIMITED_STRING_MODE = {
    161     className: 'string',
    162     begin: '[rq]"',
    163     end: '"[cwd]?',
    164     relevance: 5
    165   };
    166 
    167   /**
    168    * D alternate wysiwyg string literal
    169    *
    170    * @type {Object}
    171    */
    172   var D_ALTERNATE_WYSIWYG_STRING_MODE = {
    173     className: 'string',
    174     begin: '`',
    175     end: '`[cwd]?'
    176   };
    177 
    178   /**
    179    * D hexadecimal string literal
    180    *
    181    * @type {Object}
    182    */
    183   var D_HEX_STRING_MODE = {
    184     className: 'string',
    185     begin: 'x"[\\da-fA-F\\s\\n\\r]*"[cwd]?',
    186     relevance: 10
    187   };
    188 
    189   /**
    190    * D delimited string literal
    191    *
    192    * @type {Object}
    193    */
    194   var D_TOKEN_STRING_MODE = {
    195     className: 'string',
    196     begin: 'q"\\{',
    197     end: '\\}"'
    198   };
    199 
    200   /**
    201    * Hashbang support
    202    *
    203    * @type {Object}
    204    */
    205   var D_HASHBANG_MODE = {
    206     className: 'meta',
    207     begin: '^#!',
    208     end: '$',
    209     relevance: 5
    210   };
    211 
    212   /**
    213    * D special token sequence
    214    *
    215    * @type {Object}
    216    */
    217   var D_SPECIAL_TOKEN_SEQUENCE_MODE = {
    218     className: 'meta',
    219     begin: '#(line)',
    220     end: '$',
    221     relevance: 5
    222   };
    223 
    224   /**
    225    * D attributes
    226    *
    227    * @type {Object}
    228    */
    229   var D_ATTRIBUTE_MODE = {
    230     className: 'keyword',
    231     begin: '@[a-zA-Z_][a-zA-Z_\\d]*'
    232   };
    233 
    234   /**
    235    * D nesting comment
    236    *
    237    * @type {Object}
    238    */
    239   var D_NESTING_COMMENT_MODE = hljs.COMMENT(
    240     '\\/\\+',
    241     '\\+\\/',
    242     {
    243       contains: ['self'],
    244       relevance: 10
    245     }
    246   );
    247 
    248   return {
    249     name: 'D',
    250     keywords: D_KEYWORDS,
    251     contains: [
    252       hljs.C_LINE_COMMENT_MODE,
    253         hljs.C_BLOCK_COMMENT_MODE,
    254         D_NESTING_COMMENT_MODE,
    255         D_HEX_STRING_MODE,
    256         D_STRING_MODE,
    257         D_WYSIWYG_DELIMITED_STRING_MODE,
    258         D_ALTERNATE_WYSIWYG_STRING_MODE,
    259         D_TOKEN_STRING_MODE,
    260         D_FLOAT_MODE,
    261         D_INTEGER_MODE,
    262         D_CHARACTER_MODE,
    263         D_HASHBANG_MODE,
    264         D_SPECIAL_TOKEN_SEQUENCE_MODE,
    265         D_ATTRIBUTE_MODE
    266     ]
    267   };
    268 }
    269 
    270 module.exports = d;