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

Reads a Lemon grammar file into a tree that keeps everything the file says.

The file is read as Lemon reads it: %ifdef regions are settled with the names given, the text is split into tokens the way lemon.c splits it, and rules and declarations are accepted and rejected where Lemon accepts and rejects them.

Reading a grammardoctest
$file = (new \LemonParser\Parser())->parse("%token_prefix TK_\n%left PLUS MINUS.\nexpr(A) ::= expr(B) PLUS expr(C). { A = B + C; }\nexpr(A) ::= NUM(B). { A = B; }\n");
    [count($file->declarations()), count($file->rules()), $file->rules()[1]->items[0]->symbols[0]->name] // => [2, 2, 'NUM']
Settling a conditional regiondoctest
$source = "%ifndef OMIT_WINDOW\nwindow ::= OVER.\n%endif\ncmd ::= SELECT.\n";
    $parser = new \LemonParser\Parser();
    [count($parser->parse($source)->rules()), count($parser->parse($source, ['OMIT_WINDOW'])->rules())] // => [2, 1]
Rejecting what Lemon rejectsdoctest
try { (new \LemonParser\Parser())->parse("expr ::= expr ? expr.\n"); } catch (\LemonParser\SyntaxException $e) { $error = $e->getMessage(); }
    $error // => 'Illegal character on RHS of rule: "?". at 1:15'

Methods§

public function __construct(
    private Preprocessor $preprocessor = new \LemonParser\Preprocessor\Preprocessor(),
    private Scanner $scanner = new \LemonParser\Scanner\Scanner(),
    private GrammarReader $reader = new \LemonParser\Syntax\GrammarReader(),
)

Parameters

$preprocessorPreprocessorSettles conditional regions
$scannerScannerSplits the text into tokens
$readerGrammarReaderBuilds the tree
Calls 3

Private surface 3§

Implementation details, listed for orientation only.

private Preprocessor $preprocessor = new \LemonParser\Preprocessor\Preprocessor()
private Scanner $scanner = new \LemonParser\Scanner\Scanner()
private GrammarReader $reader = new \LemonParser\Syntax\GrammarReader()

Test cases 12§

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

Dedicated tests 2
Other tests reaching this symbol 10

Relations§

Instantiated in 2
Method calls 2
Type declarations 2