php.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. module.exports = function(hljs) {
  2. var VARIABLE = {
  3. begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
  4. };
  5. var PREPROCESSOR = {
  6. className: 'meta', begin: /<\?(php)?|\?>/
  7. };
  8. var STRING = {
  9. className: 'string',
  10. contains: [hljs.BACKSLASH_ESCAPE, PREPROCESSOR],
  11. variants: [
  12. {
  13. begin: 'b"', end: '"'
  14. },
  15. {
  16. begin: 'b\'', end: '\''
  17. },
  18. hljs.inherit(hljs.APOS_STRING_MODE, {illegal: null}),
  19. hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null})
  20. ]
  21. };
  22. var NUMBER = {variants: [hljs.BINARY_NUMBER_MODE, hljs.C_NUMBER_MODE]};
  23. return {
  24. aliases: ['php', 'php3', 'php4', 'php5', 'php6', 'php7'],
  25. case_insensitive: true,
  26. keywords:
  27. 'and include_once list abstract global private echo interface as static endswitch ' +
  28. 'array null if endwhile or const for endforeach self var while isset public ' +
  29. 'protected exit foreach throw elseif include __FILE__ empty require_once do xor ' +
  30. 'return parent clone use __CLASS__ __LINE__ else break print eval new ' +
  31. 'catch __METHOD__ case exception default die require __FUNCTION__ ' +
  32. 'enddeclare final try switch continue endfor endif declare unset true false ' +
  33. 'trait goto instanceof insteadof __DIR__ __NAMESPACE__ ' +
  34. 'yield finally',
  35. contains: [
  36. hljs.HASH_COMMENT_MODE,
  37. hljs.COMMENT('//', '$', {contains: [PREPROCESSOR]}),
  38. hljs.COMMENT(
  39. '/\\*',
  40. '\\*/',
  41. {
  42. contains: [
  43. {
  44. className: 'doctag',
  45. begin: '@[A-Za-z]+'
  46. }
  47. ]
  48. }
  49. ),
  50. hljs.COMMENT(
  51. '__halt_compiler.+?;',
  52. false,
  53. {
  54. endsWithParent: true,
  55. keywords: '__halt_compiler',
  56. lexemes: hljs.UNDERSCORE_IDENT_RE
  57. }
  58. ),
  59. {
  60. className: 'string',
  61. begin: /<<<['"]?\w+['"]?$/, end: /^\w+;?$/,
  62. contains: [
  63. hljs.BACKSLASH_ESCAPE,
  64. {
  65. className: 'subst',
  66. variants: [
  67. {begin: /\$\w+/},
  68. {begin: /\{\$/, end: /\}/}
  69. ]
  70. }
  71. ]
  72. },
  73. PREPROCESSOR,
  74. {
  75. className: 'keyword', begin: /\$this\b/
  76. },
  77. VARIABLE,
  78. {
  79. // swallow composed identifiers to avoid parsing them as keywords
  80. begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/
  81. },
  82. {
  83. className: 'function',
  84. beginKeywords: 'function', end: /[;{]/, excludeEnd: true,
  85. illegal: '\\$|\\[|%',
  86. contains: [
  87. hljs.UNDERSCORE_TITLE_MODE,
  88. {
  89. className: 'params',
  90. begin: '\\(', end: '\\)',
  91. contains: [
  92. 'self',
  93. VARIABLE,
  94. hljs.C_BLOCK_COMMENT_MODE,
  95. STRING,
  96. NUMBER
  97. ]
  98. }
  99. ]
  100. },
  101. {
  102. className: 'class',
  103. beginKeywords: 'class interface', end: '{', excludeEnd: true,
  104. illegal: /[:\(\$"]/,
  105. contains: [
  106. {beginKeywords: 'extends implements'},
  107. hljs.UNDERSCORE_TITLE_MODE
  108. ]
  109. },
  110. {
  111. beginKeywords: 'namespace', end: ';',
  112. illegal: /[\.']/,
  113. contains: [hljs.UNDERSCORE_TITLE_MODE]
  114. },
  115. {
  116. beginKeywords: 'use', end: ';',
  117. contains: [hljs.UNDERSCORE_TITLE_MODE]
  118. },
  119. {
  120. begin: '=>' // No markup, just a relevance booster
  121. },
  122. STRING,
  123. NUMBER
  124. ]
  125. };
  126. };