File tree Expand file tree Collapse file tree 7 files changed +47
-7
lines changed Expand file tree Collapse file tree 7 files changed +47
-7
lines changed Original file line number Diff line number Diff line change @@ -793,7 +793,7 @@ ctor_arguments:
793
793
794
794
common_scalar :
795
795
T_LNUMBER { $$ = $this- >parseLNumber ($1 , attributes(), true); }
796
- | T_DNUMBER { $$ = Scalar\DNumber[Scalar\DNumber::parse ($1 )] ; }
796
+ | T_DNUMBER { $$ = Scalar\DNumber::fromString ($1 , attributes()) ; }
797
797
| T_CONSTANT_ENCAPSED_STRING
798
798
{ $attrs = attributes(); $attrs [' kind' ] = strKind($1 );
799
799
$$ = new Scalar\String_(Scalar\String_::parse($1 , false ), $attrs ); }
Original file line number Diff line number Diff line change @@ -1024,7 +1024,7 @@ dereferencable_scalar:
1024
1024
1025
1025
scalar :
1026
1026
T_LNUMBER { $$ = $this- >parseLNumber ($1 , attributes()); }
1027
- | T_DNUMBER { $$ = Scalar\DNumber[Scalar\DNumber::parse ($1 )] ; }
1027
+ | T_DNUMBER { $$ = Scalar\DNumber::fromString ($1 , attributes()) ; }
1028
1028
| dereferencable_scalar { $$ = $1 ; }
1029
1029
| constant { $$ = $1 ; }
1030
1030
| class_constant { $$ = $1 ; }
Original file line number Diff line number Diff line change @@ -24,6 +24,17 @@ public function getSubNodeNames() : array {
24
24
return ['value ' ];
25
25
}
26
26
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
+
27
38
/**
28
39
* @internal
29
40
*
@@ -63,7 +74,7 @@ public static function parse(string $str) : float {
63
74
// dec
64
75
return (float ) $ str ;
65
76
}
66
-
77
+
67
78
public function getType () : string {
68
79
return 'Scalar_DNumber ' ;
69
80
}
Original file line number Diff line number Diff line change @@ -2275,7 +2275,7 @@ protected function initReduceCallbacks() {
2275
2275
$ this ->semValue = $ this ->parseLNumber ($ this ->semStack [$ stackPos -(1 -1 )], $ this ->startAttributeStack [$ stackPos -(1 -1 )] + $ this ->endAttributes , true );
2276
2276
},
2277
2277
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 );
2279
2279
},
2280
2280
435 => function ($ stackPos ) {
2281
2281
$ 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 );
Original file line number Diff line number Diff line change @@ -2554,7 +2554,7 @@ protected function initReduceCallbacks() {
2554
2554
$ this ->semValue = $ this ->parseLNumber ($ this ->semStack [$ stackPos -(1 -1 )], $ this ->startAttributeStack [$ stackPos -(1 -1 )] + $ this ->endAttributes );
2555
2555
},
2556
2556
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 );
2558
2558
},
2559
2559
516 => function ($ stackPos ) {
2560
2560
$ this ->semValue = $ this ->semStack [$ stackPos -(1 -1 )];
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -274,7 +274,8 @@ function functionName(&$a = 0, $b = 1.0) {
274
274
"value": 1,
275
275
"attributes": {
276
276
"startLine": 4,
277
- "endLine": 4
277
+ "endLine": 4,
278
+ "rawValue": "1.0"
278
279
}
279
280
},
280
281
"flags": 0,
@@ -428,7 +429,8 @@ function functionName(&$a = 0, $b = 1.0) {
428
429
"nodeType": "Scalar_DNumber",
429
430
"attributes": {
430
431
"startLine": 4,
431
- "endLine": 4
432
+ "endLine": 4,
433
+ "rawValue": "1.0"
432
434
},
433
435
"value": 1
434
436
},
You can’t perform that action at this time.
0 commit comments