Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
parameters:
level: 5
level: max
paths:
- src
- tests
Expand Down
20 changes: 20 additions & 0 deletions spec/Token/TokenSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use chrisjenkinson\StructuredDocumentParser\Token\NonexistentKeyException;
use chrisjenkinson\StructuredDocumentParser\Token\TokenPosition;
use InvalidArgumentException;
use PhpSpec\ObjectBehavior;

class TokenSpec extends ObjectBehavior
Expand Down Expand Up @@ -49,4 +50,23 @@ public function it_casts_to_a_string(): void
{
$this->__toString()->shouldReturn('Something (value)');
}

public function it_has_the_matched_text(): void
{
$this->getText()->shouldReturn('value');
}

public function it_requires_the_matched_text_to_be_a_string(): void
{
$this->beConstructedWith('Something', ['all' => 13], new TokenPosition(3, 7));

$this->shouldThrow(InvalidArgumentException::class)->duringInstantiation();
}

public function it_requires_the_matched_text(): void
{
$this->beConstructedWith('Something', ['heading' => 'value'], new TokenPosition(3, 7));

$this->shouldThrow(InvalidArgumentException::class)->duringInstantiation();
}
}
7 changes: 6 additions & 1 deletion src/Finder/RegexFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class RegexFinder
{
/**
* @var mixed[]
* @var array<int|string, string>
*/
private array $matches = [];

Expand All @@ -32,6 +32,11 @@ public function find(string $text): bool
return false;
}

/**
* @param string[] $keys
*
* @return array<int|string, string>
*/
public function getMatches(array $keys): array
{
return array_intersect_key($this->matches, array_flip($keys));
Expand Down
2 changes: 1 addition & 1 deletion src/Lexer/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ private function tokeniseFromInitialState(string $text): TokenStream

$tokens->add($token);

$length = mb_strlen($token->getValue('all'));
$length = mb_strlen($token->getText());

if (0 < $length) {
$cursor->advance($length);
Expand Down
3 changes: 3 additions & 0 deletions src/Matcher/MatchedText.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function get(string $key)
return null;
}

/**
* @return mixed[]
*/
public function getAll(): array
{
return $this->matches;
Expand Down
2 changes: 1 addition & 1 deletion src/Node/AbstractNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __toString(): string
}

/**
* @return array{attributes: array, nodes: NodeInterface[]}
* @return array{attributes: array<string, mixed>, nodes: NodeInterface[]}
*/
public function jsonSerialize(): array
{
Expand Down
8 changes: 8 additions & 0 deletions src/NodeTraverser/NodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public function traverseNode(NodeInterface $node): NodeInterface|NodeVisitorActi
return $node;
}

/**
* @param mixed[] $children
*
* @return mixed[]
*/
public function traverseChildren(array $children): array
{
$isList = array_is_list($children);
Expand Down Expand Up @@ -131,6 +136,9 @@ private function runTraverseNodeOnSubNodes(NodeInterface $node, array $children)
}, $children);
}

/**
* @param array<string, mixed> $attributes
*/
private function runTraverseChildrenOnAttributes(NodeInterface $node, array $attributes): void
{
array_walk($attributes, function ($attribute, $key) use (&$node): void {
Expand Down
16 changes: 11 additions & 5 deletions src/State/AmbiguousTokenFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public function __construct(
int $code = 0,
?Throwable $previous = null
) {
$matches = array_map(
static fn (string $matcherName, MatchedText $matchedText): string => sprintf('%s (%s)', $matcherName, TextExcerpt::of($matchedText->getAll()['all'])),
$calledMatchers,
$matchedTokens
);
$matches = array_map(static function (string $matcherName, MatchedText $matchedText): string {
$all = $matchedText->getAll()['all'] ?? null;

return sprintf('%s (%s)', $matcherName, TextExcerpt::of(is_string($all) ? $all : ''));
}, $calledMatchers, $matchedTokens);

$message = sprintf(
'Ambiguous token found with state %s at line %d, column %d: matchers %s',
Expand All @@ -41,11 +41,17 @@ public function __construct(
parent::__construct($message, $code, $previous);
}

/**
* @return string[]
*/
public function getCalledMatchers(): array
{
return $this->calledMatchers;
}

/**
* @return MatchedText[]
*/
public function getMatchedTokens(): array
{
return $this->matchedTokens;
Expand Down
16 changes: 15 additions & 1 deletion src/Token/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace chrisjenkinson\StructuredDocumentParser\Token;

use InvalidArgumentException;

class Token implements TokenInterface
{
private readonly string $text;

/**
* @param mixed[] $value
*/
Expand All @@ -14,11 +18,21 @@ public function __construct(
private readonly array $value,
private readonly TokenPosition $position
) {
if (!isset($value['all']) || !is_string($value['all'])) {
throw new InvalidArgumentException(sprintf('Token %s needs a string "all" value', $type));
}

$this->text = $value['all'];
}

public function __toString(): string
{
return sprintf('%s (%s)', $this->getType(), trim($this->getValue('all')));
return sprintf('%s (%s)', $this->getType(), trim($this->getText()));
}

public function getText(): string
{
return $this->text;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/Token/TokenInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public function getValue(string $key): mixed;

public function getType(): string;

/**
* The text the token consumed from the input.
*/
public function getText(): string;

public function getPosition(): TokenPosition;

public function hasKey(string $key): bool;
Expand Down
12 changes: 6 additions & 6 deletions src/Token/TokenStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ public function getCurrentToken(): ?TokenInterface
*/
public function expectTokenType(string $expectedType): bool
{
if (0 === count($this)) {
$currentToken = $this->getCurrentToken();

if (null === $currentToken) {
throw new RuntimeException(sprintf('No more tokens; expected %s', $expectedType));
}

$currentToken = $this->getCurrentToken();

if ($expectedType !== $currentToken->getType()) {
throw new RuntimeException(
sprintf('Token type is %s; expected %s', $currentToken->getType(), $expectedType)
Expand All @@ -77,12 +77,12 @@ public function expectTokenType(string $expectedType): bool
*/
public function expectTokenTypes(array $expectedTypes): bool
{
if (0 === count($this)) {
$currentToken = $this->getCurrentToken();

if (null === $currentToken) {
throw new RuntimeException(sprintf('No more tokens; expected any of %s', implode(', ', $expectedTypes)));
}

$currentToken = $this->getCurrentToken();

$expected = array_filter($expectedTypes, static fn ($expectedType) => $expectedType === $currentToken->getType());

if (count($expected) >= 1) {
Expand Down
4 changes: 3 additions & 1 deletion src/Visitor/VisitableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
*
* public function accept(VisitorInterface $visitor): mixed
* {
* assert($visitor instanceof MarkdownVisitor);
* if (!$visitor instanceof MarkdownVisitor) {
* throw new InvalidArgumentException('Heading can only be visited by a MarkdownVisitor');
* }
*
* return $visitor->visitHeading($this);
* }
Expand Down
16 changes: 16 additions & 0 deletions tests/NodeTraverser/NodeTraverserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function testItReplacesANode(): void

$node = $traverser->traverse($node);

Assert::assertNotNull($node);

Assert::assertFalse($node->hasNode('ChildNode'));
Assert::assertInstanceOf(NodeFromVisitor::class, $node->getNode('NodeFromVisitor'));
}
Expand All @@ -52,6 +54,8 @@ public function testItCanChangeGrandchildrenNodes(): void

$node = $traverser->traverse($node);

Assert::assertNotNull($node);

Assert::assertEquals('replacement', $node->getNode('ChildNode')->getNode('GrandchildNode')->getAttribute('testAttribute'));
}

Expand All @@ -66,6 +70,8 @@ public function testItRemovesAChildNode(): void

$node = $traverser->traverse($node);

Assert::assertNotNull($node);

Assert::assertFalse($node->hasNode('ChildNode'));
}

Expand All @@ -80,6 +86,8 @@ public function testItRemovesAChildNodeWhenAVisitorReturnsTheRemoveNodeAction():

$node = $traverser->traverse($node);

Assert::assertNotNull($node);

Assert::assertFalse($node->hasNode('ChildNode'));
}

Expand Down Expand Up @@ -109,6 +117,8 @@ public function testItPreservesKeysOfArrayAttributesWithoutNodes(): void

$node = $traverser->traverse($node);

Assert::assertNotNull($node);

Assert::assertSame([5 => 'five', 9 => 'nine'], $node->getAttribute('values'));
}

Expand All @@ -124,6 +134,8 @@ public function testItPreservesKeysWhenRemovingANodeFromAKeyedArrayAttribute():

$node = $traverser->traverse($node);

Assert::assertNotNull($node);

Assert::assertSame(['second' => $keep], $node->getAttribute('children'));
}

Expand All @@ -139,6 +151,8 @@ public function testItReindexesAListAttributeWhenRemovingANode(): void

$node = $traverser->traverse($node);

Assert::assertNotNull($node);

Assert::assertSame([$keep], $node->getAttribute('children'));
}

Expand All @@ -154,6 +168,8 @@ public function testItVisitsNodesInNestedArrayAttributes(): void

$node = $traverser->traverse($node);

Assert::assertNotNull($node);

Assert::assertSame(['group' => [$keep]], $node->getAttribute('children'));
}
}
Expand Down
Loading