From 8354aa3d2639d0cefd1483403b0d298f316f1dfb Mon Sep 17 00:00:00 2001 From: Wojciech Kania Date: Mon, 6 Dec 2021 21:36:26 +0100 Subject: [PATCH] enh(php) Add support for constants and php 8.1 keywords --- CHANGES.md | 1 + src/languages/php.js | 30 +++++++++++++++++++++++++++--- test/markup/php/titles.expect.txt | 10 ++++++++++ test/markup/php/titles.txt | 10 ++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 test/markup/php/titles.expect.txt create mode 100644 test/markup/php/titles.txt diff --git a/CHANGES.md b/CHANGES.md index 7d6636393f..c53c42d96b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,7 @@ Grammars: - enh(clojure) Add `regex` mode to regex literal - fix(clojure) Remove inconsistent/broken highlighting for metadata - enh(clojure) Add `punctuation` mode for commas. +- enh(php) Add support for constants and PHP 8.1 keywords [Wojciech Kania][] Developer Tools: diff --git a/src/languages/php.js b/src/languages/php.js index ce0af400a8..8ecb293bf6 100644 --- a/src/languages/php.js +++ b/src/languages/php.js @@ -18,6 +18,28 @@ export default function(hljs) { // Perl at all like $ident$, @ident@, etc. `(?![A-Za-z0-9])(?![$])` }; + const CONSTANT = { + className: "variable", + relevance: 0, + variants: [ + { + begin: /const\s*/, + end: /\w+/, + excludeBegin: true + }, + { + begin: /::(?!class\b)/, + end: /\w+/, + excludeBegin: true + }, + { + className: "keyword", + begin: /::/, + end: /class/, + excludeBegin: true + }, + ] + }; const PREPROCESSOR = { className: 'meta', variants: [ @@ -87,7 +109,7 @@ export default function(hljs) { '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 ' + + 'isset iterable list match|0 mixed new never object or private protected public readonly real return string switch throw trait ' + 'try unset use var void while xor yield', literal: 'false null true', built_in: @@ -97,10 +119,10 @@ export default function(hljs) { '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: // - 'ArrayAccess Closure Generator Iterator IteratorAggregate Serializable Stringable Throwable Traversable WeakReference WeakMap ' + + 'ArrayAccess BackedEnum Closure Fiber Generator Iterator IteratorAggregate Serializable Stringable Throwable Traversable UnitEnum WeakReference WeakMap ' + // Reserved classes: // - 'Directory __PHP_Incomplete_Class parent php_user_filter self static stdClass' + 'Directory __PHP_Incomplete_Class parent php_user_filter self static stdClass', }; return { case_insensitive: true, @@ -133,6 +155,7 @@ export default function(hljs) { className: 'keyword', begin: /\$this\b/ }, VARIABLE, + CONSTANT, { // swallow composed identifiers to avoid parsing them as keywords begin: /(::|->)+[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/ @@ -160,6 +183,7 @@ export default function(hljs) { contains: [ 'self', VARIABLE, + CONSTANT, hljs.C_BLOCK_COMMENT_MODE, STRING, NUMBER diff --git a/test/markup/php/titles.expect.txt b/test/markup/php/titles.expect.txt new file mode 100644 index 0000000000..989aaef437 --- /dev/null +++ b/test/markup/php/titles.expect.txt @@ -0,0 +1,10 @@ +final class Example { + const FOO='foo'; + public function __construct( + public readonly string $name = self::FOO + ) {} + + public function getClass(): string { + return DateTimeImmutable::class; + } +} diff --git a/test/markup/php/titles.txt b/test/markup/php/titles.txt new file mode 100644 index 0000000000..4a26cb4a46 --- /dev/null +++ b/test/markup/php/titles.txt @@ -0,0 +1,10 @@ +final class Example { + const FOO='foo'; + public function __construct( + public readonly string $name = self::FOO + ) {} + + public function getClass(): string { + return DateTimeImmutable::class; + } +}