Skip to content

Commit

Permalink
[PHP] Improve type hints (#3502)
Browse files Browse the repository at this point in the history
* [PHP] Tweak type-hint performance

This commit adjusts type hint implementation to avoid some redundant
lookaheads once a type hint context is on stack.

The new test case is meant to ensure not to break reference operator
precedence in function parameter lists as this is the only change in
behavior this commit includes.

Benchmark:

10k lines of:

   function unionTypeFunction(int | string $param0, Foo | \Foo\Bar | ?int $param1, Foo|\Foo\Bar|?int $param2, (?A|B)&(C|?D)&?E $param2, string $param3, $param4): Foo|\Foo\Bar|?int|static {}

Result:

   -5% parsing time

* [PHP] Tweak property type hints

This commit simplifies patterns needed to handle property type hints.

Properties begin with at least one visibility or type modifier
optionally followed by a type hint. The `type-hint` context is used by
`property-type-hints` only. The complicated lookahead pattern is not
needed in this use case.

Benchmark:

10k lines of:

    readonly $var = 0; public int | string $param0; public Foo | \Foo\Bar | ?int $param1; public Foo|\Foo\Bar|?int $param2; public (?A|B)&(C|?D)&?E $param2; public string $param3; public $param4;

Result:

   -4% parsing time

* [PHP] Tweak function parameter type-hints

This commit...

1. improves behavior with regards to incomplete type-hints to limit
   escalation of broken syntax highlighting.
2. improves performance of function parameter lists

Benchmark:

10k lines of:

   function unionTypeFunction(int | string $param0, Foo | \Foo\Bar | ?int $param1, Foo|\Foo\Bar|?int $param2, (?A|B)&(C|?D)&?E $param2, string $param3, $param4): Foo|\Foo\Bar|?int|static {}

Result:

   -10% parsing time

* [PHP] Optimize type-hints in catch expression

This commit applies new type-hint strategy to `catch(<type> $e)`
statements to get rid of `type-hints` context.
  • Loading branch information
deathaxe authored Oct 9, 2022
1 parent 9ecdeab commit 721cfe1
Show file tree
Hide file tree
Showing 2 changed files with 298 additions and 69 deletions.
159 changes: 90 additions & 69 deletions PHP/PHP Source.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,6 @@ contexts:
set:
- type-hint-body
- type-hint-simple-type
- type-hint-nullable
- include: else-pop

enum-declaration-body:
Expand Down Expand Up @@ -953,16 +952,13 @@ contexts:
push: property-type-hint

property-type-hint:
- match: (?=(?i:const|function)\b)
- match: (?=\$|(?i:const|function)\b)
pop: 1
- match: '{{property_modifier}}'
scope: storage.modifier.php
- include: access-modifiers
- include: static-modifiers
- include: type-hint
- include: attributes
- include: comments
- include: else-pop
- include: expect-type-hint

###[ ARROW FUNCTION DECLARATIONS ]############################################

Expand Down Expand Up @@ -991,9 +987,12 @@ contexts:

arrow-function-parameters:
- meta_include_prototype: false
- match: \(
scope: punctuation.section.group.begin.php
set: arrow-function-parameters-body
- match: (?=\()
set:
- arrow-function-parameters-body
- function-parameter-variable
- expect-type-hint
- group-begin
- include: else-pop

arrow-function-parameters-body:
Expand All @@ -1007,7 +1006,6 @@ contexts:
set:
- arrow-function-return-type-body
- type-hint-simple-type
- type-hint-nullable
- include: else-pop

arrow-function-return-type-body:
Expand Down Expand Up @@ -1087,47 +1085,75 @@ contexts:
set: function-parameters

constructor-parameters:
- match: \(
scope: punctuation.section.group.begin.php
set: constructor-parameters-body
- meta_include_prototype: false
- match: (?=\()
set:
- constructor-parameters-body
- function-parameter-variable
- constructor-type-hint
- group-begin
- include: comments
- include: else-pop

constructor-parameters-body:
- clear_scopes: 1
- meta_scope: meta.function.parameters.php meta.group.php
- match: ',|(?=[(?\\[:alpha:]])' # handle missing comma gracefully
scope: punctuation.separator.sequence.php
push:
- function-parameter-variable
- constructor-type-hint
- include: function-parameter-values
- include: group-end
- include: comments
- include: else-pop

constructor-type-hint:
- match: '{{constructor_modifier}}'
scope: storage.modifier.php
- include: access-modifiers
- include: static-modifiers
- include: function-parameters-body
- include: expect-type-hint

function-parameters:
- meta_include_prototype: false
- match: \(
scope: punctuation.section.group.begin.php
set: function-parameters-body
- match: (?=\()
set:
- function-parameters-body
- function-parameter-variable
- expect-type-hint
- group-begin
- include: comments
- include: else-pop

function-parameters-body:
- clear_scopes: 1
- meta_scope: meta.function.parameters.php meta.group.php
- match: \)
scope: punctuation.section.group.end.php
pop: 1
- match: =
scope: keyword.operator.assignment.php
push: single-expression
- include: expression-end-pop
- include: attributes
- match: ',|(?=[(?\\[:alpha:]])' # handle missing comma gracefully
scope: punctuation.separator.sequence.php
push:
- function-parameter-variable
- expect-type-hint
- include: function-parameter-values
- include: group-end
- include: comments
- include: punctuation-separators
- include: reference-modifiers
- include: variadic-operators
- include: type-hints
- include: else-pop

function-parameter-variable:
- match: (\$+){{identifier}}
scope: variable.parameter.php
captures:
1: punctuation.definition.variable.php
pop: 1
- include: reference-modifiers
- include: variadic-operators
- include: comments
- include: else-pop

function-parameter-values:
- match: =
scope: keyword.operator.assignment.php
push: single-expression

function-use:
- match: (?i:use)\b
Expand All @@ -1141,6 +1167,7 @@ contexts:
- match: \(
scope: punctuation.section.group.begin.php
set: function-use-group-body
- include: comments
- include: else-pop

function-use-group-body:
Expand All @@ -1154,7 +1181,6 @@ contexts:
set:
- function-return-type-body
- type-hint-simple-type
- type-hint-nullable
- include: else-pop

function-return-type-body:
Expand All @@ -1167,6 +1193,7 @@ contexts:
- match: \{
scope: punctuation.section.block.begin.php
set: function-block-body
- include: comments
- include: else-pop

function-block-body:
Expand Down Expand Up @@ -1285,21 +1312,21 @@ contexts:
- meta_content_scope: meta.catch.php
- match: \(
scope: punctuation.section.group.begin.php
set: catch-arguments-body
set:
- catch-arguments-body
- expect-type-hint
- include: comments
- include: else-pop

catch-arguments-body:
- meta_scope: meta.catch.arguments.php meta.group.php
- include: group-end
- include: expression-end-pop
- include: attributes
- include: comments
- include: type-hints
- match: (\$){{identifier}}
scope: variable.other.php
captures:
1: punctuation.definition.variable.php
- include: group-end
- include: comments
- include: else-pop

###[ FOR STATEMENTS ]#########################################################

Expand Down Expand Up @@ -1479,6 +1506,12 @@ contexts:
scope: punctuation.section.group.begin.php
push: group-body

group-begin:
- meta_include_prototype: false
- match: \(
scope: punctuation.section.group.begin.php
pop: 1

group-body:
- meta_scope: meta.group.php
- include: group-end
Expand Down Expand Up @@ -1905,52 +1938,37 @@ contexts:

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

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

type-hint:
expect-type-hint:
- include: attributes
- include: comments
# https://wiki.php.net/rfc/union_types_v2
- match: (?=(?:\(\s*)?(?:\?\s*)?\\?{{identifier_start}})
- match: (?=\S)
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
- match: \(
scope: punctuation.section.group.begin.php
push:
- type-hint-group-body
- type-hint-simple-type
- type-hint-nullable
- match: '&(?!\s*\$)'
- match: \|
scope: punctuation.separator.type.union.php
push: type-hint-simple-type
- match: \&(?!\s*\$) # references have precedence
scope: punctuation.separator.type.intersection.php
push:
- type-hint-simple-type
- type-hint-nullable
- include: type-hint-groups
push: type-hint-simple-type
- include: comments
- 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
type-hint-group-body:
- meta_scope: meta.group.php
- match: \)
scope: punctuation.section.group.end.php
pop: 1
- include: else-pop
- include: type-hint-body

type-hint-simple-type:
# note that
Expand All @@ -1962,8 +1980,11 @@ contexts:
- match: (?=(?i:extends|implements)\b)
pop: 1
- include: expression-end-pop
- match: \?
scope: storage.type.nullable.php
- include: binding
- include: class-name
- include: comments
- include: else-pop

###[ CLASSES ]################################################################
Expand Down
Loading

0 comments on commit 721cfe1

Please sign in to comment.