final class Rule
Public API: explicitly declared with @visibility public.

A rule: a nonterminal, ::=, the right-hand side, and a period.

A precedence mark [TOKEN] and a code block { ... } may follow the period, and Lemon attaches them to the rule most recently completed. {NEVER-REDUCE} in place of code marks a rule that is never reduced.

Reading a rule with everythingdoctest
$file = (new \LemonParser\Parser())->parse("expr(A) ::= expr(B) MINUS expr(C). [PLUS] { A = B - C; }\n");
    $rule = $file->rules()[0];
    [$rule->lhs->name, $rule->lhsAlias, count($rule->items), $rule->precedence?->name, $rule->code?->code, $rule->neverReduce] // => ['expr', 'A', 3, 'PLUS', ' A = B - C; ', false]

Properties§

public Symbol $lhs
public string|null $lhsAlias
public list<RhsItem> $items
public Symbol|null $precedence
public CodeBlock|null $code
public bool $neverReduce
public Location $location

Methods§

public function __construct(
    public Symbol $lhs,
    public string|null $lhsAlias,
    public list<RhsItem> $items,
    public Symbol|null $precedence,
    public CodeBlock|null $code,
    public bool $neverReduce,
    public Location $location,
)

Parameters

$lhsSymbolThe nonterminal being defined
$lhsAliasstring|nullThe name given to the rule's value, or null
$itemslist<RhsItem>The right-hand side, possibly empty
$precedenceSymbol|nullThe terminal of the `[TOKEN]` mark, or null
$codeCodeBlock|nullThe action, or null
$neverReduceboolWhether `{NEVER-REDUCE}` follows the rule
$locationLocationWhere the left-hand side is written
public function withPrecedence(Symbol $precedence): self

Copies the rule with a precedence mark.

Parameters

$precedenceSymbolThe terminal of the mark

Returns

self The rule with the mark
Test cases 1
Calls 1
public function withCode(CodeBlock $code): self

Copies the rule with an action.

Parameters

$codeCodeBlockThe action

Returns

self The rule with the action
Test cases 1
Calls 1
public function withNeverReduce(): self

Copies the rule marked as never reduced.

Returns

self The marked rule
Test cases 1
Calls 1
public function symbols(): list<Symbol>

Lists the symbols of the right-hand side in order, every terminal of a shared position included.

Returns

list<Symbol> The symbols
Test cases 1

Test cases 19§

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

Dedicated tests 4
Other tests reaching this symbol 15

Relations§

Instantiated in 1
Type checks 3
Type declarations 6