Browse Source

Changed scope exceptions to match new parser's behavior

main
Dustin Wilson 3 years ago
parent
commit
5d64330e96
  1. 32
      lib/Scope/Exception.php
  2. 4
      lib/Scope/Matchers/GroupMatcher.php
  3. 4
      lib/Scope/Matchers/PathMatcher.php
  4. 14
      lib/Scope/Parser.php

32
lib/Scope/Exception.php

@ -9,23 +9,25 @@ namespace dW\Highlighter\Scope;
class Exception extends \Exception { class Exception extends \Exception {
const MESSAGE = '%s expected; found %s'; const MESSAGE = '%s expected; found %s';
public function __construct(string $expected, string|bool $found) { public function __construct(array|string $expected, string|bool $found) {
$strlen = strlen($expected); if (!is_string($expected)) {
if ($strlen > 1) { $expectedLen = count($expected);
$temp = []; if ($expectedLen === 1) {
for ($i = 0; $i < $strlen; $i++) { $expected = ($expected[0] !== false) ? $expected[0] : 'end of input';
$temp[] = ($expected[$i] !== false) ? "\"{$expected[$i]}\"" : 'end of input';
}
$expected = $temp;
if (count($expected) > 2) {
$last = array_pop($expected);
$expected = implode(', ', $expected) . ', or ' . $last;
} else { } else {
$expected = implode(' or ', $expected); $temp = [];
for ($i = 0; $i < $strlen; $i++) {
$temp[] = ($expected[$i] !== false) ? "{$expected[$i]}" : 'end of input';
}
$expected = $temp;
if ($expectedLen > 2) {
$last = array_pop($expected);
$expected = implode(', ', $expected) . ', or ' . $last;
} else {
$expected = implode(' or ', $expected);
}
} }
} else {
$expected = ($expected !== false) ? "\"$expected\"" : 'end of input';
} }
$found = ($found !== false) ? "\"$found\"" : 'end of input'; $found = ($found !== false) ? "\"$found\"" : 'end of input';

4
lib/Scope/Matchers/GroupMatcher.php

@ -10,8 +10,8 @@ class GroupMatcher extends Matcher {
protected string|null $prefix; protected string|null $prefix;
protected Matcher $selector; protected Matcher $selector;
public function __construct(string $prefix, Matcher $selector) { public function __construct(string|null $prefix, Matcher $selector) {
$this->prefix = $prefix[0]; $this->prefix = ($prefix !== null) ? $prefix[0] : null;
$this->selector = $selector; $this->selector = $selector;
} }

4
lib/Scope/Matchers/PathMatcher.php

@ -10,8 +10,8 @@ class PathMatcher extends Matcher {
protected string|null $prefix; protected string|null $prefix;
protected array $matchers; protected array $matchers;
public function __construct(string $prefix, ScopeMatcher ...$matchers) { public function __construct(string|null $prefix, ScopeMatcher ...$matchers) {
$this->prefix = $prefix[0]; $this->prefix = ($prefix !== null) ? $prefix[0] : null;
$this->matchers = $matchers; $this->matchers = $matchers;
} }

14
lib/Scope/Parser.php

@ -86,10 +86,8 @@ class Parser {
$result = self::parseGroup($prefix); $result = self::parseGroup($prefix);
} elseif (preg_match(self::SCOPE_REGEX, $peek)) { } elseif (preg_match(self::SCOPE_REGEX, $peek)) {
$result = self::parsePath($prefix); $result = self::parsePath($prefix);
} elseif ($peek !== false) {
die('Group or path expected.');
} else { } else {
die('Unexpected eod'); throw new Exception([ 'Instance of ' . __NAMESPACE__ . 'GroupMatcher', 'Instance of ' . __NAMESPACE__ . 'PathMatcher' ], $peek);
} }
if (self::$debug) { if (self::$debug) {
@ -107,7 +105,7 @@ class Parser {
$result = self::parseSelector(); $result = self::parseSelector();
$token = self::$instance->data->consume(); $token = self::$instance->data->consume();
if ($token !== ')') { if ($token !== ')') {
die(($token !== false) ? 'Close parenthesis expected' : 'Unexpected eod'); throw new Exception(')', $token);
} }
$result = ($prefix === null) ? $result : new GroupMatcher($prefix, $result); $result = ($prefix === null) ? $result : new GroupMatcher($prefix, $result);
@ -126,7 +124,6 @@ class Parser {
$result = []; $result = [];
$result[] = self::parseScope(); $result[] = self::parseScope();
die(var_export($result));
$peek = self::$instance->data->peek(); $peek = self::$instance->data->peek();
while (!in_array($peek, [ '-', false ]) && preg_match(self::SCOPE_REGEX, $peek)) { while (!in_array($peek, [ '-', false ]) && preg_match(self::SCOPE_REGEX, $peek)) {
@ -173,10 +170,9 @@ class Parser {
} }
$token = self::$instance->data->consume(); $token = self::$instance->data->consume();
if ($token === false) { if ($token === false || !preg_match('/^(?:[A-Za-z0-9-_]+|\*)(?:\.(?:[A-Za-z0-9-+_]+|\*))*$/S', $token)) {
die('Unexpected eod'); // TODO: Take the effort to make this more descriptive
} elseif (!preg_match('/^(?:[A-Za-z0-9-_]+|\*)(?:\.(?:[A-Za-z0-9-+_]+|\*))*$/S', $token)) { throw new Exception('valid scope syntax', $token);
die('Invalid scope');
} }
$segments = explode('.', $token); $segments = explode('.', $token);

Loading…
Cancel
Save