prism-c.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. 'class-name': {
  7. pattern: /(\b(?:enum|struct)\s+(?:__attribute__\s*\(\([\s\S]*?\)\)\s*)?)\w+/,
  8. lookbehind: true
  9. },
  10. 'keyword': /\b(?:__attribute__|_Alignas|_Alignof|_Atomic|_Bool|_Complex|_Generic|_Imaginary|_Noreturn|_Static_assert|_Thread_local|asm|typeof|inline|auto|break|case|char|const|continue|default|do|double|else|enum|extern|float|for|goto|if|int|long|register|return|short|signed|sizeof|static|struct|switch|typedef|union|unsigned|void|volatile|while)\b/,
  11. 'function': /[a-z_]\w*(?=\s*\()/i,
  12. 'operator': />>=?|<<=?|->|([-+&|:])\1|[?:~]|[-+*/%&|^!=<>]=?/,
  13. 'number': /(?:\b0x(?:[\da-f]+\.?[\da-f]*|\.[\da-f]+)(?:p[+-]?\d+)?|(?:\b\d+\.?\d*|\B\.\d+)(?:e[+-]?\d+)?)[ful]*/i
  14. });
  15. Prism.languages.insertBefore('c', 'string', {
  16. 'macro': {
  17. // allow for multiline macro definitions
  18. // spaces after the # character compile fine with gcc
  19. pattern: /(^\s*)#\s*[a-z]+(?:[^\r\n\\/]|\/(?!\*)|\/\*(?:[^*]|\*(?!\/))*\*\/|\\(?:\r\n|[\s\S]))*/im,
  20. lookbehind: true,
  21. greedy: true,
  22. alias: 'property',
  23. inside: {
  24. 'string': [
  25. {
  26. // highlight the path of the include statement as a string
  27. pattern: /^(#\s*include\s*)<[^>]+>/,
  28. lookbehind: true
  29. },
  30. Prism.languages.c['string']
  31. ],
  32. 'comment': Prism.languages.c['comment'],
  33. // highlight macro directives as keywords
  34. 'directive': {
  35. pattern: /^(#\s*)[a-z]+/,
  36. lookbehind: true,
  37. alias: 'keyword'
  38. },
  39. 'directive-hash': /^#/,
  40. 'punctuation': /##|\\(?=[\r\n])/,
  41. 'expression': {
  42. pattern: /\S[\s\S]*/,
  43. inside: Prism.languages.c
  44. }
  45. }
  46. },
  47. // highlight predefined macros as constants
  48. 'constant': /\b(?:__FILE__|__LINE__|__DATE__|__TIME__|__TIMESTAMP__|__func__|EOF|NULL|SEEK_CUR|SEEK_END|SEEK_SET|stdin|stdout|stderr)\b/
  49. });
  50. delete Prism.languages.c['boolean'];