classParser
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.
$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']$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]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
$preprocessor | Preprocessor | Settles conditional regions |
$scanner | Scanner | Splits the text into tokens |
$reader | GrammarReader | Builds the tree |
Calls 3
- new Preprocessor line 42
- new Scanner line 43
- new GrammarReader line 44
public function parse(string $source, list<string> $defines = []): GrammarFileReads a grammar file.
Parameters
$source | string | The file's text |
$defines | list<string> | Names defined for `%ifdef`, as Lemon's `-D` option defines them |
Returns
GrammarFile The treeThrows
SyntaxException Where Lemon reports an errorTest cases 10
ParseBench::benchParsecallsParseBench::benchRoundTripcallsGrammarContext::theFileIsParsedcallsGrammarContext::theTreeIsTheSameAsForcallsLemonGrammarCompilerTest::testExpandcallsLemonGrammarCompilerTest::testTerminalscallsLemonGrammarReaderTest::testDeclarationcallsLemonGrammarReaderTest::testRulecallsParserTest::testParsecallsParserTest::testParseRejectsWhatLemonRejectscalls
Called from 2
Calls 4
- method-call Scanner::scan() line 60
- method-call Preprocessor::preprocess() line 60
- method-call GrammarReader::read() line 62
- new TokenStream line 62
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
ParseBench::benchParsecallsParseBench::benchRoundTripcallsParseBench::setUpcallsGrammarContext::__constructcallsGrammarContext::theFileIsParsedcallsGrammarContext::theTreeIsTheSameAsForcallsLemonGrammarCompilerTest::testExpandcallsLemonGrammarCompilerTest::testTerminalscallsLemonGrammarReaderTest::testDeclarationcallsLemonGrammarReaderTest::testRulecalls