Skip to content

Commit

Permalink
[PHP] Add support for nullsafe operator
Browse files Browse the repository at this point in the history
https://wiki.php.net/rfc/nullsafe_operator

Signed-off-by: Jack Cherng <jfcherng@gmail.com>
  • Loading branch information
jfcherng committed Jul 17, 2020
1 parent fd0e7dc commit a4882bd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
32 changes: 18 additions & 14 deletions PHP/PHP Source.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -1386,17 +1386,18 @@ contexts:
(?=
\${{identifier}}
(?:
->{{identifier}}
\??->{{identifier}}
|
\[ ( \d+ | \$?{{identifier}} ) \]
)
)
)
push:
- match: (->)({{identifier}})
- match: (\??)(->)({{identifier}})
captures:
1: punctuation.accessor.arrow.php
2: variable.other.member.php
1: punctuation.accessor.nullsafe.php
2: punctuation.accessor.arrow.php
3: variable.other.member.php
pop: true
- match: '\['
scope: meta.item-access.php punctuation.section.brackets.begin.php
Expand Down Expand Up @@ -1446,28 +1447,31 @@ contexts:
- match: '\b(?!_)(?:\d|_(?!_))+\b'
scope: constant.numeric.integer.decimal.php
object:
- match: '(->)(\$?\{)'
- match: (\??)(->)(\$?\{)
captures:
1: punctuation.accessor.arrow.php
2: punctuation.definition.variable.php
1: punctuation.accessor.nullsafe.php
2: punctuation.accessor.arrow.php
3: punctuation.definition.variable.php
push:
- match: '(\})'
captures:
1: punctuation.definition.variable.php
set: after-identifier
- include: expressions
- match: (->)({{identifier}})(?=\s*\()
- match: (\??)(->)({{identifier}})(?=\s*\()
scope: meta.function-call.method.php
captures:
1: punctuation.accessor.arrow.php
2: variable.function.php
1: punctuation.accessor.nullsafe.php
2: punctuation.accessor.arrow.php
3: variable.function.php
push: function-call-parameters
- include: function-call-static
- match: (->)((\$+)?{{identifier}})?
- match: (\??)(->)((\$+)?{{identifier}})?
captures:
1: punctuation.accessor.arrow.php
2: variable.other.member.php
3: punctuation.definition.variable.php
1: punctuation.accessor.nullsafe.php
2: punctuation.accessor.arrow.php
3: variable.other.member.php
4: punctuation.definition.variable.php
push: after-identifier
- match: '(::)({{identifier}})?'
captures:
Expand Down
33 changes: 24 additions & 9 deletions PHP/tests/syntax_test_php.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,12 @@ function foo(?stinrg ...$args) {}
// ^ punctuation.section.brackets.begin.php
// ^ punctuation.section.brackets.end.php

$var->meth()[10];
// ^^^^ meta.item-access
// ^ punctuation.section.brackets.begin.php
// ^ punctuation.section.brackets.end.php
$var?->meth()[10];
// ^ punctuation.accessor.nullsafe
// ^^ punctuation.accessor.arrow
// ^^^^ meta.item-access
// ^ punctuation.section.brackets.begin
// ^ punctuation.section.brackets.end

@@@@@@@@@@ExampleAttribute
// <- keyword.operator.error-control
Expand Down Expand Up @@ -842,11 +844,24 @@ interface MyInter2 extends \MyNamespace\Foo {
// ^^^^^^^^^ variable.function
// ^^ meta.group meta.group

$object->property::method();
// ^^ punctuation.accessor.arrow
// ^^ punctuation.accessor.double-colon
// ^^^^^^ meta.function-call.static variable.function
// ^^ meta.group
$object?->property::method();
// ^ punctuation.accessor.nullsafe
// ^^ punctuation.accessor.arrow
// ^^ punctuation.accessor.double-colon
// ^^^^^^ meta.function-call.static variable.function
// ^^ meta.group

$country = $session?->user?->getAddress()?->country;
// ^ punctuation.accessor.nullsafe
// ^^ punctuation.accessor.arrow
// ^ punctuation.accessor.nullsafe
// ^^ punctuation.accessor.arrow
// ^ punctuation.accessor.nullsafe
// ^^ punctuation.accessor.arrow

null?->foo(bar())->baz();
// ^ punctuation.accessor.nullsafe
// ^^ punctuation.accessor.arrow

strval($foo);
//^^^^^^^^^^ meta.function-call
Expand Down

0 comments on commit a4882bd

Please sign in to comment.