From: fabien Date: Wed, 6 Jan 2010 08:19:36 +0000 (+0000) Subject: added the // operator (like in Jinja2, closes #44) X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=575ed89d1965bf9d141cc74dc11838ec081f0819;p=konrad%2Ftwig.git added the // operator (like in Jinja2, closes #44) git-svn-id: http://svn.twig-project.org/trunk@208 93ef8e89-cb99-4229-a87c-7fa0fa45744b --- diff --git a/CHANGELOG b/CHANGELOG index c455abc..235831b 100644 --- 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 diff --git a/lib/Twig/ExpressionParser.php b/lib/Twig/ExpressionParser.php index 8e5e81a..20b6f34 100644 --- a/lib/Twig/ExpressionParser.php +++ b/lib/Twig/ExpressionParser.php @@ -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(); diff --git a/lib/Twig/Extension/Core.php b/lib/Twig/Extension/Core.php index b598b28..3ae0905 100644 --- a/lib/Twig/Extension/Core.php +++ b/lib/Twig/Extension/Core.php @@ -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); diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index b3ef4e5..2568437 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -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 index 0000000..2a9be20 --- /dev/null +++ b/lib/Twig/Node/Expression/Binary/FloorDiv.php @@ -0,0 +1,28 @@ +raw('floor(') + ->subcompile($this->left) + ->raw(' / ') + ->subcompile($this->right) + ->raw(')') + ; + } + + public function operator($compiler) + { + return; + } +} diff --git a/test/fixtures/expressions/binary.test b/test/fixtures/expressions/binary.test index a969c5d..009c8c8 100644 --- a/test/fixtures/expressions/binary.test +++ b/test/fixtures/expressions/binary.test @@ -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