Skip to content

Commit

Permalink
added the // operator (like in Jinja2, closes twigphp#44)
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.twig-project.org/trunk@208 93ef8e89-cb99-4229-a87c-7fa0fa45744b
  • Loading branch information
fabien committed Jan 6, 2010
1 parent 6218418 commit 575ed89
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 16 additions & 1 deletion lib/Twig/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function parseMulExpression()
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();
Expand All @@ -176,6 +176,21 @@ public function parseDivExpression()
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();
Expand Down
6 changes: 0 additions & 6 deletions lib/Twig/Extension/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function getFilters()
// 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)),
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
{
Expand Down
28 changes: 28 additions & 0 deletions lib/Twig/Node/Expression/Binary/FloorDiv.php
Original file line number Diff line number Diff line change
@@ -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;
}
}
2 changes: 2 additions & 0 deletions test/fixtures/expressions/binary.test
Original file line number Diff line number Diff line change
Expand Up @@ -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--
Expand All @@ -38,3 +39,4 @@ foobar
barbar
foofoo
barfoo
2

0 comments on commit 575ed89

Please sign in to comment.