From 385a83b4e4a5bc6e447d9d02efcabf3f99516f6b Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 16 Oct 2014 10:29:01 +0200 Subject: [PATCH] Revert "bug #1535 fixed negative number lexing (fabpot)" This reverts commit 7bda3b61037fdfcae159b490053cfb3d0c5bc43f, reversing changes made to c86e621e09ffbfe9f7d082476fc01d32b1f759fb. Conflicts: CHANGELOG --- CHANGELOG | 1 - lib/Twig/Lexer.php | 22 ++++++++++---------- .../Fixtures/expressions/negative_numbers.test | 13 ----------- test/Twig/Tests/LexerTest.php | 17 --------------- 4 files changed, 11 insertions(+), 42 deletions(-) delete mode 100644 test/Twig/Tests/Fixtures/expressions/negative_numbers.test diff --git a/CHANGELOG b/CHANGELOG index 04c0de2..8055704 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,7 +1,6 @@ * 1.16.2 (2014-XX-XX) * fixed timezone on dates as strings - * fixed lexing of negative numbers * fixed 2-words test names when a custom node class is not used * fixed macros when using an argument named like a PHP super global (like GET or POST) * fixed date_modify when working with DateTimeImmutable diff --git a/lib/Twig/Lexer.php b/lib/Twig/Lexer.php index 43d6daa..ad3ec7d 100644 --- a/lib/Twig/Lexer.php +++ b/lib/Twig/Lexer.php @@ -40,7 +40,7 @@ class Twig_Lexer implements Twig_LexerInterface const STATE_INTERPOLATION = 4; const REGEX_NAME = '/[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*/A'; - const REGEX_NUMBER = '/\-?[0-9]+(?:\.[0-9]+)?/A'; + const REGEX_NUMBER = '/[0-9]+(?:\.[0-9]+)?/A'; const REGEX_STRING = '/"([^#"\\\\]*(?:\\\\.[^#"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'/As'; const REGEX_DQ_STRING_DELIM = '/"/A'; const REGEX_DQ_STRING_PART = '/[^#"\\\\]*(?:(?:\\\\.|#(?!\{))[^#"\\\\]*)*/As'; @@ -228,17 +228,8 @@ class Twig_Lexer implements Twig_LexerInterface } } - // numbers - if (preg_match(self::REGEX_NUMBER, $this->code, $match, null, $this->cursor)) { - $number = (float) $match[0]; // floats - if (ctype_digit($match[0]) && $number <= PHP_INT_MAX) { - $number = (int) $match[0]; // integers lower than the maximum - } - $this->pushToken(Twig_Token::NUMBER_TYPE, $number); - $this->moveCursor($match[0]); - } // operators - elseif (preg_match($this->regexes['operator'], $this->code, $match, null, $this->cursor)) { + if (preg_match($this->regexes['operator'], $this->code, $match, null, $this->cursor)) { $this->pushToken(Twig_Token::OPERATOR_TYPE, preg_replace('/\s+/', ' ', $match[0])); $this->moveCursor($match[0]); } @@ -247,6 +238,15 @@ class Twig_Lexer implements Twig_LexerInterface $this->pushToken(Twig_Token::NAME_TYPE, $match[0]); $this->moveCursor($match[0]); } + // numbers + elseif (preg_match(self::REGEX_NUMBER, $this->code, $match, null, $this->cursor)) { + $number = (float) $match[0]; // floats + if (ctype_digit($match[0]) && $number <= PHP_INT_MAX) { + $number = (int) $match[0]; // integers lower than the maximum + } + $this->pushToken(Twig_Token::NUMBER_TYPE, $number); + $this->moveCursor($match[0]); + } // punctuation elseif (false !== strpos(self::PUNCTUATION, $this->code[$this->cursor])) { // opening bracket diff --git a/test/Twig/Tests/Fixtures/expressions/negative_numbers.test b/test/Twig/Tests/Fixtures/expressions/negative_numbers.test deleted file mode 100644 index 3f7afda..0000000 --- a/test/Twig/Tests/Fixtures/expressions/negative_numbers.test +++ /dev/null @@ -1,13 +0,0 @@ ---TEST-- -Twig manages negative numbers correctly ---TEMPLATE-- -{% import _self as macros %} -{{ macros.negative_number1() }} -{{ macros.negative_number2() }} -{% macro negative_number1(nb=-1) %}{{ nb }}{% endmacro %} -{% macro negative_number2(nb = -1) %}{{ nb }}{% endmacro %} ---DATA-- -return array() ---EXPECT-- --1 --1 diff --git a/test/Twig/Tests/LexerTest.php b/test/Twig/Tests/LexerTest.php index 74e8397..c4d7083 100644 --- a/test/Twig/Tests/LexerTest.php +++ b/test/Twig/Tests/LexerTest.php @@ -260,23 +260,6 @@ class Twig_Tests_LexerTest extends PHPUnit_Framework_TestCase $stream->expect(Twig_Token::OPERATOR_TYPE, 'and'); } - public function testNegativeNumbers() - { - $template = "{{ -3 }}{{ 1 - 2 }}"; - - $lexer = new Twig_Lexer(new Twig_Environment()); - $stream = $lexer->tokenize($template); - $stream->expect(Twig_Token::VAR_START_TYPE); - $stream->expect(Twig_Token::NUMBER_TYPE, -3); - $stream->expect(Twig_Token::VAR_END_TYPE); - - $stream->expect(Twig_Token::VAR_START_TYPE); - $stream->expect(Twig_Token::NUMBER_TYPE, 1); - $stream->expect(Twig_Token::OPERATOR_TYPE, '-'); - $stream->expect(Twig_Token::NUMBER_TYPE, 2); - $stream->expect(Twig_Token::VAR_END_TYPE); - } - /** * @expectedException Twig_Error_Syntax * @expectedExceptionMessage Unclosed "variable" at line 3 -- 1.7.2.5