From 449ce95f13074855fc948e4ef927da57ed6315eb Mon Sep 17 00:00:00 2001 From: Jack Cherng Date: Sat, 16 Nov 2019 03:41:03 +0800 Subject: [PATCH] [PHP] Fix "fn" function call (#2202) 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. --- PHP/PHP Source.sublime-syntax | 33 +++++++++++++++++++++++---------- PHP/syntax_test_php.php | 9 +++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/PHP/PHP Source.sublime-syntax b/PHP/PHP Source.sublime-syntax index c584d5c00a..346a420597 100644 --- a/PHP/PHP Source.sublime-syntax +++ b/PHP/PHP Source.sublime-syntax @@ -635,7 +635,18 @@ 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 @@ -643,19 +654,21 @@ contexts: 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*(?=\() diff --git a/PHP/syntax_test_php.php b/PHP/syntax_test_php.php index 84ea543bf7..82413f4353 100644 --- a/PHP/syntax_test_php.php +++ b/PHP/syntax_test_php.php @@ -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