From: fabien Date: Wed, 14 Oct 2009 08:08:04 +0000 (+0000) Subject: added a rewind() method to the token stream X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=2cfb51a143a958d8286bac1b2a6f90814f31d7cc;p=konrad%2Ftwig.git added a rewind() method to the token stream git-svn-id: http://svn.twig-project.org/trunk@42 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- diff --git a/lib/Twig/TokenStream.php b/lib/Twig/TokenStream.php index d4cae1e..cae0be9 100644 --- a/lib/Twig/TokenStream.php +++ b/lib/Twig/TokenStream.php @@ -103,6 +103,23 @@ class Twig_TokenStream } /** + * Rewinds the pushed tokens. + */ + public function rewind() + { + $tokens = array(); + while ($this->pushed) + { + $tokens[] = array_shift($this->pushed); + array_shift($this->pushed); + } + + $this->tokens = array_merge($tokens, array($this->current), $this->tokens); + + $this->next(); + } + + /** * Expects a token (like $token->test()) and returns it or throw a syntax error. */ public function expect($primary, $secondary = null) diff --git a/test/unit/Twig/TokenStreamTest.php b/test/unit/Twig/TokenStreamTest.php index 335d719..7a39180 100644 --- a/test/unit/Twig/TokenStreamTest.php +++ b/test/unit/Twig/TokenStreamTest.php @@ -15,7 +15,7 @@ LimeAutoloader::register(); require_once dirname(__FILE__).'/../../../lib/Twig/Autoloader.php'; Twig_Autoloader::register(); -$t = new LimeTest(0); +$t = new LimeTest(13); $tokens = array( new Twig_Token(Twig_Token::TEXT_TYPE, 1, 0), @@ -66,3 +66,20 @@ while (!$stream->isEOF()) $repr[] = $token->getValue(); } $t->is(implode(', ', $repr), '1, 2, 3, 4, 5, 6, 7', '->look() pushes the token to the stack'); + +// ->rewind() +$t->diag('->rewind()'); +$stream = new Twig_TokenStream($tokens, '', false); +$t->is($stream->look()->getValue(), 2, '->look() returns the next token'); +$t->is($stream->look()->getValue(), 3, '->look() can be called several times to look more than one upcoming token'); +$t->is($stream->look()->getValue(), 4, '->look() can be called several times to look more than one upcoming token'); +$t->is($stream->look()->getValue(), 5, '->look() can be called several times to look more than one upcoming token'); +$stream->rewind(); +$repr = array(); +while (!$stream->isEOF()) +{ + $token = $stream->next(false); + + $repr[] = $token->getValue(); +} +$t->is(implode(', ', $repr), '1, 2, 3, 4, 5, 6, 7', '->rewind() pushes all pushed tokens to the token array');