prism-cpp.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. (function (Prism) {
  2. var keyword = /\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/;
  3. var modName = /\b(?!<keyword>)\w+(?:\s*\.\s*\w+)*\b/.source.replace(/<keyword>/g, function () { return keyword.source; });
  4. Prism.languages.cpp = Prism.languages.extend('c', {
  5. 'class-name': [
  6. {
  7. pattern: RegExp(/(\b(?:class|concept|enum|struct|typename)\s+)(?!<keyword>)\w+/.source
  8. .replace(/<keyword>/g, function () { return keyword.source; })),
  9. lookbehind: true
  10. },
  11. // This is intended to capture the class name of method implementations like:
  12. // void foo::bar() const {}
  13. // However! The `foo` in the above example could also be a namespace, so we only capture the class name if
  14. // it starts with an uppercase letter. This approximation should give decent results.
  15. /\b[A-Z]\w*(?=\s*::\s*\w+\s*\()/,
  16. // This will capture the class name before destructors like:
  17. // Foo::~Foo() {}
  18. /\b[A-Z_]\w*(?=\s*::\s*~\w+\s*\()/i,
  19. // This also intends to capture the class name of method implementations but here the class has template
  20. // parameters, so it can't be a namespace (until C++ adds generic namespaces).
  21. /\b\w+(?=\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>\s*::\s*\w+\s*\()/
  22. ],
  23. 'keyword': keyword,
  24. 'number': {
  25. pattern: /(?:\b0b[01']+|\b0x(?:[\da-f']+(?:\.[\da-f']*)?|\.[\da-f']+)(?:p[+-]?[\d']+)?|(?:\b[\d']+(?:\.[\d']*)?|\B\.[\d']+)(?:e[+-]?[\d']+)?)[ful]{0,4}/i,
  26. greedy: true
  27. },
  28. 'operator': />>=?|<<=?|->|--|\+\+|&&|\|\||[?:~]|<=>|[-+*/%&|^!=<>]=?|\b(?:and|and_eq|bitand|bitor|not|not_eq|or|or_eq|xor|xor_eq)\b/,
  29. 'boolean': /\b(?:false|true)\b/
  30. });
  31. Prism.languages.insertBefore('cpp', 'string', {
  32. 'module': {
  33. // https://en.cppreference.com/w/cpp/language/modules
  34. pattern: RegExp(
  35. /(\b(?:import|module)\s+)/.source +
  36. '(?:' +
  37. // header-name
  38. /"(?:\\(?:\r\n|[\s\S])|[^"\\\r\n])*"|<[^<>\r\n]*>/.source +
  39. '|' +
  40. // module name or partition or both
  41. /<mod-name>(?:\s*:\s*<mod-name>)?|:\s*<mod-name>/.source.replace(/<mod-name>/g, function () { return modName; }) +
  42. ')'
  43. ),
  44. lookbehind: true,
  45. greedy: true,
  46. inside: {
  47. 'string': /^[<"][\s\S]+/,
  48. 'operator': /:/,
  49. 'punctuation': /\./
  50. }
  51. },
  52. 'raw-string': {
  53. pattern: /R"([^()\\ ]{0,16})\([\s\S]*?\)\1"/,
  54. alias: 'string',
  55. greedy: true
  56. }
  57. });
  58. Prism.languages.insertBefore('cpp', 'keyword', {
  59. 'generic-function': {
  60. pattern: /\b(?!operator\b)[a-z_]\w*\s*<(?:[^<>]|<[^<>]*>)*>(?=\s*\()/i,
  61. inside: {
  62. 'function': /^\w+/,
  63. 'generic': {
  64. pattern: /<[\s\S]+/,
  65. alias: 'class-name',
  66. inside: Prism.languages.cpp
  67. }
  68. }
  69. }
  70. });
  71. Prism.languages.insertBefore('cpp', 'operator', {
  72. 'double-colon': {
  73. pattern: /::/,
  74. alias: 'punctuation'
  75. }
  76. });
  77. Prism.languages.insertBefore('cpp', 'class-name', {
  78. // the base clause is an optional list of parent classes
  79. // https://en.cppreference.com/w/cpp/language/class
  80. 'base-clause': {
  81. pattern: /(\b(?:class|struct)\s+\w+\s*:\s*)[^;{}"'\s]+(?:\s+[^;{}"'\s]+)*(?=\s*[;{])/,
  82. lookbehind: true,
  83. greedy: true,
  84. inside: Prism.languages.extend('cpp', {})
  85. }
  86. });
  87. Prism.languages.insertBefore('inside', 'double-colon', {
  88. // All untokenized words that are not namespaces should be class names
  89. 'class-name': /\b[a-z_]\w*\b(?!\s*::)/i
  90. }, Prism.languages.cpp['base-clause']);
  91. }(Prism));