php.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /**
  9. * @param {HLJSApi} hljs
  10. * @returns {LanguageDetail}
  11. * */
  12. function php(hljs) {
  13. const VARIABLE = {
  14. className: 'variable',
  15. begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' +
  16. // negative look-ahead tries to avoid matching patterns that are not
  17. // Perl at all like $ident$, @ident@, etc.
  18. `(?![A-Za-z0-9])(?![$])`
  19. };
  20. const PREPROCESSOR = {
  21. className: 'meta',
  22. variants: [
  23. { begin: /<\?php/, relevance: 10 }, // boost for obvious PHP
  24. { begin: /<\?[=]?/ },
  25. { begin: /\?>/ } // end php tag
  26. ]
  27. };
  28. const SUBST = {
  29. className: 'subst',
  30. variants: [
  31. { begin: /\$\w+/ },
  32. { begin: /\{\$/, end: /\}/ }
  33. ]
  34. };
  35. const SINGLE_QUOTED = hljs.inherit(hljs.APOS_STRING_MODE, {
  36. illegal: null,
  37. });
  38. const DOUBLE_QUOTED = hljs.inherit(hljs.QUOTE_STRING_MODE, {
  39. illegal: null,
  40. contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
  41. });
  42. const HEREDOC = hljs.END_SAME_AS_BEGIN({
  43. begin: /<<<[ \t]*(\w+)\n/,
  44. end: /[ \t]*(\w+)\b/,
  45. contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
  46. });
  47. const STRING = {
  48. className: 'string',
  49. contains: [hljs.BACKSLASH_ESCAPE, PREPROCESSOR],
  50. variants: [
  51. hljs.inherit(SINGLE_QUOTED, {
  52. begin: "b'", end: "'",
  53. }),
  54. hljs.inherit(DOUBLE_QUOTED, {
  55. begin: 'b"', end: '"',
  56. }),
  57. DOUBLE_QUOTED,
  58. SINGLE_QUOTED,
  59. HEREDOC
  60. ]
  61. };
  62. const NUMBER = {
  63. className: 'number',
  64. variants: [
  65. { begin: `\\b0b[01]+(?:_[01]+)*\\b` }, // Binary w/ underscore support
  66. { begin: `\\b0o[0-7]+(?:_[0-7]+)*\\b` }, // Octals w/ underscore support
  67. { begin: `\\b0x[\\da-f]+(?:_[\\da-f]+)*\\b` }, // Hex w/ underscore support
  68. // Decimals w/ underscore support, with optional fragments and scientific exponent (e) suffix.
  69. { begin: `(?:\\b\\d+(?:_\\d+)*(\\.(?:\\d+(?:_\\d+)*))?|\\B\\.\\d+)(?:e[+-]?\\d+)?` }
  70. ],
  71. relevance: 0
  72. };
  73. const KEYWORDS = {
  74. keyword:
  75. // Magic constants:
  76. // <https://www.php.net/manual/en/language.constants.predefined.php>
  77. '__CLASS__ __DIR__ __FILE__ __FUNCTION__ __LINE__ __METHOD__ __NAMESPACE__ __TRAIT__ ' +
  78. // Function that look like language construct or language construct that look like function:
  79. // List of keywords that may not require parenthesis
  80. 'die echo exit include include_once print require require_once ' +
  81. // These are not language construct (function) but operate on the currently-executing function and can access the current symbol table
  82. // 'compact extract func_get_arg func_get_args func_num_args get_called_class get_parent_class ' +
  83. // Other keywords:
  84. // <https://www.php.net/manual/en/reserved.php>
  85. // <https://www.php.net/manual/en/language.types.type-juggling.php>
  86. 'array abstract and as binary bool boolean break callable case catch class clone const continue declare ' +
  87. 'default do double else elseif empty enddeclare endfor endforeach endif endswitch endwhile enum eval extends ' +
  88. 'final finally float for foreach from global goto if implements instanceof insteadof int integer interface ' +
  89. 'isset iterable list match|0 mixed new object or private protected public real return string switch throw trait ' +
  90. 'try unset use var void while xor yield',
  91. literal: 'false null true',
  92. built_in:
  93. // Standard PHP library:
  94. // <https://www.php.net/manual/en/book.spl.php>
  95. 'Error|0 ' + // error is too common a name esp since PHP is case in-sensitive
  96. '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 UnhandledMatchError ' +
  97. // Reserved interfaces:
  98. // <https://www.php.net/manual/en/reserved.interfaces.php>
  99. 'ArrayAccess Closure Generator Iterator IteratorAggregate Serializable Stringable Throwable Traversable WeakReference WeakMap ' +
  100. // Reserved classes:
  101. // <https://www.php.net/manual/en/reserved.classes.php>
  102. 'Directory __PHP_Incomplete_Class parent php_user_filter self static stdClass'
  103. };
  104. return {
  105. aliases: ['php3', 'php4', 'php5', 'php6', 'php7', 'php8'],
  106. case_insensitive: true,
  107. keywords: KEYWORDS,
  108. contains: [
  109. hljs.HASH_COMMENT_MODE,
  110. hljs.COMMENT('//', '$', {contains: [PREPROCESSOR]}),
  111. hljs.COMMENT(
  112. '/\\*',
  113. '\\*/',
  114. {
  115. contains: [
  116. {
  117. className: 'doctag',
  118. begin: '@[A-Za-z]+'
  119. }
  120. ]
  121. }
  122. ),
  123. hljs.COMMENT(
  124. '__halt_compiler.+?;',
  125. false,
  126. {
  127. endsWithParent: true,
  128. keywords: '__halt_compiler'
  129. }
  130. ),
  131. PREPROCESSOR,
  132. {
  133. className: 'keyword', begin: /\$this\b/
  134. },
  135. VARIABLE,
  136. {
  137. // swallow composed identifiers to avoid parsing them as keywords
  138. begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/
  139. },
  140. {
  141. className: 'function',
  142. relevance: 0,
  143. beginKeywords: 'fn function', end: /[;{]/, excludeEnd: true,
  144. illegal: '[$%\\[]',
  145. contains: [
  146. {
  147. beginKeywords: 'use',
  148. },
  149. hljs.UNDERSCORE_TITLE_MODE,
  150. {
  151. begin: '=>', // No markup, just a relevance booster
  152. endsParent: true
  153. },
  154. {
  155. className: 'params',
  156. begin: '\\(', end: '\\)',
  157. excludeBegin: true,
  158. excludeEnd: true,
  159. keywords: KEYWORDS,
  160. contains: [
  161. 'self',
  162. VARIABLE,
  163. hljs.C_BLOCK_COMMENT_MODE,
  164. STRING,
  165. NUMBER
  166. ]
  167. }
  168. ]
  169. },
  170. {
  171. className: 'class',
  172. variants: [
  173. { beginKeywords: "enum", illegal: /[($"]/ },
  174. { beginKeywords: "class interface trait", illegal: /[:($"]/ }
  175. ],
  176. relevance: 0,
  177. end: /\{/,
  178. excludeEnd: true,
  179. contains: [
  180. {beginKeywords: 'extends implements'},
  181. hljs.UNDERSCORE_TITLE_MODE
  182. ]
  183. },
  184. {
  185. beginKeywords: 'namespace',
  186. relevance: 0,
  187. end: ';',
  188. illegal: /[.']/,
  189. contains: [hljs.UNDERSCORE_TITLE_MODE]
  190. },
  191. {
  192. beginKeywords: 'use',
  193. relevance: 0,
  194. end: ';',
  195. contains: [hljs.UNDERSCORE_TITLE_MODE]
  196. },
  197. STRING,
  198. NUMBER
  199. ]
  200. };
  201. }
  202. module.exports = php;