classParser
final class Parser
Public API: explicitly declared with
@visibility public.Reads a GNU Bison grammar file into a syntax tree that keeps everything the file says.
The reader follows Bison's own scan-gram.l and parse-gram.y, so a file Bison accepts is read the way Bison reads it, and a file Bison rejects raises a SyntaxException naming the place. Host code is kept as text, never interpreted.
$file = (new \BisonParser\Parser())->parse("%token NUM\n%left '+'\n%%\nexpr: expr '+' expr | NUM ;\n");
count($file->declarations) // => 2
$file->rules()[0]->name->value // => 'expr'(new \BisonParser\Parser())->parse("%tokens A\n%%\ns: A;\n") // throws \BisonParser\SyntaxException: Invalid directive: %tokens at 1:1Methods§
public function __construct(
private Scanner $scanner = new \BisonParser\Scanner\Scanner(),
private GrammarParser $grammar = new \BisonParser\Syntax\GrammarParser(),
)Parameters
$scanner | Scanner | Splits the file into tokens |
$grammar | GrammarParser | Builds the tree from the tokens |
Calls 2
- new Scanner line 36
- new GrammarParser line 37
public function parse(string $source): GrammarFileReads a grammar file.
Parameters
$source | string | Contents of the file |
Returns
GrammarFile The treeThrows
SyntaxException When the file is not a grammar Bison would acceptTest cases 14
ParseBench::benchParsecallsParseBench::benchRoundTripcallsGrammarContext::theFileIsParsedcallsGrammarContext::theTreeIsTheSameAsForcallsBisonGrammarCompilerTest::testDeclaredTokenscallsBisonGrammarCompilerTest::testStartSymbolcallsBisonGrammarCompilerTest::testSymbolscallsBisonGrammarCompilerTest::testSymbolsRejectsAnUnknownNamecallsBisonGrammarReaderTest::testAlternativecallsBisonGrammarReaderTest::testContinuescallsBisonGrammarReaderTest::testContinuesLooksOnlyAtWhatFollowsTheItemcallsBisonGrammarReaderTest::testDeclarationcallsParserTest::testParsecallsParserTest::testParseRejectsAGrammarBisonRejectscalls
Called from 2
Calls 3
- method-call GrammarParser::parse() line 52
- new TokenStream line 52
- method-call Scanner::scan() line 52
Private surface 2§
Implementation details, listed for orientation only.
private Scanner $scanner = new \BisonParser\Scanner\Scanner()private GrammarParser $grammar = new \BisonParser\Syntax\GrammarParser()Test cases 16§
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 14
ParseBench::benchParsecallsParseBench::benchRoundTripcallsParseBench::setUpcallsGrammarContext::__constructcallsGrammarContext::theFileIsParsedcallsGrammarContext::theTreeIsTheSameAsForcallsBisonGrammarCompilerTest::testDeclaredTokenscallsBisonGrammarCompilerTest::testStartSymbolcallsBisonGrammarCompilerTest::testSymbolscallsBisonGrammarCompilerTest::testSymbolsRejectsAnUnknownNamecallsBisonGrammarReaderTest::testAlternativecallsBisonGrammarReaderTest::testContinuescallsBisonGrammarReaderTest::testContinuesLooksOnlyAtWhatFollowsTheItemcallsBisonGrammarReaderTest::testDeclarationcalls