[d24f17c] | 1 | 'use strict'
|
---|
| 2 | var refractorC = require('./c.js')
|
---|
| 3 | module.exports = cpp
|
---|
| 4 | cpp.displayName = 'cpp'
|
---|
| 5 | cpp.aliases = []
|
---|
| 6 | function cpp(Prism) {
|
---|
| 7 | Prism.register(refractorC)
|
---|
| 8 | ;(function (Prism) {
|
---|
| 9 | var keyword =
|
---|
| 10 | /\b(?:alignas|alignof|asm|auto|bool|break|case|catch|char|char16_t|char32_t|char8_t|class|co_await|co_return|co_yield|compl|concept|const|const_cast|consteval|constexpr|constinit|continue|decltype|default|delete|do|double|dynamic_cast|else|enum|explicit|export|extern|final|float|for|friend|goto|if|import|inline|int|int16_t|int32_t|int64_t|int8_t|long|module|mutable|namespace|new|noexcept|nullptr|operator|override|private|protected|public|register|reinterpret_cast|requires|return|short|signed|sizeof|static|static_assert|static_cast|struct|switch|template|this|thread_local|throw|try|typedef|typeid|typename|uint16_t|uint32_t|uint64_t|uint8_t|union|unsigned|using|virtual|void|volatile|wchar_t|while)\b/
|
---|
| 11 | var modName = /\b(?!<keyword>)\w+(?:\s*\.\s*\w+)*\b/.source.replace(
|
---|
| 12 | /<keyword>/g,
|
---|
| 13 | function () {
|
---|
| 14 | return keyword.source
|
---|
| 15 | }
|
---|
| 16 | )
|
---|
| 17 | Prism.languages.cpp = Prism.languages.extend('c', {
|
---|
| 18 | 'class-name': [
|
---|
| 19 | {
|
---|
| 20 | pattern: RegExp(
|
---|
| 21 | /(\b(?:class|concept|enum|struct|typename)\s+)(?!<keyword>)\w+/.source.replace(
|
---|
| 22 | /<keyword>/g,
|
---|
| 23 | function () {
|
---|
| 24 | return keyword.source
|
---|
| 25 | }
|
---|
| 26 | )
|
---|
| 27 | ),
|
---|
| 28 | lookbehind: true
|
---|
| 29 | }, // This is intended to capture the class name of method implementations like:
|
---|
| 30 | // void foo::bar() const {}
|
---|
| 31 | // However! The `foo` in the above example could also be a namespace, so we only capture the class name if
|
---|
| 32 | // it starts with an uppercase letter. This approximation should give decent results.
|
---|
| 33 | /\b[A-Z]\w*(?=\s*::\s*\w+\s*\()/, // This will capture the class name before destructors like:
|
---|
| 34 | // Foo::~Foo() {}
|
---|
| 35 | /\b[A-Z_]\w*(?=\s*::\s*~\w+\s*\()/i, // This also intends to capture the class name of method implementations but here the class has template
|
---|
| 36 | // parameters, so it can't be a namespace (until C++ adds generic namespaces).
|
---|
| 37 | /\b\w+(?=\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>\s*::\s*\w+\s*\()/
|
---|
| 38 | ],
|
---|
| 39 | keyword: keyword,
|
---|
| 40 | number: {
|
---|
| 41 | pattern:
|
---|
| 42 | /(?:\b0b[01']+|\b0x(?:[\da-f']+(?:\.[\da-f']*)?|\.[\da-f']+)(?:p[+-]?[\d']+)?|(?:\b[\d']+(?:\.[\d']*)?|\B\.[\d']+)(?:e[+-]?[\d']+)?)[ful]{0,4}/i,
|
---|
| 43 | greedy: true
|
---|
| 44 | },
|
---|
| 45 | operator:
|
---|
| 46 | />>=?|<<=?|->|--|\+\+|&&|\|\||[?:~]|<=>|[-+*/%&|^!=<>]=?|\b(?:and|and_eq|bitand|bitor|not|not_eq|or|or_eq|xor|xor_eq)\b/,
|
---|
| 47 | boolean: /\b(?:false|true)\b/
|
---|
| 48 | })
|
---|
| 49 | Prism.languages.insertBefore('cpp', 'string', {
|
---|
| 50 | module: {
|
---|
| 51 | // https://en.cppreference.com/w/cpp/language/modules
|
---|
| 52 | pattern: RegExp(
|
---|
| 53 | /(\b(?:import|module)\s+)/.source +
|
---|
| 54 | '(?:' + // header-name
|
---|
| 55 | /"(?:\\(?:\r\n|[\s\S])|[^"\\\r\n])*"|<[^<>\r\n]*>/.source +
|
---|
| 56 | '|' + // module name or partition or both
|
---|
| 57 | /<mod-name>(?:\s*:\s*<mod-name>)?|:\s*<mod-name>/.source.replace(
|
---|
| 58 | /<mod-name>/g,
|
---|
| 59 | function () {
|
---|
| 60 | return modName
|
---|
| 61 | }
|
---|
| 62 | ) +
|
---|
| 63 | ')'
|
---|
| 64 | ),
|
---|
| 65 | lookbehind: true,
|
---|
| 66 | greedy: true,
|
---|
| 67 | inside: {
|
---|
| 68 | string: /^[<"][\s\S]+/,
|
---|
| 69 | operator: /:/,
|
---|
| 70 | punctuation: /\./
|
---|
| 71 | }
|
---|
| 72 | },
|
---|
| 73 | 'raw-string': {
|
---|
| 74 | pattern: /R"([^()\\ ]{0,16})\([\s\S]*?\)\1"/,
|
---|
| 75 | alias: 'string',
|
---|
| 76 | greedy: true
|
---|
| 77 | }
|
---|
| 78 | })
|
---|
| 79 | Prism.languages.insertBefore('cpp', 'keyword', {
|
---|
| 80 | 'generic-function': {
|
---|
| 81 | pattern: /\b(?!operator\b)[a-z_]\w*\s*<(?:[^<>]|<[^<>]*>)*>(?=\s*\()/i,
|
---|
| 82 | inside: {
|
---|
| 83 | function: /^\w+/,
|
---|
| 84 | generic: {
|
---|
| 85 | pattern: /<[\s\S]+/,
|
---|
| 86 | alias: 'class-name',
|
---|
| 87 | inside: Prism.languages.cpp
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 | })
|
---|
| 92 | Prism.languages.insertBefore('cpp', 'operator', {
|
---|
| 93 | 'double-colon': {
|
---|
| 94 | pattern: /::/,
|
---|
| 95 | alias: 'punctuation'
|
---|
| 96 | }
|
---|
| 97 | })
|
---|
| 98 | Prism.languages.insertBefore('cpp', 'class-name', {
|
---|
| 99 | // the base clause is an optional list of parent classes
|
---|
| 100 | // https://en.cppreference.com/w/cpp/language/class
|
---|
| 101 | 'base-clause': {
|
---|
| 102 | pattern:
|
---|
| 103 | /(\b(?:class|struct)\s+\w+\s*:\s*)[^;{}"'\s]+(?:\s+[^;{}"'\s]+)*(?=\s*[;{])/,
|
---|
| 104 | lookbehind: true,
|
---|
| 105 | greedy: true,
|
---|
| 106 | inside: Prism.languages.extend('cpp', {})
|
---|
| 107 | }
|
---|
| 108 | })
|
---|
| 109 | Prism.languages.insertBefore(
|
---|
| 110 | 'inside',
|
---|
| 111 | 'double-colon',
|
---|
| 112 | {
|
---|
| 113 | // All untokenized words that are not namespaces should be class names
|
---|
| 114 | 'class-name': /\b[a-z_]\w*\b(?!\s*::)/i
|
---|
| 115 | },
|
---|
| 116 | Prism.languages.cpp['base-clause']
|
---|
| 117 | )
|
---|
| 118 | })(Prism)
|
---|
| 119 | }
|
---|