Browse Source

Break all the things!

main
Dustin Wilson 3 years ago
parent
commit
53151a674c
  1. 16
      lib/Grammar.php
  2. 8
      lib/Grammar/BaseReference.php
  3. 4
      lib/Grammar/CaptureList.php
  4. 5
      lib/Grammar/GrammarReference.php
  5. 12
      lib/Grammar/ImmutableList.php
  6. 11
      lib/Grammar/Pattern.php
  7. 22
      lib/Grammar/Reference.php
  8. 20
      lib/Grammar/RepositoryReference.php
  9. 3
      lib/Grammar/SelfReference.php
  10. 7
      lib/GrammarRegistry.php
  11. 12
      lib/Tokenizer.php

16
lib/Grammar.php

@ -52,7 +52,20 @@ class Grammar {
/** Clones the supplied grammar with this grammar set as its owner grammar */
public function adoptGrammar(self $grammar): self {
return new self($grammar->scopeName, $grammar->contentScopeName, $grammar->patterns, $grammar->name, $grammar->contentRegex, $grammar->firstLineMatch, $grammar->injections, $grammar->repository, $this);
$new = clone $this;
if ($new->_patterns !== null) {
$new->_patterns = $new->_patterns->withOwnerGrammar($new);
}
if ($new->_injections !== null) {
$new->_injections = $new->_injections->withOwnerGrammar($new);
}
if ($new->_repository !== null) {
$new->_repository = $new->_repository->withOwnerGrammar($new);
}
return GrammarRegistry::cacheChild($new);
}
@ -141,6 +154,7 @@ class Grammar {
}
$p = [
'ownerGrammar' => $this,
'name' => null,
'contentName' => null,
'begin' => null,

8
lib/Grammar/BaseReference.php

@ -11,21 +11,15 @@ use dW\Lit\Grammar;
* Acts as a sort of lazy weak reference for a base grammar in a grammar.
*/
class BaseReference extends Reference {
protected \WeakReference $grammar;
protected ?\WeakReference $object;
public function __construct(Grammar $grammar) {
$this->grammar = \WeakReference::create($grammar);
}
public function get(): Grammar {
if ($this->object !== null) {
return $this->object->get();
}
$grammar = $this->grammar->get();
$grammar = $this->_ownerGrammar->get();
do {
$result = $grammar;
} while ($grammar = $grammar->ownerGrammar);

4
lib/Grammar/CaptureList.php

@ -14,13 +14,13 @@ class CaptureList extends ImmutableList {
throw new Exception(Exception::LIST_INVALID_TYPE, 'Integer', 'supplied array index', gettype($k));
}
if (!$v instanceof Pattern && !$v instanceof PatternList && !$v instanceof Reference && !$v instanceof \WeakReference) {
if (!$v instanceof Pattern && !$v instanceof PatternList && !$v instanceof Reference) {
$type = gettype($v);
if ($type === 'object') {
$type = get_class($v);
}
throw new Exception(Exception::LIST_INVALID_TYPE, __NAMESPACE__.'\Pattern, '.__NAMESPACE__.'\PatternList, '.__NAMESPACE__.'\Reference, or \WeakReference', 'supplied array value', $type);
throw new Exception(Exception::LIST_INVALID_TYPE, __NAMESPACE__.'\Pattern, '.__NAMESPACE__.'\PatternList, '.__NAMESPACE__.'\Reference', 'supplied array value', $type);
}
}

5
lib/Grammar/GrammarReference.php

@ -14,13 +14,12 @@ use dW\Lit\Grammar,
*/
class GrammarReference extends Reference {
protected ?Grammar $object = null;
protected \WeakReference $ownerGrammar;
protected string $_scopeName;
public function __construct(string $scopeName, Grammar $ownerGrammar) {
$this->ownerGrammar = \WeakReference::create($ownerGrammar);
$this->_scopeName = $scopeName;
parent::__construct($ownerGrammar);
}
@ -37,7 +36,7 @@ class GrammarReference extends Reference {
return null;
}
$this->object = $this->ownerGrammar->get()->adoptGrammar($grammar);
$this->object = $this->_ownerGrammar->get()->adoptGrammar($grammar);
return $this->object;
}
}

12
lib/Grammar/ImmutableList.php

@ -5,6 +5,7 @@
declare(strict_types=1);
namespace dW\Lit\Grammar;
use dW\Lit\Grammar;
abstract class ImmutableList implements \ArrayAccess, \Countable, \Iterator {
protected int $count = 0;
@ -17,6 +18,17 @@ abstract class ImmutableList implements \ArrayAccess, \Countable, \Iterator {
$this->count = count($this->storage);
}
// Used when adopting to change the $ownerGrammar property of items in the
// list.
public function withOwnerGrammar(Grammar $ownerGrammar): self {
$new = clone $this;
foreach ($new->storage as &$s) {
$s = $s->withOwnerGrammar($ownerGrammar);
}
return $new;
}
public function count(): int {
return $this->count;

11
lib/Grammar/Pattern.php

@ -19,10 +19,11 @@ class Pattern extends Rule {
protected ?CaptureList $_endCaptures;
protected ?string $_match;
protected ?string $_name;
protected \WeakReference $_ownerGrammar;
protected ?PatternList $_patterns;
public function __construct(?string $name = null, ?string $contentName = null, ?string $begin = null, ?string $end = null, ?string $match = null, ?PatternList $patterns = null, ?CaptureList $captures = null, ?CaptureList $beginCaptures = null, ?CaptureList $endCaptures = null, bool $applyEndPatternLast = false) {
public function __construct(Grammar $ownerGrammar, ?string $name = null, ?string $contentName = null, ?string $begin = null, ?string $end = null, ?string $match = null, ?PatternList $patterns = null, ?CaptureList $captures = null, ?CaptureList $beginCaptures = null, ?CaptureList $endCaptures = null, bool $applyEndPatternLast = false) {
$this->_name = $name;
$this->_contentName = $contentName;
$this->_begin = $begin;
@ -33,5 +34,13 @@ class Pattern extends Rule {
$this->_beginCaptures = $beginCaptures;
$this->_endCaptures = $endCaptures;
$this->_applyEndPatternLast = $applyEndPatternLast;
$this->_ownerGrammar = ($ownerGrammar === null) ? null : \WeakReference::create($ownerGrammar);
}
// Used when adopting to change the $ownerGrammar property.
public function withOwnerGrammar(Grammar $ownerGrammar): self {
$new = clone $this;
$new->_ownerGrammar = \WeakReference::create($ownerGrammar);
return $new;
}
}

22
lib/Grammar/Reference.php

@ -5,6 +5,26 @@
declare(strict_types=1);
namespace dW\Lit\Grammar;
use dW\Lit\{
FauxReadOnly,
Grammar
};
/** Acts as a catch-all type for references */
abstract class Reference extends Rule {}
abstract class Reference extends Rule {
use FauxReadOnly;
protected \WeakReference $_ownerGrammar;
public function __construct(Grammar $ownerGrammar) {
$this->_ownerGrammar = \WeakReference::create($ownerGrammar);
}
// Used when adopting to change the $ownerGrammar property.
public function withOwnerGrammar(Grammar $ownerGrammar): self {
$new = clone $this;
$new->_ownerGrammar = \WeakReference::create($ownerGrammar);
return $new;
}
}

20
lib/Grammar/RepositoryReference.php

@ -12,28 +12,28 @@ use dW\Lit\Grammar;
* Acts as a sort of lazy reference for repository items in grammars.
*/
class RepositoryReference extends Reference {
protected ?Grammar $grammar;
protected string $_name;
protected PatternList|Pattern|null|false $object = null;
public function __construct(string $name, Grammar $grammar) {
public function __construct(string $name, Grammar $ownerGrammar) {
$this->_name = $name;
// Using a \WeakReference here doesn't work for some reason even though
// the grammar would still be stored in memory. Cloning works because grammars
// are immutable, so the referenced object never will change.
$this->grammar = clone $grammar;
parent::__construct($ownerGrammar);
}
public function get(): PatternList|Pattern|null {
if ($this->object !== null) {
return $this->object;
} elseif ($this->object === false) {
if ($this->object === false) {
return null;
} elseif ($this->object !== null) {
return $this->object;
}
$grammar = $this->_ownerGrammar->get();
if (!isset($grammar->repository)) {
die(var_export($grammar));
}
$grammar = $this->grammar;
if (!isset($grammar->repository[$this->name])) {
$this->object = false;
return null;

3
lib/Grammar/SelfReference.php

@ -15,8 +15,9 @@ class SelfReference extends Reference {
protected ?Grammar $grammar;
public function __construct(Grammar $grammar) {
public function __construct(Grammar $grammar, Grammar $ownerGrammar) {
$this->grammar = $grammar;
parent::__construct($ownerGrammar);
}
public function __destruct() {

7
lib/GrammarRegistry.php

@ -10,9 +10,16 @@ namespace dW\Lit;
/** Static storage for grammars; a map of a scope string and a Grammar object */
class GrammarRegistry implements \IteratorAggregate {
protected static array $storage = [];
protected static array $childStorage = [];
public static function cacheChild(Grammar $grammar): Grammar {
self::$childStorage[] = $grammar;
return $grammar;
}
public static function clear(): bool {
self::$storage = [];
self::$childStorage = [];
return true;
}

12
lib/Tokenizer.php

@ -51,16 +51,16 @@ class Tokenizer {
if ($regex !== null && $match = $this->getMatch($regex, $line)) {
$scopeStack = $this->scopeStack;
if ($this->rule->name !== null) {
$scopeStack[] = $this->rule->name;
if ($rule->name !== null) {
$scopeStack[] = $rule->name;
}
if ($this->rule->contentName !== null) {
$scopeStack[] = $this->rule->contentName;
if ($rule->contentName !== null) {
$scopeStack[] = $rule->contentName;
}
$results[] = [
'scopeStack' => $scopeStack,
'matches' => $matches
'matches' => $match
];
if ($rule->begin !== null) {
@ -87,7 +87,7 @@ class Tokenizer {
}
protected function getMatch(string $regex, string $line): array {
protected function getMatch(string $regex, string $line): ?array {
if (preg_match($regex, $line, $match, PREG_OFFSET_CAPTURE) !== 1) {
return null;
}

Loading…
Cancel
Save