Skip to content

Commit 8e75539

Browse files
authored
Add "+" sign support for float exponent
1 parent 362bc12 commit 8e75539

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

doc/grammars/type.abnf

+3-3
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ ConstantFloat
9090
/ ["-"] "." 1*ByteDecDigit *("_" 1*ByteDecDigit) [ConstantFloatExp]
9191

9292
ConstantFloatExp
93-
= "e" ["-"] 1*ByteDecDigit *("_" 1*ByteDecDigit)
93+
= "e" ["+" / "-"] 1*ByteDecDigit *("_" 1*ByteDecDigit)
9494

9595
ConstantInt
9696
= ["-"] "0b" 1*ByteBinDigit *("_" 1*ByteBinDigit)
@@ -229,14 +229,14 @@ ByteHexDigit
229229

230230
ByteIdentifierFirst
231231
= %x41-5A ; A-Z
232-
/ %x5F ; _
232+
/ "_"
233233
/ %x61-7A ; a-z
234234
/ %x80-FF
235235

236236
ByteIdentifierSecond
237237
= %x30-39 ; 0-9
238238
/ %x41-5A ; A-Z
239-
/ %x5F ; _
239+
/ "_"
240240
/ %x61-7A ; a-z
241241
/ %x80-FF
242242

src/Lexer/Lexer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private function generateRegexp(): string
160160
self::TOKEN_PHPDOC_TAG => '@(?:[a-z][a-z0-9-\\\\]+:)?[a-z][a-z0-9-\\\\]*+',
161161
self::TOKEN_PHPDOC_EOL => '\\r?+\\n[\\x09\\x20]*+(?:\\*(?!/)\\x20?+)?',
162162

163-
self::TOKEN_FLOAT => '(?:-?[0-9]++(_[0-9]++)*\\.[0-9]*(_[0-9]++)*+(?:e-?[0-9]++(_[0-9]++)*)?)|(?:-?[0-9]*+(_[0-9]++)*\\.[0-9]++(_[0-9]++)*(?:e-?[0-9]++(_[0-9]++)*)?)|(?:-?[0-9]++(_[0-9]++)*e-?[0-9]++(_[0-9]++)*)',
163+
self::TOKEN_FLOAT => '(?:-?[0-9]++(_[0-9]++)*\\.[0-9]*+(_[0-9]++)*(?:e[+-]?[0-9]++(_[0-9]++)*)?)|(?:-?[0-9]*+(_[0-9]++)*\\.[0-9]++(_[0-9]++)*(?:e[+-]?[0-9]++(_[0-9]++)*)?)|(?:-?[0-9]++(_[0-9]++)*e[+-]?[0-9]++(_[0-9]++)*)',
164164
self::TOKEN_INTEGER => '-?(?:(?:0b[0-1]++(_[0-1]++)*)|(?:0o[0-7]++(_[0-7]++)*)|(?:0x[0-9a-f]++(_[0-9a-f]++)*)|(?:[0-9]++(_[0-9]++)*))',
165165
self::TOKEN_SINGLE_QUOTED_STRING => '\'(?:\\\\[^\\r\\n]|[^\'\\r\\n\\\\])*+\'',
166166
self::TOKEN_DOUBLE_QUOTED_STRING => '"(?:\\\\[^\\r\\n]|[^"\\r\\n\\\\])*+"',

tests/PHPStan/Parser/ConstExprParserTest.php

+37-2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ public function provideIntegerNodeParseData(): Iterator
152152
new ConstExprIntegerNode('123'),
153153
];
154154

155+
yield [
156+
'-123',
157+
new ConstExprIntegerNode('-123'),
158+
];
159+
155160
yield [
156161
'0b0110101',
157162
new ConstExprIntegerNode('0b0110101'),
@@ -191,6 +196,11 @@ public function provideIntegerNodeParseData(): Iterator
191196
'-0X7_Fb_4',
192197
new ConstExprIntegerNode('-0X7Fb4'),
193198
];
199+
200+
yield [
201+
'18_446_744_073_709_551_616', // 64-bit unsigned long + 1, larger than PHP_INT_MAX
202+
new ConstExprIntegerNode('18446744073709551616'),
203+
];
194204
}
195205

196206

@@ -227,8 +237,8 @@ public function provideFloatNodeParseData(): Iterator
227237
];
228238

229239
yield [
230-
'-123',
231-
new ConstExprIntegerNode('-123'),
240+
'-123.',
241+
new ConstExprFloatNode('-123.'),
232242
];
233243

234244
yield [
@@ -260,6 +270,31 @@ public function provideFloatNodeParseData(): Iterator
260270
'-1_2.3_4e5_6',
261271
new ConstExprFloatNode('-12.34e56'),
262272
];
273+
274+
yield [
275+
'123.4e+8',
276+
new ConstExprFloatNode('123.4e+8'),
277+
];
278+
279+
yield [
280+
'.4e+8',
281+
new ConstExprFloatNode('.4e+8'),
282+
];
283+
284+
yield [
285+
'123E+80',
286+
new ConstExprFloatNode('123E+80'),
287+
];
288+
289+
yield [
290+
'8.2023437675747321', // greater precision than 64-bit double
291+
new ConstExprFloatNode('8.2023437675747321'),
292+
];
293+
294+
yield [
295+
'-0.0',
296+
new ConstExprFloatNode('-0.0'),
297+
];
263298
}
264299

265300

tests/PHPStan/Parser/TypeParserTest.php

+7
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,13 @@ public function provideParseData(): array
10951095
'123_456.789_012',
10961096
new ConstTypeNode(new ConstExprFloatNode('123456.789012')),
10971097
],
1098+
[
1099+
'18_446_744_073_709_551_616|8.2023437675747321e-18_446_744_073_709_551_617',
1100+
new UnionTypeNode([
1101+
new ConstTypeNode(new ConstExprIntegerNode('18446744073709551616')),
1102+
new ConstTypeNode(new ConstExprFloatNode('8.2023437675747321e-18446744073709551617')),
1103+
]),
1104+
],
10981105
[
10991106
'"bar"',
11001107
new ConstTypeNode(new QuoteAwareConstExprStringNode('bar', QuoteAwareConstExprStringNode::DOUBLE_QUOTED)),

0 commit comments

Comments
 (0)