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

Turns a Lemon grammar file into the grammar the automaton is built from.

The file is read with lemon-parser after its %ifdef regions are settled with the names given; this class keeps what the automaton needs. %token names terminals, %left, %right and %nonassoc rank them, %token_class declarations and A|B positions become token classes, %fallback, %wildcard and %start_symbol are recorded for the parser, a [PREC] mark names a rule's precedence, and any upper-case name on a right-hand side is a terminal.

Reading a small grammardoctest
$grammar = (new \SqlParser\Compiler\LemonGrammarReader())->read("%left PLUS.\nexpr ::= expr PLUS|MINUS expr.\nexpr ::= NUM.\n");
    [count($grammar->rules), $grammar->symbols->name($grammar->startSymbol())] // => [3, "expr"]
Settling a conditional regiondoctest
$source = "%ifndef OMIT\ncmd ::= EXTRA.\n%endif\ncmd ::= SELECT.\n";
    [count((new \SqlParser\Compiler\LemonGrammarReader())->read($source)->rules), count((new \SqlParser\Compiler\LemonGrammarReader())->read($source, ['OMIT'])->rules)] // => [3, 2]

Methods§

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

Parameters

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

Reads 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 with Lemon's precedence policy

Throws

SyntaxException When the file is not a grammar Lemon accepts
GrammarException When the grammar is inconsistent
UnknownSymbolException When a declaration names an unknown symbol
Test cases 3
Calls 7
public function declaration(Declaration $declaration, GrammarBuilder $builder): void

Records what a declaration says about terminals, precedence, token classes, fallbacks, the wildcard or the start symbol.

Parameters

$declarationDeclarationThe declaration
$builderGrammarBuilderCollects the grammar
Test cases 1
Called from 1
Calls 11
public function associativity(Associativity $associativity): Associativity

Maps Lemon's associativity onto the grammar's.

Parameters

$associativityAssociativityAs declared

Returns

Associativity As the automaton resolves conflicts
Test cases 1
Called from 1
Calls 6
public function rule(Rule $rule, GrammarBuilder $builder): void

Adds a rule, turning a position shared by several terminals into a token class.

Parameters

$ruleRuleThe rule
$builderGrammarBuilderCollects the grammar
Test cases 1
Called from 1
Calls 6

Private surface 1§

Implementation details, listed for orientation only.

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

Test cases 7§

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

Dedicated tests 7