Skip to content

Commit

Permalink
Added Stream methods skipUntil, consumeUntil and copyStreamUntil
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewatts committed May 30, 2022
1 parent df66d73 commit 71f05f9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 10 deletions.
79 changes: 73 additions & 6 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public function current(): \Affinity4\Tokenizer\Token
*
* Skip each token that is a match in ...$args
* Stops when a token does not match ...$args list
*
* @since 0.0.3
*
* @param string $type
*
Expand All @@ -74,16 +76,37 @@ public function skipWhile(...$args): void
}
}

/**
* skip Until
*
* Moves forward until one of the provided token types is matched
*
* @since 0.0.4
*
* @param array $args
*/
public function skipUntil(...$args)
{
$Token = $this->current();
while ($Token && !in_array($Token->type, $args)) {
$Token = $this->next();
}
}

/**
* Consume While
*
* Moves pointer ahead while type is one of ...$args
*
* Returns array of tokens which where 'consumed'
*
* @since 0.0.3
*
* @param array $args
*
* @return array
*/
public function consumeWhile(... $args): array
public function consumeWhile(...$args): array
{
$token = $this->current();
$tokens = [];
Expand All @@ -97,18 +120,21 @@ public function consumeWhile(... $args): array

/**
* consumeValueWhile
*
* @since 0.0.3
*
* @param arglist $args
* @param array $args
*
* @return string
*/
public function consumeValueWhile(... $args): string
public function consumeValueWhile(...$args): string
{
$tokens = $this->consumeWhile(...$args);
$i = 0;
$value = '';
while ($tokens[$i]) {
while (!empty($tokens) && isset($tokens[$i])) {
$value .= $tokens[$i]->value;
++$i;
}

return $value;
Expand All @@ -118,8 +144,10 @@ public function consumeValueWhile(... $args): string
* Consume Value Until
*
* Returns value up until one of the provided token types is matched
*
* @since 0.0.3
*
* @param arglist $args
* @param array $args
*
* @return string
*/
Expand All @@ -135,6 +163,45 @@ public function consumeValueUntil(...$args): string
return $value;
}

/**
* Consume Until
*
* Returns tokens array up until one of the provided token types is matched
*
* @since 0.0.4
*
* @param array $args
*
* @return array
*/
public function consumeUntil(...$args): array
{
$Token = $this->current();
$tokens = [];
while ($Token && !in_array($Token->type, $args)) {
$tokens[] = $Token;
$Token = $this->next();
}

return $tokens;
}

/**
* Copy Stream Until
*
* @since 0.0.4
*
* @param array $args
*
* @return \Affinity4\Tokenizer\Stream
*/
public function copyStreamUntil(...$args): \Affinity4\Tokenizer\Stream
{
$tokens = $this->consumeUntil(...$args);

return new Stream($tokens);
}

/**
* Next Token
*
Expand All @@ -151,7 +218,7 @@ public function next(): false|\Affinity4\Tokenizer\Token
/**
* Is Current
*
* @param mixed ...$args
* @param string $type
*
* @return boolean
*/
Expand Down
6 changes: 2 additions & 4 deletions tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,9 @@ public function testConsumeValueWhileMethod(): void

$Stream = $Tokenizer->tokenize($template);

$tokens = $Stream->consumeValueWhile('T_WORD');
$value = $Stream->consumeValueWhile('T_WORD');

$this->assertCount(1, $tokens);
$this->assertSame('T_WORD', $tokens[0]->type);
$this->assertSame('div', $tokens[0]->value);
$this->assertSame('div', $value);
}

/**
Expand Down

0 comments on commit 71f05f9

Please sign in to comment.