Skip to content

Commit

Permalink
[PHP] Fix "fn" function call (sublimehq#2202)
Browse files Browse the repository at this point in the history
Fix:
    $var = fn($x)
    //     ^^ meta.function.arrow-function.php - meta.function-call
       => $x * 2;
    // ^^ punctuation.definition.arrow-function

    $var = fn($x)
    //     ^^ meta.function-call - meta.function.arrow-function.php
    ;

- Before PHP 7.4, "fn" can be used as a function name.
- Since PHP 7.4, "fn" is a reserved word.

Previously, we assumed that "fn" is always a reserved word
because of ST's parsing engine's limitation.
  • Loading branch information
jfcherng authored and wbond committed Nov 15, 2019
1 parent 93383eb commit 449ce95
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
33 changes: 23 additions & 10 deletions PHP/PHP Source.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -635,27 +635,40 @@ contexts:

# https://wiki.php.net/rfc/arrow_functions_v2
arrow-function:
- match: '(?i)\b(fn)\b\s*(&)?\s*(?=\()'
# "fn" can be a function name before PHP 7.4
# Since PHP 7.4, "fn" is a reserved word
- match: (?i)(?=\bfn\b\s*&?\s*\()
branch_point: arrow-function-branch
branch:
# Anonymous function definition: fn($a) => $a * 2;
- is-arrow-function
# Function call, where "fn" is the function name: fn($a);
- fn-function-call

is-arrow-function:
- match: (?i)\b(fn)\b\s*(&)?\s*(?=\()
scope: meta.function.arrow-function.php
captures:
1: storage.type.function.php
2: storage.modifier.reference.php
push:
- match: \(
scope: meta.function.parameters.php meta.group.php punctuation.section.group.begin.php
push:
- include: function-parameters
- match: (?==>)
pop: true
- match: "=>"
push: function-parameters
- match: '=>'
scope: punctuation.definition.arrow-function.php
push:
set:
- match: '(?=;|,|\)|\s*\?>)'
pop: true
pop: 2
- include: expressions
# Exit on unexpected content
- include: comments
- match: (?=\S)
pop: true
fail: arrow-function-branch

fn-function-call:
- include: function-call
- match: ''
pop: true

closure:
- match: (?i)\b(function)\s*(&)?\s*(?=\()
Expand Down
9 changes: 9 additions & 0 deletions PHP/syntax_test_php.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,15 @@ function array_values_from_keys($arr, $keys) {
// ^ punctuation.section.group.end
// ^^ punctuation.definition.arrow-function.php

$var = fn($x)
// ^^ meta.function.arrow-function.php - meta.function-call
=> $x * 2;
// ^^ punctuation.definition.arrow-function

$var = fn($x)
// ^^ meta.function-call - meta.function.arrow-function.php
;

$var = function(array $ar=array(), ClassName $cls) use ($var1, $var2) {
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.function
// ^^^^^^^^ meta.function.closure
Expand Down

0 comments on commit 449ce95

Please sign in to comment.