prismjs_components_prism-cpp.js 4.2 KB

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