Skip to content

Commit

Permalink
[DNumber] Add rawValue attribute to hold the original value (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 15, 2022
1 parent d3eb10a commit 3bf0082
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion grammar/php5.y
Expand Up @@ -793,7 +793,7 @@ ctor_arguments:

common_scalar:
T_LNUMBER { $$ = $this->parseLNumber($1, attributes(), true); }
| T_DNUMBER { $$ = Scalar\DNumber[Scalar\DNumber::parse($1)]; }
| T_DNUMBER { $$ = Scalar\DNumber::fromString($1, attributes()); }
| T_CONSTANT_ENCAPSED_STRING
{ $attrs = attributes(); $attrs['kind'] = strKind($1);
$$ = new Scalar\String_(Scalar\String_::parse($1, false), $attrs); }
Expand Down
2 changes: 1 addition & 1 deletion grammar/php7.y
Expand Up @@ -1024,7 +1024,7 @@ dereferencable_scalar:

scalar:
T_LNUMBER { $$ = $this->parseLNumber($1, attributes()); }
| T_DNUMBER { $$ = Scalar\DNumber[Scalar\DNumber::parse($1)]; }
| T_DNUMBER { $$ = Scalar\DNumber::fromString($1, attributes()); }
| dereferencable_scalar { $$ = $1; }
| constant { $$ = $1; }
| class_constant { $$ = $1; }
Expand Down
13 changes: 12 additions & 1 deletion lib/PhpParser/Node/Scalar/DNumber.php
Expand Up @@ -24,6 +24,17 @@ public function getSubNodeNames() : array {
return ['value'];
}

/**
* @param mixed[] $attributes
*/
public static function fromString(string $str, array $attributes = []): DNumber
{
$attributes['rawValue'] = $str;
$float = self::parse($str);

return new DNumber($float, $attributes);
}

/**
* @internal
*
Expand Down Expand Up @@ -63,7 +74,7 @@ public static function parse(string $str) : float {
// dec
return (float) $str;
}

public function getType() : string {
return 'Scalar_DNumber';
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Parser/Php5.php
Expand Up @@ -2275,7 +2275,7 @@ protected function initReduceCallbacks() {
$this->semValue = $this->parseLNumber($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes, true);
},
434 => function ($stackPos) {
$this->semValue = new Scalar\DNumber(Scalar\DNumber::parse($this->semStack[$stackPos-(1-1)]), $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
$this->semValue = Scalar\DNumber::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
},
435 => function ($stackPos) {
$attrs = $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes; $attrs['kind'] = ($this->semStack[$stackPos-(1-1)][0] === "'" || ($this->semStack[$stackPos-(1-1)][1] === "'" && ($this->semStack[$stackPos-(1-1)][0] === 'b' || $this->semStack[$stackPos-(1-1)][0] === 'B')) ? Scalar\String_::KIND_SINGLE_QUOTED : Scalar\String_::KIND_DOUBLE_QUOTED);
Expand Down
2 changes: 1 addition & 1 deletion lib/PhpParser/Parser/Php7.php
Expand Up @@ -2554,7 +2554,7 @@ protected function initReduceCallbacks() {
$this->semValue = $this->parseLNumber($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
},
515 => function ($stackPos) {
$this->semValue = new Scalar\DNumber(Scalar\DNumber::parse($this->semStack[$stackPos-(1-1)]), $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
$this->semValue = Scalar\DNumber::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
},
516 => function ($stackPos) {
$this->semValue = $this->semStack[$stackPos-(1-1)];
Expand Down
27 changes: 27 additions & 0 deletions test/PhpParser/Node/Scalar/DNumberTest.php
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);

namespace PhpParser\Node\Scalar;

use PhpParser\Node\Stmt\Echo_;
use PhpParser\ParserFactory;

class DNumberTest extends \PHPUnit\Framework\TestCase
{
public function testRawValue()
{
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$nodes = $parser->parse('<?php echo 1_234.56;');

$echo = $nodes[0];
$this->assertInstanceOf(Echo_::class, $echo);

/** @var Echo_ $echo */
$lLumber = $echo->exprs[0];
$this->assertInstanceOf(DNumber::class, $lLumber);

/** @var DNumber $dnumber */
$this->assertSame(1234.56, $lLumber->value);
$this->assertSame('1_234.56', $lLumber->getAttribute('rawValue'));
}
}
6 changes: 4 additions & 2 deletions test/PhpParser/NodeAbstractTest.php
Expand Up @@ -274,7 +274,8 @@ function functionName(&$a = 0, $b = 1.0) {
"value": 1,
"attributes": {
"startLine": 4,
"endLine": 4
"endLine": 4,
"rawValue": "1.0"
}
},
"flags": 0,
Expand Down Expand Up @@ -428,7 +429,8 @@ function functionName(&$a = 0, $b = 1.0) {
"nodeType": "Scalar_DNumber",
"attributes": {
"startLine": 4,
"endLine": 4
"endLine": 4,
"rawValue": "1.0"
},
"value": 1
},
Expand Down

0 comments on commit 3bf0082

Please sign in to comment.