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

[PHP] Updates to keep up with PHP 8 #2997

Merged
merged 8 commits into from
Feb 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 | |
joshgoebel marked this conversation as resolved.
Show resolved Hide resolved
| 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]',
};