Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enh(php) some PHP cleanups #3427

Merged
merged 9 commits into from Dec 10, 2021
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -9,6 +9,9 @@ These changes should be for the better and should not be super noticeable but if

Grammars:

- enh(php) improve `namespace` and `use` highlighting (#3427) [Josh Goebel][]
- enh(php) `$this` is a `variable.language` now (#3427) [Josh Goebel][]
- enh(php) add `__COMPILER_HALT_OFFSET__` (#3427) [Josh Goebel][]
- enh(js/ts) fix => async function title highlights (#3405) [Josh Goebel][]
- enh(twig) update keywords list (#3415) [Matthieu Lempereur][]
- fix(python) def, class keywords detected mid-identifier (#3381) [Josh Goebel][]
Expand Down
253 changes: 220 additions & 33 deletions src/languages/php.js
Expand Up @@ -47,14 +47,7 @@ export default function(hljs) {
});
const STRING = {
className: 'string',
contains: [hljs.BACKSLASH_ESCAPE, PREPROCESSOR],
variants: [
hljs.inherit(SINGLE_QUOTED, {
begin: "b'", end: "'",
}),
hljs.inherit(DOUBLE_QUOTED, {
begin: 'b"', end: '"',
}),
DOUBLE_QUOTED,
SINGLE_QUOTED,
HEREDOC
Expand All @@ -71,43 +64,216 @@ export default function(hljs) {
],
relevance: 0
};
const KEYWORDS = {
keyword:
const LITERALS = [
"false",
"null",
"true"
];
const KWS = [
// Magic constants:
// <https://www.php.net/manual/en/language.constants.predefined.php>
'__CLASS__ __DIR__ __FILE__ __FUNCTION__ __LINE__ __METHOD__ __NAMESPACE__ __TRAIT__ ' +
"__CLASS__",
"__DIR__",
"__FILE__",
"__FUNCTION__",
"__COMPILER_HALT_OFFSET__",
"__LINE__",
"__METHOD__",
"__NAMESPACE__",
"__TRAIT__",
// Function that look like language construct or language construct that look like function:
// List of keywords that may not require parenthesis
'die echo exit include include_once print require require_once ' +
"die",
"echo",
"exit",
"include",
"include_once",
"print",
"require",
"require_once",
// These are not language construct (function) but operate on the currently-executing function and can access the current symbol table
// 'compact extract func_get_arg func_get_args func_num_args get_called_class get_parent_class ' +
// Other keywords:
// <https://www.php.net/manual/en/reserved.php>
// <https://www.php.net/manual/en/language.types.type-juggling.php>
'array abstract and as binary bool boolean break callable case catch class clone const continue declare ' +
'default do double else elseif empty enddeclare endfor endforeach endif endswitch endwhile enum eval extends ' +
'final finally float for foreach from global goto if implements instanceof insteadof int integer interface ' +
'isset iterable list match|0 mixed new object or private protected public real return string switch throw trait ' +
'try unset use var void while xor yield',
literal: 'false null true',
built_in:
"array",
"abstract",
"and",
"as",
"binary",
"bool",
"boolean",
"break",
"callable",
"case",
"catch",
"class",
"clone",
"const",
"continue",
"declare",
"default",
"do",
"double",
"else",
"elseif",
"empty",
"enddeclare",
"endfor",
"endforeach",
"endif",
"endswitch",
"endwhile",
"enum",
"eval",
"extends",
"final",
"finally",
"float",
"for",
"foreach",
"from",
"global",
"goto",
"if",
"implements",
"instanceof",
"insteadof",
"int",
"integer",
"interface",
"isset",
"iterable",
"list",
"match|0",
"mixed",
"new",
"object",
"or",
"private",
"protected",
"public",
"real",
"return",
"string",
"switch",
"throw",
"trait",
"try",
"unset",
"use",
"var",
"void",
"while",
"xor",
"yield"
];

const BUILT_INS = [
// Standard PHP library:
// <https://www.php.net/manual/en/book.spl.php>
'Error|0 ' + // error is too common a name esp since PHP is case in-sensitive
'AppendIterator ArgumentCountError ArithmeticError ArrayIterator ArrayObject AssertionError BadFunctionCallException BadMethodCallException CachingIterator CallbackFilterIterator CompileError Countable DirectoryIterator DivisionByZeroError DomainException EmptyIterator ErrorException Exception FilesystemIterator FilterIterator GlobIterator InfiniteIterator InvalidArgumentException IteratorIterator LengthException LimitIterator LogicException MultipleIterator NoRewindIterator OutOfBoundsException OutOfRangeException OuterIterator OverflowException ParentIterator ParseError RangeException RecursiveArrayIterator RecursiveCachingIterator RecursiveCallbackFilterIterator RecursiveDirectoryIterator RecursiveFilterIterator RecursiveIterator RecursiveIteratorIterator RecursiveRegexIterator RecursiveTreeIterator RegexIterator RuntimeException SeekableIterator SplDoublyLinkedList SplFileInfo SplFileObject SplFixedArray SplHeap SplMaxHeap SplMinHeap SplObjectStorage SplObserver SplObserver SplPriorityQueue SplQueue SplStack SplSubject SplSubject SplTempFileObject TypeError UnderflowException UnexpectedValueException UnhandledMatchError ' +
"Error|0",
"AppendIterator",
"ArgumentCountError",
"ArithmeticError",
"ArrayIterator",
"ArrayObject",
"AssertionError",
"BadFunctionCallException",
"BadMethodCallException",
"CachingIterator",
"CallbackFilterIterator",
"CompileError",
"Countable",
"DirectoryIterator",
"DivisionByZeroError",
"DomainException",
"EmptyIterator",
"ErrorException",
"Exception",
"FilesystemIterator",
"FilterIterator",
"GlobIterator",
"InfiniteIterator",
"InvalidArgumentException",
"IteratorIterator",
"LengthException",
"LimitIterator",
"LogicException",
"MultipleIterator",
"NoRewindIterator",
"OutOfBoundsException",
"OutOfRangeException",
"OuterIterator",
"OverflowException",
"ParentIterator",
"ParseError",
"RangeException",
"RecursiveArrayIterator",
"RecursiveCachingIterator",
"RecursiveCallbackFilterIterator",
"RecursiveDirectoryIterator",
"RecursiveFilterIterator",
"RecursiveIterator",
"RecursiveIteratorIterator",
"RecursiveRegexIterator",
"RecursiveTreeIterator",
"RegexIterator",
"RuntimeException",
"SeekableIterator",
"SplDoublyLinkedList",
"SplFileInfo",
"SplFileObject",
"SplFixedArray",
"SplHeap",
"SplMaxHeap",
"SplMinHeap",
"SplObjectStorage",
"SplObserver",
"SplPriorityQueue",
"SplQueue",
"SplStack",
"SplSubject",
"SplTempFileObject",
"TypeError",
"UnderflowException",
"UnexpectedValueException",
"UnhandledMatchError",
// Reserved interfaces:
// <https://www.php.net/manual/en/reserved.interfaces.php>
'ArrayAccess Closure Generator Iterator IteratorAggregate Serializable Stringable Throwable Traversable WeakReference WeakMap ' +
"ArrayAccess",
"Closure",
"Generator",
"Iterator",
"IteratorAggregate",
"Serializable",
"Stringable",
"Throwable",
"Traversable",
"WeakReference",
"WeakMap",
// Reserved classes:
// <https://www.php.net/manual/en/reserved.classes.php>
'Directory __PHP_Incomplete_Class parent php_user_filter self static stdClass'
"Directory",
"__PHP_Incomplete_Class",
"parent",
"php_user_filter",
"self",
"static",
"stdClass"
];

const KEYWORDS = {
keyword: KWS,
literal: LITERALS,
built_in: BUILT_INS
};
return {
case_insensitive: true,
keywords: KEYWORDS,
contains: [
hljs.HASH_COMMENT_MODE,
hljs.COMMENT('//', '$', {contains: [PREPROCESSOR]}),
hljs.COMMENT('//', '$'),
hljs.COMMENT(
'/\\*',
'\\*/',
Expand All @@ -120,17 +286,25 @@ export default function(hljs) {
]
}
),
hljs.COMMENT(
'__halt_compiler.+?;',
false,
{
endsWithParent: true,
keywords: '__halt_compiler'
{
match: /__halt_compiler\(\);/,
keywords: '__halt_compiler',
starts: {
scope: "comment",
end: hljs.MATCH_NOTHING_RE,
contains: [
{
match: /\?>/,
scope: "meta",
endsParent: true
}
]
}
),
},
PREPROCESSOR,
{
className: 'keyword', begin: /\$this\b/
className: 'variable.language',
begin: /\$this\b/
},
VARIABLE,
{
Expand Down Expand Up @@ -181,18 +355,31 @@ export default function(hljs) {
hljs.UNDERSCORE_TITLE_MODE
]
},
// both use and namespace still use "old style" rules (vs multi-match)
// because the namespace name can include `\` and we still want each
// element to be treated as it's own *individual* title
{
beginKeywords: 'namespace',
relevance: 0,
end: ';',
illegal: /[.']/,
contains: [hljs.UNDERSCORE_TITLE_MODE]
contains: [
hljs.inherit(hljs.UNDERSCORE_TITLE_MODE, { scope: "title.class" })
]
},
{
beginKeywords: 'use',
relevance: 0,
end: ';',
contains: [hljs.UNDERSCORE_TITLE_MODE]
contains: [
// TODO: title.function vs title.class
{
match: /\b(as|const|function)\b/,
scope: "keyword"
},
// TODO: could be title.class or title.function
hljs.UNDERSCORE_TITLE_MODE
]
},
STRING,
NUMBER
Expand Down
9 changes: 9 additions & 0 deletions test/markup/php/namespace.expect.txt
@@ -0,0 +1,9 @@
<span class="hljs-keyword">namespace</span> <span class="hljs-title class_">Location</span>\<span class="hljs-title class_">Web</span>;
<span class="hljs-keyword">namespace</span> <span class="hljs-title class_">foo</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">My</span>\<span class="hljs-title">Full</span>\<span class="hljs-title">Classname</span> <span class="hljs-keyword">as</span> <span class="hljs-title">Another</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">My</span>\<span class="hljs-title">Full</span>\<span class="hljs-title">NSname</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">ArrayObject</span>;
<span class="hljs-keyword">use</span> <span class="hljs-keyword">function</span> <span class="hljs-title">My</span>\<span class="hljs-title">Full</span>\<span class="hljs-title">functionName</span>;
<span class="hljs-keyword">use</span> <span class="hljs-keyword">function</span> <span class="hljs-title">My</span>\<span class="hljs-title">Full</span>\<span class="hljs-title">functionName</span> <span class="hljs-keyword">as</span> <span class="hljs-title">func</span>;
<span class="hljs-keyword">use</span> <span class="hljs-keyword">const</span> <span class="hljs-title">My</span>\<span class="hljs-title">Full</span>\<span class="hljs-title">CONSTANT</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">Example</span>\{<span class="hljs-title">ClassA</span>, <span class="hljs-title">ClassB</span>, <span class="hljs-title">ClassC</span> <span class="hljs-keyword">as</span> <span class="hljs-title">C</span>};
9 changes: 9 additions & 0 deletions test/markup/php/namespace.txt
@@ -0,0 +1,9 @@
namespace Location\Web;
namespace foo;
use My\Full\Classname as Another;
use My\Full\NSname;
use ArrayObject;
use function My\Full\functionName;
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
use function My\Full\functionName as func;
use const My\Full\CONSTANT;
use Example\{ClassA, ClassB, ClassC as C};