prism-cpp.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. (function (Prism) {
  2. var keyword = /\b(?:alignas|alignof|asm|auto|bool|break|case|catch|char|char8_t|char16_t|char32_t|class|compl|concept|const|consteval|constexpr|constinit|const_cast|continue|co_await|co_return|co_yield|decltype|default|delete|do|double|dynamic_cast|else|enum|explicit|export|extern|float|for|friend|goto|if|inline|int|int8_t|int16_t|int32_t|int64_t|uint8_t|uint16_t|uint32_t|uint64_t|long|mutable|namespace|new|noexcept|nullptr|operator|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|union|unsigned|using|virtual|void|volatile|wchar_t|while)\b/;
  3. Prism.languages.cpp = Prism.languages.extend('c', {
  4. 'class-name': [
  5. {
  6. pattern: RegExp(/(\b(?:class|concept|enum|struct|typename)\s+)(?!<keyword>)\w+/.source
  7. .replace(/<keyword>/g, function () { return keyword.source; })),
  8. lookbehind: true
  9. },
  10. // This is intended to capture the class name of method implementations like:
  11. // void foo::bar() const {}
  12. // However! The `foo` in the above example could also be a namespace, so we only capture the class name if
  13. // it starts with an uppercase letter. This approximation should give decent results.
  14. /\b[A-Z]\w*(?=\s*::\s*\w+\s*\()/,
  15. // This will capture the class name before destructors like:
  16. // Foo::~Foo() {}
  17. /\b[A-Z_]\w*(?=\s*::\s*~\w+\s*\()/i,
  18. // This also intends to capture the class name of method implementations but here the class has template
  19. // parameters, so it can't be a namespace (until C++ adds generic namespaces).
  20. /\w+(?=\s*<(?:[^<>]|<(?:[^<>]|<[^<>]*>)*>)*>\s*::\s*\w+\s*\()/
  21. ],
  22. 'keyword': keyword,
  23. 'number': {
  24. pattern: /(?:\b0b[01']+|\b0x(?:[\da-f']+\.?[\da-f']*|\.[\da-f']+)(?:p[+-]?[\d']+)?|(?:\b[\d']+\.?[\d']*|\B\.[\d']+)(?:e[+-]?[\d']+)?)[ful]*/i,
  25. greedy: true
  26. },
  27. 'operator': />>=?|<<=?|->|([-+&|:])\1|[?:~]|<=>|[-+*/%&|^!=<>]=?|\b(?:and|and_eq|bitand|bitor|not|not_eq|or|or_eq|xor|xor_eq)\b/,
  28. 'boolean': /\b(?:true|false)\b/
  29. });
  30. Prism.languages.insertBefore('cpp', 'string', {
  31. 'raw-string': {
  32. pattern: /R"([^()\\ ]{0,16})\([\s\S]*?\)\1"/,
  33. alias: 'string',
  34. greedy: true
  35. }
  36. });
  37. Prism.languages.insertBefore('cpp', 'class-name', {
  38. // the base clause is an optional list of parent classes
  39. // https://en.cppreference.com/w/cpp/language/class
  40. 'base-clause': {
  41. pattern: /(\b(?:class|struct)\s+\w+\s*:\s*)(?:[^;{}"'])+?(?=\s*[;{])/,
  42. lookbehind: true,
  43. greedy: true,
  44. inside: Prism.languages.extend('cpp', {})
  45. }
  46. });
  47. Prism.languages.insertBefore('inside', 'operator', {
  48. // All untokenized words that are not namespaces should be class names
  49. 'class-name': /\b[a-z_]\w*\b(?!\s*::)/i
  50. }, Prism.languages.cpp['base-clause']);
  51. }(Prism));