Skip to content

Commit

Permalink
Added curly-brace variant + object destructuring
Browse files Browse the repository at this point in the history
  • Loading branch information
assertchris committed Jun 8, 2017
1 parent 68beef0 commit 305210d
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 14 deletions.
55 changes: 49 additions & 6 deletions src/macros.yay
Original file line number Diff line number Diff line change
@@ -1,16 +1,59 @@
<?php

macro {
<>
= <>
} >> {
(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
)
Expand All @@ -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))
}
)}
2 changes: 2 additions & 0 deletions src/parsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
const ANGLE_LAYER_DELIMITERS = [
"<" => 1,
">" => -1,
"{" => 2,
"}" => -2,
];

function angle_layer(): Parser
Expand Down
45 changes: 37 additions & 8 deletions tests/specs/collections.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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];

0 comments on commit 305210d

Please sign in to comment.