Skip to content

Commit

Permalink
Add general "+" sign support for integer/float numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored and ondrejmirtes committed May 30, 2023
1 parent 8e75539 commit 492f9b8
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
20 changes: 12 additions & 8 deletions doc/grammars/type.abnf
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,18 @@ ConstantExpr
/ ConstantFetch *ByteHorizontalWs

ConstantFloat
= ["-"] 1*ByteDecDigit *("_" 1*ByteDecDigit) "." [1*ByteDecDigit *("_" 1*ByteDecDigit)] [ConstantFloatExp]
/ ["-"] 1*ByteDecDigit *("_" 1*ByteDecDigit) ConstantFloatExp
/ ["-"] "." 1*ByteDecDigit *("_" 1*ByteDecDigit) [ConstantFloatExp]
= [ByteNumberSign] 1*ByteDecDigit *("_" 1*ByteDecDigit) "." [1*ByteDecDigit *("_" 1*ByteDecDigit)] [ConstantFloatExp]
/ [ByteNumberSign] 1*ByteDecDigit *("_" 1*ByteDecDigit) ConstantFloatExp
/ [ByteNumberSign] "." 1*ByteDecDigit *("_" 1*ByteDecDigit) [ConstantFloatExp]

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

ConstantInt
= ["-"] "0b" 1*ByteBinDigit *("_" 1*ByteBinDigit)
/ ["-"] "0o" 1*ByteOctDigit *("_" 1*ByteOctDigit)
/ ["-"] "0x" 1*ByteHexDigit *("_" 1*ByteHexDigit)
/ ["-"] 1*ByteDecDigit *("_" 1*ByteDecDigit)
= [ByteNumberSign] "0b" 1*ByteBinDigit *("_" 1*ByteBinDigit)
/ [ByteNumberSign] "0o" 1*ByteOctDigit *("_" 1*ByteOctDigit)
/ [ByteNumberSign] "0x" 1*ByteHexDigit *("_" 1*ByteHexDigit)
/ [ByteNumberSign] 1*ByteDecDigit *("_" 1*ByteDecDigit)

ConstantTrue
= "true"
Expand Down Expand Up @@ -213,6 +213,10 @@ ByteHorizontalWs
= %x09 ; horizontal tab
/ %x20 ; space

ByteNumberSign
= "+"
/ "-"

ByteBinDigit
= %x30-31 ; 0-1

Expand Down
4 changes: 2 additions & 2 deletions src/Lexer/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ 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_INTEGER => '-?(?:(?:0b[0-1]++(_[0-1]++)*)|(?:0o[0-7]++(_[0-7]++)*)|(?:0x[0-9a-f]++(_[0-9a-f]++)*)|(?:[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
10 changes: 10 additions & 0 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 [
'-123',
new ConstExprIntegerNode('-123'),
Expand Down Expand Up @@ -236,6 +241,11 @@ public function provideFloatNodeParseData(): Iterator
new ConstExprFloatNode('12.3e4'),
];

yield [
'+123.5',
new ConstExprFloatNode('+123.5'),
];

yield [
'-123.',
new ConstExprFloatNode('-123.'),
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Parser/TypeParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,14 @@ public function provideParseData(): array
'123_456.789_012',
new ConstTypeNode(new ConstExprFloatNode('123456.789012')),
],
[
'+0x10_20|+8e+2 | -0b11',
new UnionTypeNode([
new ConstTypeNode(new ConstExprIntegerNode('+0x1020')),
new ConstTypeNode(new ConstExprFloatNode('+8e+2')),
new ConstTypeNode(new ConstExprIntegerNode('-0b11')),
]),
],
[
'18_446_744_073_709_551_616|8.2023437675747321e-18_446_744_073_709_551_617',
new UnionTypeNode([
Expand Down

0 comments on commit 492f9b8

Please sign in to comment.