added a rewind() method to the token stream
authorfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Wed, 14 Oct 2009 08:08:04 +0000 (08:08 +0000)
committerfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Wed, 14 Oct 2009 08:08:04 +0000 (08:08 +0000)
git-svn-id: http://svn.twig-project.org/trunk@42 93ef8e89-cb99-4229-a87c-7fa0fa45744b

lib/Twig/TokenStream.php
test/unit/Twig/TokenStreamTest.php

index d4cae1e..cae0be9 100644 (file)
@@ -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)
index 335d719..7a39180 100644 (file)
@@ -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');