From 3eb97e5f74416a0758a92e1d75bea9510d25086a Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Thu, 11 Feb 2021 12:56:37 +0700 Subject: [PATCH 1/8] [PHP] Add WeakMap class to internal list PHP 8.0 adds a new [`WeakMap` built-in class](https://php.watch/versions/8.0/weakmap). Adding it to `KEYWORDS.keyword` list. --- src/languages/php.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/php.js b/src/languages/php.js index ad80479427..da4e5ccf74 100644 --- a/src/languages/php.js +++ b/src/languages/php.js @@ -87,7 +87,7 @@ 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 ' + // Reserved interfaces: // - 'ArrayAccess Closure Generator Iterator IteratorAggregate Serializable Throwable Traversable WeakReference ' + + 'ArrayAccess Closure Generator Iterator IteratorAggregate Serializable Throwable Traversable WeakReference WeakMap ' + // Reserved classes: // 'Directory __PHP_Incomplete_Class parent php_user_filter self static stdClass' From 5f758e18e38cc14ff165e879c881a7e5942df1df Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Thu, 11 Feb 2021 13:00:16 +0700 Subject: [PATCH 2/8] [PHP] Add `UnhandledMatchError` exception type PHP 8.0 match expression throws an [`UnhandledMatchError` exception](https://php.watch/versions/8.0/match-expression#UnhandledMatchError). It's a new built-in exception type, and adding it the keywords list. --- src/languages/php.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/php.js b/src/languages/php.js index da4e5ccf74..05fcea9565 100644 --- a/src/languages/php.js +++ b/src/languages/php.js @@ -84,7 +84,7 @@ export default function(hljs) { // Standard PHP library: // '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 ' + + '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 Throwable Traversable WeakReference WeakMap ' + From 47503048e2d0ccee546d8fd61f500bb699c9dc9e Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Thu, 11 Feb 2021 13:01:58 +0700 Subject: [PATCH 3/8] [PHP] Add new `mixed` type in PHP 8.0 PHP 8.0 adds a new [`mixed` type](https://php.watch/versions/8.0/mixed-type) as a new reserved keyword and a type. --- src/languages/php.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/php.js b/src/languages/php.js index 05fcea9565..ba6f805b99 100644 --- a/src/languages/php.js +++ b/src/languages/php.js @@ -77,7 +77,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 eval extends ' + 'final finally float for foreach from global goto if implements instanceof insteadof int integer interface ' + - 'isset iterable list match|0 new object or private protected public real return string switch throw trait ' + + '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: From 7200fc68d460c9fd8949f3237d847adb4933f0b5 Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Thu, 11 Feb 2021 13:03:54 +0700 Subject: [PATCH 4/8] [PHP] Add `Stringable` interface PHP 8.0 adds a new built-in interface called [`Stringable`](https://php.watch/versions/8.0/stringable). Adding it to the keywords list. --- src/languages/php.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/php.js b/src/languages/php.js index ba6f805b99..4f2b1a89de 100644 --- a/src/languages/php.js +++ b/src/languages/php.js @@ -87,7 +87,7 @@ 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 Throwable Traversable WeakReference WeakMap ' + + 'ArrayAccess Closure Generator Iterator IteratorAggregate Serializable Stringable Throwable Traversable WeakReference WeakMap ' + // Reserved classes: // 'Directory __PHP_Incomplete_Class parent php_user_filter self static stdClass' From 368c7922f0e035d73f1df8377e77ed708b2481fa Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Thu, 11 Feb 2021 13:12:05 +0700 Subject: [PATCH 5/8] [PHP] Add `trait` to list of class-like naming patterns Traits follow [class-like syntax](https://www.php.net/manual/en/language.oop5.traits.php), and was missing from the class-like naming pattern matches. --- src/languages/php.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/languages/php.js b/src/languages/php.js index 4f2b1a89de..05278df07a 100644 --- a/src/languages/php.js +++ b/src/languages/php.js @@ -160,7 +160,7 @@ export default function(hljs) { }, { className: 'class', - beginKeywords: 'class interface', + beginKeywords: 'class interface trait', relevance: 0, end: /\{/, excludeEnd: true, From 1e88f4d4d91969fe6e87017a9e0582ee7ba7dce0 Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Thu, 11 Feb 2021 13:53:22 +0700 Subject: [PATCH 6/8] [PHP] Update tests with a `match` expression --- test/detect/php/default.txt | 8 ++++++++ test/markup/php/php80.expect.txt | 10 ++++++++++ test/markup/php/php80.txt | 9 +++++++++ 3 files changed, 27 insertions(+) create mode 100644 test/markup/php/php80.expect.txt create mode 100644 test/markup/php/php80.txt diff --git a/test/detect/php/default.txt b/test/detect/php/default.txt index ec43315f0d..de7e89ba16 100644 --- a/test/detect/php/default.txt +++ b/test/detect/php/default.txt @@ -44,6 +44,14 @@ line description'; } } +match ($key) { + 1 => 'Integer 1', + '1' => 'String 1', + true => 'Bool true', + [] => 'Empty array', + [1] => 'Array [1]', +}; + echo URI::ME . URI::$st1; __halt_compiler () ; datahere diff --git a/test/markup/php/php80.expect.txt b/test/markup/php/php80.expect.txt new file mode 100644 index 0000000000..0139012b26 --- /dev/null +++ b/test/markup/php/php80.expect.txt @@ -0,0 +1,10 @@ +<?php + +match ($key) { + 1 => 'Integer 1', + '1' => 'String 1', + true => 'Bool true', + [] => 'Empty array', + [1] => 'Array [1]', +}; + diff --git a/test/markup/php/php80.txt b/test/markup/php/php80.txt new file mode 100644 index 0000000000..768b88695f --- /dev/null +++ b/test/markup/php/php80.txt @@ -0,0 +1,9 @@ + 'Integer 1', + '1' => 'String 1', + true => 'Bool true', + [] => 'Empty array', + [1] => 'Array [1]', +}; From eec1a964775a8768f41994af8543a046bff3a9d7 Mon Sep 17 00:00:00 2001 From: Ayesh Karunaratne Date: Fri, 12 Feb 2021 00:57:41 +0700 Subject: [PATCH 7/8] [PHP] Number pattern improvements Expands the number detection to support to all numerals supported up to PHP 8.1. - Decimal numbers - Floats - Binary numbers (`0b1100` and `0B1100`). - Hex number support (`0xAFAF` and `0XAFAF`). - Octal number support (`0777`) - [New `0o` and `0O` prefix support for Octals](https://php.watch/versions/8.1/explicit-octal-notation) (`0o777` and `0O777`) (Already accepted and merged to upcoming PHP 8.1) - Scientific notation (`7E-10` and `1.2e3`) - [Underscore number separator](https://php.watch/versions/7.4/underscore_numeric_separator) (Already implemented in PHP 7.4) --- src/languages/php.js | 12 +++++++++++- test/markup/php/numbers.expect.txt | 12 ++++++++++++ test/markup/php/numbers.txt | 12 ++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 test/markup/php/numbers.expect.txt create mode 100644 test/markup/php/numbers.txt diff --git a/src/languages/php.js b/src/languages/php.js index 05278df07a..c41a6452bf 100644 --- a/src/languages/php.js +++ b/src/languages/php.js @@ -60,7 +60,17 @@ export default function(hljs) { HEREDOC ] }; - const NUMBER = {variants: [hljs.BINARY_NUMBER_MODE, hljs.C_NUMBER_MODE]}; + 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: diff --git a/test/markup/php/numbers.expect.txt b/test/markup/php/numbers.expect.txt new file mode 100644 index 0000000000..d0640810e0 --- /dev/null +++ b/test/markup/php/numbers.expect.txt @@ -0,0 +1,12 @@ +<?php + +$decimal = 12398; +$binary = 0b10101; +$binary_alt = 0B1001_0000_1111; +$hex = 0x12AF; +$hex_alt = 0X12_AF; +$oct = 0o1234567; +$oct_alt = 0O12_34_56_7; +$oct_alt2 = 0777_555; +$sci = 1.2e3; +$sci2 = 7E-10; diff --git a/test/markup/php/numbers.txt b/test/markup/php/numbers.txt new file mode 100644 index 0000000000..0a325e9ffb --- /dev/null +++ b/test/markup/php/numbers.txt @@ -0,0 +1,12 @@ + Date: Thu, 11 Feb 2021 13:31:06 +0700 Subject: [PATCH 8/8] [PHP] Misc updates to CHANGES, AUTHORS, and SUPPORTED_LANGUAGES files --- AUTHORS.txt | 1 + CHANGES.md | 5 +++++ SUPPORTED_LANGUAGES.md | 2 +- 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/AUTHORS.txt b/AUTHORS.txt index dcdc2d0ce5..3f607cfd44 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -316,3 +316,4 @@ Contributors: - MichaƂ Lipok (https://github.com/mlipok) - davidhcefx - xDGameStudios +- Ayesh Karunaratne diff --git a/CHANGES.md b/CHANGES.md index 05d75b409b..30afc8e210 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,10 +7,15 @@ New Languages: Language grammar improvements: - enh(gml) Add additional GML 2.3 keywords (#2984) [xDGameStudios][] +- enh(php) Add `trait` to class-like naming patterns (#2997) [Ayesh][] +- enh(php) Add `Stringable`, `UnhandledMatchError`, and `WeakMap` classes/interfaces (#2997) [Ayesh][] +- enh(php) Add `mixed` to list of keywords (#2997) [Ayesh][] +- enh(php) Add support binary, octal, hex and scientific numerals with underscore separator support (#2997) [Ayesh][] [Josh Goebel]: https://github.com/joshgoebel [John Cheung]: https://github.com/Real-John-Cheung [xDGameStudios]: https://github.com/xDGameStudios +[Ayesh]: https://github.com/Ayesh ## Version 10.6.0 diff --git a/SUPPORTED_LANGUAGES.md b/SUPPORTED_LANGUAGES.md index 97fb9dd0c7..1d23d67413 100644 --- a/SUPPORTED_LANGUAGES.md +++ b/SUPPORTED_LANGUAGES.md @@ -136,7 +136,7 @@ Languages that listed a **Package** below are 3rd party languages and are not bu | Oracle Rules Language | ruleslanguage | | | Oxygene | oxygene | | | PF | pf, pf.conf | | -| PHP | php, php3, php4, php5, php6, php7 | | +| PHP | php, php3, php4, php5, php6, php7, php8 | | | Parser3 | parser3 | | | Perl | perl, pl, pm | | | Plaintext | plaintext, txt, text | |