Skip to content

Commit 7f2fd51

Browse files
committed
Output SelectedNode from OneOf
1 parent a4dec81 commit 7f2fd51

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Vojtechdobes\GrammarProcessing;
4+
5+
use LogicException;
6+
7+
8+
final class SelectedNode implements Node
9+
{
10+
11+
public string $name {
12+
13+
get {
14+
throw new LogicException(
15+
self::class . " must be parsed manually",
16+
);
17+
}
18+
19+
}
20+
21+
22+
23+
public function __construct(
24+
public readonly int $index,
25+
public readonly Node $value,
26+
) {}
27+
28+
}

src/GrammarProcessing/Vocabulary/OneOf.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ public function acceptNode(
3636
$attempts = [];
3737
$localError = new GrammarProcessing\Error();
3838

39-
foreach ($this->symbols as $symbol) {
39+
foreach ($this->symbols as $i => $symbol) {
4040
$tokenStreamCopy = clone $tokenStream;
4141

4242
try {
4343
$node = $symbol->acceptNode($error, $tokenStreamCopy, $nonterminals);
4444

4545
$attempts[] = [
46+
'index' => $i,
4647
'node' => $node,
4748
'tokenStream' => $tokenStreamCopy,
4849
];
@@ -64,7 +65,11 @@ public function acceptNode(
6465
$result = array_pop($attempts);
6566

6667
$tokenStream->advanceTo($result['tokenStream']);
67-
return $result['node'];
68+
69+
return new GrammarProcessing\SelectedNode(
70+
$result['index'],
71+
$result['node'],
72+
);
6873
}
6974

7075

0 commit comments

Comments
 (0)