added the // operator (like in Jinja2, closes #44)
authorfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Wed, 6 Jan 2010 08:19:36 +0000 (08:19 +0000)
committerfabien <fabien@93ef8e89-cb99-4229-a87c-7fa0fa45744b>
Wed, 6 Jan 2010 08:19:36 +0000 (08:19 +0000)
git-svn-id: http://svn.twig-project.org/trunk@208 93ef8e89-cb99-4229-a87c-7fa0fa45744b

CHANGELOG
lib/Twig/ExpressionParser.php
lib/Twig/Extension/Core.php
lib/Twig/Lexer.php
lib/Twig/Node/Expression/Binary/FloorDiv.php [new file with mode: 0644]
test/fixtures/expressions/binary.test

index c455abc..235831b 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -22,9 +22,10 @@ upgrade to the new interface (please not that the interface is not yet stable).
  * improved the filter system to allow object methods to be used as filters
  * changed the Array and String loaders to actually make use of the cache mechanism
  * included the default filter function definitions in the extension class files directly (Core, Escaper)
+ * added the // operator (like the floor() PHP function)
  * added the .. operator (as a syntactic sugar for the range filter when the step is 1)
  * added the in operator (as a syntactic sugar for the in filter)
- * added the following filters in the Core extension: in, range, floor
+ * added the following filters in the Core extension: in, range
  * added support for arrays (same behavior as in PHP, a mix between lists and dictionaries, arrays and hashes)
  * enhanced some error messages to provide better feedback in case of parsing errors
 
index 8e5e81a..20b6f34 100644 (file)
@@ -164,7 +164,7 @@ class Twig_ExpressionParser
   public function parseDivExpression()
   {
     $lineno = $this->parser->getCurrentToken()->getLine();
-    $left = $this->parseModExpression();
+    $left = $this->parseFloorDivExpression();
     while ($this->parser->getStream()->test(Twig_Token::OPERATOR_TYPE, '/'))
     {
       $this->parser->getStream()->next();
@@ -176,6 +176,21 @@ class Twig_ExpressionParser
     return $left;
   }
 
+  public function parseFloorDivExpression()
+  {
+    $lineno = $this->parser->getCurrentToken()->getLine();
+    $left = $this->parseModExpression();
+    while ($this->parser->getStream()->test(Twig_Token::OPERATOR_TYPE, '//'))
+    {
+      $this->parser->getStream()->next();
+      $right = $this->parseModExpression();
+      $left = new Twig_Node_Expression_Binary_FloorDiv($left, $right, $lineno);
+      $lineno = $this->parser->getCurrentToken()->getLine();
+    }
+
+    return $left;
+  }
+
   public function parseModExpression()
   {
     $lineno = $this->parser->getCurrentToken()->getLine();
index b598b28..3ae0905 100644 (file)
@@ -58,7 +58,6 @@ class Twig_Extension_Core extends Twig_Extension
       // numbers
       'even'  => new Twig_Filter_Function('twig_is_even_filter'),
       'odd'   => new Twig_Filter_Function('twig_is_odd_filter'),
-      'floor' => new Twig_Filter_Function('twig_floor_filter'),
 
       // encoding
       'urlencode' => new Twig_Filter_Function('twig_urlencode_filter', array('is_escaper' => true)),
@@ -173,11 +172,6 @@ function twig_is_odd_filter($value)
   return $value % 2 == 1;
 }
 
-function twig_floor_filter($value, $div)
-{
-  return floor($value / $div);
-}
-
 function twig_length_filter($thing)
 {
   return is_string($thing) ? strlen($thing) : count($thing);
index b3ef4e5..2568437 100644 (file)
@@ -36,7 +36,7 @@ class Twig_Lexer implements Twig_LexerInterface
   const REGEX_NAME     = '/[A-Za-z_][A-Za-z0-9_]*/A';
   const REGEX_NUMBER   = '/[0-9]+(?:\.[0-9]+)?/A';
   const REGEX_STRING   = '/(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')/Asm';
-  const REGEX_OPERATOR = '/<=? | >=? | [!=]= | \.\. | [(){}.,%*\/+~|-] | \[ | \] | \? | \:/Ax';
+  const REGEX_OPERATOR = '/<=? | >=? | [!=]= | \/\/ | \.\. | [(){}.,%*\/+~|-] | \[ | \] | \? | \:/Ax';
 
   public function __construct(Twig_Environment $env = null, array $options = array())
   {
diff --git a/lib/Twig/Node/Expression/Binary/FloorDiv.php b/lib/Twig/Node/Expression/Binary/FloorDiv.php
new file mode 100644 (file)
index 0000000..2a9be20
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2009 Fabien Potencier
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+class Twig_Node_Expression_Binary_FloorDiv extends Twig_Node_Expression_Binary
+{
+  public function compile($compiler)
+  {
+    $compiler
+      ->raw('floor(')
+      ->subcompile($this->left)
+      ->raw(' / ')
+      ->subcompile($this->right)
+      ->raw(')')
+    ;
+  }
+
+  public function operator($compiler)
+  {
+    return;
+  }
+}
index a969c5d..009c8c8 100644 (file)
@@ -18,6 +18,7 @@ Twig supports binary operations (+, -, *, /, ~, %, and, or)
 {{ foo ~ "bar" }}
 {{ "foo" ~ bar }}
 {{ foo ~ bar }}
+{{ 20 // 7 }}
 --DATA--
 return array('foo' => 'bar', 'bar' => 'foo')
 --EXPECT--
@@ -38,3 +39,4 @@ foobar
 barbar
 foofoo
 barfoo
+2