prism-php.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Original by Aaron Harun: http://aahacreative.com/2012/07/31/php-syntax-highlighting-prism/
  3. * Modified by Miles Johnson: http://milesj.me
  4. *
  5. * Supports the following:
  6. * - Extends clike syntax
  7. * - Support for PHP 5.3+ (namespaces, traits, generators, etc)
  8. * - Smarter constant and function matching
  9. *
  10. * Adds the following new token classes:
  11. * constant, delimiter, variable, function, package
  12. */
  13. (function (Prism) {
  14. Prism.languages.php = Prism.languages.extend('clike', {
  15. 'keyword': /\b(?:__halt_compiler|abstract|and|array|as|break|callable|case|catch|class|clone|const|continue|declare|default|die|do|echo|else|elseif|empty|enddeclare|endfor|endforeach|endif|endswitch|endwhile|eval|exit|extends|final|finally|for|foreach|function|global|goto|if|implements|include|include_once|instanceof|insteadof|interface|isset|list|match|namespace|new|or|parent|print|private|protected|public|require|require_once|return|static|switch|throw|trait|try|unset|use|var|while|xor|yield)\b/i,
  16. 'boolean': {
  17. pattern: /\b(?:false|true)\b/i,
  18. alias: 'constant'
  19. },
  20. 'constant': [
  21. /\b[A-Z_][A-Z0-9_]*\b/,
  22. /\b(?:null)\b/i,
  23. ],
  24. 'comment': {
  25. pattern: /(^|[^\\])(?:\/\*[\s\S]*?\*\/|\/\/.*)/,
  26. lookbehind: true
  27. }
  28. });
  29. Prism.languages.insertBefore('php', 'string', {
  30. 'shell-comment': {
  31. pattern: /(^|[^\\])#.*/,
  32. lookbehind: true,
  33. alias: 'comment'
  34. }
  35. });
  36. Prism.languages.insertBefore('php', 'comment', {
  37. 'delimiter': {
  38. pattern: /\?>$|^<\?(?:php(?=\s)|=)?/i,
  39. alias: 'important'
  40. }
  41. });
  42. Prism.languages.insertBefore('php', 'keyword', {
  43. 'variable': /\$+(?:\w+\b|(?={))/i,
  44. 'package': {
  45. pattern: /(\\|namespace\s+|use\s+)[\w\\]+/,
  46. lookbehind: true,
  47. inside: {
  48. punctuation: /\\/
  49. }
  50. }
  51. });
  52. // Must be defined after the function pattern
  53. Prism.languages.insertBefore('php', 'operator', {
  54. 'property': {
  55. pattern: /(->)[\w]+/,
  56. lookbehind: true
  57. }
  58. });
  59. var string_interpolation = {
  60. pattern: /{\$(?:{(?:{[^{}]+}|[^{}]+)}|[^{}])+}|(^|[^\\{])\$+(?:\w+(?:\[[^\r\n\[\]]+\]|->\w+)*)/,
  61. lookbehind: true,
  62. inside: Prism.languages.php
  63. };
  64. Prism.languages.insertBefore('php', 'string', {
  65. 'nowdoc-string': {
  66. pattern: /<<<'([^']+)'[\r\n](?:.*[\r\n])*?\1;/,
  67. greedy: true,
  68. alias: 'string',
  69. inside: {
  70. 'delimiter': {
  71. pattern: /^<<<'[^']+'|[a-z_]\w*;$/i,
  72. alias: 'symbol',
  73. inside: {
  74. 'punctuation': /^<<<'?|[';]$/
  75. }
  76. }
  77. }
  78. },
  79. 'heredoc-string': {
  80. pattern: /<<<(?:"([^"]+)"[\r\n](?:.*[\r\n])*?\1;|([a-z_]\w*)[\r\n](?:.*[\r\n])*?\2;)/i,
  81. greedy: true,
  82. alias: 'string',
  83. inside: {
  84. 'delimiter': {
  85. pattern: /^<<<(?:"[^"]+"|[a-z_]\w*)|[a-z_]\w*;$/i,
  86. alias: 'symbol',
  87. inside: {
  88. 'punctuation': /^<<<"?|[";]$/
  89. }
  90. },
  91. 'interpolation': string_interpolation // See below
  92. }
  93. },
  94. 'single-quoted-string': {
  95. pattern: /'(?:\\[\s\S]|[^\\'])*'/,
  96. greedy: true,
  97. alias: 'string'
  98. },
  99. 'double-quoted-string': {
  100. pattern: /"(?:\\[\s\S]|[^\\"])*"/,
  101. greedy: true,
  102. alias: 'string',
  103. inside: {
  104. 'interpolation': string_interpolation // See below
  105. }
  106. }
  107. });
  108. // The different types of PHP strings "replace" the C-like standard string
  109. delete Prism.languages.php['string'];
  110. Prism.hooks.add('before-tokenize', function(env) {
  111. if (!/<\?/.test(env.code)) {
  112. return;
  113. }
  114. var phpPattern = /<\?(?:[^"'/#]|\/(?![*/])|("|')(?:\\[\s\S]|(?!\1)[^\\])*\1|(?:\/\/|#)(?:[^?\n\r]|\?(?!>))*(?=$|\?>|[\r\n])|\/\*[\s\S]*?(?:\*\/|$))*?(?:\?>|$)/ig;
  115. Prism.languages['markup-templating'].buildPlaceholders(env, 'php', phpPattern);
  116. });
  117. Prism.hooks.add('after-tokenize', function(env) {
  118. Prism.languages['markup-templating'].tokenizePlaceholders(env, 'php');
  119. });
  120. }(Prism));