Skip to content

Commit

Permalink
Merge pull request #37 from assertchris/bugfix/add-ast-check-for-midrule
Browse files Browse the repository at this point in the history
Added Ast check for midrule
  • Loading branch information
assertchris authored Mar 25, 2017
2 parents fb315ce + 5b8ce0f commit 2279ed4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,15 @@ protected function compileParserArgs(Ast $args) : array {
return $compiled;
}

private function compileAnonymousFunctionArg(array $arg) : \Closure {
private function compileAnonymousFunctionArg($arg) : \Closure {
if ($arg instanceof Ast) {
$arg = $arg->unwrap();
}

if (!is_array($arg)) {
throw new InvalidArgumentException('$arg should be an array or instance of Yay\Ast');
}

$arglist = implode('', $arg['args']);
$body = implode('', $arg['body']);
$source = "<?php\nreturn static function({$arglist}){\n{$body}\n};";
Expand Down
42 changes: 42 additions & 0 deletions tests/phpt/midrule.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
--TEST--
Check that midrule works
--FILE--
<?php

macro {
·chain(
matched,
·midrule(function($stream) {
$index = $stream->index();

if (in_array(strtolower($stream->current()), ["true", "false", "null"])) {
return new \Yay\Error(null, null, $stream->last());
}

return new \Yay\Ast;
}),
·ns()·ns
)
} >> {
replaced ·ns
}

matched true
matched false
matched null

matched strtoupper
matched ucwords

?>
--EXPECTF--
<?php

matched true
matched false
matched null

replaced strtoupper
replaced ucwords

?>

0 comments on commit 2279ed4

Please sign in to comment.