Skip to content

Commit d6f0bda

Browse files
committed
Add visitor system to Vocabulary\Symbol
1 parent 35fb8c7 commit d6f0bda

File tree

9 files changed

+101
-0
lines changed

9 files changed

+101
-0
lines changed

src/GrammarProcessing/Vocabulary/Literal.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ public function acceptNode(
3737
return new GrammarProcessing\TokenNode($token);
3838
}
3939

40+
41+
42+
public function visit(callable $visitor): Symbol
43+
{
44+
return $visitor($this);
45+
}
46+
4047
}

src/GrammarProcessing/Vocabulary/NegativeLookahead.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,11 @@ public function acceptNode(
3333
return new GrammarProcessing\EmptyNode('NegativeLookahead');
3434
}
3535

36+
37+
38+
public function visit(callable $visitor): Symbol
39+
{
40+
return $visitor($this);
41+
}
42+
3643
}

src/GrammarProcessing/Vocabulary/Nonterminal.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,11 @@ public function acceptNode(
3232
: new GrammarProcessing\TokenNode($tokenStream->consumeTokenWithType($this->nonterminal));
3333
}
3434

35+
36+
37+
public function visit(callable $visitor): Symbol
38+
{
39+
return $visitor($this);
40+
}
41+
3542
}

src/GrammarProcessing/Vocabulary/OneOf.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,21 @@ public function acceptNode(
6767
return $result['node'];
6868
}
6969

70+
71+
72+
public function visit(callable $visitor): Symbol
73+
{
74+
$result = [];
75+
76+
foreach ($this->symbols as $symbol) {
77+
$result[] = $symbol->visit($visitor);
78+
}
79+
80+
return $visitor(
81+
$result === $this->symbols
82+
? $this
83+
: new self($result),
84+
);
85+
}
86+
7087
}

src/GrammarProcessing/Vocabulary/Regexp.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,11 @@ public function acceptNode(
3737
);
3838
}
3939

40+
41+
42+
public function visit(callable $visitor): Symbol
43+
{
44+
return $visitor($this);
45+
}
46+
4047
}

src/GrammarProcessing/Vocabulary/Repeat.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,19 @@ public function acceptNode(
6969
return new GrammarProcessing\ListNode($result);
7070
}
7171

72+
73+
74+
public function visit(callable $visitor): Symbol
75+
{
76+
$visitedSymbol = $this->symbol->visit($visitor);
77+
78+
return $visitor(
79+
$visitedSymbol === $this->symbol ? $this : new self(
80+
$visitedSymbol,
81+
$this->min,
82+
$this->max,
83+
),
84+
);
85+
}
86+
7287
}

src/GrammarProcessing/Vocabulary/Sequence.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,21 @@ public function acceptNode(
4242
return new GrammarProcessing\ListNode($result);
4343
}
4444

45+
46+
47+
public function visit(callable $visitor): Symbol
48+
{
49+
$visitedSymbols = [];
50+
51+
foreach ($this->symbols as $symbol) {
52+
$visitedSymbols[] = $symbol->visit($visitor);
53+
}
54+
55+
return $visitor(
56+
$visitedSymbols === $this->symbols
57+
? $this
58+
: new self($visitedSymbols),
59+
);
60+
}
61+
4562
}

src/GrammarProcessing/Vocabulary/Subtract.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,21 @@ public function acceptNode(
5555
return $node;
5656
}
5757

58+
59+
60+
public function visit(callable $visitor): Symbol
61+
{
62+
$visitedBaseSymbol = $this->baseSymbol->visit($visitor);
63+
$visitedSubtractedSymbol = $this->subtractedSymbol->visit($visitor);
64+
65+
$isDifferent = $visitedBaseSymbol !== $this->baseSymbol || $visitedSubtractedSymbol !== $this->subtractedSymbol;
66+
67+
return $visitor(
68+
$isDifferent ? $this : new self(
69+
$visitedBaseSymbol,
70+
$visitedSubtractedSymbol,
71+
),
72+
);
73+
}
74+
5875
}

src/GrammarProcessing/Vocabulary/Symbol.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,11 @@ function acceptNode(
2424
array $nonterminals,
2525
): GrammarProcessing\Node;
2626

27+
28+
29+
/**
30+
* @param callable(Symbol): Symbol $visitor
31+
*/
32+
function visit(callable $visitor): Symbol;
33+
2734
}

0 commit comments

Comments
 (0)