Skip to content

Commit

Permalink
Add "+" sign support for float exponent
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored May 29, 2023
1 parent 362bc12 commit 8e75539
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
6 changes: 3 additions & 3 deletions doc/grammars/type.abnf
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ ConstantFloat
/ ["-"] "." 1*ByteDecDigit *("_" 1*ByteDecDigit) [ConstantFloatExp]

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

ConstantInt
= ["-"] "0b" 1*ByteBinDigit *("_" 1*ByteBinDigit)
Expand Down Expand Up @@ -229,14 +229,14 @@ ByteHexDigit

ByteIdentifierFirst
= %x41-5A ; A-Z
/ %x5F ; _
/ "_"
/ %x61-7A ; a-z
/ %x80-FF

ByteIdentifierSecond
= %x30-39 ; 0-9
/ %x41-5A ; A-Z
/ %x5F ; _
/ "_"
/ %x61-7A ; a-z
/ %x80-FF

Expand Down
2 changes: 1 addition & 1 deletion src/Lexer/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private function generateRegexp(): string
self::TOKEN_PHPDOC_TAG => '@(?:[a-z][a-z0-9-\\\\]+:)?[a-z][a-z0-9-\\\\]*+',
self::TOKEN_PHPDOC_EOL => '\\r?+\\n[\\x09\\x20]*+(?:\\*(?!/)\\x20?+)?',

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]++)*)',
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]++)*)',
self::TOKEN_INTEGER => '-?(?:(?:0b[0-1]++(_[0-1]++)*)|(?:0o[0-7]++(_[0-7]++)*)|(?:0x[0-9a-f]++(_[0-9a-f]++)*)|(?:[0-9]++(_[0-9]++)*))',
self::TOKEN_SINGLE_QUOTED_STRING => '\'(?:\\\\[^\\r\\n]|[^\'\\r\\n\\\\])*+\'',
self::TOKEN_DOUBLE_QUOTED_STRING => '"(?:\\\\[^\\r\\n]|[^"\\r\\n\\\\])*+"',
Expand Down
39 changes: 37 additions & 2 deletions tests/PHPStan/Parser/ConstExprParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ public function provideIntegerNodeParseData(): Iterator
new ConstExprIntegerNode('123'),
];

yield [
'-123',
new ConstExprIntegerNode('-123'),
];

yield [
'0b0110101',
new ConstExprIntegerNode('0b0110101'),
Expand Down Expand Up @@ -191,6 +196,11 @@ public function provideIntegerNodeParseData(): Iterator
'-0X7_Fb_4',
new ConstExprIntegerNode('-0X7Fb4'),
];

yield [
'18_446_744_073_709_551_616', // 64-bit unsigned long + 1, larger than PHP_INT_MAX
new ConstExprIntegerNode('18446744073709551616'),
];
}


Expand Down Expand Up @@ -227,8 +237,8 @@ public function provideFloatNodeParseData(): Iterator
];

yield [
'-123',
new ConstExprIntegerNode('-123'),
'-123.',
new ConstExprFloatNode('-123.'),
];

yield [
Expand Down Expand Up @@ -260,6 +270,31 @@ public function provideFloatNodeParseData(): Iterator
'-1_2.3_4e5_6',
new ConstExprFloatNode('-12.34e56'),
];

yield [
'123.4e+8',
new ConstExprFloatNode('123.4e+8'),
];

yield [
'.4e+8',
new ConstExprFloatNode('.4e+8'),
];

yield [
'123E+80',
new ConstExprFloatNode('123E+80'),
];

yield [
'8.2023437675747321', // greater precision than 64-bit double
new ConstExprFloatNode('8.2023437675747321'),
];

yield [
'-0.0',
new ConstExprFloatNode('-0.0'),
];
}


Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,13 @@ public function provideParseData(): array
'123_456.789_012',
new ConstTypeNode(new ConstExprFloatNode('123456.789012')),
],
[
'18_446_744_073_709_551_616|8.2023437675747321e-18_446_744_073_709_551_617',
new UnionTypeNode([
new ConstTypeNode(new ConstExprIntegerNode('18446744073709551616')),
new ConstTypeNode(new ConstExprFloatNode('8.2023437675747321e-18446744073709551617')),
]),
],
[
'"bar"',
new ConstTypeNode(new QuoteAwareConstExprStringNode('bar', QuoteAwareConstExprStringNode::DOUBLE_QUOTED)),
Expand Down

0 comments on commit 8e75539

Please sign in to comment.