CoolFace
Datasetpublic

basant307/AI_Governance_Project

sourceHugging Faceapache-2.0updated 2mo agoView on Hugging Face
0likes48downloads
cpp.js606 linesDownload Raw Back to languages
1/*2Language: C++3Category: common, system4Website: https://isocpp.org5*/6 7/** @type LanguageFn */8function cpp(hljs) {9  const regex = hljs.regex;10  // added for historic reasons because `hljs.C_LINE_COMMENT_MODE` does11  // not include such support nor can we be sure all the grammars depending12  // on it would desire this behavior13  const C_LINE_COMMENT_MODE = hljs.COMMENT('//', '$', { contains: [ { begin: /\\\n/ } ] });14  const DECLTYPE_AUTO_RE = 'decltype\\(auto\\)';15  const NAMESPACE_RE = '[a-zA-Z_]\\w*::';16  const TEMPLATE_ARGUMENT_RE = '<[^<>]+>';17  const FUNCTION_TYPE_RE = '(?!struct)('18    + DECLTYPE_AUTO_RE + '|'19    + regex.optional(NAMESPACE_RE)20    + '[a-zA-Z_]\\w*' + regex.optional(TEMPLATE_ARGUMENT_RE)21  + ')';22 23  const CPP_PRIMITIVE_TYPES = {24    className: 'type',25    begin: '\\b[a-z\\d_]*_t\\b'26  };27 28  // https://en.cppreference.com/w/cpp/language/escape29  // \\ \x \xFF \u2837 \u00323747 \37430  const CHARACTER_ESCAPES = '\\\\(x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4,8}|[0-7]{3}|\\S)';31  const STRINGS = {32    className: 'string',33    variants: [34      {35        begin: '(u8?|U|L)?"',36        end: '"',37        illegal: '\\n',38        contains: [ hljs.BACKSLASH_ESCAPE ]39      },40      {41        begin: '(u8?|U|L)?\'(' + CHARACTER_ESCAPES + '|.)',42        end: '\'',43        illegal: '.'44      },45      hljs.END_SAME_AS_BEGIN({46        begin: /(?:u8?|U|L)?R"([^()\\ ]{0,16})\(/,47        end: /\)([^()\\ ]{0,16})"/48      })49    ]50  };51 52  const NUMBERS = {53    className: 'number',54    variants: [55      // Floating-point literal.56      { begin:57        "[+-]?(?:" // Leading sign.58          // Decimal.59          + "(?:"60            +"[0-9](?:'?[0-9])*\\.(?:[0-9](?:'?[0-9])*)?"61            + "|\\.[0-9](?:'?[0-9])*"62          + ")(?:[Ee][+-]?[0-9](?:'?[0-9])*)?"63          + "|[0-9](?:'?[0-9])*[Ee][+-]?[0-9](?:'?[0-9])*"64          // Hexadecimal.65          + "|0[Xx](?:"66            +"[0-9A-Fa-f](?:'?[0-9A-Fa-f])*(?:\\.(?:[0-9A-Fa-f](?:'?[0-9A-Fa-f])*)?)?"67            + "|\\.[0-9A-Fa-f](?:'?[0-9A-Fa-f])*"68          + ")[Pp][+-]?[0-9](?:'?[0-9])*"69        + ")(?:" // Literal suffixes.70          + "[Ff](?:16|32|64|128)?"71          + "|(BF|bf)16"72          + "|[Ll]"73          + "|" // Literal suffix is optional.74        + ")"75      },76      // Integer literal.77      { begin:78        "[+-]?\\b(?:" // Leading sign.79          + "0[Bb][01](?:'?[01])*" // Binary.80          + "|0[Xx][0-9A-Fa-f](?:'?[0-9A-Fa-f])*" // Hexadecimal.81          + "|0(?:'?[0-7])*" // Octal or just a lone zero.82          + "|[1-9](?:'?[0-9])*" // Decimal.83        + ")(?:" // Literal suffixes.84          + "[Uu](?:LL?|ll?)"85          + "|[Uu][Zz]?"86          + "|(?:LL?|ll?)[Uu]?"87          + "|[Zz][Uu]"88          + "|" // Literal suffix is optional.89        + ")"90        // Note: there are user-defined literal suffixes too, but perhaps having the custom suffix not part of the91        // literal highlight actually makes it stand out more.92      }93    ],94    relevance: 095  };96 97  const PREPROCESSOR = {98    className: 'meta',99    begin: /#\s*[a-z]+\b/,100    end: /$/,101    keywords: { keyword:102        'if else elif endif define undef warning error line '103        + 'pragma _Pragma ifdef ifndef include' },104    contains: [105      {106        begin: /\\\n/,107        relevance: 0108      },109      hljs.inherit(STRINGS, { className: 'string' }),110      {111        className: 'string',112        begin: /<.*?>/113      },114      C_LINE_COMMENT_MODE,115      hljs.C_BLOCK_COMMENT_MODE116    ]117  };118 119  const TITLE_MODE = {120    className: 'title',121    begin: regex.optional(NAMESPACE_RE) + hljs.IDENT_RE,122    relevance: 0123  };124 125  const FUNCTION_TITLE = regex.optional(NAMESPACE_RE) + hljs.IDENT_RE + '\\s*\\(';126 127  // https://en.cppreference.com/w/cpp/keyword128  const RESERVED_KEYWORDS = [129    'alignas',130    'alignof',131    'and',132    'and_eq',133    'asm',134    'atomic_cancel',135    'atomic_commit',136    'atomic_noexcept',137    'auto',138    'bitand',139    'bitor',140    'break',141    'case',142    'catch',143    'class',144    'co_await',145    'co_return',146    'co_yield',147    'compl',148    'concept',149    'const_cast|10',150    'consteval',151    'constexpr',152    'constinit',153    'continue',154    'decltype',155    'default',156    'delete',157    'do',158    'dynamic_cast|10',159    'else',160    'enum',161    'explicit',162    'export',163    'extern',164    'false',165    'final',166    'for',167    'friend',168    'goto',169    'if',170    'import',171    'inline',172    'module',173    'mutable',174    'namespace',175    'new',176    'noexcept',177    'not',178    'not_eq',179    'nullptr',180    'operator',181    'or',182    'or_eq',183    'override',184    'private',185    'protected',186    'public',187    'reflexpr',188    'register',189    'reinterpret_cast|10',190    'requires',191    'return',192    'sizeof',193    'static_assert',194    'static_cast|10',195    'struct',196    'switch',197    'synchronized',198    'template',199    'this',200    'thread_local',201    'throw',202    'transaction_safe',203    'transaction_safe_dynamic',204    'true',205    'try',206    'typedef',207    'typeid',208    'typename',209    'union',210    'using',211    'virtual',212    'volatile',213    'while',214    'xor',215    'xor_eq'216  ];217 218  // https://en.cppreference.com/w/cpp/keyword219  const RESERVED_TYPES = [220    'bool',221    'char',222    'char16_t',223    'char32_t',224    'char8_t',225    'double',226    'float',227    'int',228    'long',229    'short',230    'void',231    'wchar_t',232    'unsigned',233    'signed',234    'const',235    'static'236  ];237 238  const TYPE_HINTS = [239    'any',240    'auto_ptr',241    'barrier',242    'binary_semaphore',243    'bitset',244    'complex',245    'condition_variable',246    'condition_variable_any',247    'counting_semaphore',248    'deque',249    'false_type',250    'flat_map',251    'flat_set',252    'future',253    'imaginary',254    'initializer_list',255    'istringstream',256    'jthread',257    'latch',258    'lock_guard',259    'multimap',260    'multiset',261    'mutex',262    'optional',263    'ostringstream',264    'packaged_task',265    'pair',266    'promise',267    'priority_queue',268    'queue',269    'recursive_mutex',270    'recursive_timed_mutex',271    'scoped_lock',272    'set',273    'shared_future',274    'shared_lock',275    'shared_mutex',276    'shared_timed_mutex',277    'shared_ptr',278    'stack',279    'string_view',280    'stringstream',281    'timed_mutex',282    'thread',283    'true_type',284    'tuple',285    'unique_lock',286    'unique_ptr',287    'unordered_map',288    'unordered_multimap',289    'unordered_multiset',290    'unordered_set',291    'variant',292    'vector',293    'weak_ptr',294    'wstring',295    'wstring_view'296  ];297 298  const FUNCTION_HINTS = [299    'abort',300    'abs',301    'acos',302    'apply',303    'as_const',304    'asin',305    'atan',306    'atan2',307    'calloc',308    'ceil',309    'cerr',310    'cin',311    'clog',312    'cos',313    'cosh',314    'cout',315    'declval',316    'endl',317    'exchange',318    'exit',319    'exp',320    'fabs',321    'floor',322    'fmod',323    'forward',324    'fprintf',325    'fputs',326    'free',327    'frexp',328    'fscanf',329    'future',330    'invoke',331    'isalnum',332    'isalpha',333    'iscntrl',334    'isdigit',335    'isgraph',336    'islower',337    'isprint',338    'ispunct',339    'isspace',340    'isupper',341    'isxdigit',342    'labs',343    'launder',344    'ldexp',345    'log',346    'log10',347    'make_pair',348    'make_shared',349    'make_shared_for_overwrite',350    'make_tuple',351    'make_unique',352    'malloc',353    'memchr',354    'memcmp',355    'memcpy',356    'memset',357    'modf',358    'move',359    'pow',360    'printf',361    'putchar',362    'puts',363    'realloc',364    'scanf',365    'sin',366    'sinh',367    'snprintf',368    'sprintf',369    'sqrt',370    'sscanf',371    'std',372    'stderr',373    'stdin',374    'stdout',375    'strcat',376    'strchr',377    'strcmp',378    'strcpy',379    'strcspn',380    'strlen',381    'strncat',382    'strncmp',383    'strncpy',384    'strpbrk',385    'strrchr',386    'strspn',387    'strstr',388    'swap',389    'tan',390    'tanh',391    'terminate',392    'to_underlying',393    'tolower',394    'toupper',395    'vfprintf',396    'visit',397    'vprintf',398    'vsprintf'399  ];400 401  const LITERALS = [402    'NULL',403    'false',404    'nullopt',405    'nullptr',406    'true'407  ];408 409  // https://en.cppreference.com/w/cpp/keyword410  const BUILT_IN = [ '_Pragma' ];411 412  const CPP_KEYWORDS = {413    type: RESERVED_TYPES,414    keyword: RESERVED_KEYWORDS,415    literal: LITERALS,416    built_in: BUILT_IN,417    _type_hints: TYPE_HINTS418  };419 420  const FUNCTION_DISPATCH = {421    className: 'function.dispatch',422    relevance: 0,423    keywords: {424      // Only for relevance, not highlighting.425      _hint: FUNCTION_HINTS },426    begin: regex.concat(427      /\b/,428      /(?!decltype)/,429      /(?!if)/,430      /(?!for)/,431      /(?!switch)/,432      /(?!while)/,433      hljs.IDENT_RE,434      regex.lookahead(/(<[^<>]+>|)\s*\(/))435  };436 437  const EXPRESSION_CONTAINS = [438    FUNCTION_DISPATCH,439    PREPROCESSOR,440    CPP_PRIMITIVE_TYPES,441    C_LINE_COMMENT_MODE,442    hljs.C_BLOCK_COMMENT_MODE,443    NUMBERS,444    STRINGS445  ];446 447  const EXPRESSION_CONTEXT = {448    // This mode covers expression context where we can't expect a function449    // definition and shouldn't highlight anything that looks like one:450    // `return some()`, `else if()`, `(x*sum(1, 2))`451    variants: [452      {453        begin: /=/,454        end: /;/455      },456      {457        begin: /\(/,458        end: /\)/459      },460      {461        beginKeywords: 'new throw return else',462        end: /;/463      }464    ],465    keywords: CPP_KEYWORDS,466    contains: EXPRESSION_CONTAINS.concat([467      {468        begin: /\(/,469        end: /\)/,470        keywords: CPP_KEYWORDS,471        contains: EXPRESSION_CONTAINS.concat([ 'self' ]),472        relevance: 0473      }474    ]),475    relevance: 0476  };477 478  const FUNCTION_DECLARATION = {479    className: 'function',480    begin: '(' + FUNCTION_TYPE_RE + '[\\*&\\s]+)+' + FUNCTION_TITLE,481    returnBegin: true,482    end: /[{;=]/,483    excludeEnd: true,484    keywords: CPP_KEYWORDS,485    illegal: /[^\w\s\*&:<>.]/,486    contains: [487      { // to prevent it from being confused as the function title488        begin: DECLTYPE_AUTO_RE,489        keywords: CPP_KEYWORDS,490        relevance: 0491      },492      {493        begin: FUNCTION_TITLE,494        returnBegin: true,495        contains: [ TITLE_MODE ],496        relevance: 0497      },498      // needed because we do not have look-behind on the below rule499      // to prevent it from grabbing the final : in a :: pair500      {501        begin: /::/,502        relevance: 0503      },504      // initializers505      {506        begin: /:/,507        endsWithParent: true,508        contains: [509          STRINGS,510          NUMBERS511        ]512      },513      // allow for multiple declarations, e.g.:514      // extern void f(int), g(char);515      {516        relevance: 0,517        match: /,/518      },519      {520        className: 'params',521        begin: /\(/,522        end: /\)/,523        keywords: CPP_KEYWORDS,524        relevance: 0,525        contains: [526          C_LINE_COMMENT_MODE,527          hljs.C_BLOCK_COMMENT_MODE,528          STRINGS,529          NUMBERS,530          CPP_PRIMITIVE_TYPES,531          // Count matching parentheses.532          {533            begin: /\(/,534            end: /\)/,535            keywords: CPP_KEYWORDS,536            relevance: 0,537            contains: [538              'self',539              C_LINE_COMMENT_MODE,540              hljs.C_BLOCK_COMMENT_MODE,541              STRINGS,542              NUMBERS,543              CPP_PRIMITIVE_TYPES544            ]545          }546        ]547      },548      CPP_PRIMITIVE_TYPES,549      C_LINE_COMMENT_MODE,550      hljs.C_BLOCK_COMMENT_MODE,551      PREPROCESSOR552    ]553  };554 555  return {556    name: 'C++',557    aliases: [558      'cc',559      'c++',560      'h++',561      'hpp',562      'hh',563      'hxx',564      'cxx'565    ],566    keywords: CPP_KEYWORDS,567    illegal: '</',568    classNameAliases: { 'function.dispatch': 'built_in' },569    contains: [].concat(570      EXPRESSION_CONTEXT,571      FUNCTION_DECLARATION,572      FUNCTION_DISPATCH,573      EXPRESSION_CONTAINS,574      [575        PREPROCESSOR,576        { // containers: ie, `vector <int> rooms (9);`577          begin: '\\b(deque|list|queue|priority_queue|pair|stack|vector|map|set|bitset|multiset|multimap|unordered_map|unordered_set|unordered_multiset|unordered_multimap|array|tuple|optional|variant|function|flat_map|flat_set)\\s*<(?!<)',578          end: '>',579          keywords: CPP_KEYWORDS,580          contains: [581            'self',582            CPP_PRIMITIVE_TYPES583          ]584        },585        {586          begin: hljs.IDENT_RE + '::',587          keywords: CPP_KEYWORDS588        },589        {590          match: [591            // extra complexity to deal with `enum class` and `enum struct`592            /\b(?:enum(?:\s+(?:class|struct))?|class|struct|union)/,593            /\s+/,594            /\w+/595          ],596          className: {597            1: 'keyword',598            3: 'title.class'599          }600        }601      ])602  };603}604 605export { cpp as default };606 
basant307/AI_Governance_Project · CoolFace