packages/sql-parser/src/Automaton/Lr0Automaton.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlParser\Automaton;
6
7/**
8 * The states of an LR(0) automaton, their transitions and their completed rules.
9 *
10 * @visibility root
11 */
12final class Lr0Automaton
13{
14 /**
15 * @param list<list<int>> $kernels Kernel items of each state, sorted
16 * @param list<array<int, int>> $transitions Target state by symbol, per state
17 * @param list<list<int>> $reductions Rules completed in each state, in rule order
18 */
19 public function __construct(
20 public readonly array $kernels,
21 public readonly array $transitions,
22 public readonly array $reductions,
23 ) {
24 }
25
26 /**
27 * Answers how many states there are.
28 *
29 * @return int State count
30 */
31 public function stateCount(): int
32 {
33 return count($this->kernels);
34 }
35
36 /**
37 * Answers the state reached on a symbol, if any.
38 *
39 * @param int $state State to leave
40 * @param int $symbol Symbol to move on
41 *
42 * @return int|null The target state, or null when the state has no such transition
43 */
44 public function transition(int $state, int $symbol): ?int
45 {
46 return $this->transitions[$state][$symbol] ?? null;
47 }
48}
49