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

Turns a Lemon grammar file into the grammar the generators walk.

The file is read with lemon-parser after its %ifdef regions are settled; this class keeps what generation needs. A position shared by several terminals, A|B, is spelled out into one alternative per terminal, a %token_class becomes a rule with one alternative per member, and the first rule is the start symbol. A name written in capitals, digits and underscores is a terminal, as is the %wildcard token whatever its spelling; every other name is a nonterminal.

Compiling a small grammardoctest
$grammar = (new \SqlFaker\Compiler\Lemon\LemonGrammarCompiler())->compile("%token_class id ID|INDEXED.\nexpr(A) ::= expr(B) PLUS|MINUS term(C). { A = B + C; }\nexpr ::= term.\nterm ::= NUM.\nterm ::= id.\n");
    [$grammar->startSymbol, array_keys($grammar->ruleMap), count($grammar->ruleMap['expr']->alternatives)] // => ['expr', ['expr', 'term', 'id'], 3]

Methods§

public function __construct(private Parser $parser = new \LemonParser\Parser())

Parameters

$parserParserReads Lemon grammar files
Calls 1
public function compile(string $source, list<string> $defines = []): Grammar

Compiles a grammar file.

Parameters

$sourcestringThe text of the `.y` file
$defineslist<string>Names defined for `%ifdef`, as Lemon's `-D` option defines them

Returns

Grammar The grammar

Throws

SyntaxException When the file is not a grammar Lemon accepts
GrammarParseException When the file defines no rule
Test cases 3
Calls 11
public function expand(Rule $rule): list<list<string>>

Spells out a rule's shared positions into one sequence of names per combination.

Parameters

$ruleRuleThe rule

Returns

list<list<string>> The sequences, the first terminal of each position first
Test cases 1
Called from 1
public function terminals(GrammarFile $file): array<string, true>

Decides which names are terminals: those spelled as tokens, and the wildcard whatever its spelling.

Parameters

$fileGrammarFileThe tree

Returns

array<string, true> The terminal names
Test cases 1
Called from 1
Calls 3
public static function isTokenName(string $name): bool

Reports whether a name is spelled as a token: capitals, digits and underscores, starting with a capital.

Parameters

$namestringThe name

Returns

bool True for a token spelling
Test cases 1
Called from 1
Calls 1

Private surface 1§

Implementation details, listed for orientation only.

private Parser $parser = new \LemonParser\Parser()

Test cases 6§

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

Dedicated tests 6