Skip to content

Commit 3bf0082

Browse files
authored
[DNumber] Add rawValue attribute to hold the original value (#833)
1 parent d3eb10a commit 3bf0082

File tree

7 files changed

+47
-7
lines changed

7 files changed

+47
-7
lines changed

grammar/php5.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ ctor_arguments:
793793

794794
common_scalar:
795795
T_LNUMBER { $$ = $this->parseLNumber($1, attributes(), true); }
796-
| T_DNUMBER { $$ = Scalar\DNumber[Scalar\DNumber::parse($1)]; }
796+
| T_DNUMBER { $$ = Scalar\DNumber::fromString($1, attributes()); }
797797
| T_CONSTANT_ENCAPSED_STRING
798798
{ $attrs = attributes(); $attrs['kind'] = strKind($1);
799799
$$ = new Scalar\String_(Scalar\String_::parse($1, false), $attrs); }

grammar/php7.y

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,7 +1024,7 @@ dereferencable_scalar:
10241024

10251025
scalar:
10261026
T_LNUMBER { $$ = $this->parseLNumber($1, attributes()); }
1027-
| T_DNUMBER { $$ = Scalar\DNumber[Scalar\DNumber::parse($1)]; }
1027+
| T_DNUMBER { $$ = Scalar\DNumber::fromString($1, attributes()); }
10281028
| dereferencable_scalar { $$ = $1; }
10291029
| constant { $$ = $1; }
10301030
| class_constant { $$ = $1; }

lib/PhpParser/Node/Scalar/DNumber.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@ public function getSubNodeNames() : array {
2424
return ['value'];
2525
}
2626

27+
/**
28+
* @param mixed[] $attributes
29+
*/
30+
public static function fromString(string $str, array $attributes = []): DNumber
31+
{
32+
$attributes['rawValue'] = $str;
33+
$float = self::parse($str);
34+
35+
return new DNumber($float, $attributes);
36+
}
37+
2738
/**
2839
* @internal
2940
*
@@ -63,7 +74,7 @@ public static function parse(string $str) : float {
6374
// dec
6475
return (float) $str;
6576
}
66-
77+
6778
public function getType() : string {
6879
return 'Scalar_DNumber';
6980
}

lib/PhpParser/Parser/Php5.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2275,7 +2275,7 @@ protected function initReduceCallbacks() {
22752275
$this->semValue = $this->parseLNumber($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes, true);
22762276
},
22772277
434 => function ($stackPos) {
2278-
$this->semValue = new Scalar\DNumber(Scalar\DNumber::parse($this->semStack[$stackPos-(1-1)]), $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
2278+
$this->semValue = Scalar\DNumber::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
22792279
},
22802280
435 => function ($stackPos) {
22812281
$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);

lib/PhpParser/Parser/Php7.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2554,7 +2554,7 @@ protected function initReduceCallbacks() {
25542554
$this->semValue = $this->parseLNumber($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
25552555
},
25562556
515 => function ($stackPos) {
2557-
$this->semValue = new Scalar\DNumber(Scalar\DNumber::parse($this->semStack[$stackPos-(1-1)]), $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
2557+
$this->semValue = Scalar\DNumber::fromString($this->semStack[$stackPos-(1-1)], $this->startAttributeStack[$stackPos-(1-1)] + $this->endAttributes);
25582558
},
25592559
516 => function ($stackPos) {
25602560
$this->semValue = $this->semStack[$stackPos-(1-1)];
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace PhpParser\Node\Scalar;
5+
6+
use PhpParser\Node\Stmt\Echo_;
7+
use PhpParser\ParserFactory;
8+
9+
class DNumberTest extends \PHPUnit\Framework\TestCase
10+
{
11+
public function testRawValue()
12+
{
13+
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
14+
$nodes = $parser->parse('<?php echo 1_234.56;');
15+
16+
$echo = $nodes[0];
17+
$this->assertInstanceOf(Echo_::class, $echo);
18+
19+
/** @var Echo_ $echo */
20+
$lLumber = $echo->exprs[0];
21+
$this->assertInstanceOf(DNumber::class, $lLumber);
22+
23+
/** @var DNumber $dnumber */
24+
$this->assertSame(1234.56, $lLumber->value);
25+
$this->assertSame('1_234.56', $lLumber->getAttribute('rawValue'));
26+
}
27+
}

test/PhpParser/NodeAbstractTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,8 @@ function functionName(&$a = 0, $b = 1.0) {
274274
"value": 1,
275275
"attributes": {
276276
"startLine": 4,
277-
"endLine": 4
277+
"endLine": 4,
278+
"rawValue": "1.0"
278279
}
279280
},
280281
"flags": 0,
@@ -428,7 +429,8 @@ function functionName(&$a = 0, $b = 1.0) {
428429
"nodeType": "Scalar_DNumber",
429430
"attributes": {
430431
"startLine": 4,
431-
"endLine": 4
432+
"endLine": 4,
433+
"rawValue": "1.0"
432434
},
433435
"value": 1
434436
},

0 commit comments

Comments
 (0)