Skip to content

Commit

Permalink
[PHP] PHP 8.2 new syntax (sublimehq#3426)
Browse files Browse the repository at this point in the history
Co-authored-by: deathaxe <[email protected]>
  • Loading branch information
jfcherng and deathaxe authored Sep 2, 2022
1 parent 1bc66e1 commit 8535db7
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
27 changes: 17 additions & 10 deletions PHP/PHP Source.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -683,11 +683,8 @@ contexts:
###[ CLASS DECLARATIONS ]#####################################################

class-declarations:
- match: (?i:(abstract|final)\s+)?((?i:class))\b
scope: meta.class.php
captures:
1: storage.modifier.php
2: keyword.declaration.class.php
- match: (?i:class)\b
scope: meta.class.php keyword.declaration.class.php
push:
- class-declaration-body
- class-declaration-name
Expand Down Expand Up @@ -1910,15 +1907,15 @@ contexts:

type-hints:
# https://wiki.php.net/rfc/union_types_v2
- match: (?=(?:\?\s*)?\\?{{identifier_start}})
- match: (?=(?:\(\s*)?(?:\?\s*)?\\?{{identifier_start}})
push:
- type-hint-body
- type-hint-simple-type
- type-hint-nullable

type-hint:
# https://wiki.php.net/rfc/union_types_v2
- match: (?=(?:\?\s*)?\\?{{identifier_start}})
- match: (?=(?:\(\s*)?(?:\?\s*)?\\?{{identifier_start}})
set:
- type-hint-body
- type-hint-simple-type
Expand All @@ -1934,10 +1931,21 @@ contexts:
- match: '&(?!\s*\$)'
scope: punctuation.separator.type.intersection.php
push:
- type-hint-nullable
- type-hint-simple-type
- type-hint-nullable
- include: type-hint-groups
- include: else-pop

type-hint-groups:
- match: \(
scope: punctuation.section.group.begin.php
push:
- meta_scope: meta.group.php
- match: \)
scope: punctuation.section.group.end.php
pop: 1
- include: type-hints

type-hint-nullable:
- match: \?
scope: storage.type.nullable.php
Expand All @@ -1947,9 +1955,8 @@ contexts:
type-hint-simple-type:
# note that
# - "callable", "never" and "void" are not allowed in property type
# - "false" and "null" are only working in unioned type
# but we allow them as general types for simplifying syntax definition
- match: (?i:iterable|false|mixed|never|null|void|{{storage_types}})\b
- match: (?i:iterable|false|mixed|never|null|true|void|{{storage_types}})\b
scope: storage.type.primitive.php
pop: 1
- match: (?=(?i:extends|implements)\b)
Expand Down
49 changes: 35 additions & 14 deletions PHP/tests/syntax_test_php.php
Original file line number Diff line number Diff line change
Expand Up @@ -945,9 +945,10 @@ public function show(int $id) { }
* Class Declaration Tests
*****************************************************************************/

class
// ^^^^^ meta.class.php keyword.declaration.class.php
// ^ meta.class.php - keyword
readonly class
// ^^^^^^^^ storage.modifier.php
// ^^^^^ meta.class.php keyword.declaration.class.php
// ^ meta.class.php - keyword

class Test1
// ^^^^^^^^^^^^ meta.class.php - meta.block - meta.class meta.class
Expand All @@ -963,17 +964,18 @@ class Test2 extends implements {}
// ^^^^^^^ storage.modifier.extends.php
// ^^^^^^^^^^ storage.modifier.implements.php

class Test3 extends stdClass implements Countable {}
readonly class Test3 extends stdClass implements Countable {}
// ^ - meta.class
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - meta.class meta.class, - meta.block meta.block
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.php - meta.block
// ^^ meta.class.php meta.block.php
// ^^^^^ keyword.declaration.class.php
// ^^^^^ entity.name.class.php
// ^^^^^^^ storage.modifier.extends.php
// ^^^^^^^^ entity.other.inherited-class.php support.class.builtin.php - meta.path
// ^^^^^^^^^^ storage.modifier.implements.php
// ^^^^^^^^^ entity.other.inherited-class.php support.class.builtin.php - meta.path
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - meta.class meta.class, - meta.block meta.block
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.php - meta.block
// ^^ meta.class.php meta.block.php
// ^^^^^^^^ storage.modifier.php
// ^^^^^ keyword.declaration.class.php
// ^^^^^ entity.name.class.php
// ^^^^^^^ storage.modifier.extends.php
// ^^^^^^^^ entity.other.inherited-class.php support.class.builtin.php - meta.path
// ^^^^^^^^^^ storage.modifier.implements.php
// ^^^^^^^^^ entity.other.inherited-class.php support.class.builtin.php - meta.path

class Test4 extends Foo, Bar implements Foo, Bar {}
// ^ - meta.class
Expand Down Expand Up @@ -1009,7 +1011,7 @@ class Test5 extends \Foo\ implements \Bar\ {}
// ^ punctuation.accessor.namespace.php

ABSTRACT CLASS /**/ #[Anno] /**/ Test6 /**/ EXTENDS /**/ #[Anno] /**/ \My\Foo /**/ IMPLEMENTS /**/ #[Anno] /**/ Bar /**/, /**/ Baz /**/ { }
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.php
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.class.php
// ^^^^^^^^ storage.modifier.php
// ^^^^^ keyword.declaration.class.php
// ^^^^ comment.block.php
Expand Down Expand Up @@ -2017,6 +2019,25 @@ function unionTypeFunction(
// ^^^ storage.type
// ^ punctuation.definition.variable
// ^^^^^^ variable.parameter
(?A|B)&(C|?D)&?E $param2,
// ^ punctuation.section.group.begin
// ^ storage.type.nullable
// ^ support.class
// ^ punctuation.separator.type.union
// ^ support.class
// ^ punctuation.section.group.end
// ^ punctuation.separator.type.intersection
// ^ punctuation.section.group.begin
// ^ support.class
// ^ punctuation.separator.type.union
// ^ storage.type.nullable
// ^ support.class
// ^ punctuation.section.group.end
// ^ punctuation.separator.type.intersection
// ^ storage.type.nullable
// ^ support.class
// ^ punctuation.definition.variable
// ^^^^^^ variable.parameter
string $param3,
// ^^^^^^ storage.type
// ^ punctuation.definition.variable
Expand Down

0 comments on commit 8535db7

Please sign in to comment.