Skip to content

Commit

Permalink
enh(php) Updates to keep pace with PHP 8.0 and 8.1 (#2997)
Browse files Browse the repository at this point in the history
* [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.

* [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.

* [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.

* [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.

* [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.

* [PHP] Update tests with a `match` expression

* [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)

* [PHP] Misc updates to CHANGES, AUTHORS, and SUPPORTED_LANGUAGES files
  • Loading branch information
Ayesh committed Feb 14, 2021
1 parent 374a7e5 commit f8664dd
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -316,3 +316,4 @@ Contributors:
- Michał Lipok (https://github.com/mlipok)
- davidhcefx <davidhu0903ex3@gmail.com>
- xDGameStudios <xDGameStudios@gmail.com>
- Ayesh Karunaratne <ayesh@aye.sh>
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion SUPPORTED_LANGUAGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | |
Expand Down
20 changes: 15 additions & 5 deletions src/languages/php.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -77,17 +87,17 @@ 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:
// 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 ' +
'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 Throwable Traversable WeakReference ' +
'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'
Expand Down Expand Up @@ -160,7 +170,7 @@ export default function(hljs) {
},
{
className: 'class',
beginKeywords: 'class interface',
beginKeywords: 'class interface trait',
relevance: 0,
end: /\{/,
excludeEnd: true,
Expand Down
8 changes: 8 additions & 0 deletions test/detect/php/default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/markup/php/numbers.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<span class="hljs-meta">&lt;?php</span>

<span class="hljs-variable">$decimal</span> = <span class="hljs-number">12398</span>;
<span class="hljs-variable">$binary</span> = <span class="hljs-number">0b10101</span>;
<span class="hljs-variable">$binary_alt</span> = <span class="hljs-number">0B1001_0000_1111</span>;
<span class="hljs-variable">$hex</span> = <span class="hljs-number">0x12AF</span>;
<span class="hljs-variable">$hex_alt</span> = <span class="hljs-number">0X12_AF</span>;
<span class="hljs-variable">$oct</span> = <span class="hljs-number">0o1234567</span>;
<span class="hljs-variable">$oct_alt</span> = <span class="hljs-number">0O12_34_56_7</span>;
<span class="hljs-variable">$oct_alt2</span> = <span class="hljs-number">0777_555</span>;
<span class="hljs-variable">$sci</span> = <span class="hljs-number">1.2e3</span>;
<span class="hljs-variable">$sci2</span> = <span class="hljs-number">7E-10</span>;
12 changes: 12 additions & 0 deletions test/markup/php/numbers.txt
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 10 additions & 0 deletions test/markup/php/php80.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<span class="hljs-meta">&lt;?php</span>

<span class="hljs-keyword">match</span> (<span class="hljs-variable">$key</span>) {
<span class="hljs-number">1</span> =&gt; <span class="hljs-string">&#x27;Integer 1&#x27;</span>,
<span class="hljs-string">&#x27;1&#x27;</span> =&gt; <span class="hljs-string">&#x27;String 1&#x27;</span>,
<span class="hljs-literal">true</span> =&gt; <span class="hljs-string">&#x27;Bool true&#x27;</span>,
[] =&gt; <span class="hljs-string">&#x27;Empty array&#x27;</span>,
[<span class="hljs-number">1</span>] =&gt; <span class="hljs-string">&#x27;Array [1]&#x27;</span>,
};

9 changes: 9 additions & 0 deletions test/markup/php/php80.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

match ($key) {
1 => 'Integer 1',
'1' => 'String 1',
true => 'Bool true',
[] => 'Empty array',
[1] => 'Array [1]',
};

0 comments on commit f8664dd

Please sign in to comment.