classLemonRules
final class LemonRules
Reads the rules a Lemon grammar writes.
Lemon writes one alternative per line rather than grouping them, so the alternatives of a rule are gathered by name as the file is read. Within a line it allows an alternation between tokens in a single position, which stands for as many alternatives as the positions multiply out to, and it lets each symbol carry an alias the parser uses to name its value.
Methods§
public function __construct(private LemonText $text = new \SqlFaker\Compiler\Lemon\LemonText())Parameters
$text | LemonText | Removes what is not grammar before the rules are looked for |
Test cases 8
LemonRulesTest::testAlternativesIgnoresAPositionThatConfiguresTheParsercoversLemonRulesTest::testAlternativesMultipliesOutEachAlternationPositioncoversLemonRulesTest::testReadFromDropsTheAliasASymbolCarriescoversLemonRulesTest::testReadFromGathersTheAlternativesOfOneRuleWrittenOnSeveralLinescoversLemonRulesTest::testReadFromReadsARuleThatWritesNothingcoversLemonRulesTest::testReadFromTellsTheSymbolTableWhatEachNameTurnedOutToBecoversLemonRulesTest::testWithoutAliasDropsWhatTheParserNamesTheValuecoversLemonRulesTest::testWithoutAliasLeavesASymbolWithNoAliasAlonecovers
public function readFrom(
string $input,
LemonSymbols $symbols,
): array<string, list<list<string>>>Reads every rule, telling the symbol table what each name turned out to be.
Parameters
$input | string | Contents of the grammar file |
$symbols | LemonSymbols | Symbol table to record names in |
Returns
array<string, list<list<string>>> Rule name => its alternatives, each a list of symbol namesTest cases 4
LemonRulesTest::testReadFromDropsTheAliasASymbolCarriescovers and callsLemonRulesTest::testReadFromGathersTheAlternativesOfOneRuleWrittenOnSeveralLinescovers and callsLemonRulesTest::testReadFromReadsARuleThatWritesNothingcovers and callsLemonRulesTest::testReadFromTellsTheSymbolTableWhatEachNameTurnedOutToBecovers and calls
Called from 1
Calls 7
- method-call LemonText::withoutDirectiveBlocks() line 40
- function-call
preg_match_allline 42 - function-call
trimline 48 - function-call
str_starts_withline 49 - method-call LemonSymbols::declareRule() line 53
- function-call
array_pushline 61 - method-call LemonRules::alternatives() line 61
public function alternatives(string $rhs, LemonSymbols $symbols): non-empty-list<list<string>>Multiplies out one right-hand side into the alternatives it stands for.
A position written as A|B means either, so a right-hand side with two such positions stands for four alternatives. Positions Lemon marks with a leading percent sign configure the parser rather than name a symbol.
Parameters
$rhs | string | Right-hand side as the grammar writes it |
$symbols | LemonSymbols | Symbol table to record names in |
Returns
non-empty-list<list<string>> Every alternative the right-hand side stands forTest cases 5
LemonRulesTest::testAlternativesIgnoresAPositionThatConfiguresTheParsercovers and callsLemonRulesTest::testAlternativesMultipliesOutEachAlternationPositioncovers and callsLemonRulesTest::testReadFromDropsTheAliasASymbolCarriescoversLemonRulesTest::testReadFromGathersTheAlternativesOfOneRuleWrittenOnSeveralLinescoversLemonRulesTest::testReadFromTellsTheSymbolTableWhatEachNameTurnedOutToBecovers
Called from 1
Calls 11
- function-call
preg_splitline 84 - function-call
trimline 90 - function-call
array_valuesline 95 - function-call
array_filterline 95 - function-call
array_mapline 95 - method-call LemonRules::withoutAlias() line 96
- function-call
explodeline 97 - function-call
str_starts_withline 98 - static-call LemonSymbols::isTokenName() line 104
- method-call LemonSymbols::declareToken() line 105
- method-call LemonSymbols::declareRule() line 107
public function withoutAlias(string $symbol): stringDrops the alias Lemon lets a symbol carry, as in expr(A).
Parameters
$symbol | string | Symbol as the grammar writes it |
Returns
string The symbol's name aloneTest cases 7
LemonRulesTest::testAlternativesIgnoresAPositionThatConfiguresTheParsercoversLemonRulesTest::testAlternativesMultipliesOutEachAlternationPositioncoversLemonRulesTest::testReadFromDropsTheAliasASymbolCarriescoversLemonRulesTest::testReadFromGathersTheAlternativesOfOneRuleWrittenOnSeveralLinescoversLemonRulesTest::testReadFromTellsTheSymbolTableWhatEachNameTurnedOutToBecoversLemonRulesTest::testWithoutAliasDropsWhatTheParserNamesTheValuecovers and callsLemonRulesTest::testWithoutAliasLeavesASymbolWithNoAliasAlonecovers and calls
Called from 1
Private surface 1§
Implementation details, listed for orientation only.
private LemonText $text = new \SqlFaker\Compiler\Lemon\LemonText()Test cases 9§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 8
LemonRulesTest::testAlternativesIgnoresAPositionThatConfiguresTheParsercovers and callsLemonRulesTest::testAlternativesMultipliesOutEachAlternationPositioncovers and callsLemonRulesTest::testReadFromDropsTheAliasASymbolCarriescovers and callsLemonRulesTest::testReadFromGathersTheAlternativesOfOneRuleWrittenOnSeveralLinescovers and callsLemonRulesTest::testReadFromReadsARuleThatWritesNothingcovers and callsLemonRulesTest::testReadFromTellsTheSymbolTableWhatEachNameTurnedOutToBecovers and callsLemonRulesTest::testWithoutAliasDropsWhatTheParserNamesTheValuecovers and callsLemonRulesTest::testWithoutAliasLeavesASymbolWithNoAliasAlonecovers and calls
Other tests reaching this symbol 1
LemonParserTestcalls