From e4ed0a27e4617e0f8db0c54445689f9838334e70 Mon Sep 17 00:00:00 2001 From: Timm Friebe Date: Sun, 4 Jun 2023 09:56:16 +0200 Subject: [PATCH] Fix parsing arrow functions inside arrays --- ChangeLog.md | 2 ++ src/main/php/lang/meta/FromSyntaxTree.class.php | 1 + .../php/lang/reflection/unittest/EvaluateTest.class.php | 6 ++++++ 3 files changed, 9 insertions(+) diff --git a/ChangeLog.md b/ChangeLog.md index 2677941..3e1c3d3 100755 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,8 @@ XP Reflection ChangeLog ## ?.?.? / ????-??-?? +* Fixed parsing arrow functions inside arrays - @thekid + ## 2.13.0 / 2023-06-04 * Merged PR #36: Make Type, Member and Parameter classes implement the diff --git a/src/main/php/lang/meta/FromSyntaxTree.class.php b/src/main/php/lang/meta/FromSyntaxTree.class.php index 287af45..b778393 100755 --- a/src/main/php/lang/meta/FromSyntaxTree.class.php +++ b/src/main/php/lang/meta/FromSyntaxTree.class.php @@ -74,6 +74,7 @@ private function parse($code, $resolver) { case ')': $b--; if ($b < 0) break 2; else break; case '[': $c++; break; case ']': $c--; if ($c < 0) break 2; else break; + case ',': if ($c <= 0 && $b <= 0) break 2; else break; // outside of arrays or argument lists case '$': $parse->forward(); $parse->token->value= '$'.$parse->token->value; break; } $code.= ' '.$parse->token->value; diff --git a/src/test/php/lang/reflection/unittest/EvaluateTest.class.php b/src/test/php/lang/reflection/unittest/EvaluateTest.class.php index f923864..4ecf275 100755 --- a/src/test/php/lang/reflection/unittest/EvaluateTest.class.php +++ b/src/test/php/lang/reflection/unittest/EvaluateTest.class.php @@ -43,6 +43,12 @@ public function run($expression, $args, $value) { Assert::equals($value, $func(...$args)); } + #[Test] + public function arrow_function_with_trailing_comma() { + $func= cast(Reflection::of($this)->evaluate('[fn() => "test",]'), 'callable[]'); + Assert::equals('test', $func[0]()); + } + #[Test, Expect(class: IllegalArgumentException::class, message: 'Test')] public function throw_expression_supported_in_fn() { $func= Reflection::of($this)->evaluate('fn() => throw new \lang\IllegalArgumentException("Test")');