classPrinter
final class Printer
Public API: explicitly declared with
@visibility public.Writes a syntax tree back out as a Bison grammar file.
The output is not the original text: whitespace and comments are gone, and every declaration and rule is laid out one way. It is a grammar Bison reads to the same tree, which is what lets a tree be checked by printing it and reading it again.
$file = (new \BisonParser\Parser())->parse("%token NUM // numbers\n%%\nexpr : NUM | expr '+' NUM { \$\$ = \$1 + \$3; } ;");
(new \BisonParser\Printer\Printer())->print($file) // => "%token NUM\n%%\nexpr:\n NUM\n| expr '+' NUM { \$\$ = \$1 + \$3; }\n;\n"Methods§
public function __construct(
private DeclarationPrinter $declarations = new \BisonParser\Printer\DeclarationPrinter(),
private RhsPrinter $rhs = new \BisonParser\Printer\RhsPrinter(),
)Parameters
$declarations | DeclarationPrinter | Writes declarations |
$rhs | RhsPrinter | Writes right-hand sides |
Calls 2
public function print(GrammarFile $file): stringWrites a whole file.
Parameters
$file | GrammarFile | The tree |
Returns
string The grammar file textTest cases 2
ParseBench::benchRoundTripcallsPrinterTest::testPrintcalls
Calls 3
- method-call DeclarationPrinter::print() line 50
- method-call Printer::rule() line 54
- function-call
implodeline 56
public function rule(Rule $rule): stringWrites one rule, one alternative per line.
Parameters
$rule | Rule | The rule |
Returns
string The rule textTest cases 1
PrinterTest::testRulecalls
Called from 1
Calls 2
- method-call Printer::alternative() line 76
- function-call
implodeline 80
public function alternative(Alternative $alternative): stringWrites the items of one alternative separated by spaces.
Parameters
$alternative | Alternative | The alternative |
Returns
string The items, or `%empty` for an alternative with noneTest cases 1
Called from 1
Calls 2
- method-call RhsPrinter::print() line 97
- function-call
implodeline 100
Private surface 2§
Implementation details, listed for orientation only.
private DeclarationPrinter $declarations = new \BisonParser\Printer\DeclarationPrinter()private RhsPrinter $rhs = new \BisonParser\Printer\RhsPrinter()Test cases 7§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.
Dedicated tests 3
PrinterTest::testAlternativecallsPrinterTest::testPrintcallsPrinterTest::testRulecalls
Other tests reaching this symbol 4
ParseBench::benchRoundTripcallsParseBench::setUpcallsBisonGrammarCompilerTestcallsBisonGrammarReaderTestcalls