packages/sql-semantics-mysql/src/Model/Rendering/Output.php
1<?php
2
3declare(strict_types=1);
4
5namespace SqlSemantics\Platform\MySql\Model\Rendering;
6
7use SqlSemantics\Platform\MySql\Model\Contract\Codec;
8use SqlSemantics\Platform\MySql\Model\Contract\NameUse;
9use SqlSemantics\Platform\MySql\Model\Diagnostic\Check;
10use SqlSemantics\Platform\MySql\Model\Statement\Identifier\Name;
11use SqlSemantics\Platform\MySql\Model\Statement\Node;
12use SqlSemantics\Platform\MySql\Model\Statement\Spelling\Layout;
13
14/**
15 * The typed, temporary output a statement structure is written to.
16 *
17 * Nodes add keywords, punctuation, names and literal spellings; there is no
18 * operation that adds a free SQL fragment. The pieces are joined into text by
19 * the lexical writer and the result is parsed again and compared with the
20 * structure before an operation is published.
21 *
22 * @visibility SqlSemantics
23 */
24final class Output
25{
26 /**
27 * @var list<Piece>
28 */
29 private array $pieces = [];
30
31 private bool $glue = false;
32
33 private bool $spelling = false;
34
35 private ?string $trail = null;
36
37 /**
38 * @var list<Piece>
39 */
40 private array $canonical = [];
41
42 /**
43 * @param Codec $codec The name codec of the language profile
44 */
45 public function __construct(private readonly Codec $codec)
46 {
47 }
48
49 /**
50 * Writes fixed keywords.
51 *
52 * @return $this
53 */
54 public function keyword(string ...$words): self
55 {
56 foreach ($words as $word) {
57 Check::invariant(preg_match('/\A[A-Z][A-Z0-9_]*\z/', $word) === 1, 'A keyword piece is one upper-case word.');
58 $this->add(PieceKind::Keyword, $word);
59 }
60
61 return $this;
62 }
63
64 /**
65 * Writes punctuation or an operator made of symbol characters only.
66 *
67 * @return $this
68 */
69 public function symbol(string $symbol): self
70 {
71 Check::invariant(preg_match('/\A[][(){}.,;:=<>!+*\/%^|&~@#?$-]+\z/', $symbol) === 1, 'A symbol piece contains punctuation only.');
72 $this->add(PieceKind::Symbol, $symbol);
73
74 return $this;
75 }
76
77 /**
78 * Writes a decoded name, spelled by the profile codec for its position.
79 *
80 * @return $this
81 */
82 public function name(Name $name, NameUse $use = NameUse::Column): self
83 {
84 $this->add(PieceKind::Name, $this->codec->name($name, $use));
85
86 return $this;
87 }
88
89 /**
90 * Writes the spelling a literal or parameter value produces from its exact value.
91 *
92 * @return $this
93 */
94 public function spelled(string $text): self
95 {
96 Check::invariant($text !== '', 'A spelled piece is not empty.');
97 $this->add(PieceKind::Literal, $text);
98
99 return $this;
100 }
101
102 /**
103 * Joins the next piece to the previous one without a separating space.
104 *
105 * @return $this
106 */
107 public function glue(): self
108 {
109 $this->glue = true;
110
111 return $this;
112 }
113
114 /**
115 * Writes a child node, when there is one.
116 *
117 * @return $this
118 */
119 public function node(?Node $node): self
120 {
121 $node?->render($this);
122
123 return $this;
124 }
125
126 /**
127 * Writes child nodes separated by a symbol.
128 *
129 * @param list<Node> $nodes
130 * @return $this
131 */
132 public function list(array $nodes, string $separator = ','): self
133 {
134 foreach ($nodes as $position => $node) {
135 if ($position > 0) {
136 $this->symbol($separator);
137 }
138 $node->render($this);
139 }
140
141 return $this;
142 }
143
144 /**
145 * Writes a node in the spelling of a layout, or regularly when there is none.
146 *
147 * The node renders its pieces as always; each piece then takes the
148 * spelling and the preceding trivia of the layout token at its position,
149 * and the next piece written takes the trailing trivia of the layout.
150 * Inside a spelled region a nested layout has no effect, since the outer
151 * layout spells every token of the region.
152 *
153 * @return $this
154 * @throws \SqlSemantics\Platform\MySql\Model\Diagnostic\InvariantViolation When the layout does not have one token per rendered piece
155 */
156 public function layout(?Layout $layout, Node $node): self
157 {
158 if ($layout === null || $this->spelling) {
159 $node->render($this);
160
161 return $this;
162 }
163 $start = count($this->pieces);
164 $this->spelling = true;
165 $node->render($this);
166 $this->spelling = false;
167 $count = count($this->pieces) - $start;
168 Check::invariant($count === count($layout->tokens), 'The layout spells ' . count($layout->tokens) . ' tokens where the rendering writes ' . $count . '.');
169 $spelled = [];
170 foreach (array_slice($this->pieces, $start) as $offset => $canonical) {
171 $token = $layout->tokens[$offset];
172 $spelled[] = new Piece($canonical->kind, $token->text, $canonical->glued, $offset === 0 ? $canonical->gap : $token->gap);
173 }
174 $this->pieces = [...array_slice($this->pieces, 0, $start), ...$spelled];
175 $this->trail = $layout->trail === '' ? null : $layout->trail;
176
177 return $this;
178 }
179
180 /**
181 * Answers the trivia a layout wrote after the last piece, when nothing followed it.
182 */
183 public function trailing(): string
184 {
185 return $this->trail ?? '';
186 }
187
188 /**
189 * Answers the pieces as the structure renders them, before any layout re-spelled them, for validation.
190 *
191 * @return list<Piece>
192 */
193 public function canonical(): array
194 {
195 return $this->canonical;
196 }
197
198 /**
199 * Adds one piece, consuming a pending glue request.
200 */
201 public function add(PieceKind $kind, string $text): void
202 {
203 $this->pieces[] = new Piece($kind, $text, $this->glue, $this->trail);
204 $this->canonical[] = new Piece($kind, $text, $this->glue);
205 $this->glue = false;
206 $this->trail = null;
207 }
208
209 /**
210 * Answers the pieces written so far.
211 *
212 * @return list<Piece>
213 */
214 public function pieces(): array
215 {
216 return $this->pieces;
217 }
218}
219