Skip to content

Commit 93eecd5

Browse files
committed
allow empty strings and numbers as parameters
1 parent c976e8e commit 93eecd5

4 files changed

Lines changed: 58 additions & 28 deletions

File tree

src/Type/InnerParser.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,53 @@ public function __construct()
2525
'skip' => '\s+',
2626
'parenthesis_' => '<',
2727
'_parenthesis' => '>',
28+
'empty_string' => '""|\'\'',
29+
'number' => '(\+|\-)?(0|[1-9]\d*)(\.\d+)?',
2830
'comma' => ',',
2931
'name' => '(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\\)*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*',
3032
'quote_:quoted_string' => '"',
3133
'apostrophe_:apostrophed_string' => '\'',
3234
],
3335
'quoted_string' => [
34-
'quoted_string' => '(?:[^"]|"")+',
36+
'quoted_string' => '[^"]+',
3537
'_quote:default' => '"',
3638
],
3739
'apostrophed_string' => [
38-
'apostrophed_string' => '(?:[^\']|\'\')+',
40+
'apostrophed_string' => '[^\']+',
3941
'_apostrophe:default' => '\'',
4042
],
4143
],
4244
[
4345
'type' => new Choice('type', ['simple_type', 'compound_type'], null),
4446
1 => new Token(1, 'name', null, -1, true),
4547
2 => new Concatenation(2, [1], '#simple_type'),
46-
3 => new Token(3, 'quote_', null, -1, false),
47-
4 => new Token(4, 'quoted_string', null, -1, true),
48-
5 => new Token(5, '_quote', null, -1, false),
49-
6 => new Concatenation(6, [3, 4, 5], '#simple_type'),
50-
7 => new Token(7, 'apostrophe_', null, -1, false),
51-
8 => new Token(8, 'apostrophed_string', null, -1, true),
52-
9 => new Token(9, '_apostrophe', null, -1, false),
48+
3 => new Token(3, 'number', null, -1, true),
49+
4 => new Concatenation(4, [3], '#simple_type'),
50+
5 => new Token(5, 'empty_string', null, -1, true),
51+
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),
5355
10 => new Concatenation(10, [7, 8, 9], '#simple_type'),
54-
'simple_type' => new Choice('simple_type', [2, 6, 10], null),
55-
12 => new Token(12, 'name', null, -1, true),
56-
13 => new Token(13, 'parenthesis_', null, -1, false),
57-
14 => new Token(14, 'comma', null, -1, false),
58-
15 => new Concatenation(15, [14, 'type'], '#compound_type'),
59-
16 => new Repetition(16, 0, -1, 15, null),
60-
17 => new Token(17, '_parenthesis', null, -1, false),
61-
'compound_type' => new Concatenation('compound_type', [12, 13, 'type', 16, 17], null),
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),
6268
],
6369
[]
6470
);
6571

6672
$this->getRule('type')->setPPRepresentation(' simple_type() | compound_type()');
6773
$this->getRule('simple_type')->setDefaultId('#simple_type');
68-
$this->getRule('simple_type')->setPPRepresentation(' <name> | ::quote_:: <quoted_string> ::_quote:: | ::apostrophe_:: <apostrophed_string> ::_apostrophe::');
74+
$this->getRule('simple_type')->setPPRepresentation(' <name> | <number> | <empty_string> | ::quote_:: <quoted_string> ::_quote:: | ::apostrophe_:: <apostrophed_string> ::_apostrophe::');
6975
$this->getRule('compound_type')->setDefaultId('#compound_type');
7076
$this->getRule('compound_type')->setPPRepresentation(' <name> ::parenthesis_:: type() ( ::comma:: type() )* ::_parenthesis::');
7177
}

src/Type/TypeVisitor.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ private function visitSimpleType(TreeNode $element)
4040
return ['name' => $value, 'params' => []];
4141
}
4242

43+
if ('empty_string' === $token) {
44+
return '';
45+
}
46+
47+
if ('number' === $token) {
48+
return false === strpos($value, '.') ? intval($value) : floatval($value);
49+
}
50+
4351
$escapeChar = 'quoted_string' === $token ? '"' : "'";
4452

4553
if (false === strpos($value, $escapeChar)) {

src/Type/grammar.pp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
%token parenthesis_ <
44
%token _parenthesis >
5+
%token empty_string ""|''
6+
%token number (\+|\-)?(0|[1-9]\d*)(\.\d+)?
57
%token comma ,
68
%token name (?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\)*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*
79

810
%token quote_ " -> quoted_string
9-
%token quoted_string:quoted_string (?:[^"]|"")+
11+
%token quoted_string:quoted_string [^"]+
1012
%token quoted_string:_quote " -> default
1113
1214
%token apostrophe_ ' -> apostrophed_string
13-
%token apostrophed_string:apostrophed_string (?:[^']|'')+
15+
%token apostrophed_string:apostrophed_string [^']+
1416
%token apostrophed_string:_apostrophe ' -> default
1517
1618
type:
1719
simple_type() | compound_type()
1820
1921
#simple_type:
2022
<name>
23+
| <number>
24+
| <empty_string>
2125
| ::quote_:: <quoted_string> ::_quote::
2226
| ::apostrophe_:: <apostrophed_string> ::_apostrophe::
2327

tests/Serializer/Type/ParserTest.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@ public function validTypesProvider(): iterable
4747
'array<Foo>',
4848
$type('array', [['name' => 'Foo', 'params' => []]]),
4949
];
50+
yield [
51+
'Foo<\'a\'>',
52+
$type('Foo', ['a']),
53+
];
54+
yield [
55+
'Foo<5>',
56+
$type('Foo', [5]),
57+
];
58+
yield [
59+
'Foo<5.5>',
60+
$type('Foo', [5.5]),
61+
];
62+
yield [
63+
'Foo<\'a\',\'b\',\'c\'>',
64+
$type('Foo', ['a', 'b', 'c']),
65+
];
66+
yield [
67+
'Foo<\'a\',\'\'>',
68+
$type('Foo', ['a', '']),
69+
];
5070
yield [
5171
'array<Foo,Bar>',
5272
$type('array', [['name' => 'Foo', 'params' => []], ['name' => 'Bar', 'params' => []]]),
@@ -72,14 +92,6 @@ public function validTypesProvider(): iterable
7292
'Foo<"asdf asdf">',
7393
$type('Foo', ['asdf asdf']),
7494
];
75-
yield [
76-
'Foo<"""bar""">',
77-
$type('Foo', ['"bar"']),
78-
];
79-
yield [
80-
"Foo<'a''b'>",
81-
$type('Foo', ["a'b"]),
82-
];
8395
}
8496

8597
public function testEmptyString(): void

0 commit comments

Comments
 (0)