3 pygments.lexers.php |
3 pygments.lexers.php |
4 ~~~~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Lexers for PHP and related languages. |
6 Lexers for PHP and related languages. |
7 |
7 |
8 :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
11 |
11 |
12 import re |
12 import re |
13 |
13 |
14 from pygments.lexer import RegexLexer, include, bygroups, default, using, \ |
14 from pygments.lexer import Lexer, RegexLexer, include, bygroups, default, \ |
15 this, words |
15 using, this, words, do_insertions |
16 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
16 from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ |
17 Number, Punctuation, Other |
17 Number, Punctuation, Other, Generic |
18 from pygments.util import get_bool_opt, get_list_opt, shebang_matches |
18 from pygments.util import get_bool_opt, get_list_opt, shebang_matches |
19 |
19 |
20 __all__ = ['ZephirLexer', 'PhpLexer'] |
20 __all__ = ['ZephirLexer', 'PsyshConsoleLexer', 'PhpLexer'] |
|
21 |
|
22 line_re = re.compile('.*?\n') |
21 |
23 |
22 |
24 |
23 class ZephirLexer(RegexLexer): |
25 class ZephirLexer(RegexLexer): |
24 """ |
26 """ |
25 For `Zephir language <http://zephir-lang.com/>`_ source code. |
27 For `Zephir language <http://zephir-lang.com/>`_ source code. |
81 (r'[0-9]+', Number.Integer), |
83 (r'[0-9]+', Number.Integer), |
82 (r'"(\\\\|\\"|[^"])*"', String.Double), |
84 (r'"(\\\\|\\"|[^"])*"', String.Double), |
83 (r"'(\\\\|\\'|[^'])*'", String.Single), |
85 (r"'(\\\\|\\'|[^'])*'", String.Single), |
84 ] |
86 ] |
85 } |
87 } |
|
88 |
|
89 |
|
90 class PsyshConsoleLexer(Lexer): |
|
91 """ |
|
92 For `PsySH`_ console output, such as: |
|
93 |
|
94 .. sourcecode:: psysh |
|
95 |
|
96 >>> $greeting = function($name): string { |
|
97 ... return "Hello, {$name}"; |
|
98 ... }; |
|
99 => Closure($name): string {#2371 …3} |
|
100 >>> $greeting('World') |
|
101 => "Hello, World" |
|
102 |
|
103 .. _PsySH: https://psysh.org/ |
|
104 .. versionadded:: 2.7 |
|
105 """ |
|
106 name = 'PsySH console session for PHP' |
|
107 aliases = ['psysh'] |
|
108 |
|
109 def __init__(self, **options): |
|
110 options['startinline'] = True |
|
111 Lexer.__init__(self, **options) |
|
112 |
|
113 def get_tokens_unprocessed(self, text): |
|
114 phplexer = PhpLexer(**self.options) |
|
115 curcode = '' |
|
116 insertions = [] |
|
117 for match in line_re.finditer(text): |
|
118 line = match.group() |
|
119 if line.startswith('>>> ') or line.startswith('... '): |
|
120 insertions.append((len(curcode), |
|
121 [(0, Generic.Prompt, line[:4])])) |
|
122 curcode += line[4:] |
|
123 elif line.rstrip() == '...': |
|
124 insertions.append((len(curcode), |
|
125 [(0, Generic.Prompt, '...')])) |
|
126 curcode += line[3:] |
|
127 else: |
|
128 if curcode: |
|
129 yield from do_insertions( |
|
130 insertions, phplexer.get_tokens_unprocessed(curcode)) |
|
131 curcode = '' |
|
132 insertions = [] |
|
133 yield match.start(), Generic.Output, line |
|
134 if curcode: |
|
135 yield from do_insertions(insertions, |
|
136 phplexer.get_tokens_unprocessed(curcode)) |
86 |
137 |
87 |
138 |
88 class PhpLexer(RegexLexer): |
139 class PhpLexer(RegexLexer): |
89 """ |
140 """ |
90 For `PHP <http://www.php.net/>`_ source code. |
141 For `PHP <http://www.php.net/>`_ source code. |