packages/bison-parser/src/Ast/Declaration/Define.php
1<?php
2
3declare(strict_types=1);
4
5namespace BisonParser\Ast\Declaration;
6
7use BisonParser\Ast\Location;
8
9/**
10 * A `%define` directive: a variable and, optionally, its value.
11 *
12 * `%define api.pure` sets the variable without a value, which Bison reads
13 * as the keyword `true`; the tree keeps the value absent so that the file
14 * prints back as written.
15 *
16 * @visibility public
17 *
18 * @example Reading a definition
19 * $file = (new \BisonParser\Parser())->parse("%define api.prefix {my_}\n%%\ns: 'a';\n");
20 * $file->declarations[0]->variable // => 'api.prefix'
21 * $file->declarations[0]->value // => 'my_'
22 * $file->declarations[0]->form?->value // => 'code'
23 */
24final class Define implements Declaration
25{
26 /**
27 * @param string $variable The variable name
28 * @param string|null $value The keyword, the decoded string, or the code without its braces; null when omitted
29 * @param DefineForm|null $form Which form the value was written in, null when omitted
30 * @param Location $location Where the directive is written
31 */
32 public function __construct(
33 public readonly string $variable,
34 public readonly ?string $value,
35 public readonly ?DefineForm $form,
36 public readonly Location $location,
37 ) {
38 }
39
40 /**
41 * Answers where the declaration begins.
42 *
43 * @return Location Line and column of the percent sign
44 */
45 public function location(): Location
46 {
47 return $this->location;
48 }
49}
50