classRule
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.
$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 $lhspublic string|null $lhsAliaspublic list<RhsItem> $itemspublic Symbol|null $precedencepublic CodeBlock|null $codepublic bool $neverReducepublic Location $locationMethods§
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
$lhs | Symbol | The nonterminal being defined |
$lhsAlias | string|null | The name given to the rule's value, or null |
$items | list<RhsItem> | The right-hand side, possibly empty |
$precedence | Symbol|null | The terminal of the `[TOKEN]` mark, or null |
$code | CodeBlock|null | The action, or null |
$neverReduce | bool | Whether `{NEVER-REDUCE}` follows the rule |
$location | Location | Where the left-hand side is written |
public function withPrecedence(Symbol $precedence): selfpublic function withCode(CodeBlock $code): selfCopies the rule with an action.
Parameters
$code | CodeBlock | The action |
Returns
self The rule with the actionTest cases 1
RuleTest::testWithCodecalls
Calls 1
- new
selfline 64
public function withNeverReduce(): selfCopies the rule marked as never reduced.
Returns
self The marked ruleTest cases 1
Calls 1
- new
selfline 74
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 symbolsTest cases 1
RuleTest::testSymbolscalls
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
TreeDumper::dumpcallsGrammarFileTest::testDeclarationscallsGrammarFileTest::testRulescallsLemonGrammarCompilerTestcallsLemonGrammarReaderTestcallsParserTest::testParsecallsPrinterTest::testPrintcallsRulePrinterTest::testPrintcallsGrammarReaderTest::testCodecallsGrammarReaderTest::testCodeRejectsASecondBlockcallsGrammarReaderTest::testPrecedencecallsGrammarReaderTest::testPrecedenceRejectsAMissingClosercallsGrammarReaderTest::testPrecedenceRejectsASecondMarkcallsGrammarReaderTest::testReadcallsRuleReaderTestcalls
Relations§
Instantiated in 1
Type checks 3
- instanceof LemonParser\Ast\GrammarFile::rules() packages/lemon-parser/src/Ast/GrammarFile.php:41
- instanceof LemonParser\Printer\Printer::print() packages/lemon-parser/src/Printer/Printer.php:46
- instanceof SqlParser\Compiler\LemonGrammarReader::read() packages/sql-parser/src/Compiler/LemonGrammarReader.php:74
Type declarations 6
- type LemonParser\Printer\RulePrinter packages/lemon-parser/src/Printer/RulePrinter.php:25
- type LemonParser\Syntax\GrammarReader packages/lemon-parser/src/Syntax/GrammarReader.php:86
- type LemonParser\Syntax\GrammarReader packages/lemon-parser/src/Syntax/GrammarReader.php:112
- type LemonParser\Syntax\RuleReader packages/lemon-parser/src/Syntax/RuleReader.php:32
- type SqlFaker\Compiler\Lemon\LemonGrammarCompiler packages/sql-faker/src/Compiler/Lemon/LemonGrammarCompiler.php:95
- type SqlParser\Compiler\LemonGrammarReader packages/sql-parser/src/Compiler/LemonGrammarReader.php:131