diff options
Diffstat (limited to 'pygments/lexers/php.py')
-rw-r--r-- | pygments/lexers/php.py | 61 |
1 files changed, 57 insertions, 4 deletions
diff --git a/pygments/lexers/php.py b/pygments/lexers/php.py index f9ce1d4b..1dd3c52b 100644 --- a/pygments/lexers/php.py +++ b/pygments/lexers/php.py @@ -11,13 +11,15 @@ import re -from pygments.lexer import RegexLexer, include, bygroups, default, using, \ - this, words +from pygments.lexer import Lexer, RegexLexer, include, bygroups, default, \ + using, this, words, do_insertions from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ - Number, Punctuation, Other + Number, Punctuation, Other, Generic from pygments.util import get_bool_opt, get_list_opt, shebang_matches -__all__ = ['ZephirLexer', 'PhpLexer'] +__all__ = ['ZephirLexer', 'PsyshConsoleLexer', 'PhpLexer'] + +line_re = re.compile('.*?\n') class ZephirLexer(RegexLexer): @@ -85,6 +87,57 @@ class ZephirLexer(RegexLexer): } +class PsyshConsoleLexer(Lexer): + """ + For `PsySH`_ console output, such as: + + .. sourcecode:: psysh + + >>> $greeting = function($name): string { + ... return "Hello, {$name}"; + ... }; + => Closure($name): string {#2371 …3} + >>> $greeting('World') + => "Hello, World" + + .. _PsySH: https://psysh.org/ + .. versionadded:: 2.7 + """ + name = 'PsySH console session for PHP' + aliases = ['psysh'] + + def __init__(self, **options): + options['startinline'] = True + Lexer.__init__(self, **options) + + def get_tokens_unprocessed(self, text): + phplexer = PhpLexer(**self.options) + curcode = '' + insertions = [] + for match in line_re.finditer(text): + line = match.group() + if line.startswith(u'>>> ') or line.startswith(u'... '): + insertions.append((len(curcode), + [(0, Generic.Prompt, line[:4])])) + curcode += line[4:] + elif line.rstrip() == u'...': + insertions.append((len(curcode), + [(0, Generic.Prompt, u'...')])) + curcode += line[3:] + else: + if curcode: + for item in do_insertions( + insertions, phplexer.get_tokens_unprocessed(curcode)): + yield item + curcode = '' + insertions = [] + yield match.start(), Generic.Output, line + if curcode: + for item in do_insertions(insertions, + phplexer.get_tokens_unprocessed(curcode)): + yield item + + class PhpLexer(RegexLexer): """ For `PHP <http://www.php.net/>`_ source code. |