Skip to content

Commit

Permalink
- Implemented #21408469: Implement PHP 5.4 binary number format
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelpichler committed Nov 23, 2011
1 parent dcf12c7 commit e3bccf1
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 13 deletions.
12 changes: 11 additions & 1 deletion src/main/php/PHP/Depend/Parser.php
Expand Up @@ -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);
Expand All @@ -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.
*
Expand Down
40 changes: 35 additions & 5 deletions src/main/php/PHP/Depend/Parser/VersionAllParser.php
Expand Up @@ -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;
}
}
113 changes: 106 additions & 7 deletions src/test/php/PHP/Depend/Code/ASTLiteralTest.php
Expand Up @@ -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());
}

/**
Expand All @@ -87,7 +87,7 @@ public function testLiteralWithBooleanTrueExpression()
public function testLiteralWithBooleanFalseExpression()
{
$literal = $this->_getFirstLiteralInFunction();
self::assertEquals('False', $literal->getImage());
$this->assertEquals('False', $literal->getImage());
}

/**
Expand All @@ -98,7 +98,7 @@ public function testLiteralWithBooleanFalseExpression()
public function testLiteralWithIntegerExpression()
{
$literal = $this->_getFirstLiteralInFunction();
self::assertEquals('42', $literal->getImage());
$this->assertEquals('42', $literal->getImage());
}

/**
Expand All @@ -109,7 +109,7 @@ public function testLiteralWithIntegerExpression()
public function testLiteralWithSignedIntegerExpression()
{
$literal = $this->_getFirstLiteralInFunction();
self::assertEquals('42', $literal->getImage());
$this->assertEquals('42', $literal->getImage());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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();
}

/**
Expand Down
@@ -0,0 +1,5 @@
<?php
function testLiteralWithBrokenBinaryIntegerThrowsExpectedException()
{
return 0b100100100120;
}
@@ -0,0 +1,5 @@
<?php
function testLiteralWithNonZeroBinaryIntegerValue()
{
return 0b100100100110;
}
@@ -0,0 +1,5 @@
<?php
function testLiteralWithNonZeroHexIntegerValue()
{
return 0x926;
}
@@ -0,0 +1,5 @@
<?php
function testLiteralWithNonZeroOctalIntegerValue()
{
return 02342;
}
@@ -0,0 +1,5 @@
<?php
function testLiteralWithZeroBinaryIntegerValue()
{
return 0b0;
}
@@ -0,0 +1,5 @@
<?php
function testLiteralWithZeroHexIntegerValue()
{
return 0x0;
}
@@ -0,0 +1,5 @@
<?php
function testLiteralWithZeroIntegerValue()
{
return 0;
}
@@ -0,0 +1,5 @@
<?php
function testLiteralWithZeroOctalIntegerValue()
{
return 00;
}

0 comments on commit e3bccf1

Please sign in to comment.