php.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. """
  2. pygments.lexers.php
  3. ~~~~~~~~~~~~~~~~~~~
  4. Lexers for PHP and related languages.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import Lexer, RegexLexer, include, bygroups, default, \
  10. using, this, words, do_insertions
  11. from pygments.token import Text, Comment, Operator, Keyword, Name, String, \
  12. Number, Punctuation, Other, Generic
  13. from pygments.util import get_bool_opt, get_list_opt, shebang_matches
  14. __all__ = ['ZephirLexer', 'PsyshConsoleLexer', 'PhpLexer']
  15. line_re = re.compile('.*?\n')
  16. class ZephirLexer(RegexLexer):
  17. """
  18. For `Zephir language <http://zephir-lang.com/>`_ source code.
  19. Zephir is a compiled high level language aimed
  20. to the creation of C-extensions for PHP.
  21. .. versionadded:: 2.0
  22. """
  23. name = 'Zephir'
  24. aliases = ['zephir']
  25. filenames = ['*.zep']
  26. zephir_keywords = ['fetch', 'echo', 'isset', 'empty']
  27. zephir_type = ['bit', 'bits', 'string']
  28. flags = re.DOTALL | re.MULTILINE
  29. tokens = {
  30. 'commentsandwhitespace': [
  31. (r'\s+', Text),
  32. (r'//.*?\n', Comment.Single),
  33. (r'/\*.*?\*/', Comment.Multiline)
  34. ],
  35. 'slashstartsregex': [
  36. include('commentsandwhitespace'),
  37. (r'/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/'
  38. r'([gim]+\b|\B)', String.Regex, '#pop'),
  39. (r'/', Operator, '#pop'),
  40. default('#pop')
  41. ],
  42. 'badregex': [
  43. (r'\n', Text, '#pop')
  44. ],
  45. 'root': [
  46. (r'^(?=\s|/)', Text, 'slashstartsregex'),
  47. include('commentsandwhitespace'),
  48. (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|'
  49. r'(<<|>>>?|==?|!=?|->|[-<>+*%&|^/])=?', Operator, 'slashstartsregex'),
  50. (r'[{(\[;,]', Punctuation, 'slashstartsregex'),
  51. (r'[})\].]', Punctuation),
  52. (r'(for|in|while|do|break|return|continue|switch|case|default|if|else|loop|'
  53. r'require|inline|throw|try|catch|finally|new|delete|typeof|instanceof|void|'
  54. r'namespace|use|extends|this|fetch|isset|unset|echo|fetch|likely|unlikely|'
  55. r'empty)\b', Keyword, 'slashstartsregex'),
  56. (r'(var|let|with|function)\b', Keyword.Declaration, 'slashstartsregex'),
  57. (r'(abstract|boolean|bool|char|class|const|double|enum|export|extends|final|'
  58. r'native|goto|implements|import|int|string|interface|long|ulong|char|uchar|'
  59. r'float|unsigned|private|protected|public|short|static|self|throws|reverse|'
  60. r'transient|volatile)\b', Keyword.Reserved),
  61. (r'(true|false|null|undefined)\b', Keyword.Constant),
  62. (r'(Array|Boolean|Date|_REQUEST|_COOKIE|_SESSION|'
  63. r'_GET|_POST|_SERVER|this|stdClass|range|count|iterator|'
  64. r'window)\b', Name.Builtin),
  65. (r'[$a-zA-Z_][\w\\]*', Name.Other),
  66. (r'[0-9][0-9]*\.[0-9]+([eE][0-9]+)?[fd]?', Number.Float),
  67. (r'0x[0-9a-fA-F]+', Number.Hex),
  68. (r'[0-9]+', Number.Integer),
  69. (r'"(\\\\|\\[^\\]|[^"\\])*"', String.Double),
  70. (r"'(\\\\|\\[^\\]|[^'\\])*'", String.Single),
  71. ]
  72. }
  73. class PsyshConsoleLexer(Lexer):
  74. """
  75. For `PsySH`_ console output, such as:
  76. .. sourcecode:: psysh
  77. >>> $greeting = function($name): string {
  78. ... return "Hello, {$name}";
  79. ... };
  80. => Closure($name): string {#2371 …3}
  81. >>> $greeting('World')
  82. => "Hello, World"
  83. .. _PsySH: https://psysh.org/
  84. .. versionadded:: 2.7
  85. """
  86. name = 'PsySH console session for PHP'
  87. aliases = ['psysh']
  88. def __init__(self, **options):
  89. options['startinline'] = True
  90. Lexer.__init__(self, **options)
  91. def get_tokens_unprocessed(self, text):
  92. phplexer = PhpLexer(**self.options)
  93. curcode = ''
  94. insertions = []
  95. for match in line_re.finditer(text):
  96. line = match.group()
  97. if line.startswith('>>> ') or line.startswith('... '):
  98. insertions.append((len(curcode),
  99. [(0, Generic.Prompt, line[:4])]))
  100. curcode += line[4:]
  101. elif line.rstrip() == '...':
  102. insertions.append((len(curcode),
  103. [(0, Generic.Prompt, '...')]))
  104. curcode += line[3:]
  105. else:
  106. if curcode:
  107. yield from do_insertions(
  108. insertions, phplexer.get_tokens_unprocessed(curcode))
  109. curcode = ''
  110. insertions = []
  111. yield match.start(), Generic.Output, line
  112. if curcode:
  113. yield from do_insertions(insertions,
  114. phplexer.get_tokens_unprocessed(curcode))
  115. class PhpLexer(RegexLexer):
  116. """
  117. For `PHP <http://www.php.net/>`_ source code.
  118. For PHP embedded in HTML, use the `HtmlPhpLexer`.
  119. Additional options accepted:
  120. `startinline`
  121. If given and ``True`` the lexer starts highlighting with
  122. php code (i.e.: no starting ``<?php`` required). The default
  123. is ``False``.
  124. `funcnamehighlighting`
  125. If given and ``True``, highlight builtin function names
  126. (default: ``True``).
  127. `disabledmodules`
  128. If given, must be a list of module names whose function names
  129. should not be highlighted. By default all modules are highlighted
  130. except the special ``'unknown'`` module that includes functions
  131. that are known to php but are undocumented.
  132. To get a list of allowed modules have a look into the
  133. `_php_builtins` module:
  134. .. sourcecode:: pycon
  135. >>> from pygments.lexers._php_builtins import MODULES
  136. >>> MODULES.keys()
  137. ['PHP Options/Info', 'Zip', 'dba', ...]
  138. In fact the names of those modules match the module names from
  139. the php documentation.
  140. """
  141. name = 'PHP'
  142. aliases = ['php', 'php3', 'php4', 'php5']
  143. filenames = ['*.php', '*.php[345]', '*.inc']
  144. mimetypes = ['text/x-php']
  145. # Note that a backslash is included in the following two patterns
  146. # PHP uses a backslash as a namespace separator
  147. _ident_char = r'[\\\w]|[^\x00-\x7f]'
  148. _ident_begin = r'(?:[\\_a-z]|[^\x00-\x7f])'
  149. _ident_end = r'(?:' + _ident_char + ')*'
  150. _ident_inner = _ident_begin + _ident_end
  151. flags = re.IGNORECASE | re.DOTALL | re.MULTILINE
  152. tokens = {
  153. 'root': [
  154. (r'<\?(php)?', Comment.Preproc, 'php'),
  155. (r'[^<]+', Other),
  156. (r'<', Other)
  157. ],
  158. 'php': [
  159. (r'\?>', Comment.Preproc, '#pop'),
  160. (r'(<<<)([\'"]?)(' + _ident_inner + r')(\2\n.*?\n\s*)(\3)(;?)(\n)',
  161. bygroups(String, String, String.Delimiter, String, String.Delimiter,
  162. Punctuation, Text)),
  163. (r'\s+', Text),
  164. (r'#.*?\n', Comment.Single),
  165. (r'//.*?\n', Comment.Single),
  166. # put the empty comment here, it is otherwise seen as
  167. # the start of a docstring
  168. (r'/\*\*/', Comment.Multiline),
  169. (r'/\*\*.*?\*/', String.Doc),
  170. (r'/\*.*?\*/', Comment.Multiline),
  171. (r'(->|::)(\s*)(' + _ident_inner + ')',
  172. bygroups(Operator, Text, Name.Attribute)),
  173. (r'[~!%^&*+=|:.<>/@-]+', Operator),
  174. (r'\?', Operator), # don't add to the charclass above!
  175. (r'[\[\]{}();,]+', Punctuation),
  176. (r'(class)(\s+)', bygroups(Keyword, Text), 'classname'),
  177. (r'(function)(\s*)(?=\()', bygroups(Keyword, Text)),
  178. (r'(function)(\s+)(&?)(\s*)',
  179. bygroups(Keyword, Text, Operator, Text), 'functionname'),
  180. (r'(const)(\s+)(' + _ident_inner + ')',
  181. bygroups(Keyword, Text, Name.Constant)),
  182. (r'(and|E_PARSE|old_function|E_ERROR|or|as|E_WARNING|parent|'
  183. r'eval|PHP_OS|break|exit|case|extends|PHP_VERSION|cfunction|'
  184. r'FALSE|print|for|require|continue|foreach|require_once|'
  185. r'declare|return|default|static|do|switch|die|stdClass|'
  186. r'echo|else|TRUE|elseif|var|empty|if|xor|enddeclare|include|'
  187. r'virtual|endfor|include_once|while|endforeach|global|'
  188. r'endif|list|endswitch|new|endwhile|not|'
  189. r'array|E_ALL|NULL|final|php_user_filter|interface|'
  190. r'implements|public|private|protected|abstract|clone|try|'
  191. r'catch|throw|this|use|namespace|trait|yield|'
  192. r'finally)\b', Keyword),
  193. (r'(true|false|null)\b', Keyword.Constant),
  194. include('magicconstants'),
  195. (r'\$\{\$+' + _ident_inner + r'\}', Name.Variable),
  196. (r'\$+' + _ident_inner, Name.Variable),
  197. (_ident_inner, Name.Other),
  198. (r'(\d+\.\d*|\d*\.\d+)(e[+-]?[0-9]+)?', Number.Float),
  199. (r'\d+e[+-]?[0-9]+', Number.Float),
  200. (r'0[0-7]+', Number.Oct),
  201. (r'0x[a-f0-9]+', Number.Hex),
  202. (r'\d+', Number.Integer),
  203. (r'0b[01]+', Number.Bin),
  204. (r"'([^'\\]*(?:\\.[^'\\]*)*)'", String.Single),
  205. (r'`([^`\\]*(?:\\.[^`\\]*)*)`', String.Backtick),
  206. (r'"', String.Double, 'string'),
  207. ],
  208. 'magicfuncs': [
  209. # source: http://php.net/manual/en/language.oop5.magic.php
  210. (words((
  211. '__construct', '__destruct', '__call', '__callStatic', '__get', '__set',
  212. '__isset', '__unset', '__sleep', '__wakeup', '__toString', '__invoke',
  213. '__set_state', '__clone', '__debugInfo',), suffix=r'\b'),
  214. Name.Function.Magic),
  215. ],
  216. 'magicconstants': [
  217. # source: http://php.net/manual/en/language.constants.predefined.php
  218. (words((
  219. '__LINE__', '__FILE__', '__DIR__', '__FUNCTION__', '__CLASS__',
  220. '__TRAIT__', '__METHOD__', '__NAMESPACE__',),
  221. suffix=r'\b'),
  222. Name.Constant),
  223. ],
  224. 'classname': [
  225. (_ident_inner, Name.Class, '#pop')
  226. ],
  227. 'functionname': [
  228. include('magicfuncs'),
  229. (_ident_inner, Name.Function, '#pop'),
  230. default('#pop')
  231. ],
  232. 'string': [
  233. (r'"', String.Double, '#pop'),
  234. (r'[^{$"\\]+', String.Double),
  235. (r'\\([nrt"$\\]|[0-7]{1,3}|x[0-9a-f]{1,2})', String.Escape),
  236. (r'\$' + _ident_inner + r'(\[\S+?\]|->' + _ident_inner + ')?',
  237. String.Interpol),
  238. (r'(\{\$\{)(.*?)(\}\})',
  239. bygroups(String.Interpol, using(this, _startinline=True),
  240. String.Interpol)),
  241. (r'(\{)(\$.*?)(\})',
  242. bygroups(String.Interpol, using(this, _startinline=True),
  243. String.Interpol)),
  244. (r'(\$\{)(\S+)(\})',
  245. bygroups(String.Interpol, Name.Variable, String.Interpol)),
  246. (r'[${\\]', String.Double)
  247. ],
  248. }
  249. def __init__(self, **options):
  250. self.funcnamehighlighting = get_bool_opt(
  251. options, 'funcnamehighlighting', True)
  252. self.disabledmodules = get_list_opt(
  253. options, 'disabledmodules', ['unknown'])
  254. self.startinline = get_bool_opt(options, 'startinline', False)
  255. # private option argument for the lexer itself
  256. if '_startinline' in options:
  257. self.startinline = options.pop('_startinline')
  258. # collect activated functions in a set
  259. self._functions = set()
  260. if self.funcnamehighlighting:
  261. from pygments.lexers._php_builtins import MODULES
  262. for key, value in MODULES.items():
  263. if key not in self.disabledmodules:
  264. self._functions.update(value)
  265. RegexLexer.__init__(self, **options)
  266. def get_tokens_unprocessed(self, text):
  267. stack = ['root']
  268. if self.startinline:
  269. stack.append('php')
  270. for index, token, value in \
  271. RegexLexer.get_tokens_unprocessed(self, text, stack):
  272. if token is Name.Other:
  273. if value in self._functions:
  274. yield index, Name.Builtin, value
  275. continue
  276. yield index, token, value
  277. def analyse_text(text):
  278. if shebang_matches(text, r'php'):
  279. return True
  280. rv = 0.0
  281. if re.search(r'<\?(?!xml)', text):
  282. rv += 0.3
  283. return rv