Skip to content

Commit

Permalink
Add ExpectedFigures to Placeholder
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Jun 18, 2021
1 parent 8c432ff commit e4bc38b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
@@ -1,19 +1,28 @@
<?php
namespace TRegx\CleanRegex\Internal\Prepared\Parser\Consumer;

use TRegx\CleanRegex\Internal\Prepared\Figure\ExpectedFigures;
use TRegx\CleanRegex\Internal\Prepared\Parser\Entity\Placeholder;
use TRegx\CleanRegex\Internal\Prepared\Parser\EntitySequence;
use TRegx\CleanRegex\Internal\Prepared\Parser\Feed\Feed;

class PlaceholderConsumer implements Consumer
{
/** @var ExpectedFigures */
private $figures;

public function __construct(ExpectedFigures $provider)
{
$this->figures = $provider;
}

public function condition(Feed $feed): Condition
{
return $feed->string('@');
}

public function consume(Feed $feed, EntitySequence $entities): void
{
$entities->append(new Placeholder($entities->flags()));
$entities->append(new Placeholder($entities->flags(), $this->figures->nextToken()));
}
}
Expand Up @@ -3,22 +3,25 @@

use TRegx\CleanRegex\Internal\Flags;
use TRegx\CleanRegex\Internal\Prepared\Quotable\Quotable;
use TRegx\CleanRegex\Internal\Prepared\Quotable\RawQuotable;
use TRegx\CleanRegex\Internal\Prepared\Template\Token;

class Placeholder implements Entity
{
use TransitiveFlags;

/** @var Flags */
private $flags;
/** @var Token */
private $token;

public function __construct(Flags $flags)
public function __construct(Flags $flags, Token $token)
{
$this->flags = $flags;
$this->token = $token;
}

public function quotable(): Quotable
{
return new RawQuotable('@');
return $this->token->formatAsQuotable();
}
}
Expand Up @@ -6,6 +6,7 @@
use Test\Utils\PcreDependant;
use TRegx\CleanRegex\Exception\InternalCleanRegexException;
use TRegx\CleanRegex\Internal\Flags;
use TRegx\CleanRegex\Internal\Prepared\Figure\ExpectedFigures;
use TRegx\CleanRegex\Internal\Prepared\Parser\Consumer\CommentConsumer;
use TRegx\CleanRegex\Internal\Prepared\Parser\Consumer\ControlConsumer;
use TRegx\CleanRegex\Internal\Prepared\Parser\Consumer\EscapeConsumer;
Expand All @@ -29,6 +30,7 @@
use TRegx\CleanRegex\Internal\Prepared\Parser\Entity\Quote;
use TRegx\CleanRegex\Internal\Prepared\Parser\Feed\Feed;
use TRegx\CleanRegex\Internal\Prepared\Parser\PcreParser;
use TRegx\CleanRegex\Internal\Prepared\Template\LiteralToken;

class PcreParserTest extends TestCase
{
Expand Down Expand Up @@ -86,7 +88,7 @@ public function shouldParseWithFlags()
$consumers = [
new GroupConsumer(),
new GroupCloseConsumer(),
new PlaceholderConsumer()
new PlaceholderConsumer(new ExpectedFigures(['one', 'two', 'three']))
];

// when
Expand All @@ -96,11 +98,11 @@ public function shouldParseWithFlags()
$assertion->assertPatternRepresents('(?i:(?x:@(?m-x)@)@)', [
new GroupOpenFlags('i'),
new GroupOpenFlags('x'),
new Placeholder(new Flags('ix')),
new Placeholder(new Flags('ix'), new LiteralToken('one')),
new GroupRemainder('m-x'),
new Placeholder(new Flags('im')),
new Placeholder(new Flags('im'), new LiteralToken('two')),
new GroupClose(),
new Placeholder(new Flags('ix')),
new Placeholder(new Flags('ix'), new LiteralToken('three')),
new GroupClose(),
]);
}
Expand Down
Expand Up @@ -4,10 +4,13 @@
use PHPUnit\Framework\TestCase;
use Test\Utils\PatternEntitiesAssertion;
use TRegx\CleanRegex\Internal\Flags;
use TRegx\CleanRegex\Internal\Prepared\Figure\ExpectedFigures;
use TRegx\CleanRegex\Internal\Prepared\Parser\Consumer\GroupConsumer;
use TRegx\CleanRegex\Internal\Prepared\Parser\Consumer\PlaceholderConsumer;
use TRegx\CleanRegex\Internal\Prepared\Parser\Entity\GroupOpenFlags;
use TRegx\CleanRegex\Internal\Prepared\Parser\Entity\Placeholder;
use TRegx\CleanRegex\Internal\Prepared\Template\AlternationToken;
use TRegx\CleanRegex\Internal\Prepared\Template\LiteralToken;

class PlaceholderConsumerTest extends TestCase
{
Expand All @@ -17,10 +20,10 @@ class PlaceholderConsumerTest extends TestCase
public function test()
{
// given
$assertion = PatternEntitiesAssertion::withConsumers([new PlaceholderConsumer()]);
$assertion = PatternEntitiesAssertion::withConsumers([new PlaceholderConsumer(new ExpectedFigures(['one']))]);

// then
$assertion->assertPatternRepresents('@', [new Placeholder(new Flags(''))]);
$assertion->assertPatternRepresents('@', [new Placeholder(new Flags(''), new LiteralToken('one'))]);
}

/**
Expand All @@ -29,10 +32,10 @@ public function test()
public function testPatternFlags()
{
// given
$assertion = PatternEntitiesAssertion::withConsumers([new PlaceholderConsumer()]);
$assertion = PatternEntitiesAssertion::withConsumers([new PlaceholderConsumer(new ExpectedFigures(['one']))]);

// then
$assertion->assertPatternFlagsRepresent('@', 'xi', [new Placeholder(new Flags('xi'))]);
$assertion->assertPatternFlagsRepresent('@', 'xi', [new Placeholder(new Flags('xi'), new LiteralToken('one'))]);
}

/**
Expand All @@ -43,13 +46,25 @@ public function testSubpatternFlags()
// given
$assertion = PatternEntitiesAssertion::withConsumers([
new GroupConsumer(),
new PlaceholderConsumer(),
new PlaceholderConsumer(new ExpectedFigures(['one'])),
]);

// then
$assertion->assertPatternFlagsRepresent('(?m-i:@', 'xi', [
new GroupOpenFlags('m-i'),
new Placeholder(new Flags('xm'))
new Placeholder(new Flags('xm'), new LiteralToken('one'))
]);
}

/**
* @test
*/
public function testPatternFlagsToFigure()
{
// given
$assertion = PatternEntitiesAssertion::withConsumers([new PlaceholderConsumer(new ExpectedFigures([['one']]))]);

// then
$assertion->assertPatternFlagsRepresent('@', 'xi', [new Placeholder(new Flags('xi'), new AlternationToken(['one']))]);
}
}

0 comments on commit e4bc38b

Please sign in to comment.