You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.0 KiB
42 lines
1.0 KiB
<?php
|
|
/** @license MIT
|
|
* Copyright 2021 Dustin Wilson et al.
|
|
* See LICENSE file for details */
|
|
|
|
declare(strict_types=1);
|
|
namespace dW\Lit\Grammar;
|
|
use dW\Lit\Grammar,
|
|
dW\Lit\GrammarRegistry;
|
|
|
|
|
|
/**
|
|
* Acts as a sort of lazy reference for entire grammars in grammars.
|
|
*/
|
|
class GrammarReference extends Reference {
|
|
protected ?Grammar $object = null;
|
|
protected string $_scopeName;
|
|
|
|
|
|
public function __construct(string $scopeName, Grammar $ownerGrammar) {
|
|
$this->_scopeName = $scopeName;
|
|
parent::__construct($ownerGrammar);
|
|
}
|
|
|
|
|
|
public function get(): Grammar {
|
|
if ($this->object !== null) {
|
|
return $this->object;
|
|
} elseif ($this->object === false) {
|
|
return null;
|
|
}
|
|
|
|
$grammar = GrammarRegistry::get($this->_scopeName);
|
|
if ($grammar === null) {
|
|
$this->object = false;
|
|
return null;
|
|
}
|
|
|
|
$this->object = $this->_ownerGrammar->get()->adoptGrammar($grammar);
|
|
return $this->object;
|
|
}
|
|
}
|