Browse Source

Started adding matching functionality

main
Dustin Wilson 3 years ago
parent
commit
8239698ad8
  1. 6
      lib/Scope/Matcher.php
  2. 39
      lib/Scope/Matchers/ScopeMatcher.php
  3. 4
      lib/Scope/Matchers/SegmentMatcher.php
  4. 4
      lib/Scope/Matchers/TrueMatcher.php
  5. 2
      lib/Scope/Parser.php

6
lib/Scope/Matcher.php

@ -6,4 +6,8 @@
declare(strict_types=1);
namespace dW\Highlighter\Scope;
abstract class Matcher {}
abstract class Matcher {
public function getPrefix(string $scope) {
return null;
}
}

39
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;
}
}
}
}
}

4
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);
}
}

4
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;
}
}

2
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)

Loading…
Cancel
Save