Skip to content

Commit

Permalink
Accept arrays as type attribute values
Browse files Browse the repository at this point in the history
  • Loading branch information
Majkl578 committed Sep 19, 2019
1 parent ff89778 commit 8999769
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/Type/InnerParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function __construct()
[
'default' => [
'skip' => '\s+',
'array_' => '\[',
'_array' => '\]',
'parenthesis_' => '<',
'_parenthesis' => '>',
'empty_string' => '""|\'\'',
Expand Down Expand Up @@ -60,22 +62,33 @@ public function __construct()
14 => new Token(14, 'apostrophed_string', null, -1, true),
15 => new Token(15, '_apostrophe', null, -1, false),
16 => new Concatenation(16, [13, 14, 15], '#simple_type'),
'simple_type' => new Choice('simple_type', [2, 4, 6, 8, 12, 16], null),
18 => new Token(18, 'name', null, -1, true),
19 => new Token(19, 'parenthesis_', null, -1, false),
20 => new Token(20, 'comma', null, -1, false),
21 => new Concatenation(21, [20, 'type'], '#compound_type'),
22 => new Repetition(22, 0, -1, 21, null),
23 => new Token(23, '_parenthesis', null, -1, false),
'compound_type' => new Concatenation('compound_type', [18, 19, 'type', 22, 23], null),
17 => new Concatenation(17, ['array'], '#simple_type'),
'simple_type' => new Choice('simple_type', [2, 4, 6, 8, 12, 16, 17], null),
19 => new Token(19, 'name', null, -1, true),
20 => new Token(20, 'parenthesis_', null, -1, false),
21 => new Token(21, 'comma', null, -1, false),
22 => new Concatenation(22, [21, 'type'], '#compound_type'),
23 => new Repetition(23, 0, -1, 22, null),
24 => new Token(24, '_parenthesis', null, -1, false),
'compound_type' => new Concatenation('compound_type', [19, 20, 'type', 23, 24], null),
26 => new Token(26, 'array_', null, -1, false),
27 => new Token(27, 'comma', null, -1, false),
28 => new Concatenation(28, [27, 'simple_type'], '#array'),
29 => new Repetition(29, 0, -1, 28, null),
30 => new Concatenation(30, ['simple_type', 29], null),
31 => new Repetition(31, 0, 1, 30, null),
32 => new Token(32, '_array', null, -1, false),
'array' => new Concatenation('array', [26, 31, 32], null),
],
[]
);

$this->getRule('type')->setPPRepresentation(' simple_type() | compound_type()');
$this->getRule('simple_type')->setDefaultId('#simple_type');
$this->getRule('simple_type')->setPPRepresentation(' <name> | <number> | <null> | <empty_string> | ::quote_:: <quoted_string> ::_quote:: | ::apostrophe_:: <apostrophed_string> ::_apostrophe::');
$this->getRule('simple_type')->setPPRepresentation(' <name> | <number> | <null> | <empty_string> | ::quote_:: <quoted_string> ::_quote:: | ::apostrophe_:: <apostrophed_string> ::_apostrophe:: | array()');
$this->getRule('compound_type')->setDefaultId('#compound_type');
$this->getRule('compound_type')->setPPRepresentation(' <name> ::parenthesis_:: type() ( ::comma:: type() )* ::_parenthesis::');
$this->getRule('array')->setDefaultId('#array');
$this->getRule('array')->setPPRepresentation(' ::array_:: ( simple_type() ( ::comma:: simple_type() )* )? ::_array::');
}
}
17 changes: 17 additions & 0 deletions src/Type/TypeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public function visit(Element $element, &$handle = null, $eldnah = null)
return $this->visitSimpleType($element);
case '#compound_type':
return $this->visitCompoundType($element, $handle, $eldnah);
case '#array':
return $this->visitArrayType($element, $handle, $eldnah);
}

throw new InvalidNode();
Expand All @@ -33,6 +35,11 @@ public function visit(Element $element, &$handle = null, $eldnah = null)
private function visitSimpleType(TreeNode $element)
{
$tokenNode = $element->getChild(0);

if (!$tokenNode->isToken()) {
return $tokenNode->accept($this);
}

$token = $tokenNode->getValueToken();
$value = $tokenNode->getValueValue();

Expand Down Expand Up @@ -76,4 +83,14 @@ function (TreeNode $node) use ($handle, $eldnah) {
),
];
}

private function visitArrayType(TreeNode $node, ?int &$handle, ?int $eldnah): array
{
return array_map(
function (TreeNode $child) {
return $child->accept($this);
},
$node->getChildren()
);
}
}
6 changes: 6 additions & 0 deletions src/Type/grammar.pp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
%skip whitespace \s+

%token array_ \[
%token _array \]
%token parenthesis_ <
%token _parenthesis >
%token empty_string ""|''
Expand All @@ -26,10 +28,14 @@
| <empty_string>
| ::quote_:: <quoted_string> ::_quote::
| ::apostrophe_:: <apostrophed_string> ::_apostrophe::
| array()
#compound_type:
<name>
::parenthesis_::
type()
( ::comma:: type() )*
::_parenthesis::
#array:
::array_:: ( simple_type() ( ::comma:: simple_type() )* )? ::_array::
20 changes: 20 additions & 0 deletions tests/Serializer/Type/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ public function validTypesProvider(): iterable
'Foo<"asdf asdf">',
$type('Foo', ['asdf asdf']),
];
yield [
'Foo<[]>',
$type('Foo', [[]]),
];
yield [
'Foo<[[]]>',
$type('Foo', [[[]]]),
];
yield [
'Foo<[123]>',
$type('Foo', [[123]]),
];
yield [
'Foo<[123, 456]>',
$type('Foo', [[123, 456]]),
];
yield [
'Foo<[[123], 456, "bar"]>',
$type('Foo', [[[123], 456, 'bar']]),
];
}

public function testEmptyString(): void
Expand Down

0 comments on commit 8999769

Please sign in to comment.