Browse Source

Added prefix retrieval

main
Dustin Wilson 3 years ago
parent
commit
f592d93e23
  1. 6
      lib/Scope/Composite.php
  2. 6
      lib/Scope/Expression.php
  3. 27
      lib/Scope/Filter.php
  4. 6
      lib/Scope/Group.php
  5. 4
      lib/Scope/Node.php
  6. 4
      lib/Scope/Scope.php
  7. 20
      lib/Scope/Selector.php

6
lib/Scope/Composite.php

@ -15,6 +15,12 @@ class Composite extends Node {
}
public function getPrefix(array $scopes): ?int {
if ($this->matches($scopes)) {
return $this->_expressions[0]->getPrefix($scopes);
}
}
public function matches(array $scopes): bool {
$result = false;
foreach ($this->_expressions as $expression) {

6
lib/Scope/Expression.php

@ -24,6 +24,12 @@ class Expression extends Node {
}
public function getPrefix(array $scopes): ?int {
if ($this->matches($scopes)) {
return $this->_child->getPrefix($scopes);
}
}
public function matches(array $scopes): bool {
$matches = $this->_child->matches($scopes);
return ($this->_negate) ? !$matches : $matches;

27
lib/Scope/Filter.php

@ -10,39 +10,44 @@ class Filter extends Node {
protected Group|Path $_child;
protected int $_prefix;
const SIDE_LEFT = 0;
const SIDE_RIGHT = 1;
const SIDE_BOTH = 2;
const PREFIX_LEFT = 0;
const PREFIX_RIGHT = 1;
const PREFIX_BOTH = 2;
public function __construct(Group|Path $child, string $prefix) {
$this->_child = $child;
switch ($prefix) {
case 'L': $this->_prefix = self::SIDE_LEFT;
case 'L': $this->_prefix = self::PREFIX_LEFT;
break;
case 'R': $this->_prefix = self::SIDE_RIGHT;
case 'R': $this->_prefix = self::PREFIX_RIGHT;
break;
case 'B': $this->_prefix = self::SIDE_BOTH;
case 'B': $this->_prefix = self::PREFIX_BOTH;
break;
}
}
public function getPrefix(array $scopes): ?int {
return ($this->matches($scopes)) ? $this->_prefix : null;
}
public function matches(array $scopes): bool {
// No idea if prefixes are supposed to affect matches. Appears to in the
// TextMate original but not in Atom's implementation...
// TODO: Handle prefixes when matching; AFAIK prefixes only apply when
// determining when to inject grammars. It appears there's more to it in
// TextMate's original C++, but it may just be for determining "rank"?
return $this->_child->matches($scopes);
}
public function __toString(): string {
switch ($this->_prefix) {
case self::SIDE_LEFT: $prefix = 'L';
case self::PREFIX_LEFT: $prefix = 'L';
break;
case self::SIDE_RIGHT: $prefix = 'R';
case self::PREFIX_RIGHT: $prefix = 'R';
break;
case self::SIDE_BOTH: $prefix = 'B';
case self::PREFIX_BOTH: $prefix = 'B';
break;
}

6
lib/Scope/Group.php

@ -15,6 +15,12 @@ class Group extends Node {
}
public function getPrefix(array $scopes): ?int {
if ($this->matches($scopes)) {
return $this->_child->getPrefix($scopes);
}
}
public function matches(array $scopes): bool {
return $this->_child->matches($scopes);
}

4
lib/Scope/Node.php

@ -9,4 +9,8 @@ use dW\Lit\FauxReadOnly;
class Node {
use FauxReadOnly;
public function getPrefix(array $scopes): ?int {
return null;
}
}

4
lib/Scope/Scope.php

@ -20,10 +20,6 @@ class Scope extends Node {
public function matches(Scope $scope): bool {
/*if (count($this->_atoms) !== count($scope->atoms)) {
return false;
}*/
foreach ($this->_atoms as $index => $atom) {
if ($atom === '*') {
continue;

20
lib/Scope/Selector.php

@ -15,15 +15,21 @@ class Selector extends Node {
}
public function matches(array $scopes, &$match = null): bool {
foreach ($scopes as &$s) {
$isString = is_string($s);
if (!$isString && !$s instanceof Scope) {
throw new \Exception("Argument \$scopes must be an array containing only Scopes and/or strings.\n");
public function getPrefix(array $scopes): ?int {
foreach ($scopes as $s) {
if (!$s instanceof Scope) {
throw new \Exception("Argument \$scopes must be an array of Scope instances.\n");
}
}
$matches = $this->matches($scopes, $match);
return ($matches) ? $match->getPrefix($scopes) : null;
}
if ($isString) {
$s = Parser::parseScope($s);
public function matches(array $scopes, ?Composite &$match = null): bool {
foreach ($scopes as $s) {
if (!$s instanceof Scope) {
throw new \Exception("Argument \$scopes must be an array of Scope instances.\n");
}
}

Loading…
Cancel
Save