diff options
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | doc/docs/tokens.rst | 8 | ||||
-rw-r--r-- | pygments/lexers/php.py | 30 | ||||
-rw-r--r-- | tests/examplefiles/test.php | 24 |
4 files changed, 55 insertions, 9 deletions
@@ -30,6 +30,8 @@ Version 2.1.1 - Fixed deepcopy-ing of Token instances (#1168) - Fixed Julia string interpolation (#1170) - Fixed statefulness of HttpLexer between get_tokens calls +- Added new token types and lexing for magic methods and variables in Python + and PHP. - Many smaller fixes to various lexers diff --git a/doc/docs/tokens.rst b/doc/docs/tokens.rst index f9ed3d92..96a6d003 100644 --- a/doc/docs/tokens.rst +++ b/doc/docs/tokens.rst @@ -175,8 +175,8 @@ Name Tokens Token type for function names. `Name.Function.Magic` - same as `Name.Function` but for function names that have an implicit use in - a language (e.g. ``__init__`` method in Python). + same as `Name.Function` but for special function names that have an implicit use + in a language (e.g. ``__init__`` method in Python). `Name.Label` Token type for label names (e.g. in languages that support ``goto``). @@ -206,8 +206,8 @@ Name Tokens same as `Name.Variable` but for instance variables. `Name.Variable.Magic` - same as `Name.Variable` but for variable names that have an implicit use in - a language (e.g. ``__doc__`` in Python). + same as `Name.Variable` but for special variable names that have an implicit use + in a language (e.g. ``__doc__`` in Python). Literals diff --git a/pygments/lexers/php.py b/pygments/lexers/php.py index 75b662cb..abcccffa 100644 --- a/pygments/lexers/php.py +++ b/pygments/lexers/php.py @@ -11,7 +11,8 @@ import re -from pygments.lexer import RegexLexer, include, bygroups, default, using, this +from pygments.lexer import RegexLexer, include, bygroups, default, using, \ + this, words from pygments.token import Text, Comment, Operator, Keyword, Name, String, \ Number, Punctuation, Other from pygments.util import get_bool_opt, get_list_opt, iteritems @@ -162,13 +163,14 @@ class PhpLexer(RegexLexer): r'FALSE|print|for|require|continue|foreach|require_once|' r'declare|return|default|static|do|switch|die|stdClass|' r'echo|else|TRUE|elseif|var|empty|if|xor|enddeclare|include|' - r'virtual|endfor|include_once|while|endforeach|global|__FILE__|' - r'endif|list|__LINE__|endswitch|new|__sleep|endwhile|not|' - r'array|__wakeup|E_ALL|NULL|final|php_user_filter|interface|' + r'virtual|endfor|include_once|while|endforeach|global|' + r'endif|list|endswitch|new|endwhile|not|' + r'array|E_ALL|NULL|final|php_user_filter|interface|' r'implements|public|private|protected|abstract|clone|try|' r'catch|throw|this|use|namespace|trait|yield|' r'finally)\b', Keyword), (r'(true|false|null)\b', Keyword.Constant), + include('magicvars'), (r'\$\{\$+' + _ident_inner + '\}', Name.Variable), (r'\$+' + _ident_inner, Name.Variable), (_ident_inner, Name.Other), @@ -182,11 +184,29 @@ class PhpLexer(RegexLexer): (r'`([^`\\]*(?:\\.[^`\\]*)*)`', String.Backtick), (r'"', String.Double, 'string'), ], + 'magicfuncs': [ + # source: http://php.net/manual/en/language.oop5.magic.php + (words(( + '__construct', '__destruct', '__call', '__callStatic', '__get', '__set', + '__isset', '__unset', '__sleep', '__wakeup', '__toString', '__invoke', + '__set_state', '__clone', '__debugInfo',), suffix=r'\b'), + Name.Function.Magic), + ], + 'magicvars': [ + # source: http://php.net/manual/en/language.constants.predefined.php + (words(( + '__LINE__', '__FILE__', '__DIR__', '__FUNCTION__', '__CLASS__', + '__TRAIT__', '__METHOD__', '__NAMESPACE__',), + suffix=r'\b'), + Name.Variable.Magic), + ], 'classname': [ (_ident_inner, Name.Class, '#pop') ], 'functionname': [ - (_ident_inner, Name.Function, '#pop') + include('magicfuncs'), + (_ident_inner, Name.Function, '#pop'), + default('#pop') ], 'string': [ (r'"', String.Double, '#pop'), diff --git a/tests/examplefiles/test.php b/tests/examplefiles/test.php index 2ce4023e..794961c1 100644 --- a/tests/examplefiles/test.php +++ b/tests/examplefiles/test.php @@ -505,6 +505,30 @@ function &byref() { return $x; } +// Test highlighting of magic methods and variables +class MagicClass { + public $magic_str; + public $ordinary_str; + + public function __construct($some_var) { + $this->magic_str = __FILE__; + $this->ordinary_str = $some_var; + } + + public function __toString() { + return $this->magic_str; + } + + public function nonMagic() { + return $this->ordinary_str; + } +} + +$magic = new MagicClass(__DIR__); +__toString(); +$magic->nonMagic(); +$magic->__toString(); + echo <<<EOF Test the heredocs... |