Skip to content

Commit

Permalink
[PHP] Refactor enum declarations (sublimehq#3310)
Browse files Browse the repository at this point in the history
  • Loading branch information
deathaxe authored Apr 4, 2022
1 parent cedbeb7 commit cb51d06
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 111 deletions.
222 changes: 118 additions & 104 deletions PHP/PHP Source.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ contexts:
- include: statements

statements:
- include: class
- include: enum
- include: classes
- include: enums
- include: use-statement
- match: '(?i)^\s*(interface)\s+({{identifier}})\s*'
captures:
Expand Down Expand Up @@ -407,38 +407,68 @@ contexts:
pop: 1
- include: nested-expressions

###[ TYPE HINTS ]#############################################################

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

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

type-hint-body:
- meta_scope: meta.type.php
- match: \|(?!\s*\$)
scope: punctuation.separator.type.union.php
push:
- match: (?=(?i:implements|extends)\b)
pop: 1
- match: \?
scope: storage.type.nullable.php
- match: \|
scope: punctuation.separator.type.union.php
- match: '&(?!\s*\$)'
scope: punctuation.separator.type.intersection.php
# 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: |-
\b(?xi:
array | bool | callable | double | float | int | iterable |
object | string | false | null | void | parent | self | static |
mixed | never
)\b
scope: storage.type.php
- include: class-builtin
- include: identifier-parts-as-path
- match: '(\\)?({{identifier}})(?!\\)'
scope: meta.path.php
captures:
1: punctuation.separator.namespace.php
2: support.class.php
- match: (?=\S)
pop: 1
- type-hint-simple-type
- type-hint-nullable
- match: '&(?!\s*\$)'
scope: punctuation.separator.type.intersection.php
push:
- type-hint-nullable
- type-hint-simple-type
- include: else-pop

type-hint-nullable:
- match: \?
scope: storage.type.nullable.php
set: else-pop
- include: else-pop

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: |-
\b(?xi:
array | bool | callable | double | float | int | iterable | object
| string | false | null | void | parent | self | static | mixed | never
)\b
scope: storage.type.php
pop: 1
- match: (?=(?i:extends|implements)\b)
pop: 1
- include: expression-end-pop
- include: class-builtin
- include: identifier-parts-as-path
- match: (\\)?({{identifier}})
scope: meta.path.php
captures:
1: punctuation.separator.namespace.php
2: support.class.php
pop: 1
- include: immediately-pop

attributes:
# https://wiki.php.net/rfc/attributes_v2
Expand Down Expand Up @@ -658,77 +688,9 @@ contexts:
- include: use-statement-common
- include: identifier-constant-pop

enum:
- match: (?i)\b(enum)\s+({{identifier}})\s*
scope: meta.enum.php
captures:
1: keyword.declaration.enum.php
2: entity.name.enum.php
push: enum-definition

enum-definition:
- meta_content_scope: meta.enum.php
- match: (?=;)
pop: 1
- match: ':'
scope: punctuation.separator.colon.php
push:
- match: (?={{path}})
set:
- meta_scope: meta.path.php
- match: (?=(?i:implements|extends)\b)
pop: 1
- include: type-hint
- match: (?=\S)
pop: 1
- match: (?=\S)
pop: 1
- match: \{
scope: punctuation.section.block.begin.php
set: enum-body
- include: attributes
- include: comments
- match: (?i)(extends)\b
scope: invalid.illegal.disallowed.php
- match: (?i)(implements)\b\s*
captures:
1: storage.modifier.implements.php
push:
- include: attributes
- include: comments
- match: (?={{path}})
push:
- meta_scope: meta.path.php
- meta_content_scope: entity.other.inherited-class.php
- include: class-builtin
- include: identifier-parts
- match: (\\)?{{identifier}}(?!\\)
captures:
1: punctuation.separator.namespace.php
- match: (?=\S)
pop: 1
- match: ','
scope: punctuation.separator.comma.php
- match: (?=\S)
pop: 1

enum-body:
- meta_scope: meta.enum.php meta.block.php
- match: \b(?i:case)\b
scope: keyword.control.php
push: constant-name
- include: class-body

constant-name:
- include: attributes
- include: comments
- match: '{{identifier}}'
scope: entity.name.constant.php
pop: true
- match: (?=\S)
pop: true
###[ CLASS DECLARATIONS ]#####################################################

class:
classes:
- match: (?i)\b((?:abstract|final)\s+)?(class)\s+({{identifier}})
scope: meta.class.php
captures:
Expand Down Expand Up @@ -828,6 +790,58 @@ contexts:
- include: identifier-parts-as-path
- include: identifier-as-path

###[ ENUM DECLARATIONS ]######################################################

enums:
- match: (?i)\b(enum)\s+({{identifier}})
scope: meta.enum.php
captures:
1: keyword.declaration.enum.php
2: entity.name.enum.php
push:
- enum-definition
- enum-type-hint

enum-type-hint:
- match: ':'
scope: punctuation.separator.colon.php
set:
- type-hint-body
- type-hint-simple-type
- type-hint-nullable
- include: else-pop

enum-definition:
- meta_content_scope: meta.enum.php
- include: expression-end-pop
- include: attributes
- include: comments
- match: \{
scope: punctuation.section.block.begin.php
set: enum-body
- match: (?i:extends)\b
scope: invalid.illegal.disallowed.php
- match: (?i:implements)\b
scope: storage.modifier.implements.php
push: class-implements

enum-body:
- meta_scope: meta.enum.php meta.block.php
- match: (?i:case)\b
scope: keyword.control.php
push: constant-name
- include: class-body

constant-name:
- include: attributes
- include: comments
- match: '{{identifier}}'
scope: entity.name.constant.php
set: nested-expressions
- include: else-pop

###[ TYPED PROPERTIES ]#######################################################

typed-property:
# https://wiki.php.net/rfc/typed_properties_v2
- match: (?=\b(?:{{visibility_modifier}}|static|readonly)\b)
Expand All @@ -836,7 +850,7 @@ contexts:
pop: 1
- match: \b(?:{{visibility_modifier}}|static|readonly)\b
scope: storage.modifier.php
- include: type-hint
- include: type-hints
# Exit on unexpected content
- match: (?=\S)
pop: 1
Expand Down Expand Up @@ -956,7 +970,7 @@ contexts:
- include: attributes
- include: comments
# Class name type hint
- include: type-hint
- include: type-hints
- match: '\.\.\.'
scope: keyword.operator.spread.php
- match: '&'
Expand Down Expand Up @@ -1007,7 +1021,7 @@ contexts:
set: function-body
- match: ':'
scope: punctuation.separator.colon.php
- include: type-hint
- include: type-hints
# Exit on unexpected content
- match: (?=\S)
pop: 1
Expand Down
90 changes: 83 additions & 7 deletions PHP/tests/syntax_test_php.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,33 @@ public function show(int $id) { }
// ^^ punctuation.definition.comment.end
// ^ - comment

enum
// <- constant.other.php
//^^ constant.other.php
// ^ - constant

enum Suit ;
// <- meta.enum.php keyword.declaration.enum.php
//^^^^^^^^ - meta.enum meta.enum
// ^ - meta.enum
//^^ keyword.declaration.enum.php
// ^ - keyword - entity
// ^^^^ entity.name.enum.php
// ^ - entity - punctuation
// ^ punctuation.terminator.expression.php

enum Suit : ;
// <- meta.enum.php keyword.declaration.enum.php
//^^^^^^^^^^ - meta.enum meta.enum
// ^ - meta.enum
//^^ keyword.declaration.enum.php
// ^ - keyword - entity
// ^^^^ entity.name.enum.php
// ^ - entity - punctuation
// ^ punctuation.separator.colon.php
// ^ - punctuation
// ^ punctuation.terminator.expression.php

enum Suit {
// <- meta.enum.php keyword.declaration.enum.php
//^^^^^^^^^^ - meta.enum meta.enum, - meta.block meta.block
Expand All @@ -846,24 +873,55 @@ enum Suit {
case Spades;
}

enum Suit: extends Colorful {}
// <- meta.enum.php keyword.declaration.enum.php
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - meta.enum meta.enum, - meta.block meta.block
//^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.enum.php - meta.block
// ^^ meta.enum.php meta.block.php
// ^ punctuation.separator.colon.php
// ^^^^^^^ invalid.illegal.disallowed.php
// ^^^^^^^^ - entity
// ^ punctuation.section.block.begin.php
// ^ punctuation.section.block.end.php

enum Suit: string extends Colorful {}
// <- meta.enum.php keyword.declaration.enum.php
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - meta.enum meta.enum, - meta.block meta.block
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.enum.php - meta.block
// ^^ meta.enum.php meta.block.php
// ^^^^^^^ invalid
// ^ punctuation.separator.colon.php
// ^^^^^^ storage.type.php
// ^^^^^^^ invalid.illegal.disallowed.php
// ^^^^^^^^ - entity
// ^ punctuation.section.block.begin.php
// ^ punctuation.section.block.end.php

enum Suit: string | mixed extends Colorful {}
// <- meta.enum.php keyword.declaration.enum.php
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - meta.enum meta.enum, - meta.block meta.block
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.enum.php - meta.block
// ^^ meta.enum.php meta.block.php
// ^ punctuation.separator.colon.php
// ^^^^^^ storage.type.php
// ^ punctuation.separator.type.union.php
// ^^^^^ storage.type.php
// ^^^^^^^ invalid.illegal.disallowed.php
// ^^^^^^^^ - entity
// ^ punctuation.section.block.begin.php
// ^ punctuation.section.block.end.php

enum Suit: string implements Colorful {
// <- meta.enum.php keyword.declaration.enum.php
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - meta.enum meta.enum, - meta.block meta.block
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.enum.php - meta.block
// ^^ meta.enum.php meta.block.php
// ^ keyword.declaration.enum
// ^^^^ entity.name.enum
// ^ punctuation.separator
// ^^^^^^ storage.type
// ^^^^^^^^^^ storage.modifier.implements
// ^^^^^^^^ entity.other.inherited-class
// ^ keyword.declaration.enum.php
// ^^^^ entity.name.enum.php
// ^ punctuation.separator.colon.php
// ^^^^^^ storage.type.php
// ^^^^^^^^^^ storage.modifier.implements.php
// ^^^^^^^^ entity.other.inherited-class.php
// ^ punctuation.section.block.begin.php
case Hearts = 'H';
// ^^^^ keyword.control
// ^^^^^^ entity.name.constant
Expand All @@ -886,6 +944,24 @@ public function color(): string {
}
}

enum Test1 extends Foo, Bar implements Foo, Bar {}
// <- meta.enum.php keyword.declaration.enum.php
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - meta.enum meta.enum, - meta.block meta.block
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.enum.php - meta.block
// ^^ meta.enum.php meta.block.php
//^^ keyword.declaration.enum.php
// ^^^^^ entity.name.enum.php
// ^^^^^^^ invalid.illegal.disallowed.php
// ^^^ - entity
// ^ - punctuation
// ^^^ - entity
// ^^^^^^^^^^ storage.modifier.implements.php
// ^^^ entity.other.inherited-class.php
// ^ punctuation.separator.comma.php
// ^^^ entity.other.inherited-class.php
// ^ punctuation.section.block.begin.php
// ^ punctuation.section.block.end.php

class

class Test1
Expand Down

0 comments on commit cb51d06

Please sign in to comment.