Skip to content

Commit

Permalink
Implement Pattern::instanceof(), Pattern::not()
Browse files Browse the repository at this point in the history
  • Loading branch information
Muqsit committed Oct 28, 2022
1 parent 408a45d commit 1de0e16
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/muqsit/arithmexp/pattern/Pattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use Generator;
use muqsit\arithmexp\expression\token\ExpressionToken;
use muqsit\arithmexp\pattern\matcher\NotPatternMatcher;
use muqsit\arithmexp\pattern\matcher\PatternMatcher;
use muqsit\arithmexp\pattern\matcher\InstanceOfPatternMatcher;
use muqsit\arithmexp\Util;

final class Pattern{
Expand All @@ -24,4 +26,16 @@ public static function &findMatching(PatternMatcher $matcher, array &$tree) : Ge
}
}
}

public static function not(PatternMatcher $matcher) : PatternMatcher{
return new NotPatternMatcher($matcher);
}

/**
* @param class-string<ExpressionToken> $type
* @return PatternMatcher
*/
public static function instanceof(string $type) : PatternMatcher{
return new InstanceOfPatternMatcher($type);
}
}
29 changes: 29 additions & 0 deletions src/muqsit/arithmexp/pattern/matcher/InstanceOfPatternMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace muqsit\arithmexp\pattern\matcher;

use muqsit\arithmexp\expression\token\ExpressionToken;

final class InstanceOfPatternMatcher implements PatternMatcher{

/**
* @param class-string<ExpressionToken> $class
*/
public function __construct(
private string $class
){}

public function matches(ExpressionToken|array $entry) : bool{
if($entry instanceof ExpressionToken){
return $entry instanceof $this->class;
}
foreach($entry as $token){
if(!($token instanceof $this->class)){
return false;
}
}
return true;
}
}
18 changes: 18 additions & 0 deletions src/muqsit/arithmexp/pattern/matcher/NotPatternMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace muqsit\arithmexp\pattern\matcher;

use muqsit\arithmexp\expression\token\ExpressionToken;

final class NotPatternMatcher implements PatternMatcher{

public function __construct(
private PatternMatcher $matcher
){}

public function matches(ExpressionToken|array $entry) : bool{
return !$this->matcher->matches($entry);
}
}

0 comments on commit 1de0e16

Please sign in to comment.