From 8239698ad8f48bdc71c977896c1b843fea97be6a Mon Sep 17 00:00:00 2001 From: Dustin Wilson Date: Wed, 9 Jun 2021 22:59:05 -0500 Subject: [PATCH] Started adding matching functionality --- lib/Scope/Matcher.php | 6 ++++- lib/Scope/Matchers/ScopeMatcher.php | 39 +++++++++++++++++++++++++++ lib/Scope/Matchers/SegmentMatcher.php | 4 +++ lib/Scope/Matchers/TrueMatcher.php | 4 +++ lib/Scope/Parser.php | 2 +- 5 files changed, 53 insertions(+), 2 deletions(-) diff --git a/lib/Scope/Matcher.php b/lib/Scope/Matcher.php index de2d285..3f42780 100644 --- a/lib/Scope/Matcher.php +++ b/lib/Scope/Matcher.php @@ -6,4 +6,8 @@ declare(strict_types=1); namespace dW\Highlighter\Scope; -abstract class Matcher {} \ No newline at end of file +abstract class Matcher { + public function getPrefix(string $scope) { + return null; + } +} \ No newline at end of file diff --git a/lib/Scope/Matchers/ScopeMatcher.php b/lib/Scope/Matchers/ScopeMatcher.php index 5bf614c..e1284b3 100644 --- a/lib/Scope/Matchers/ScopeMatcher.php +++ b/lib/Scope/Matchers/ScopeMatcher.php @@ -12,4 +12,43 @@ class ScopeMatcher extends Matcher { public function __construct(SegmentMatcher|TrueMatcher ...$matchers) { $this->segments = $matchers; } + + public function matches(string $scope): bool { + $lastDotIndex = 0; + $scopeLen = strlen($scope); + foreach ($this->segments as $index => $segment) { + if ($lastIndex > $scopeLen) { + break; + } + + $nextDotIndex = strpos($scope, '.', $lastDotIndex); + if ($nextDotIndex === false) { + $nextDotIndex = $scopeLen; + } + + $scopeSegment = substr($scope, $lastDotIndex, $nextDotIndex); + if (!$segment->matches($scopeSegment)) { + return false; + } + + $lastDotIndex = $nextDotIndex + 1; + } + + return ($index === count($this->segments)); + } + + public function getPrefix(string $scope): string|null|false { + $scopeSegments = explode('.', $scope); + if (count($scopeSegments) < count($this->segments)) { + return false; + } + + foreach ($this->segments as $index => $segment) { + if ($segment->matches($scopeSegments[$index])) { + if ($segment->prefix !== null) { + return $segment->prefix; + } + } + } + } } diff --git a/lib/Scope/Matchers/SegmentMatcher.php b/lib/Scope/Matchers/SegmentMatcher.php index f043d8b..723668d 100644 --- a/lib/Scope/Matchers/SegmentMatcher.php +++ b/lib/Scope/Matchers/SegmentMatcher.php @@ -12,4 +12,8 @@ class SegmentMatcher extends Matcher { public function __construct(string $segment) { $this->segment = $segment; } + + public function matches(string $scope): bool { + return ($scope === $this->segment); + } } diff --git a/lib/Scope/Matchers/TrueMatcher.php b/lib/Scope/Matchers/TrueMatcher.php index dd34b04..2c94c9d 100644 --- a/lib/Scope/Matchers/TrueMatcher.php +++ b/lib/Scope/Matchers/TrueMatcher.php @@ -12,4 +12,8 @@ class TrueMatcher extends Matcher { public function __construct(string $scopeName) { $this->scopeName = $scopeName; } + + public function matches(string $scope): bool { + return true; + } } diff --git a/lib/Scope/Parser.php b/lib/Scope/Parser.php index dabad04..dc63731 100644 --- a/lib/Scope/Parser.php +++ b/lib/Scope/Parser.php @@ -371,7 +371,7 @@ class Parser { } printf($message, - self::instance->debugCount++, + self::$instance->debugCount++, ltrim($methodTree, '->'), self::$instance->data->position, var_export(self::$instance->data->peek(), true)