diff --git a/src/macros.yay b/src/macros.yay index cd0ba99..4018626 100644 --- a/src/macros.yay +++ b/src/macros.yay @@ -1,16 +1,59 @@ + = <> } >> { - (new \Pre\Collections\Collection) + = (new \Pre\Collections\Collection) } +macro { + = {} +} >> { + = (new \Pre\Collections\Collection) +} + +macro ·recursion { + ·chain( + ·either( + ·token("<"), + ·token("{") + ), + ·angle_layer()·values, + ·either( + ·token(">"), + ·token("}") + ), + ·token("="), + ·_()·keys + ) +} >> function($ast) { + $keys = new \Yay\Ast("·keys"); + + foreach ($ast->{"·values"} as $token) { + if (!$token->is(T_VARIABLE)) { + continue; + } + + $keys->push(new \Yay\Ast("·var", $token)); + } + + $ast->append($keys); +} >> {··trim( + [··trim(·keys ···(, ) {··trim( ··stringify(··unvar(·var)) => ·var )})] = +)} + macro ·recursion { ·chain( - ·token("<"), + ·token("="), + ·either( + ·token("<"), + ·token("{") + ), ·angle_layer()·values, - ·token(">"), + ·either( + ·token(">"), + ·token("}") + ), ·_()·is_array, ·_()·is_not_array ) @@ -36,10 +79,10 @@ macro ·recursion { $ast->append($append); } >> {··trim( ·is_array ?·{ - (new \Pre\Collections\Collection([·values])) + = (new \Pre\Collections\Collection([·values])) } ·is_not_array ?·{ - (new \Pre\Collections\Collection(·values)) + = (new \Pre\Collections\Collection(·values)) } )} diff --git a/src/parsers.php b/src/parsers.php index e52749c..640389f 100644 --- a/src/parsers.php +++ b/src/parsers.php @@ -5,6 +5,8 @@ const ANGLE_LAYER_DELIMITERS = [ "<" => 1, ">" => -1, + "{" => 2, + "}" => -2, ]; function angle_layer(): Parser diff --git a/tests/specs/collections.spec b/tests/specs/collections.spec index b5d7bea..4c0f109 100644 --- a/tests/specs/collections.spec +++ b/tests/specs/collections.spec @@ -4,25 +4,54 @@ Test collections macros --GIVEN-- -$a = <>; +$a1 = <>; -$b = <$a>; +$a2 = {}; -$c = <"foo" => "bar">; +$b1 = <$a1>; -$d = < +$b2 = {$a2}; + +$c1 = <"foo" => "bar">; + +$c2 = {"foo" => "bar"}; + +$d1 = < "hello" => "world", "goodbye" => "world", >; +$d2 = { + "hello" => "world", + "goodbye" => "world", +}; + +< $a, $b, $c > = ["a" => 1, "b" => 2, "c" => 3]; + +{ $a, $b, $c } = ["a" => 1, "b" => 2, "c" => 3]; + --EXPECT-- -$a = (new \Pre\Collections\Collection); +$a1 = (new \Pre\Collections\Collection); -$b = (new \Pre\Collections\Collection($a)); +$a2 = (new \Pre\Collections\Collection); -$c = (new \Pre\Collections\Collection(["foo" => "bar"])); +$b1 = (new \Pre\Collections\Collection($a1)); -$d = (new \Pre\Collections\Collection(["hello" => "world", +$b2 = (new \Pre\Collections\Collection($a2)); + +$c1 = (new \Pre\Collections\Collection(["foo" => "bar"])); + +$c2 = (new \Pre\Collections\Collection(["foo" => "bar"])); + +$d1 = (new \Pre\Collections\Collection(["hello" => "world", + "goodbye" => "world", +])); + +$d2 = (new \Pre\Collections\Collection(["hello" => "world", "goodbye" => "world", ])); + +['a' => $a, 'b' => $b, 'c' => $c] = ["a" => 1, "b" => 2, "c" => 3]; + +['a' => $a, 'b' => $b, 'c' => $c] = ["a" => 1, "b" => 2, "c" => 3];