final class GrammarBuilder
Restricted visibility: declared "@visibility root". Code outside that scope must not name this declaration.

Collects the declarations of a grammar source and numbers them into a grammar.

Readers meet symbols in whatever order the source spells them, so names are gathered first and numbered only when the grammar is built: terminals in declaration order after the end marker, then the accept symbol and the nonterminals in the order their rules appear.

Constants§

public const ACCEPT = '$accept'

The name of the nonterminal the augmented start rule derives.

Methods§

public function terminal(string $name): void

Declares a terminal, keeping the position of its first declaration.

Parameters

$namestringTerminal name
Test cases 30
Called from 10
public function nonterminal(string $name): void

Declares a nonterminal, keeping the position of its first declaration.

Parameters

$namestringNonterminal name
Test cases 1
Called from 1
public function isNonterminal(string $name): bool

Reports whether a name has been declared as a nonterminal.

Parameters

$namestringSymbol name

Returns

bool True when a rule derives the name
Test cases 3
public function rule(
    string $lhs,
    list<string> $rhs,
    string|null $precedenceSymbol = null,
    bool $hidden = false,
): void

Records one production.

The left-hand side becomes a nonterminal; right-hand names are resolved when the grammar is built, so they may be declared later.

Parameters

$lhsstringNonterminal the rule derives
$rhslist<string>Symbol names the rule expands to
$precedenceSymbolstring|nullTerminal named to lend its precedence
$hiddenboolWhether the rule stands in for a mid-rule action
Test cases 40
Called from 3
Calls 1
public function start(string $name): void

Names the start symbol; the first rule's left-hand side is used otherwise.

Parameters

$namestringNonterminal to start from
Test cases 1
Called from 2
public function expect(int $count): void

Records how many shift/reduce conflicts the source declares as expected.

Parameters

$countintDeclared conflict count
Test cases 1
Called from 1
public function tokenClass(string $name, list<string> $members): void
public function fallback(string $target, list<string> $names): void

Declares which terminal to retry with when a listed terminal cannot be used.

Parameters

$targetstringTerminal to fall back to
$nameslist<string>Terminals that fall back
Test cases 1
Called from 1
Calls 1
public function wildcard(string $name): void
public function build(): Grammar

Numbers everything collected and answers the augmented grammar.

Returns

Grammar The grammar

Throws

GrammarException When no rule was recorded or a name is both a terminal and a nonterminal
UnknownSymbolException When a rule names a symbol nothing declares
Test cases 41
Called from 2
Calls 12
public function numberedRules(SymbolTable $symbols): list<Rule>

Numbers the recorded rules after the augmented start rule.

Parameters

$symbolsSymbolTableThe numbering

Returns

list<Rule> Rules numbered from one, alternatives counted per nonterminal

Throws

UnknownSymbolException When a rule names a symbol nothing declares
Test cases 1
Called from 1
Calls 4
public function firstVisibleLhs(): string

Answers the left-hand side of the first rule that was not synthesised.

A rule synthesised for a mid-rule action is numbered before the rule it appears in, so the first recorded rule is not always the first rule the source wrote.

Returns

string Nonterminal of the first rule as written
Test cases 1
Called from 1

Private surface 11§

Implementation details, listed for orientation only.

private array<string, true> $terminals = []
private array<string, true> $nonterminals = []
private list<array{string, list<string>, string|null, bool}> $rules = []
private array<string, Precedence> $precedences = []
private int $level = 0
private ?string $start = null
private ?int $expectedConflicts = null
private PrecedencePolicy $policy = PrecedencePolicy::LastTerminal
private array<string, list<string>> $tokenClasses = []
private array<string, string> $fallbacks = []
private ?string $wildcard = null

Test cases 48§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 22
Other tests reaching this symbol 26

Relations§

Instantiated in 2
Method calls 23
Type declarations 5