Skip to content

Commit

Permalink
Merge pull request #2 from preprocess/feature/new-sigil
Browse files Browse the repository at this point in the history
New sigil
  • Loading branch information
assertchris authored Sep 10, 2018
2 parents 9da701d + ca568e8 commit ecd935a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 70 deletions.
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
"name": "pre/parameter-loaders",
"license": "MIT",
"require": {
"pre/plugin": "^0.10.0"
"pre/plugin": "^0.11.0"
},
"require-dev": {
"phpunit/phpunit": "^5.0|^6.0"
},
"extra": {
"macros": ["source/macros.yay"]
"macros": [
"source/macros.yay"
]
}
}
114 changes: 57 additions & 57 deletions source/macros.yay
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
<?php

macro {
·chain(
$(macro) {
$(chain(
function,
·optional(·token(T_STRING)function,
·between(
·token("("),
·optional(
·ls(
·either(
·chain(
·optional(·ns())·enhanced_type,
·token(T_VARIABLE)·enhanced_name,
·token("="),
·chain(
·optional(·token(T_NEW))·enhanced_new,
·midrule(function($stream) {
optional(token(T_STRING) as function),
between(
token("("),
optional(
ls(
either(
chain(
optional(ns() as enhancedType),
token(T_VARIABLE) as enhancedName,
token("="),
chain(
optional(token(T_NEW) as enhancedNew),
midrule(function($stream) {
$index = $stream->index();

if (in_array(strtolower($stream->current()), ["true", "false", "null"])) {
Expand All @@ -24,58 +24,58 @@ macro {

return new \Yay\Ast;
}),
·ns()·enhanced_ns,
·optional(
·chain(
·token("("), ·layer(), ·token(")")
)
)·enhanced_layer
)·enhanced_value
)·enhanced_parameter,
·chain(
·optional(·ns())·regular_type,
·token(T_VARIABLE)·regular_name,
·optional(
·chain(
·token("="),
·either(
·token(T_CONSTANT_ENCAPSED_STRING),
·token(T_LNUMBER),
·token(T_DNUMBER),
·token(T_STRING)
ns() as enhancedNs,
optional(
chain(
token("("), layer() as enhancedLayerValue, token(")")
)
) as enhancedLayer
) as enhancedValue
) as enhancedParameter,
chain(
optional(ns() as regularType),
token(T_VARIABLE) as regularName,
optional(
chain(
token("="),
either(
token(T_CONSTANT_ENCAPSED_STRING),
token(T_LNUMBER),
token(T_DNUMBER),
token(T_STRING)
) as regularValue
)
)·regular_value
)·regular_parameter
) as regularEq
) as regularParameter
),
·token(",")
)·parameter
token(",")
) as parameter
),
·token(")")
token(")")
),
·optional(
·chain(
·token(":"),
·ns()
optional(
chain(
token(":"),
ns()
)
)·return_type,
·between(
·token("{"), ·layer()·rest, ·token("}")
) as returnType,
between(
token("{"), layer() as rest, token("}")
)
)
))
} >> {
function ·functionparameter ··· (, ) { ·regular_parameter ?···{·regular_type ·regular_name ·regular_value} ·enhanced_parameter ?··· {·enhanced_type ·enhanced_name = null}}) ·return_type
function $(function)($(parameter ... (, ) { $(regularParameter ? ... { $(regularType) $(regularName) $(regularEq ... {= $(regularValue)}) }) $(enhancedParameter ? ... { $(enhancedType) $(enhancedName) = null }) })) $(returnType)
{
··collapse(··trim(
·parameter ··· {
·enhanced_parameter ?··· {
if (is_null(·enhanced_name)) {
·enhanced_name = ·enhanced_value ··· {·enhanced_new ·enhanced_ns ·enhanced_layer};
}
$(parameter ... {$$(trim(
$(enhancedParameter ? ... {
if (is_null($(enhancedName))) {
$(enhancedName) = $(enhancedValue ... {$$(trim(
$(enhancedNew) $$(trim( $(enhancedNs) $(enhancedLayer ... {($(enhancedLayerValue))}) ));
))})
}
}
))
})
))})

·rest
$(rest)
}
}
36 changes: 25 additions & 11 deletions tests/specs/parameter-loaders.spec
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Fixture

class Fixture
{
public function typeHintedRegular(string $string ="typeHintedRegular")
public function typeHintedRegular(string $string = "typeHintedRegular")
{
return "working";
}
Expand All @@ -44,32 +44,30 @@ class Fixture
return "working";
}

public function multipleRegular($one ="one", $two =2.2, $three =true, $four =null)
public function multipleRegular($one = "one", $two = 2.2, $three = true, $four = null)
{
return "working";
}

public function multipleEnhanced($one = null, $two = null)
{
if (is_null($one)) {
$one = ucwords("one");
$one = ucwords("one");
}

if (is_null($two)) {
$two = round(2.2);
$two = round(2.2);
}

return "working";
}

public function mixed($one, $two =2.2, $three = null, $four = null)
public function mixed($one, $two = 2.2, $three = null, $four = null)
{
if (is_null($three)) {
$three = round(3.3);
$three = round(3.3);
}

if (is_null($four)) {
$four = new stdClass ;
$four = new stdClass;
}

return "working";
Expand All @@ -84,10 +82,26 @@ $anon = function($a = true, $b = "hello", $c = ucwords("world")) {

~~~

$anon = function ($a =true, $b ="hello", $c = null) {
$anon = function ($a = true, $b = "hello", $c = null) {
if (is_null($c)) {
$c = ucwords("world");
$c = ucwords("world");
}

print $b . " " . $c;
};

---

$anon = function($driver = new Driver("hello world", 12)) {
return $driver->fn();
};

~~~

$anon = function ($driver = null) {
if (is_null($driver)) {
$driver = new Driver("hello world", 12);
}

return $driver->fn();
};

0 comments on commit ecd935a

Please sign in to comment.