Skip to content

Commit

Permalink
Add PHP 8.4 (alpha build) tests support
Browse files Browse the repository at this point in the history
  • Loading branch information
SerafimArts committed Jul 28, 2024
1 parent 5398b2d commit 80e6472
Show file tree
Hide file tree
Showing 21 changed files with 51 additions and 52 deletions.
14 changes: 8 additions & 6 deletions src/ConfigurableParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,25 @@

/**
* @template TNode of object
*
* @template-extends ParserInterface<TNode>
*/
interface ConfigurableParserInterface extends ParserInterface
{
/**
* Parses sources into an abstract source tree (AST) or list of AST nodes.
*
* @param array<non-empty-string, mixed> $options list of additional
* runtime options for the parser (parsing context)
* @param array<non-empty-string, mixed> $options List of additional
* runtime options for the parser (parsing context).
*
* @return iterable<array-key, TNode>
* @throws ParserExceptionInterface an error occurs before source processing
*
* @throws ParserExceptionInterface An error occurs before source processing
* starts, when the given source cannot be recognized or if the
* parser settings contain errors
* @throws ParserRuntimeExceptionInterface an exception that occurs after
* parser settings contain errors.
* @throws ParserRuntimeExceptionInterface An exception that occurs after
* starting the parsing and indicates problems in the analyzed
* source
* source.
*/
public function parse(ReadableInterface $source, array $options = []): iterable;
}
3 changes: 1 addition & 2 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Phplrt\Parser;

use Phplrt\Buffer\BufferInterface;
use Phplrt\Contracts\Ast\NodeInterface;
use Phplrt\Contracts\Lexer\TokenInterface;
use Phplrt\Contracts\Source\ReadableInterface;
use Phplrt\Parser\Context\ContextOptionsTrait;
Expand Down Expand Up @@ -74,7 +75,6 @@ class Context implements ContextInterface
* Contains information about the processed source.
*
* @readonly marked as readonly since phplrt 3.4 and will be readonly since 4.0
*
* @psalm-readonly-allow-private-mutation
*/
public ReadableInterface $source;
Expand All @@ -83,7 +83,6 @@ class Context implements ContextInterface
* Contains a buffer of tokens that were collected from lexical analysis.
*
* @readonly marked as readonly since phplrt 3.4 and will be readonly since 4.0
*
* @psalm-readonly-allow-private-mutation
*/
public BufferInterface $buffer;
Expand Down
3 changes: 1 addition & 2 deletions src/Context/ContextOptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
namespace Phplrt\Parser\Context;

/**
* @psalm-require-implements ContextOptionsProviderInterface
*
* @mixin ContextOptionsProviderInterface
* @psalm-require-implements ContextOptionsProviderInterface
*/
trait ContextOptionsTrait
{
Expand Down
2 changes: 1 addition & 1 deletion src/Context/TreeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Phplrt\Parser\Context;

/**
* @internal this is an internal library class, please do not use it in your code
* @internal This is an internal library class, please do not use it in your code.
* @psalm-internal Phplrt\Parser
*/
class TreeBuilder implements BuilderInterface
Expand Down
1 change: 0 additions & 1 deletion src/Environment/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ final class Factory implements SelectorInterface
{
/**
* @var list<SelectorInterface>
*
* @readonly
*/
private array $selectors;
Expand Down
1 change: 0 additions & 1 deletion src/Environment/XdebugSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ final class XdebugSelector implements SelectorInterface

/**
* @var int<0, max>
*
* @readonly
*/
private int $expectedRecursionDepth;
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/ParserRuntimeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ final public function __construct(
string $message,
ReadableInterface $src,
?TokenInterface $tok,
?\Throwable $prev = null
\Throwable $prev = null
) {
parent::__construct($message, 0, $prev);

Expand Down
2 changes: 1 addition & 1 deletion src/Exception/UnexpectedTokenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class UnexpectedTokenException extends UnrecognizedTokenException
public static function fromToken(
ReadableInterface $src,
TokenInterface $tok,
?\Throwable $prev = null,
\Throwable $prev = null,
array $expected = []
): self {
switch (\count($expected)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/UnrecognizedTokenException.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class UnrecognizedTokenException extends ParserRuntimeException
{
public static function fromToken(ReadableInterface $src, TokenInterface $tok, ?\Throwable $prev = null): self
public static function fromToken(ReadableInterface $src, TokenInterface $tok, \Throwable $prev = null): self
{
$message = \sprintf('Syntax error, unrecognized %s', self::getTokenValue($tok));

Expand Down
1 change: 0 additions & 1 deletion src/Grammar/Alternation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class Alternation extends Production
* @var list<array-key>
*
* @readonly
*
* @psalm-readonly-allow-private-mutation
*/
public array $sequence = [];
Expand Down
5 changes: 2 additions & 3 deletions src/Grammar/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Builder implements \IteratorAggregate
/**
* @param \Closure():\Generator|null $generator
*/
public function __construct(?\Closure $generator = null)
public function __construct(\Closure $generator = null)
{
if ($generator !== null) {
$this->extend($generator);
Expand Down Expand Up @@ -48,6 +48,7 @@ public function extend(\Closure $rules): self
case \is_string($key) && $value instanceof RuleInterface:
$generator->send($this->add($value, $key));
continue 2;

case $value instanceof RuleInterface:
$generator->send($this->add($value));
continue 2;
Expand Down Expand Up @@ -93,7 +94,6 @@ public function maybe(...$of): Optional

/**
* @param non-empty-list<array-key> $args
*
* @return array-key
*/
private function unwrap(array $args)
Expand Down Expand Up @@ -126,7 +126,6 @@ private function read(\Closure $rules): \Generator

/**
* @param array-key|null $id
*
* @return array-key
*/
public function add(RuleInterface $rule, $id = null)
Expand Down
1 change: 0 additions & 1 deletion src/Grammar/Concatenation.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class Concatenation extends Production
* @var list<array-key>
*
* @readonly
*
* @psalm-readonly-allow-private-mutation
*/
public array $sequence = [];
Expand Down
1 change: 0 additions & 1 deletion src/Grammar/Lexeme.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Lexeme extends Terminal
{
/**
* @var non-empty-string|int
*
* @readonly
*/
public $token;
Expand Down
1 change: 0 additions & 1 deletion src/Grammar/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class Optional extends Production
* @var array-key
*
* @readonly
*
* @psalm-readonly-allow-private-mutation
*/
public $rule;
Expand Down
1 change: 0 additions & 1 deletion src/Grammar/Production.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ abstract class Production extends Rule implements ProductionInterface
/**
* @param list<NodeInterface|TokenInterface> $children
* @param NodeInterface|TokenInterface|array $result
*
* @return list<NodeInterface|TokenInterface>
*/
protected function mergeWith(array $children, $result): array
Expand Down
7 changes: 2 additions & 5 deletions src/Grammar/Repetition.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ class Repetition extends Production
* @var int<0, max>
*
* @readonly
*
* @psalm-readonly-allow-private-mutation
*/
public int $gte;
Expand All @@ -24,7 +23,6 @@ class Repetition extends Production
* @var int<0, max>|float
*
* @readonly
*
* @psalm-readonly-allow-private-mutation
*/
public $lte;
Expand All @@ -33,7 +31,6 @@ class Repetition extends Production
* @var array-key
*
* @readonly
*
* @psalm-readonly-allow-private-mutation
*/
public $rule;
Expand All @@ -48,8 +45,8 @@ public function __construct($rule, int $gte = 0, $lte = \INF)
\assert($lte >= $gte, 'Min repetitions count must be greater or equal than max repetitions');

$this->rule = $rule;
$this->gte = $gte;
$this->lte = \is_infinite($lte) ? \INF : (int) $lte;
$this->gte = $gte;
$this->lte = \is_infinite($lte) ? \INF : (int) $lte;
}

public function getTerminals(array $rules): iterable
Expand Down
30 changes: 19 additions & 11 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Phplrt\Parser;

use Phplrt\Buffer\BufferInterface;
use Phplrt\Contracts\Ast\NodeInterface;
use Phplrt\Contracts\Exception\RuntimeExceptionInterface;
use Phplrt\Contracts\Lexer\LexerInterface;
use Phplrt\Contracts\Lexer\LexerRuntimeExceptionInterface;
Expand All @@ -23,6 +24,7 @@
use Phplrt\Parser\Grammar\ProductionInterface;
use Phplrt\Parser\Grammar\RuleInterface;
use Phplrt\Parser\Grammar\TerminalInterface;
use Phplrt\Source\File;
use Phplrt\Source\SourceFactory;

/**
Expand Down Expand Up @@ -59,6 +61,7 @@
* </code>
*
* @template TNode of object
*
* @template-implements ConfigurableParserInterface<TNode>
*/
final class Parser implements ConfigurableParserInterface, ParserConfigsInterface
Expand Down Expand Up @@ -104,7 +107,6 @@ final class Parser implements ConfigurableParserInterface, ParserConfigsInterfac
* The initial state (initial rule identifier) of the parser.
*
* @var array-key
*
* @psalm-readonly-allow-private-mutation
*/
private $initial;
Expand All @@ -115,16 +117,15 @@ final class Parser implements ConfigurableParserInterface, ParserConfigsInterfac
* @var array<array-key, RuleInterface>
*
* @readonly
*
* @psalm-readonly-allow-private-mutation
*/
private array $rules = [];

private ?Context $context = null;

/**
* @param iterable<array-key, RuleInterface> $grammar an iterable of the
* transition rules for the parser
* @param iterable<array-key, RuleInterface> $grammar An iterable of the
* transition rules for the parser.
* @param array<ParserConfigsInterface::CONFIG_*, mixed> $options
*/
public function __construct(
Expand Down Expand Up @@ -244,17 +245,18 @@ public function buildUsing(BuilderInterface $builder): self
/**
* Parses sources into an abstract source tree (AST) or list of AST nodes.
*
* @param mixed $source any source supported by the {@see SourceFactoryInterface::create()}
* @param array<non-empty-string, mixed> $options list of additional
* runtime options for the parser (parsing context)
* @param mixed $source Any source supported by the {@see SourceFactoryInterface::create()}.
* @param array<non-empty-string, mixed> $options List of additional
* runtime options for the parser (parsing context).
*
* @return iterable<array-key, TNode>
* @throws ParserExceptionInterface an error occurs before source processing
*
* @throws ParserExceptionInterface An error occurs before source processing
* starts, when the given source cannot be recognized or if the
* parser settings contain errors
* @throws ParserRuntimeExceptionInterface an exception that occurs after
* parser settings contain errors.
* @throws ParserRuntimeExceptionInterface An exception that occurs after
* starting the parsing and indicates problems in the analyzed
* source
* source.
*/
public function parse($source, array $options = []): iterable
{
Expand Down Expand Up @@ -360,6 +362,9 @@ private function lookupExpectedTokens(Context $context): array
return \array_values($tokens);
}

/**
* @return mixed
*/
private function next(Context $context)
{
if ($this->step !== null) {
Expand All @@ -371,6 +376,9 @@ private function next(Context $context)
return $this->runNextStep($context);
}

/**
* @return mixed
*/
private function runNextStep(Context $context)
{
$rule = $context->rule = $this->rules[$context->state];
Expand Down
1 change: 1 addition & 0 deletions src/polyfill.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace Phplrt\Grammar {

if (
!\class_exists(Alternation::class, false)
&& \class_exists(\Phplrt\Parser\Grammar\Alternation::class)
Expand Down
8 changes: 4 additions & 4 deletions tests/Functional/GrammarGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public function testHelpers(): void
});

$expected = [
0 => new Lexeme('digit'),
1 => new Lexeme('plus'),
2 => new Concatenation([1, 0]),
3 => new Repetition(2),
0 => new Lexeme('digit'),
1 => new Lexeme('plus'),
2 => new Concatenation([1, 0]),
3 => new Repetition(2),
'sum' => new Concatenation([0, 3]),
];

Expand Down
16 changes: 9 additions & 7 deletions tests/Functional/SimpleSumParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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 @@ -13,6 +14,7 @@
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
{
Expand Down Expand Up @@ -53,21 +55,21 @@ private function parseSum(string $expr): iterable
{
$lexer = new Lexer([
'T_WHITESPACE' => '\s+',
'T_DIGIT' => '\d+',
'T_PLUS' => '\+',
'T_DIGIT' => '\d+',
'T_PLUS' => '\+',
], ['T_WHITESPACE']);

$grammar = [
0 => new Lexeme('T_DIGIT'),
1 => new Lexeme('T_PLUS'),
2 => new Repetition('suffix'),
'sum' => new Concatenation([0, 2]),
0 => new Lexeme('T_DIGIT'),
1 => new Lexeme('T_PLUS'),
2 => new Repetition('suffix'),
'sum' => new Concatenation([0, 2]),
'suffix' => new Concatenation([1, 0]),
];

$parser = new Parser($lexer, $grammar, [
Parser::CONFIG_INITIAL_RULE => 'sum',
Parser::CONFIG_AST_BUILDER => $this,
Parser::CONFIG_AST_BUILDER => $this,
]);

return $parser->parse($expr);
Expand Down
1 change: 0 additions & 1 deletion tests/Functional/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public function __construct(array &$result)

/**
* @param NodeInterface|AstNode $node
*
* @return mixed|void|null
*/
public function enter(NodeInterface $node)
Expand Down

0 comments on commit 80e6472

Please sign in to comment.