Skip to content

Commit 0e17bb6

Browse files
authored
Merge pull request #1019 from schmittjoh/extra-type-tests
allow empty strings and numbers as metadata type parameters
2 parents 0898544 + 319d3b2 commit 0e17bb6

4 files changed

Lines changed: 72 additions & 29 deletions

File tree

src/Type/InnerParser.php

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,47 +25,56 @@ public function __construct()
2525
'skip' => '\s+',
2626
'parenthesis_' => '<',
2727
'_parenthesis' => '>',
28+
'empty_string' => '""|\'\'',
29+
'number' => '(\+|\-)?(0|[1-9]\d*)(\.\d+)?',
30+
'null' => 'null',
2831
'comma' => ',',
2932
'name' => '(?:[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*\\\)*[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*',
3033
'quote_:quoted_string' => '"',
3134
'apostrophe_:apostrophed_string' => '\'',
3235
],
3336
'quoted_string' => [
34-
'quoted_string' => '(?:[^"]|"")+',
37+
'quoted_string' => '[^"]+',
3538
'_quote:default' => '"',
3639
],
3740
'apostrophed_string' => [
38-
'apostrophed_string' => '(?:[^\']|\'\')+',
41+
'apostrophed_string' => '[^\']+',
3942
'_apostrophe:default' => '\'',
4043
],
4144
],
4245
[
4346
'type' => new Choice('type', ['simple_type', 'compound_type'], null),
4447
1 => new Token(1, 'name', null, -1, true),
4548
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),
53-
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),
49+
3 => new Token(3, 'number', null, -1, true),
50+
4 => new Concatenation(4, [3], '#simple_type'),
51+
5 => new Token(5, 'null', null, -1, true),
52+
6 => new Concatenation(6, [5], '#simple_type'),
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),
6271
],
6372
[]
6473
);
6574

6675
$this->getRule('type')->setPPRepresentation(' simple_type() | compound_type()');
6776
$this->getRule('simple_type')->setDefaultId('#simple_type');
68-
$this->getRule('simple_type')->setPPRepresentation(' <name> | ::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::');
6978
$this->getRule('compound_type')->setDefaultId('#compound_type');
7079
$this->getRule('compound_type')->setPPRepresentation(' <name> ::parenthesis_:: type() ( ::comma:: type() )* ::_parenthesis::');
7180
}

src/Type/TypeVisitor.php

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

43+
if ('empty_string' === $token) {
44+
return '';
45+
}
46+
47+
if ('null' === $token) {
48+
return null;
49+
}
50+
51+
if ('number' === $token) {
52+
return false === strpos($value, '.') ? intval($value) : floatval($value);
53+
}
54+
4355
$escapeChar = 'quoted_string' === $token ? '"' : "'";
4456

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

src/Type/grammar.pp

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

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

811
%token quote_ " -> quoted_string
9-
%token quoted_string:quoted_string (?:[^"]|"")+
12+
%token quoted_string:quoted_string [^"]+
1013
%token quoted_string:_quote " -> default
1114
1215
%token apostrophe_ ' -> apostrophed_string
13-
%token apostrophed_string:apostrophed_string (?:[^']|'')+
16+
%token apostrophed_string:apostrophed_string [^']+
1417
%token apostrophed_string:_apostrophe ' -> default
1518
1619
type:
1720
simple_type() | compound_type()
1821
1922
#simple_type:
2023
<name>
24+
| <number>
25+
| <null>
26+
| <empty_string>
2127
| ::quote_:: <quoted_string> ::_quote::
2228
| ::apostrophe_:: <apostrophed_string> ::_apostrophe::
2329

tests/Serializer/Type/ParserTest.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ 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<null>',
64+
$type('Foo', [null]),
65+
];
66+
yield [
67+
'Foo<\'a\',\'b\',\'c\'>',
68+
$type('Foo', ['a', 'b', 'c']),
69+
];
70+
yield [
71+
'Foo<\'a\',\'\'>',
72+
$type('Foo', ['a', '']),
73+
];
5074
yield [
5175
'array<Foo,Bar>',
5276
$type('array', [['name' => 'Foo', 'params' => []], ['name' => 'Bar', 'params' => []]]),
@@ -72,14 +96,6 @@ public function validTypesProvider(): iterable
7296
'Foo<"asdf asdf">',
7397
$type('Foo', ['asdf asdf']),
7498
];
75-
yield [
76-
'Foo<"""bar""">',
77-
$type('Foo', ['"bar"']),
78-
];
79-
yield [
80-
"Foo<'a''b'>",
81-
$type('Foo', ["a'b"]),
82-
];
8399
}
84100

85101
public function testEmptyString(): void

0 commit comments

Comments
 (0)