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.

Printing a grammar backdoctest
$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

$declarationsDeclarationPrinterWrites declarations
$rhsRhsPrinterWrites right-hand sides
Calls 2
public function print(GrammarFile $file): string

Writes a whole file.

Parameters

$fileGrammarFileThe tree

Returns

string The grammar file text
Test cases 2
Calls 3
public function rule(Rule $rule): string

Writes one rule, one alternative per line.

Parameters

$ruleRuleThe rule

Returns

string The rule text
Test cases 1
Called from 1
Calls 2
public function alternative(Alternative $alternative): string

Writes the items of one alternative separated by spaces.

Parameters

$alternativeAlternativeThe alternative

Returns

string The items, or `%empty` for an alternative with none
Test cases 1
Called from 1
Calls 2

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
Other tests reaching this symbol 4