diff --git a/src/main/php/PHP/Depend/Parser.php b/src/main/php/PHP/Depend/Parser.php index 325901483..82901aafd 100644 --- a/src/main/php/PHP/Depend/Parser.php +++ b/src/main/php/PHP/Depend/Parser.php @@ -4298,7 +4298,6 @@ private function _parseLiteralOrString() case self::T_NULL: case self::T_TRUE: case self::T_FALSE: - case self::T_LNUMBER: case self::T_DNUMBER: case self::T_CONSTANT_ENCAPSED_STRING: $token = $this->consumeToken($tokenType); @@ -4312,11 +4311,22 @@ private function _parseLiteralOrString() ); return $literal; + case self::T_LNUMBER: + return $this->parseIntegerNumber(); + default: return $this->_parseString($tokenType); } } + /** + * Parses an integer value. + * + * @return PHP_Depend_Code_ASTLiteral + * @since 0.11.0 + */ + protected abstract function parseIntegerNumber(); + /** * Parses a here- or nowdoc string instance. * diff --git a/src/main/php/PHP/Depend/Parser/VersionAllParser.php b/src/main/php/PHP/Depend/Parser/VersionAllParser.php index f97efbf47..381417b58 100644 --- a/src/main/php/PHP/Depend/Parser/VersionAllParser.php +++ b/src/main/php/PHP/Depend/Parser/VersionAllParser.php @@ -201,13 +201,43 @@ protected function parseFormalParameterTypeHint() return $this->builder->buildASTTypeCallable(); } return $this->builder->buildASTClassOrInterfaceReference($name); + } + } - case PHP_Depend_TokenizerI::T_EOF: - throw new PHP_Depend_Parser_TokenStreamEndException($this->tokenizer); + /** + * Parses an integer value. + * + * @return PHP_Depend_Code_ASTLiteral + * @since 0.11.0 + */ + protected function parseIntegerNumber() + { + $token = $this->consumeToken(self::T_LNUMBER); + + if ('0' === $token->image) { + if (self::T_STRING === $this->tokenizer->peek()) { + $token1 = $this->consumeToken(self::T_STRING); + if (preg_match('(^b[01]+$)', $token1->image)) { + $token->image = $token->image . $token1->image; + $token->endLine = $token1->endLine; + $token->endColumn = $token1->endColumn; + } else { + throw new PHP_Depend_Parser_UnexpectedTokenException( + $token1, + $this->tokenizer->getSourceFile() + ); + } + } } - throw new PHP_Depend_Parser_UnexpectedTokenException( - $this->tokenizer->next(), - $this->tokenizer->getSourceFile() + + $literal = $this->builder->buildASTLiteral($token->image); + $literal->configureLinesAndColumns( + $token->startLine, + $token->endLine, + $token->startColumn, + $token->endColumn ); + + return $literal; } } diff --git a/src/test/php/PHP/Depend/Code/ASTLiteralTest.php b/src/test/php/PHP/Depend/Code/ASTLiteralTest.php index 3b0c7c721..4cba6aa3e 100644 --- a/src/test/php/PHP/Depend/Code/ASTLiteralTest.php +++ b/src/test/php/PHP/Depend/Code/ASTLiteralTest.php @@ -76,7 +76,7 @@ class PHP_Depend_Code_ASTLiteralTest extends PHP_Depend_Code_ASTNodeTest public function testLiteralWithBooleanTrueExpression() { $literal = $this->_getFirstLiteralInFunction(); - self::assertEquals('True', $literal->getImage()); + $this->assertEquals('True', $literal->getImage()); } /** @@ -87,7 +87,7 @@ public function testLiteralWithBooleanTrueExpression() public function testLiteralWithBooleanFalseExpression() { $literal = $this->_getFirstLiteralInFunction(); - self::assertEquals('False', $literal->getImage()); + $this->assertEquals('False', $literal->getImage()); } /** @@ -98,7 +98,7 @@ public function testLiteralWithBooleanFalseExpression() public function testLiteralWithIntegerExpression() { $literal = $this->_getFirstLiteralInFunction(); - self::assertEquals('42', $literal->getImage()); + $this->assertEquals('42', $literal->getImage()); } /** @@ -109,7 +109,7 @@ public function testLiteralWithIntegerExpression() public function testLiteralWithSignedIntegerExpression() { $literal = $this->_getFirstLiteralInFunction(); - self::assertEquals('42', $literal->getImage()); + $this->assertEquals('42', $literal->getImage()); } /** @@ -120,7 +120,7 @@ public function testLiteralWithSignedIntegerExpression() public function testLiteralWithFloatExpression() { $literal = $this->_getFirstLiteralInFunction(); - self::assertEquals('42.23', $literal->getImage()); + $this->assertEquals('42.23', $literal->getImage()); } /** @@ -131,7 +131,7 @@ public function testLiteralWithFloatExpression() public function testLiteralWithSignedFloatExpression() { $literal = $this->_getFirstLiteralInFunction(); - self::assertEquals('42.23', $literal->getImage()); + $this->assertEquals('42.23', $literal->getImage()); } /** @@ -142,7 +142,106 @@ public function testLiteralWithSignedFloatExpression() public function testLiteralWithNullExpression() { $literal = $this->_getFirstLiteralInFunction(); - self::assertEquals('NULL', $literal->getImage()); + $this->assertEquals('NULL', $literal->getImage()); + } + + /** + * testLiteralWithZeroIntegerValue + * + * @return void + * @since 0.11.0 + */ + public function testLiteralWithZeroIntegerValue() + { + $literal = $this->_getFirstLiteralInFunction(); + $this->assertEquals('0', $literal->getImage()); + } + + /** + * testLiteralWithZeroOctalIntegerValue + * + * @return void + * @since 0.11.0 + */ + public function testLiteralWithZeroOctalIntegerValue() + { + $literal = $this->_getFirstLiteralInFunction(); + $this->assertEquals('00', $literal->getImage()); + } + + /** + * testLiteralWithZeroHexIntegerValue + * + * @return void + * @since 0.11.0 + */ + public function testLiteralWithZeroHexIntegerValue() + { + $literal = $this->_getFirstLiteralInFunction(); + $this->assertEquals('0x0', $literal->getImage()); + } + + /** + * testLiteralWithZeroBinaryIntegerValue + * + * @return void + * @since 0.11.0 + */ + public function testLiteralWithZeroBinaryIntegerValue() + { + $literal = $this->_getFirstLiteralInFunction(); + $this->assertEquals('0b0', $literal->getImage()); + } + + /** + * testLiteralWithNonZeroOctalIntegerValue + * + * @return void + * @since 0.11.0 + */ + public function testLiteralWithNonZeroOctalIntegerValue() + { + $literal = $this->_getFirstLiteralInFunction(); + $this->assertEquals('02342', $literal->getImage()); + } + + /** + * testLiteralWithNonZeroHexIntegerValue + * + * @return void + * @since 0.11.0 + */ + public function testLiteralWithNonZeroHexIntegerValue() + { + $literal = $this->_getFirstLiteralInFunction(); + $this->assertEquals('0x926', $literal->getImage()); + } + + /** + * testLiteralWithNonZeroBinaryIntegerValue + * + * @return void + * @since 0.11.0 + */ + public function testLiteralWithNonZeroBinaryIntegerValue() + { + $literal = $this->_getFirstLiteralInFunction(); + $this->assertEquals('0b100100100110', $literal->getImage()); + } + + /** + * testLiteralWithBrokenBinaryIntegerThrowsExpectedException + * + * @return void + * @since 0.11.0 + * @expectedException PHP_Depend_Parser_UnexpectedTokenException + */ + public function testLiteralWithBrokenBinaryIntegerThrowsExpectedException() + { + if (version_compare(phpversion(), '5.4dev') >= 0) { + $this->markTestSkipped('This test only affects PHP < 5.4'); + } + $this->_getFirstLiteralInFunction(); } /** diff --git a/src/test/resources/files/Code/ASTLiteral/testLiteralWithBrokenBinaryIntegerThrowsExpectedException.php b/src/test/resources/files/Code/ASTLiteral/testLiteralWithBrokenBinaryIntegerThrowsExpectedException.php new file mode 100644 index 000000000..289e89466 --- /dev/null +++ b/src/test/resources/files/Code/ASTLiteral/testLiteralWithBrokenBinaryIntegerThrowsExpectedException.php @@ -0,0 +1,5 @@ +