php.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Language: PHP
  3. Author: Victor Karamzin <Victor.Karamzin@enterra-inc.com>
  4. Contributors: Evgeny Stepanischev <imbolk@gmail.com>, Ivan Sagalaev <maniac@softwaremaniacs.org>
  5. Website: https://www.php.net
  6. Category: common
  7. */
  8. function php(hljs) {
  9. var VARIABLE = {
  10. begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
  11. };
  12. var PREPROCESSOR = {
  13. className: 'meta',
  14. variants: [
  15. { begin: /<\?php/, relevance: 10 }, // boost for obvious PHP
  16. { begin: /<\?[=]?/ },
  17. { begin: /\?>/ } // end php tag
  18. ]
  19. };
  20. var STRING = {
  21. className: 'string',
  22. contains: [hljs.BACKSLASH_ESCAPE, PREPROCESSOR],
  23. variants: [
  24. {
  25. begin: 'b"', end: '"'
  26. },
  27. {
  28. begin: 'b\'', end: '\''
  29. },
  30. hljs.inherit(hljs.APOS_STRING_MODE, {illegal: null}),
  31. hljs.inherit(hljs.QUOTE_STRING_MODE, {illegal: null})
  32. ]
  33. };
  34. var NUMBER = {variants: [hljs.BINARY_NUMBER_MODE, hljs.C_NUMBER_MODE]};
  35. var KEYWORDS = {
  36. keyword:
  37. // Magic constants:
  38. // <https://www.php.net/manual/en/language.constants.predefined.php>
  39. '__CLASS__ __DIR__ __FILE__ __FUNCTION__ __LINE__ __METHOD__ __NAMESPACE__ __TRAIT__ ' +
  40. // Function that look like language construct or language construct that look like function:
  41. // List of keywords that may not require parenthesis
  42. 'die echo exit include include_once print require require_once ' +
  43. // These are not language construct (function) but operate on the currently-executing function and can access the current symbol table
  44. // 'compact extract func_get_arg func_get_args func_num_args get_called_class get_parent_class ' +
  45. // Other keywords:
  46. // <https://www.php.net/manual/en/reserved.php>
  47. // <https://www.php.net/manual/en/language.types.type-juggling.php>
  48. 'array abstract and as binary bool boolean break callable case catch class clone const continue declare default do double else elseif empty enddeclare endfor endforeach endif endswitch endwhile eval extends final finally float for foreach from global goto if implements instanceof insteadof int integer interface isset iterable list new object or private protected public real return string switch throw trait try unset use var void while xor yield',
  49. literal: 'false null true',
  50. built_in:
  51. // Standard PHP library:
  52. // <https://www.php.net/manual/en/book.spl.php>
  53. 'Error|0 ' + // error is too common a name esp since PHP is case in-sensitive
  54. 'AppendIterator ArgumentCountError ArithmeticError ArrayIterator ArrayObject AssertionError BadFunctionCallException BadMethodCallException CachingIterator CallbackFilterIterator CompileError Countable DirectoryIterator DivisionByZeroError DomainException EmptyIterator ErrorException Exception FilesystemIterator FilterIterator GlobIterator InfiniteIterator InvalidArgumentException IteratorIterator LengthException LimitIterator LogicException MultipleIterator NoRewindIterator OutOfBoundsException OutOfRangeException OuterIterator OverflowException ParentIterator ParseError RangeException RecursiveArrayIterator RecursiveCachingIterator RecursiveCallbackFilterIterator RecursiveDirectoryIterator RecursiveFilterIterator RecursiveIterator RecursiveIteratorIterator RecursiveRegexIterator RecursiveTreeIterator RegexIterator RuntimeException SeekableIterator SplDoublyLinkedList SplFileInfo SplFileObject SplFixedArray SplHeap SplMaxHeap SplMinHeap SplObjectStorage SplObserver SplObserver SplPriorityQueue SplQueue SplStack SplSubject SplSubject SplTempFileObject TypeError UnderflowException UnexpectedValueException ' +
  55. // Reserved interfaces:
  56. // <https://www.php.net/manual/en/reserved.interfaces.php>
  57. 'ArrayAccess Closure Generator Iterator IteratorAggregate Serializable Throwable Traversable WeakReference ' +
  58. // Reserved classes:
  59. // <https://www.php.net/manual/en/reserved.classes.php>
  60. 'Directory __PHP_Incomplete_Class parent php_user_filter self static stdClass'
  61. };
  62. return {
  63. aliases: ['php', 'php3', 'php4', 'php5', 'php6', 'php7'],
  64. case_insensitive: true,
  65. keywords: KEYWORDS,
  66. contains: [
  67. hljs.HASH_COMMENT_MODE,
  68. hljs.COMMENT('//', '$', {contains: [PREPROCESSOR]}),
  69. hljs.COMMENT(
  70. '/\\*',
  71. '\\*/',
  72. {
  73. contains: [
  74. {
  75. className: 'doctag',
  76. begin: '@[A-Za-z]+'
  77. }
  78. ]
  79. }
  80. ),
  81. hljs.COMMENT(
  82. '__halt_compiler.+?;',
  83. false,
  84. {
  85. endsWithParent: true,
  86. keywords: '__halt_compiler'
  87. }
  88. ),
  89. {
  90. className: 'string',
  91. begin: /<<<['"]?\w+['"]?$/, end: /^\w+;?$/,
  92. contains: [
  93. hljs.BACKSLASH_ESCAPE,
  94. {
  95. className: 'subst',
  96. variants: [
  97. {begin: /\$\w+/},
  98. {begin: /\{\$/, end: /\}/}
  99. ]
  100. }
  101. ]
  102. },
  103. PREPROCESSOR,
  104. {
  105. className: 'keyword', begin: /\$this\b/
  106. },
  107. VARIABLE,
  108. {
  109. // swallow composed identifiers to avoid parsing them as keywords
  110. begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/
  111. },
  112. {
  113. className: 'function',
  114. beginKeywords: 'fn function', end: /[;{]/, excludeEnd: true,
  115. illegal: '[$%\\[]',
  116. contains: [
  117. hljs.UNDERSCORE_TITLE_MODE,
  118. {
  119. className: 'params',
  120. begin: '\\(', end: '\\)',
  121. excludeBegin: true,
  122. excludeEnd: true,
  123. keywords: KEYWORDS,
  124. contains: [
  125. 'self',
  126. VARIABLE,
  127. hljs.C_BLOCK_COMMENT_MODE,
  128. STRING,
  129. NUMBER
  130. ]
  131. }
  132. ]
  133. },
  134. {
  135. className: 'class',
  136. beginKeywords: 'class interface', end: '{', excludeEnd: true,
  137. illegal: /[:\(\$"]/,
  138. contains: [
  139. {beginKeywords: 'extends implements'},
  140. hljs.UNDERSCORE_TITLE_MODE
  141. ]
  142. },
  143. {
  144. beginKeywords: 'namespace', end: ';',
  145. illegal: /[\.']/,
  146. contains: [hljs.UNDERSCORE_TITLE_MODE]
  147. },
  148. {
  149. beginKeywords: 'use', end: ';',
  150. contains: [hljs.UNDERSCORE_TITLE_MODE]
  151. },
  152. {
  153. begin: '=>' // No markup, just a relevance booster
  154. },
  155. STRING,
  156. NUMBER
  157. ]
  158. };
  159. }
  160. module.exports = php;