Dustin Wilson
3 years ago
8 changed files with 54 additions and 197 deletions
@ -1,29 +0,0 @@ |
|||
<?php |
|||
/** @license MIT |
|||
* Copyright 2021 Dustin Wilson et al. |
|||
* See LICENSE file for details */ |
|||
|
|||
declare(strict_types=1); |
|||
namespace dW\Lit\Grammar; |
|||
|
|||
class CaptureList extends ImmutableList { |
|||
public function __construct(array $array) { |
|||
// This shit is here because PHP doesn't have array types or generics :) |
|||
foreach ($array as $k => $v) { |
|||
if (!is_int($k)) { |
|||
throw new Exception(Exception::LIST_INVALID_TYPE, 'Integer', 'supplied array index', gettype($k)); |
|||
} |
|||
|
|||
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', 'supplied array value', $type); |
|||
} |
|||
} |
|||
|
|||
$this->storage = $array; |
|||
} |
|||
} |
@ -1,83 +0,0 @@ |
|||
<?php |
|||
/** @license MIT |
|||
* Copyright 2017 , Dustin Wilson, J. King et al. |
|||
* See LICENSE and AUTHORS files for details */ |
|||
|
|||
declare(strict_types=1); |
|||
namespace dW\Lit\Grammar; |
|||
use dW\Lit\Grammar; |
|||
|
|||
abstract class ImmutableList implements \ArrayAccess, \Countable, \Iterator { |
|||
protected int $count = 0; |
|||
protected int|string|null $position; |
|||
protected array $storage = []; |
|||
|
|||
|
|||
public function __construct(...$values) { |
|||
$this->storage = $values; |
|||
$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; |
|||
} |
|||
|
|||
public function current() { |
|||
return current($this->storage); |
|||
} |
|||
|
|||
public function getIterator(): array { |
|||
return $this->storage; |
|||
} |
|||
|
|||
public function key(){ |
|||
$this->position = key($this->storage); |
|||
return $this->position; |
|||
} |
|||
|
|||
public function next() { |
|||
next($this->storage); |
|||
$this->position = key($this->storage); |
|||
} |
|||
|
|||
public function offsetExists($offset) { |
|||
return isset($this->storage[$offset]); |
|||
} |
|||
|
|||
public function offsetGet($offset) { |
|||
if (!isset($this->storage[$offset])) { |
|||
throw new Exception(Exception::LIST_INVALID_INDEX, __CLASS__, $offset); |
|||
} |
|||
|
|||
return $this->storage[$offset]; |
|||
} |
|||
|
|||
public function offsetSet($offset, $value) { |
|||
throw new Exception(Exception::LIST_IMMUTABLE, __CLASS__); |
|||
} |
|||
|
|||
public function offsetUnset($offset) { |
|||
throw new Exception(Exception::LIST_IMMUTABLE, __CLASS__); |
|||
} |
|||
|
|||
public function rewind() { |
|||
reset($this->storage); |
|||
$this->position = key($this->storage); |
|||
} |
|||
|
|||
public function valid() { |
|||
return $this->offsetExists($this->position); |
|||
} |
|||
} |
@ -1,14 +0,0 @@ |
|||
<?php |
|||
/** @license MIT |
|||
* Copyright 2021 Dustin Wilson et al. |
|||
* See LICENSE file for details */ |
|||
|
|||
declare(strict_types=1); |
|||
namespace dW\Lit\Grammar; |
|||
|
|||
/** |
|||
* An immutable list of injection pattern rules which allows for creation of a |
|||
* new grammar; instead of applying to an entire file it's instead applied to a |
|||
* specific scope selector. |
|||
*/ |
|||
class InjectionList extends NamedPatternList {} |
@ -1,13 +0,0 @@ |
|||
<?php |
|||
/** @license MIT |
|||
* Copyright 2021 Dustin Wilson et al. |
|||
* See LICENSE file for details */ |
|||
|
|||
declare(strict_types=1); |
|||
namespace dW\Lit\Grammar; |
|||
|
|||
/** |
|||
* An immutable list of rules which can be included from other places in the |
|||
* grammar; The key is the name of the rule and the value is the actual rule. |
|||
*/ |
|||
class Repository extends NamedPatternList {} |
Loading…
Reference in new issue