Skip to content

Commit

Permalink
Add missing multistate lexer constructor type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Aug 1, 2024
1 parent bd6a3b1 commit 61ecbce
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/Exception/EndlessRecursionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class EndlessRecursionException extends UnexpectedStateException
{
public static function fromState($state, ReadableInterface $src, ?TokenInterface $tok, ?\Throwable $e = null): self
public static function fromState(mixed $state, ReadableInterface $src, ?TokenInterface $tok, ?\Throwable $e = null): self
{
$message = \vsprintf('An unsolvable infinite lexer state transitions was found at %s', [
$state,
Expand Down
5 changes: 1 addition & 4 deletions src/Exception/UnexpectedStateException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ public static function fromEmptyStates(ReadableInterface $src, ?\Throwable $e =
return new static($message, $src, null, $e);
}

/**
* @param string|int $state
*/
public static function fromState($state, ReadableInterface $src, ?TokenInterface $tok, ?\Throwable $e = null): self
public static function fromState(string|int $state, ReadableInterface $src, ?TokenInterface $tok, ?\Throwable $e = null): self
{
$message = \sprintf('Unrecognized token state #%s', $state);

Expand Down
2 changes: 1 addition & 1 deletion src/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ public function remove(string ...$tokens): self
* starting the lexical analysis and indicates problems in the
* analyzed source
*/
public function lex($source, int $offset = 0): iterable
public function lex(mixed $source, int $offset = 0): iterable
{
try {
$source = $this->sources->create($source);
Expand Down
19 changes: 7 additions & 12 deletions src/Multistate.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Multistate implements PositionalLexerInterface
/**
* @var array-key|null
*/
private $state;
private string|int|null $state;

/**
* @var array<non-empty-string|int<0, max>, array<non-empty-string, non-empty-string|int<0, max>>>
Expand All @@ -52,7 +52,7 @@ class Multistate implements PositionalLexerInterface
public function __construct(
array $states,
array $transitions = [],
$state = null,
string|int|null $state = null,
?HandlerInterface $onEndOfInput = null,
?SourceFactoryInterface $sources = null
) {
Expand All @@ -75,10 +75,8 @@ public function __construct(
*
* @param array-key|null $state
*/
public function startsWith($state): self
public function startsWith(int|string|null $state): self
{
assert(\is_string($state) || \is_int($state) || $state === null);

$this->state = $state;

return $this;
Expand All @@ -90,11 +88,8 @@ public function startsWith($state): self
* @param array-key $name
* @param array<non-empty-string, non-empty-string>|PositionalLexerInterface $data
*/
public function setState($name, $data): self
public function setState(string|int $name, array|PositionalLexerInterface $data): self
{
assert(\is_string($name) || \is_int($name));
assert(\is_array($data) || $data instanceof PositionalLexerInterface);

if (\is_array($data)) {
$data = new Lexer($data);
}
Expand All @@ -109,7 +104,7 @@ public function setState($name, $data): self
*
* @param array-key $name
*/
public function removeState($name): self
public function removeState(string|int $name): self
{
unset($this->states[$name]);

Expand All @@ -123,7 +118,7 @@ public function removeState($name): self
* @param array-key $in
* @param array-key $then
*/
public function when(string $token, $in, $then): self
public function when(string $token, string|int $in, string|int $then): self
{
$this->transitions[$in][$token] = $then;

Expand Down Expand Up @@ -151,7 +146,7 @@ public function when(string $token, $in, $then): self
*
* @psalm-suppress TypeDoesNotContainType
*/
public function lex($source, int $offset = 0): iterable
public function lex(mixed $source, int $offset = 0): iterable
{
try {
$source = $this->sources->create($source);
Expand Down
26 changes: 8 additions & 18 deletions src/Token/Composite.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Composite extends Token implements CompositeTokenInterface
* @param int<0, max> $offset
* @param array<int, TokenInterface> $children
*/
public function __construct($name, string $value, int $offset, array $children)
public function __construct(int|string $name, string $value, int $offset, array $children)
{
$this->children = $children;

Expand Down Expand Up @@ -49,48 +49,38 @@ public function getIterator(): \Traversable
return new \ArrayIterator($this->children);
}

/**
* @param int $offset
*/
public function offsetExists($offset): bool
public function offsetExists(mixed $offset): bool
{
\assert(\is_int($offset));

return isset($this->children[$offset]);
}

/**
* @param int $offset
*/
public function offsetGet($offset): ?TokenInterface
public function offsetGet(mixed $offset): ?TokenInterface
{
\assert(\is_int($offset));

return $this->children[$offset] ?? null;
}

/**
* @param int $offset
* @param TokenInterface $value
*/
public function offsetSet($offset, $value): void
public function offsetSet(mixed $offset, mixed $value): void
{
\assert(\is_int($offset));
\assert($value instanceof TokenInterface);

$this->children[$offset] = $value;
}

/**
* @param int $offset
*/
public function offsetUnset($offset): void
public function offsetUnset(mixed $offset): void
{
\assert(\is_int($offset));

unset($this->children[$offset]);
}

/**
* @return int<0, max>
*/
public function count(): int
{
return \count($this->children);
Expand Down
4 changes: 2 additions & 2 deletions src/Token/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Token extends BaseToken
/**
* @var array-key
*/
private $name;
private string|int $name;

private string $value;

Expand All @@ -27,7 +27,7 @@ class Token extends BaseToken
* @param array-key $name
* @param int<0, max> $offset
*/
public function __construct($name, string $value, int $offset = 0)
public function __construct(string|int $name, string $value, int $offset = 0)
{
if ($name === '') {
$name = self::$anonymousId++;
Expand Down

0 comments on commit 61ecbce

Please sign in to comment.