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-2017 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2019 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 RegexLexer, include, bygroups, default, using, \ |
15 this, words |
15 this, words |
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 |
18 from pygments.util import get_bool_opt, get_list_opt, iteritems |
18 from pygments.util import get_bool_opt, get_list_opt, shebang_matches |
19 |
19 |
20 __all__ = ['ZephirLexer', 'PhpLexer'] |
20 __all__ = ['ZephirLexer', 'PhpLexer'] |
21 |
21 |
22 |
22 |
23 class ZephirLexer(RegexLexer): |
23 class ZephirLexer(RegexLexer): |
47 ], |
47 ], |
48 'slashstartsregex': [ |
48 'slashstartsregex': [ |
49 include('commentsandwhitespace'), |
49 include('commentsandwhitespace'), |
50 (r'/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/' |
50 (r'/(\\.|[^[/\\\n]|\[(\\.|[^\]\\\n])*])+/' |
51 r'([gim]+\b|\B)', String.Regex, '#pop'), |
51 r'([gim]+\b|\B)', String.Regex, '#pop'), |
|
52 (r'/', Operator, '#pop'), |
52 default('#pop') |
53 default('#pop') |
53 ], |
54 ], |
54 'badregex': [ |
55 'badregex': [ |
55 (r'\n', Text, '#pop') |
56 (r'\n', Text, '#pop') |
56 ], |
57 ], |
57 'root': [ |
58 'root': [ |
58 (r'^(?=\s|/|<!--)', Text, 'slashstartsregex'), |
59 (r'^(?=\s|/)', Text, 'slashstartsregex'), |
59 include('commentsandwhitespace'), |
60 include('commentsandwhitespace'), |
60 (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|' |
61 (r'\+\+|--|~|&&|\?|:|\|\||\\(?=\n)|' |
61 r'(<<|>>>?|==?|!=?|->|[-<>+*%&|^/])=?', Operator, 'slashstartsregex'), |
62 r'(<<|>>>?|==?|!=?|->|[-<>+*%&|^/])=?', Operator, 'slashstartsregex'), |
62 (r'[{(\[;,]', Punctuation, 'slashstartsregex'), |
63 (r'[{(\[;,]', Punctuation, 'slashstartsregex'), |
63 (r'[})\].]', Punctuation), |
64 (r'[})\].]', Punctuation), |
241 |
242 |
242 # collect activated functions in a set |
243 # collect activated functions in a set |
243 self._functions = set() |
244 self._functions = set() |
244 if self.funcnamehighlighting: |
245 if self.funcnamehighlighting: |
245 from pygments.lexers._php_builtins import MODULES |
246 from pygments.lexers._php_builtins import MODULES |
246 for key, value in iteritems(MODULES): |
247 for key, value in MODULES.items(): |
247 if key not in self.disabledmodules: |
248 if key not in self.disabledmodules: |
248 self._functions.update(value) |
249 self._functions.update(value) |
249 RegexLexer.__init__(self, **options) |
250 RegexLexer.__init__(self, **options) |
250 |
251 |
251 def get_tokens_unprocessed(self, text): |
252 def get_tokens_unprocessed(self, text): |
259 yield index, Name.Builtin, value |
260 yield index, Name.Builtin, value |
260 continue |
261 continue |
261 yield index, token, value |
262 yield index, token, value |
262 |
263 |
263 def analyse_text(text): |
264 def analyse_text(text): |
|
265 if shebang_matches(text, r'php'): |
|
266 return True |
264 rv = 0.0 |
267 rv = 0.0 |
265 if re.search(r'<\?(?!xml)', text): |
268 if re.search(r'<\?(?!xml)', text): |
266 rv += 0.3 |
269 rv += 0.3 |
267 return rv |
270 return rv |