From 71f05f9530eef43c55419c6abf9db2b20a9d1dbc Mon Sep 17 00:00:00 2001 From: Luke Watts Date: Mon, 30 May 2022 23:42:22 +0100 Subject: [PATCH] Added Stream methods skipUntil, consumeUntil and copyStreamUntil --- src/Stream.php | 79 ++++++++++++++++++++++++++++++++++++++++---- tests/StreamTest.php | 6 ++-- 2 files changed, 75 insertions(+), 10 deletions(-) diff --git a/src/Stream.php b/src/Stream.php index 57fc248..7c1cfaf 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -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 * @@ -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 = []; @@ -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; @@ -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 */ @@ -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 * @@ -151,7 +218,7 @@ public function next(): false|\Affinity4\Tokenizer\Token /** * Is Current * - * @param mixed ...$args + * @param string $type * * @return boolean */ diff --git a/tests/StreamTest.php b/tests/StreamTest.php index a76851c..88996bc 100644 --- a/tests/StreamTest.php +++ b/tests/StreamTest.php @@ -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); } /**