Skip to content

Commit e4ed0a2

Browse files
committed
Fix parsing arrow functions inside arrays
1 parent 489acd9 commit e4ed0a2

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ XP Reflection ChangeLog
33

44
## ?.?.? / ????-??-??
55

6+
* Fixed parsing arrow functions inside arrays - @thekid
7+
68
## 2.13.0 / 2023-06-04
79

810
* Merged PR #36: Make Type, Member and Parameter classes implement the

src/main/php/lang/meta/FromSyntaxTree.class.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ private function parse($code, $resolver) {
7474
case ')': $b--; if ($b < 0) break 2; else break;
7575
case '[': $c++; break;
7676
case ']': $c--; if ($c < 0) break 2; else break;
77+
case ',': if ($c <= 0 && $b <= 0) break 2; else break; // outside of arrays or argument lists
7778
case '$': $parse->forward(); $parse->token->value= '$'.$parse->token->value; break;
7879
}
7980
$code.= ' '.$parse->token->value;

src/test/php/lang/reflection/unittest/EvaluateTest.class.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ public function run($expression, $args, $value) {
4343
Assert::equals($value, $func(...$args));
4444
}
4545

46+
#[Test]
47+
public function arrow_function_with_trailing_comma() {
48+
$func= cast(Reflection::of($this)->evaluate('[fn() => "test",]'), 'callable[]');
49+
Assert::equals('test', $func[0]());
50+
}
51+
4652
#[Test, Expect(class: IllegalArgumentException::class, message: 'Test')]
4753
public function throw_expression_supported_in_fn() {
4854
$func= Reflection::of($this)->evaluate('fn() => throw new \lang\IllegalArgumentException("Test")');

0 commit comments

Comments
 (0)