prism-c.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. Prism.languages.c = Prism.languages.extend('clike', {
  2. 'comment': {
  3. pattern: /\/\/(?:[^\r\n\\]|\\(?:\r\n?|\n|(?![\r\n])))*|\/\*[\s\S]*?(?:\*\/|$)/,
  4. greedy: true
  5. },
  6. 'string': {
  7. // https://en.cppreference.com/w/c/language/string_literal
  8. pattern: /"(?:\\(?:\r\n|[\s\S])|[^"\\\r\n])*"/,
  9. greedy: true
  10. },
  11. 'class-name': {
  12. pattern: /(\b(?:enum|struct)\s+(?:__attribute__\s*\(\([\s\S]*?\)\)\s*)?)\w+|\b[a-z]\w*_t\b/,
  13. lookbehind: true
  14. },
  15. 'keyword': /\b(?:_Alignas|_Alignof|_Atomic|_Bool|_Complex|_Generic|_Imaginary|_Noreturn|_Static_assert|_Thread_local|__attribute__|asm|auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|inline|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|typeof|union|unsigned|void|volatile|while)\b/,
  16. 'function': /\b[a-z_]\w*(?=\s*\()/i,
  17. 'number': /(?:\b0x(?:[\da-f]+(?:\.[\da-f]*)?|\.[\da-f]+)(?:p[+-]?\d+)?|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?)[ful]{0,4}/i,
  18. 'operator': />>=?|<<=?|->|([-+&|:])\1|[?:~]|[-+*/%&|^!=<>]=?/
  19. });
  20. Prism.languages.insertBefore('c', 'string', {
  21. 'char': {
  22. // https://en.cppreference.com/w/c/language/character_constant
  23. pattern: /'(?:\\(?:\r\n|[\s\S])|[^'\\\r\n]){0,32}'/,
  24. greedy: true
  25. }
  26. });
  27. Prism.languages.insertBefore('c', 'string', {
  28. 'macro': {
  29. // allow for multiline macro definitions
  30. // spaces after the # character compile fine with gcc
  31. pattern: /(^[\t ]*)#\s*[a-z](?:[^\r\n\\/]|\/(?!\*)|\/\*(?:[^*]|\*(?!\/))*\*\/|\\(?:\r\n|[\s\S]))*/im,
  32. lookbehind: true,
  33. greedy: true,
  34. alias: 'property',
  35. inside: {
  36. 'string': [
  37. {
  38. // highlight the path of the include statement as a string
  39. pattern: /^(#\s*include\s*)<[^>]+>/,
  40. lookbehind: true
  41. },
  42. Prism.languages.c['string']
  43. ],
  44. 'char': Prism.languages.c['char'],
  45. 'comment': Prism.languages.c['comment'],
  46. 'macro-name': [
  47. {
  48. pattern: /(^#\s*define\s+)\w+\b(?!\()/i,
  49. lookbehind: true
  50. },
  51. {
  52. pattern: /(^#\s*define\s+)\w+\b(?=\()/i,
  53. lookbehind: true,
  54. alias: 'function'
  55. }
  56. ],
  57. // highlight macro directives as keywords
  58. 'directive': {
  59. pattern: /^(#\s*)[a-z]+/,
  60. lookbehind: true,
  61. alias: 'keyword'
  62. },
  63. 'directive-hash': /^#/,
  64. 'punctuation': /##|\\(?=[\r\n])/,
  65. 'expression': {
  66. pattern: /\S[\s\S]*/,
  67. inside: Prism.languages.c
  68. }
  69. }
  70. }
  71. });
  72. Prism.languages.insertBefore('c', 'function', {
  73. // highlight predefined macros as constants
  74. 'constant': /\b(?:EOF|NULL|SEEK_CUR|SEEK_END|SEEK_SET|__DATE__|__FILE__|__LINE__|__TIMESTAMP__|__TIME__|__func__|stderr|stdin|stdout)\b/
  75. });
  76. delete Prism.languages.c['boolean'];