Skip to content

Commit 319d3b2

Browse files
committed
allow also null as parameters
1 parent 93eecd5 commit 319d3b2

4 files changed

Lines changed: 31 additions & 18 deletions

File tree

src/Type/InnerParser.php

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public function __construct()
2727
'_parenthesis' => '>',
2828
'empty_string' => '""|\'\'',
2929
'number' => '(\+|\-)?(0|[1-9]\d*)(\.\d+)?',
30+
'null' => 'null',
3031
'comma' => ',',
3132
'name' => '(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\\)*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*',
3233
'quote_:quoted_string' => '"',
@@ -47,31 +48,33 @@ public function __construct()
4748
2 => new Concatenation(2, [1], '#simple_type'),
4849
3 => new Token(3, 'number', null, -1, true),
4950
4 => new Concatenation(4, [3], '#simple_type'),
50-
5 => new Token(5, 'empty_string', null, -1, true),
51+
5 => new Token(5, 'null', null, -1, true),
5152
6 => new Concatenation(6, [5], '#simple_type'),
52-
7 => new Token(7, 'quote_', null, -1, false),
53-
8 => new Token(8, 'quoted_string', null, -1, true),
54-
9 => new Token(9, '_quote', null, -1, false),
55-
10 => new Concatenation(10, [7, 8, 9], '#simple_type'),
56-
11 => new Token(11, 'apostrophe_', null, -1, false),
57-
12 => new Token(12, 'apostrophed_string', null, -1, true),
58-
13 => new Token(13, '_apostrophe', null, -1, false),
59-
14 => new Concatenation(14, [11, 12, 13], '#simple_type'),
60-
'simple_type' => new Choice('simple_type', [2, 4, 6, 10, 14], null),
61-
16 => new Token(16, 'name', null, -1, true),
62-
17 => new Token(17, 'parenthesis_', null, -1, false),
63-
18 => new Token(18, 'comma', null, -1, false),
64-
19 => new Concatenation(19, [18, 'type'], '#compound_type'),
65-
20 => new Repetition(20, 0, -1, 19, null),
66-
21 => new Token(21, '_parenthesis', null, -1, false),
67-
'compound_type' => new Concatenation('compound_type', [16, 17, 'type', 20, 21], null),
53+
7 => new Token(7, 'empty_string', null, -1, true),
54+
8 => new Concatenation(8, [7], '#simple_type'),
55+
9 => new Token(9, 'quote_', null, -1, false),
56+
10 => new Token(10, 'quoted_string', null, -1, true),
57+
11 => new Token(11, '_quote', null, -1, false),
58+
12 => new Concatenation(12, [9, 10, 11], '#simple_type'),
59+
13 => new Token(13, 'apostrophe_', null, -1, false),
60+
14 => new Token(14, 'apostrophed_string', null, -1, true),
61+
15 => new Token(15, '_apostrophe', null, -1, false),
62+
16 => new Concatenation(16, [13, 14, 15], '#simple_type'),
63+
'simple_type' => new Choice('simple_type', [2, 4, 6, 8, 12, 16], null),
64+
18 => new Token(18, 'name', null, -1, true),
65+
19 => new Token(19, 'parenthesis_', null, -1, false),
66+
20 => new Token(20, 'comma', null, -1, false),
67+
21 => new Concatenation(21, [20, 'type'], '#compound_type'),
68+
22 => new Repetition(22, 0, -1, 21, null),
69+
23 => new Token(23, '_parenthesis', null, -1, false),
70+
'compound_type' => new Concatenation('compound_type', [18, 19, 'type', 22, 23], null),
6871
],
6972
[]
7073
);
7174

7275
$this->getRule('type')->setPPRepresentation(' simple_type() | compound_type()');
7376
$this->getRule('simple_type')->setDefaultId('#simple_type');
74-
$this->getRule('simple_type')->setPPRepresentation(' <name> | <number> | <empty_string> | ::quote_:: <quoted_string> ::_quote:: | ::apostrophe_:: <apostrophed_string> ::_apostrophe::');
77+
$this->getRule('simple_type')->setPPRepresentation(' <name> | <number> | <null> | <empty_string> | ::quote_:: <quoted_string> ::_quote:: | ::apostrophe_:: <apostrophed_string> ::_apostrophe::');
7578
$this->getRule('compound_type')->setDefaultId('#compound_type');
7679
$this->getRule('compound_type')->setPPRepresentation(' <name> ::parenthesis_:: type() ( ::comma:: type() )* ::_parenthesis::');
7780
}

src/Type/TypeVisitor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ private function visitSimpleType(TreeNode $element)
4444
return '';
4545
}
4646

47+
if ('null' === $token) {
48+
return null;
49+
}
50+
4751
if ('number' === $token) {
4852
return false === strpos($value, '.') ? intval($value) : floatval($value);
4953
}

src/Type/grammar.pp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
%token _parenthesis >
55
%token empty_string ""|''
66
%token number (\+|\-)?(0|[1-9]\d*)(\.\d+)?
7+
%token null null
78
%token comma ,
89
%token name (?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\)*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
910

@@ -21,6 +22,7 @@
2122
#simple_type:
2223
<name>
2324
| <number>
25+
| <null>
2426
| <empty_string>
2527
| ::quote_:: <quoted_string> ::_quote::
2628
| ::apostrophe_:: <apostrophed_string> ::_apostrophe::

tests/Serializer/Type/ParserTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public function validTypesProvider(): iterable
5959
'Foo<5.5>',
6060
$type('Foo', [5.5]),
6161
];
62+
yield [
63+
'Foo<null>',
64+
$type('Foo', [null]),
65+
];
6266
yield [
6367
'Foo<\'a\',\'b\',\'c\'>',
6468
$type('Foo', ['a', 'b', 'c']),

0 commit comments

Comments
 (0)