Browse Source

Minor cleanup

main
Dustin Wilson 3 years ago
parent
commit
676971850e
  1. 16
      lib/Grammar.php
  2. 51
      lib/Grammar/Registry.php
  3. 4
      lib/Tokenizer.php

16
lib/Grammar.php

@ -11,8 +11,14 @@ use dW\Lit\Grammar\CaptureList,
dW\Lit\Grammar\InjectionList,
dW\Lit\Grammar\Pattern,
dW\Lit\Grammar\PatternList,
dW\Lit\Grammar\Registry,
dW\Lit\Grammar\Repository;
/**
* When the input data is read line-by-line a grammar object is used as a set of
* instructions as to what to match and highlight
*/
class Grammar {
use FauxReadOnly;
@ -25,7 +31,7 @@ class Grammar {
protected string $_scopeName;
public function __construct(string $scopeName, PatternList $patterns, ?string $name = null, ?string $contentRegex = null, ?string $firstLineMatch = null, ?InjectionList $injections = null, ?Repository $repository = null) {
public function __construct(string $scopeName, PatternList $patterns, ?string $name = null, ?string $contentRegex = null, ?string $firstLineMatch = null, ?InjectionList $injections = null, ?Repository $repository = null, bool $register = false) {
$this->_name = $name;
$this->_scopeName = $scopeName;
$this->_patterns = $patterns;
@ -33,10 +39,14 @@ class Grammar {
$this->_firstLineMatch = $firstLineMatch;
$this->_injections = $injections;
$this->_repository = $repository;
if ($register) {
Registry::set($scopeName, $this);
}
}
/** Parses an Atom JSON grammar and converts to a Grammar object */
public static function fromJSON(string $jsonPath): self {
public static function fromJSON(string $jsonPath, bool $register = false): self {
if (!is_file($jsonPath)) {
throw new Exception(Exception::JSON_INVALID_FILE, $jsonPath);
}
@ -89,7 +99,7 @@ class Grammar {
}
}
return new self($scopeName, $patterns, $name, $contentRegex, $firstLineMatch, $injections, $repository);
return new self($scopeName, $patterns, $name, $contentRegex, $firstLineMatch, $injections, $repository, $register);
}

51
lib/Grammar/Registry.php

@ -5,19 +5,20 @@
declare(strict_types=1);
namespace dW\Lit\Grammar;
use dW\Lit\Grammar;
/** Static storage for grammars; a map of scope and a Grammar object */
class Registry {
protected static array $grammars = [];
/** Static storage for grammars; a map of a scope string and a Grammar object */
class Registry implements \IteratorAggregate {
protected static array $storage = [];
public static function clear(): bool {
self::$grammars = [];
self::$storage = [];
return true;
}
public static function delete(string $scopeName): bool {
try {
unset(self::$grammars[$scopeName]);
unset(self::$storage[$scopeName]);
} catch (\Exception $e) {
return false;
}
@ -25,13 +26,43 @@ class Registry {
return true;
}
public static function get(string $scopeName): array|bool {
foreach (self::$grammars as $grammar) {
if ($grammar['scopeName'] === $scopeName) {
return $grammar;
}
public static function get(string $scopeName): Grammar|bool {
if (array_key_exists($scopeName, self::$storage)) {
return self::$storage[$scopeName];
}
return false;
}
public function getIterator(): \Traversable {
foreach (self::$storage as $scopeName => $grammar) {
yield $scopeName => $grammar;
}
}
public static function has(string $scopeName): bool {
return (array_key_exists($scopeName, self::$storage));
}
public static function keys(): \Traversable {
foreach (self::$storage as $scopeName => $_) {
yield $scopeName;
}
}
public static function set(string $scopeName, Grammar $grammar): bool {
try {
self::$storage[$scopeName] = $grammar;
} catch (\Exception $e) {
return false;
}
return true;
}
public function values(): \Traversable {
foreach (self::$storage as $grammar) {
yield $grammar;
}
}
}

4
lib/Tokenizer.php

@ -9,13 +9,13 @@ namespace dW\Lit;
class Tokenizer {
protected \Generator $data;
public function __constructor(\Generator $data) {
public function __construct(\Generator $data) {
$this->data = $data;
}
public function tokenize(): \Generator {
foreach ($this->data as $lineNumber => $line) {
yield $lineNumber => $line;
}
}
}
Loading…
Cancel
Save