-
Notifications
You must be signed in to change notification settings - Fork 2
/
MultipleLabelSelectionGroupConverter.php
73 lines (68 loc) · 2.41 KB
/
MultipleLabelSelectionGroupConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace Polygen\Language\Preprocessing\ConcreteToAbstractConversion;
use Polygen\Grammar\Atom;
use Polygen\Grammar\Interfaces\Node;
use Polygen\Grammar\LabelSelection;
use Polygen\Grammar\Production;
use Polygen\Grammar\ProductionCollection;
use Polygen\Grammar\Sequence;
use Polygen\Grammar\Subproduction;
use Polygen\Grammar\Unfoldable\UnfoldableBuilder;
use Polygen\Utils\DeclarationCollection;
/**
* Bonus conversion step: This is not mentioned in the official Polygen guide, as it deals with a syntax that is never
* described in the formal language representations, but that it is indeed supported. The goal is to convert
* Something.label1.(label2|label3).label4..label5
* (where the double dot is not a mistake) into
* (((((Something).label1).(label2|label3)).label4).).label5
*/
class MultipleLabelSelectionGroupConverter implements ConverterInterface
{
/**
* @return int
*/
public function getPriority()
{
return -1;
}
/**
* @param Atom|Node $node
* @return Node
*/
public function convert(Node $node, DeclarationCollection $context)
{
$nestedSelection = null;
$labelSelections = $node->getLabelSelections()->getLabelSelections();
$nestedSelection = Atom\AtomBuilder::like($node)->withLabelSelection(array_shift($labelSelections))->build();
foreach ($labelSelections as $labelSelection) {
$nestedSelection = Atom\AtomBuilder::get()
->withUnfoldable(
UnfoldableBuilder::get()
->simple()
->withContents(
new Subproduction(
[],
new ProductionCollection([
new Production(
new Sequence([
$nestedSelection
])
)
])
)
)
->build()
)
->withLabelSelection($labelSelection)
->build();
}
return $nestedSelection;
}
/**
* @return bool
*/
public function canConvert(Node $node)
{
return $node instanceof Atom && $node->getLabelSelections()->count() > 1;
}
}