2024-01-16 21:26:16 +08:00
|
|
|
/*
|
|
|
|
Language: PHP
|
|
|
|
Author: Victor Karamzin <Victor.Karamzin@enterra-inc.com>
|
|
|
|
Contributors: Evgeny Stepanischev <imbolk@gmail.com>, Ivan Sagalaev <maniac@softwaremaniacs.org>
|
|
|
|
Website: https://www.php.net
|
|
|
|
Category: common
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {HLJSApi} hljs
|
|
|
|
* @returns {LanguageDetail}
|
|
|
|
* */
|
|
|
|
function php(hljs) {
|
|
|
|
const VARIABLE = {
|
|
|
|
className: 'variable',
|
|
|
|
begin: '\\$+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*' +
|
|
|
|
// negative look-ahead tries to avoid matching patterns that are not
|
|
|
|
// Perl at all like $ident$, @ident@, etc.
|
|
|
|
`(?![A-Za-z0-9])(?![$])`
|
2023-12-18 13:12:25 +08:00
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const PREPROCESSOR = {
|
|
|
|
className: 'meta',
|
|
|
|
variants: [
|
|
|
|
{ begin: /<\?php/, relevance: 10 }, // boost for obvious PHP
|
|
|
|
{ begin: /<\?[=]?/ },
|
|
|
|
{ begin: /\?>/ } // end php tag
|
|
|
|
]
|
|
|
|
};
|
|
|
|
const SUBST = {
|
|
|
|
className: 'subst',
|
|
|
|
variants: [
|
|
|
|
{ begin: /\$\w+/ },
|
|
|
|
{ begin: /\{\$/, end: /\}/ }
|
|
|
|
]
|
2023-12-18 13:12:25 +08:00
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const SINGLE_QUOTED = hljs.inherit(hljs.APOS_STRING_MODE, {
|
|
|
|
illegal: null,
|
|
|
|
});
|
|
|
|
const DOUBLE_QUOTED = hljs.inherit(hljs.QUOTE_STRING_MODE, {
|
|
|
|
illegal: null,
|
|
|
|
contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
|
|
|
|
});
|
|
|
|
const HEREDOC = hljs.END_SAME_AS_BEGIN({
|
|
|
|
begin: /<<<[ \t]*(\w+)\n/,
|
|
|
|
end: /[ \t]*(\w+)\b/,
|
|
|
|
contains: hljs.QUOTE_STRING_MODE.contains.concat(SUBST),
|
|
|
|
});
|
|
|
|
const STRING = {
|
2023-12-18 13:12:25 +08:00
|
|
|
className: 'string',
|
|
|
|
contains: [hljs.BACKSLASH_ESCAPE, PREPROCESSOR],
|
|
|
|
variants: [
|
2024-01-16 21:26:16 +08:00
|
|
|
hljs.inherit(SINGLE_QUOTED, {
|
|
|
|
begin: "b'", end: "'",
|
|
|
|
}),
|
|
|
|
hljs.inherit(DOUBLE_QUOTED, {
|
|
|
|
begin: 'b"', end: '"',
|
|
|
|
}),
|
|
|
|
DOUBLE_QUOTED,
|
|
|
|
SINGLE_QUOTED,
|
|
|
|
HEREDOC
|
2023-12-18 13:12:25 +08:00
|
|
|
]
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
const NUMBER = {
|
|
|
|
className: 'number',
|
|
|
|
variants: [
|
|
|
|
{ begin: `\\b0b[01]+(?:_[01]+)*\\b` }, // Binary w/ underscore support
|
|
|
|
{ begin: `\\b0o[0-7]+(?:_[0-7]+)*\\b` }, // Octals w/ underscore support
|
|
|
|
{ begin: `\\b0x[\\da-f]+(?:_[\\da-f]+)*\\b` }, // Hex w/ underscore support
|
|
|
|
// Decimals w/ underscore support, with optional fragments and scientific exponent (e) suffix.
|
|
|
|
{ begin: `(?:\\b\\d+(?:_\\d+)*(\\.(?:\\d+(?:_\\d+)*))?|\\B\\.\\d+)(?:e[+-]?\\d+)?` }
|
|
|
|
],
|
|
|
|
relevance: 0
|
|
|
|
};
|
|
|
|
const KEYWORDS = {
|
|
|
|
keyword:
|
|
|
|
// Magic constants:
|
|
|
|
// <https://www.php.net/manual/en/language.constants.predefined.php>
|
|
|
|
'__CLASS__ __DIR__ __FILE__ __FUNCTION__ __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 ' +
|
|
|
|
// 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:
|
|
|
|
// 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 ' +
|
|
|
|
// Reserved interfaces:
|
|
|
|
// <https://www.php.net/manual/en/reserved.interfaces.php>
|
|
|
|
'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'
|
|
|
|
};
|
2023-12-18 13:12:25 +08:00
|
|
|
return {
|
2024-01-16 21:26:16 +08:00
|
|
|
aliases: ['php3', 'php4', 'php5', 'php6', 'php7', 'php8'],
|
2023-12-18 13:12:25 +08:00
|
|
|
case_insensitive: true,
|
2024-01-16 21:26:16 +08:00
|
|
|
keywords: KEYWORDS,
|
2023-12-18 13:12:25 +08:00
|
|
|
contains: [
|
|
|
|
hljs.HASH_COMMENT_MODE,
|
|
|
|
hljs.COMMENT('//', '$', {contains: [PREPROCESSOR]}),
|
|
|
|
hljs.COMMENT(
|
|
|
|
'/\\*',
|
|
|
|
'\\*/',
|
|
|
|
{
|
|
|
|
contains: [
|
|
|
|
{
|
|
|
|
className: 'doctag',
|
|
|
|
begin: '@[A-Za-z]+'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
),
|
|
|
|
hljs.COMMENT(
|
|
|
|
'__halt_compiler.+?;',
|
|
|
|
false,
|
|
|
|
{
|
|
|
|
endsWithParent: true,
|
2024-01-16 21:26:16 +08:00
|
|
|
keywords: '__halt_compiler'
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
),
|
|
|
|
PREPROCESSOR,
|
|
|
|
{
|
|
|
|
className: 'keyword', begin: /\$this\b/
|
|
|
|
},
|
|
|
|
VARIABLE,
|
|
|
|
{
|
|
|
|
// swallow composed identifiers to avoid parsing them as keywords
|
|
|
|
begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/
|
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'function',
|
2024-01-16 21:26:16 +08:00
|
|
|
relevance: 0,
|
|
|
|
beginKeywords: 'fn function', end: /[;{]/, excludeEnd: true,
|
|
|
|
illegal: '[$%\\[]',
|
2023-12-18 13:12:25 +08:00
|
|
|
contains: [
|
2024-01-16 21:26:16 +08:00
|
|
|
{
|
|
|
|
beginKeywords: 'use',
|
|
|
|
},
|
2023-12-18 13:12:25 +08:00
|
|
|
hljs.UNDERSCORE_TITLE_MODE,
|
2024-01-16 21:26:16 +08:00
|
|
|
{
|
|
|
|
begin: '=>', // No markup, just a relevance booster
|
|
|
|
endsParent: true
|
|
|
|
},
|
2023-12-18 13:12:25 +08:00
|
|
|
{
|
|
|
|
className: 'params',
|
|
|
|
begin: '\\(', end: '\\)',
|
2024-01-16 21:26:16 +08:00
|
|
|
excludeBegin: true,
|
|
|
|
excludeEnd: true,
|
|
|
|
keywords: KEYWORDS,
|
2023-12-18 13:12:25 +08:00
|
|
|
contains: [
|
|
|
|
'self',
|
|
|
|
VARIABLE,
|
|
|
|
hljs.C_BLOCK_COMMENT_MODE,
|
|
|
|
STRING,
|
|
|
|
NUMBER
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
className: 'class',
|
2024-01-16 21:26:16 +08:00
|
|
|
variants: [
|
|
|
|
{ beginKeywords: "enum", illegal: /[($"]/ },
|
|
|
|
{ beginKeywords: "class interface trait", illegal: /[:($"]/ }
|
|
|
|
],
|
|
|
|
relevance: 0,
|
|
|
|
end: /\{/,
|
|
|
|
excludeEnd: true,
|
2023-12-18 13:12:25 +08:00
|
|
|
contains: [
|
|
|
|
{beginKeywords: 'extends implements'},
|
|
|
|
hljs.UNDERSCORE_TITLE_MODE
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
beginKeywords: 'namespace',
|
|
|
|
relevance: 0,
|
|
|
|
end: ';',
|
|
|
|
illegal: /[.']/,
|
2023-12-18 13:12:25 +08:00
|
|
|
contains: [hljs.UNDERSCORE_TITLE_MODE]
|
|
|
|
},
|
|
|
|
{
|
2024-01-16 21:26:16 +08:00
|
|
|
beginKeywords: 'use',
|
|
|
|
relevance: 0,
|
|
|
|
end: ';',
|
2023-12-18 13:12:25 +08:00
|
|
|
contains: [hljs.UNDERSCORE_TITLE_MODE]
|
|
|
|
},
|
|
|
|
STRING,
|
|
|
|
NUMBER
|
|
|
|
]
|
|
|
|
};
|
2024-01-16 21:26:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = php;
|