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 92b1a88 commit c12628b
Show file tree
Hide file tree
Showing 13 changed files with 19 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ interface BuilderInterface
/**
* @param NodeInterface|TokenInterface|iterable<NodeInterface|TokenInterface> $result
*/
public function build(Context $context, $result);
public function build(Context $context, mixed $result);
}
2 changes: 1 addition & 1 deletion src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class Context implements ContextInterface
* @param array-key $state
* @param array<non-empty-string, mixed> $options
*/
public function __construct(BufferInterface $buffer, ReadableInterface $source, $state, array $options)
public function __construct(BufferInterface $buffer, ReadableInterface $source, int|string $state, array $options)
{
$this->state = $state;
$this->source = $source;
Expand Down
2 changes: 1 addition & 1 deletion src/Context/ContextOptionsProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getOptions(): array;
*
* @return TArgDefault|mixed
*/
public function getOption(string $name, $default = null);
public function getOption(string $name, mixed $default = null): mixed;

/**
* Determine if the given option value exists.
Expand Down
2 changes: 1 addition & 1 deletion src/Context/ContextOptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getOptions(): array
return $this->options;
}

public function getOption(string $name, $default = null)
public function getOption(string $name, mixed $default = null): mixed
{
return $this->options[$name] ?? $default;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Context/TreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function __construct(iterable $reducers)
$this->reducers = $reducers;
}

public function build(Context $context, $result)
public function build(Context $context, mixed $result): mixed
{
if (isset($this->reducers[$context->state])) {
return ($this->reducers[$context->state])($context, $result);
Expand Down
4 changes: 1 addition & 3 deletions src/Grammar/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,8 @@ private function read(\Closure $rules): \Generator
*
* @return array-key
*/
public function add(RuleInterface $rule, $id = null)
public function add(RuleInterface $rule, string|int|null $id = null): int|string
{
\assert($id === null || \is_int($id) || \is_string($id));

if ($id === null) {
$this->grammar[] = $rule;

Expand Down
9 changes: 2 additions & 7 deletions src/Grammar/Lexeme.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@
*/
class Lexeme extends Terminal
{
/**
* @var non-empty-string|int
*
* @readonly
*/
public $token;
public readonly string|int $token;

/**
* @param non-empty-string|int $token
*/
public function __construct($token, bool $keep = true)
public function __construct(string|int $token, bool $keep = true)
{
parent::__construct($keep);

Expand Down
6 changes: 2 additions & 4 deletions src/Grammar/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ class Optional extends Production
/**
* @var array-key
*
* @readonly
*
* @psalm-readonly-allow-private-mutation
*/
public $rule;
public readonly string|int $rule;

/**
* @param array-key $rule
*/
public function __construct($rule)
public function __construct(string|int $rule)
{
$this->rule = $rule;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Grammar/Production.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class Production extends Rule implements ProductionInterface
*
* @return list<NodeInterface|TokenInterface>
*/
protected function mergeWith(array $children, $result): array
protected function mergeWith(array $children, mixed $result): array
{
if (\is_array($result)) {
return \array_merge($children, $result);
Expand Down
6 changes: 3 additions & 3 deletions src/Grammar/Repetition.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class Repetition extends Production
*
* @psalm-readonly-allow-private-mutation
*/
public $lte;
public int|float $lte;

/**
* @var array-key
Expand All @@ -36,14 +36,14 @@ class Repetition extends Production
*
* @psalm-readonly-allow-private-mutation
*/
public $rule;
public string|int $rule;

/**
* @param array-key $rule
* @param int<0, max> $gte
* @param int<0, max>|float $lte
*/
public function __construct($rule, int $gte = 0, $lte = \INF)
public function __construct(string|int $rule, int $gte = 0, int|float $lte = \INF)
{
\assert($lte >= $gte, 'Min repetitions count must be greater or equal than max repetitions');

Expand Down
40 changes: 3 additions & 37 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ final class Parser implements ConfigurableParserInterface, ParserConfigsInterfac
*
* @psalm-readonly-allow-private-mutation
*/
private $initial;
private string|int $initial;

/**
* Array of transition rules for the parser.
Expand Down Expand Up @@ -190,7 +190,7 @@ private static function bootGrammar(iterable $grammar): array
*
* @return array-key
*/
private static function bootInitialRule(array $options, array $grammar)
private static function bootInitialRule(array $options, array $grammar): int|string
{
$initial = $options[self::CONFIG_INITIAL_RULE] ?? null;

Expand All @@ -207,40 +207,6 @@ private static function bootInitialRule(array $options, array $grammar)
return $result;
}

/**
* Sets an initial state (initial rule identifier) of the parser.
*
* @param array-key $initial
*
* @deprecated since phplrt 3.4 and will be removed in 4.0
*/
public function startsAt($initial): self
{
trigger_deprecation('phplrt/parser', '3.4', <<<'MSG'
Using "%s::startsAt(array-key)" is deprecated, please use "%1$s::__construct()" instead.
MSG, self::class);

$this->initial = $initial;

return $this;
}

/**
* Sets an abstract syntax tree builder.
*
* @deprecated since phplrt 3.4 and will be removed in 4.0
*/
public function buildUsing(BuilderInterface $builder): self
{
trigger_deprecation('phplrt/parser', '3.4', <<<'MSG'
Using "%s::buildUsing(BuilderInterface)" is deprecated, please use "%1$s::__construct()" instead.
MSG, self::class);

$this->builder = $builder;

return $this;
}

/**
* Parses sources into an abstract source tree (AST) or list of AST nodes.
*
Expand All @@ -256,7 +222,7 @@ public function buildUsing(BuilderInterface $builder): self
* starting the parsing and indicates problems in the analyzed
* source
*/
public function parse($source, array $options = []): iterable
public function parse(mixed $source, array $options = []): iterable
{
if ($this->rules === []) {
return [];
Expand Down
4 changes: 1 addition & 3 deletions tests/Functional/SimpleSumParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Phplrt\Parser\Tests\Functional;

use Phplrt\Contracts\Exception\RuntimeExceptionInterface;
use Phplrt\Lexer\Lexer;
use Phplrt\Lexer\Token\Token;
use Phplrt\Parser\BuilderInterface;
Expand All @@ -14,11 +13,10 @@
use Phplrt\Parser\Grammar\Repetition;
use Phplrt\Parser\Parser;
use Phplrt\Parser\Tests\Functional\Stub\AstNode;
use PHPUnit\Framework\ExpectationFailedException;

class SimpleSumParserTest extends TestCase implements BuilderInterface
{
public function build(Context $context, $result)
public function build(Context $context, mixed $result): mixed
{
if (\is_int($context->getState())) {
return $result;
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/Stub/AstNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function getIterator(): \Traversable
return new \ArrayIterator($this->children);
}

public function __set($name, $value)
public function __set(string $name, mixed $value): void
{
$this->children[$name] = $value;
}
Expand Down

0 comments on commit c12628b

Please sign in to comment.