l0bsterssg

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

c-like.js (8330B)


      1 /*
      2 Language: C-like foundation grammar for C/C++ grammars
      3 Author: Ivan Sagalaev <maniac@softwaremaniacs.org>
      4 Contributors: Evgeny Stepanischev <imbolk@gmail.com>, Zaven Muradyan <megalivoithos@gmail.com>, Roel Deckers <admin@codingcat.nl>, Sam Wu <samsam2310@gmail.com>, Jordi Petit <jordi.petit@gmail.com>, Pieter Vantorre <pietervantorre@gmail.com>, Google Inc. (David Benjamin) <davidben@google.com>
      5 Category: common, system
      6 */
      7 
      8 /* In the future the intention is to split out the C/C++ grammars distinctly
      9 since they are separate languages.  They will likely share a common foundation
     10 though, and this file sets the groundwork for that - so that we get the breaking
     11 change in v10 and don't have to change the requirements again later.
     12 
     13 See: https://github.com/highlightjs/highlight.js/issues/2146
     14 */
     15 
     16 /** @type LanguageFn */
     17 function cLike(hljs) {
     18   function optional(s) {
     19     return '(?:' + s + ')?';
     20   }
     21   var DECLTYPE_AUTO_RE = 'decltype\\(auto\\)';
     22   var NAMESPACE_RE = '[a-zA-Z_]\\w*::';
     23   var TEMPLATE_ARGUMENT_RE = '<.*?>';
     24   var FUNCTION_TYPE_RE = '(' +
     25     DECLTYPE_AUTO_RE + '|' +
     26     optional(NAMESPACE_RE) +'[a-zA-Z_]\\w*' + optional(TEMPLATE_ARGUMENT_RE) +
     27   ')';
     28   var CPP_PRIMITIVE_TYPES = {
     29     className: 'keyword',
     30     begin: '\\b[a-z\\d_]*_t\\b'
     31   };
     32 
     33   // https://en.cppreference.com/w/cpp/language/escape
     34   // \\ \x \xFF \u2837 \u00323747 \374
     35   var CHARACTER_ESCAPES = '\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)';
     36   var STRINGS = {
     37     className: 'string',
     38     variants: [
     39       {
     40         begin: '(u8?|U|L)?"', end: '"',
     41         illegal: '\\n',
     42         contains: [hljs.BACKSLASH_ESCAPE]
     43       },
     44       {
     45         begin: '(u8?|U|L)?\'(' + CHARACTER_ESCAPES + "|.)", end: '\'',
     46         illegal: '.'
     47       },
     48       hljs.END_SAME_AS_BEGIN({
     49         begin: /(?:u8?|U|L)?R"([^()\\ ]{0,16})\(/,
     50         end: /\)([^()\\ ]{0,16})"/,
     51       })
     52     ]
     53   };
     54 
     55   var NUMBERS = {
     56     className: 'number',
     57     variants: [
     58       { begin: '\\b(0b[01\']+)' },
     59       { begin: '(-?)\\b([\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)(u|U|l|L|ul|UL|f|F|b|B)' },
     60       { begin: '(-?)(\\b0[xX][a-fA-F0-9\']+|(\\b[\\d\']+(\\.[\\d\']*)?|\\.[\\d\']+)([eE][-+]?[\\d\']+)?)' }
     61     ],
     62     relevance: 0
     63   };
     64 
     65   var PREPROCESSOR =       {
     66     className: 'meta',
     67     begin: /#\s*[a-z]+\b/, end: /$/,
     68     keywords: {
     69       'meta-keyword':
     70         'if else elif endif define undef warning error line ' +
     71         'pragma _Pragma ifdef ifndef include'
     72     },
     73     contains: [
     74       {
     75         begin: /\\\n/, relevance: 0
     76       },
     77       hljs.inherit(STRINGS, {className: 'meta-string'}),
     78       {
     79         className: 'meta-string',
     80         begin: /<.*?>/, end: /$/,
     81         illegal: '\\n',
     82       },
     83       hljs.C_LINE_COMMENT_MODE,
     84       hljs.C_BLOCK_COMMENT_MODE
     85     ]
     86   };
     87 
     88   var TITLE_MODE = {
     89     className: 'title',
     90     begin: optional(NAMESPACE_RE) + hljs.IDENT_RE,
     91     relevance: 0
     92   };
     93 
     94   var FUNCTION_TITLE = optional(NAMESPACE_RE) + hljs.IDENT_RE + '\\s*\\(';
     95 
     96   var CPP_KEYWORDS = {
     97     keyword: 'int float while private char char8_t char16_t char32_t catch import module export virtual operator sizeof ' +
     98       'dynamic_cast|10 typedef const_cast|10 const for static_cast|10 union namespace ' +
     99       'unsigned long volatile static protected bool template mutable if public friend ' +
    100       'do goto auto void enum else break extern using asm case typeid wchar_t ' +
    101       'short reinterpret_cast|10 default double register explicit signed typename try this ' +
    102       'switch continue inline delete alignas alignof constexpr consteval constinit decltype ' +
    103       'concept co_await co_return co_yield requires ' +
    104       'noexcept static_assert thread_local restrict final override ' +
    105       'atomic_bool atomic_char atomic_schar ' +
    106       'atomic_uchar atomic_short atomic_ushort atomic_int atomic_uint atomic_long atomic_ulong atomic_llong ' +
    107       'atomic_ullong new throw return ' +
    108       'and and_eq bitand bitor compl not not_eq or or_eq xor xor_eq',
    109     built_in: 'std string wstring cin cout cerr clog stdin stdout stderr stringstream istringstream ostringstream ' +
    110       'auto_ptr deque list queue stack vector map set pair bitset multiset multimap unordered_set ' +
    111       'unordered_map unordered_multiset unordered_multimap priority_queue make_pair array shared_ptr abort terminate abs acos ' +
    112       'asin atan2 atan calloc ceil cosh cos exit exp fabs floor fmod fprintf fputs free frexp ' +
    113       'fscanf future isalnum isalpha iscntrl isdigit isgraph islower isprint ispunct isspace isupper ' +
    114       'isxdigit tolower toupper labs ldexp log10 log malloc realloc memchr memcmp memcpy memset modf pow ' +
    115       'printf putchar puts scanf sinh sin snprintf sprintf sqrt sscanf strcat strchr strcmp ' +
    116       'strcpy strcspn strlen strncat strncmp strncpy strpbrk strrchr strspn strstr tanh tan ' +
    117       'vfprintf vprintf vsprintf endl initializer_list unique_ptr _Bool complex _Complex imaginary _Imaginary',
    118     literal: 'true false nullptr NULL'
    119   };
    120 
    121   var EXPRESSION_CONTAINS = [
    122     CPP_PRIMITIVE_TYPES,
    123     hljs.C_LINE_COMMENT_MODE,
    124     hljs.C_BLOCK_COMMENT_MODE,
    125     NUMBERS,
    126     STRINGS
    127   ];
    128 
    129   var EXPRESSION_CONTEXT = {
    130     // This mode covers expression context where we can't expect a function
    131     // definition and shouldn't highlight anything that looks like one:
    132     // `return some()`, `else if()`, `(x*sum(1, 2))`
    133     variants: [
    134       {begin: /=/, end: /;/},
    135       {begin: /\(/, end: /\)/},
    136       {beginKeywords: 'new throw return else', end: /;/}
    137     ],
    138     keywords: CPP_KEYWORDS,
    139     contains: EXPRESSION_CONTAINS.concat([
    140       {
    141         begin: /\(/, end: /\)/,
    142         keywords: CPP_KEYWORDS,
    143         contains: EXPRESSION_CONTAINS.concat(['self']),
    144         relevance: 0
    145       }
    146     ]),
    147     relevance: 0
    148   };
    149 
    150   var FUNCTION_DECLARATION = {
    151     className: 'function',
    152     begin: '(' + FUNCTION_TYPE_RE + '[\\*&\\s]+)+' + FUNCTION_TITLE,
    153     returnBegin: true, end: /[{;=]/,
    154     excludeEnd: true,
    155     keywords: CPP_KEYWORDS,
    156     illegal: /[^\w\s\*&:<>]/,
    157     contains: [
    158 
    159       { // to prevent it from being confused as the function title
    160         begin: DECLTYPE_AUTO_RE,
    161         keywords: CPP_KEYWORDS,
    162         relevance: 0,
    163       },
    164       {
    165         begin: FUNCTION_TITLE, returnBegin: true,
    166         contains: [TITLE_MODE],
    167         relevance: 0
    168       },
    169       {
    170         className: 'params',
    171         begin: /\(/, end: /\)/,
    172         keywords: CPP_KEYWORDS,
    173         relevance: 0,
    174         contains: [
    175           hljs.C_LINE_COMMENT_MODE,
    176           hljs.C_BLOCK_COMMENT_MODE,
    177           STRINGS,
    178           NUMBERS,
    179           CPP_PRIMITIVE_TYPES,
    180           // Count matching parentheses.
    181           {
    182             begin: /\(/, end: /\)/,
    183             keywords: CPP_KEYWORDS,
    184             relevance: 0,
    185             contains: [
    186               'self',
    187               hljs.C_LINE_COMMENT_MODE,
    188               hljs.C_BLOCK_COMMENT_MODE,
    189               STRINGS,
    190               NUMBERS,
    191               CPP_PRIMITIVE_TYPES
    192             ]
    193           }
    194         ]
    195       },
    196       CPP_PRIMITIVE_TYPES,
    197       hljs.C_LINE_COMMENT_MODE,
    198       hljs.C_BLOCK_COMMENT_MODE,
    199       PREPROCESSOR
    200     ]
    201   };
    202 
    203   return {
    204     aliases: ['c', 'cc', 'h', 'c++', 'h++', 'hpp', 'hh', 'hxx', 'cxx'],
    205     keywords: CPP_KEYWORDS,
    206     // the base c-like language will NEVER be auto-detected, rather the
    207     // derivitives: c, c++, arduino turn auto-detect back on for themselves
    208     disableAutodetect: true,
    209     illegal: '</',
    210     contains: [].concat(
    211       EXPRESSION_CONTEXT,
    212       FUNCTION_DECLARATION,
    213       EXPRESSION_CONTAINS,
    214       [
    215       PREPROCESSOR,
    216       { // containers: ie, `vector <int> rooms (9);`
    217         begin: '\\b(deque|list|queue|priority_queue|pair|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array)\\s*<', end: '>',
    218         keywords: CPP_KEYWORDS,
    219         contains: ['self', CPP_PRIMITIVE_TYPES]
    220       },
    221       {
    222         begin: hljs.IDENT_RE + '::',
    223         keywords: CPP_KEYWORDS
    224       },
    225       {
    226         className: 'class',
    227         beginKeywords: 'class struct', end: /[{;:]/,
    228         contains: [
    229           {begin: /</, end: />/, contains: ['self']}, // skip generic stuff
    230           hljs.TITLE_MODE
    231         ]
    232       }
    233     ]),
    234     exports: {
    235       preprocessor: PREPROCESSOR,
    236       strings: STRINGS,
    237       keywords: CPP_KEYWORDS
    238     }
    239   };
    240 }
    241 
    242 module.exports = cLike;