Skip to content
← Back

src/syntax/process/printer/formatter.ghul

1
namespace Syntax.Process.Printer is
2
use Collections
3
use Ghul
4
5
use Trees
6
use Source
7
8
use Lexical.TRIVIA
9
10
// The ghūl code formatter.
11
//
12
// Extends the plain GHUL printer (which the diagnostics path uses via
13
// Node.to_string and must not be disturbed) and redirects its output
14
// primitives into a DOC tree, so DOC_RENDERER can lay the result out
15
// against a column budget. Comments and blank lines collected by the
16
// tokenizer as TRIVIA are interleaved back in by source position.
17
//
18
// Selected visits are overridden to introduce soft breaks (GROUP/LINE)
19
// where wrapping should happen; everything else inherits the printer's
20
// structural layout unchanged.
21
class FORMATTER: GHUL is
22
_builder: DOC_BUILDER
23
_width: int
24
25
// Types a build settled at inferred sites, written in where the
26
// source left them out. Null prints the source as written.
27
annotations: InferredAnnotations? public
28
29
_annotation(lookup: (InferredAnnotations) -> string?) -> string? =>
30
if let a = annotations then lookup(a) else null fi
31
32
// Written annotations to leave out, so the source is printed with
33
// less than its author wrote. Null prints every annotation.
34
stripper: ANNOTATION_STRIPPER? public
35
36
// Whether the variable being printed is a `let` local: the only
37
// kind of Variables.VARIABLE whose type is an annotation rather
38
// than a signature.
39
_in_let: bool
40
41
_strip(kind: string, location: Source.LOCATION, mode: string, from: Source.LOCATION, to: Source.LOCATION) -> bool =>
42
if let s = stripper then s.strip(kind, location, mode, from, to) else false fi
43
44
strip_cast_target(`cast: Trees.Expressions.CAST, type_expression: Trees.TypeExpressions.TypeExpression) -> bool =>
45
_strip("cast", `cast.location, "type", type_expression.location, type_expression.location)
46
47
_trivia: LIST[TRIVIA]
48
_trivia_index: int
49
50
// Source line of the furthest content emitted so far. Drives
51
// trailing-comment detection: a comment starting on a line we have
52
// already passed trails that line rather than leading the next node.
53
_last_line: int
54
55
// True between opening a block and emitting its first real content;
56
// suppresses a blank line immediately inside a block.
57
_at_block_start: bool
58
59
// True when nothing has been written since the last line break. A
60
// statement whose rendering ends in a block terminator - `od`, `esac` -
61
// leaves the line open, and the statement after it must not be run onto
62
// the same line.
63
_at_line_start: bool
64
65
// True while inside a union that declared a primary-constructor
66
// header. Variants emitted under this flag can drop a sole
67
// `(..)` field list — the rewriter implicitly splices the primary
68
// params for a variant with no field list. Without a primary
69
// header, a sole `..` is a hard error, and dropping the parens
70
// would silently turn the error into valid empty-fields source.
71
_union_has_primary_params: bool
72
73
init(trivia: Iterable[TRIVIA]?, width: int) is
74
super.init()
75
76
_width = width
77
_builder = DOC_BUILDER()
78
79
_trivia = LIST[TRIVIA]()
80
if trivia? then
81
for t in trivia do
82
_trivia.add(t)
83
od
84
fi
85
si
86
87
// Format a parsed file (or any node) and return the rendered text.
88
format(node: Node) -> string is
89
node.accept(self)
90
flush_remaining()
91
92
let text = DOC_RENDERER(_width).render(_builder.build())
93
94
// Strip trailing whitespace from every line (blank lines pick up
95
// indentation from the renderer), then settle each terminator.
96
let lines = Collections.LIST[string]()
97
98
for line in text.split(['\n']) do
99
lines.add(line.trim_end())
100
od
101
102
_settle_terminators(lines)
103
104
let result = System.Text.StringBuilder()
105
let first mut = true
106
for line in lines do
107
if !first then
108
result.append('\n')
109
fi
110
result.append(line)
111
first = false
112
od
113
114
return "{result.to_string().trim_end()}\n"
115
si
116
117
// A statement terminator is written as this marker rather than as
118
// `;`, because whether it is needed depends on what follows it,
119
// which is not known when the statement is emitted.
120
// _settle_terminators resolves every one of them once the text has
121
// been laid out.
122
// A control character, so it cannot occur in the source being
123
// formatted. Held and searched for as a `char`: `index_of(string)`
124
// compares by culture, under which a control character has no
125
// collation weight and so matches at the start of any string at
126
// all, while the `char` overload compares ordinally.
127
TERMINATOR_MARKER: char static => cast char(1)
128
129
write_terminator() is
130
write(TERMINATOR_MARKER)
131
write_line()
132
si
133
134
// A line break stands in for a statement terminator, so a marker
135
// that nothing but a comment follows is dropped. The exception is
136
// the one terminator that carries meaning: adjacent string literals
137
// chain into a single literal across a line break, so a statement
138
// ending on one and followed by a statement starting with one keeps
139
// its `;`. A marker with code after it separates two statements on
140
// one line and is kept.
141
_settle_terminators(lines: Collections.LIST[string]) is
142
let i mut = 0
143
144
while i < lines.count do
145
let line = lines[i]
146
let at = line.index_of(TERMINATOR_MARKER)
147
148
if at >= 0 then
149
let before = line[0..at]
150
let after = line[at + 1..<0]
151
152
let keep =
153
!_is_only_a_comment(after) \/
154
(_ends_with_string(before) /\
155
_next_line_starts_with_string(lines, i))
156
157
lines[i] = if keep then "{before};{after}" else "{before}{after}" fi
158
fi
159
160
i = i + 1
161
od
162
si
163
164
// Whether the rest of a line, after a terminator marker, is nothing
165
// but whitespace and a comment - in which case the line break is
166
// what ends the statement.
167
_is_only_a_comment(rest: string) -> bool is
168
let trimmed = rest.trim()
169
170
if trimmed.length == 0 \/ trimmed.starts_with("//") then
171
return true
172
fi
173
174
return
175
trimmed.starts_with("/*") /\
176
trimmed.ends_with("*/") /\
177
trimmed.index_of("*/") == trimmed.length - 2
178
si
179
180
_ends_with_string(line: string) -> bool is
181
let trimmed = line.trim_end()
182
183
return trimmed.length > 0 /\ trimmed[trimmed.length - 1] == cast char(34)
184
si
185
186
// Comments are transparent to string chaining, so a comment line
187
// between the two literals does not break the chain and is looked
188
// through here too - a block comment as well as a line comment.
189
//
190
// A line this cannot classify - the opening line of a block comment
191
// that runs on, say - is answered as though a literal followed.
192
// The two errors are not equal: a terminator written where none was
193
// needed costs a warning, while one dropped where it was needed
194
// merges two literals into one and changes what the program says.
195
_next_line_starts_with_string(lines: Collections.LIST[string], from: int) -> bool is
196
let i mut = from + 1
197
198
while i < lines.count do
199
let candidate = lines[i].trim()
200
201
if candidate.length == 0 \/ _is_only_a_comment(candidate) then
202
i = i + 1
203
elif candidate.starts_with("/*") then
204
return true
205
else
206
return candidate[0] == cast char(34)
207
fi
208
od
209
210
return false
211
si
212
213
// --- output primitives, redirected into the DOC builder ---
214
215
write(value: string?) is
216
if !value? then
217
return
218
fi
219
220
if value.length > 0 then
221
_at_block_start = false
222
_at_line_start = false
223
fi
224
225
_builder.text(value)
226
si
227
228
write(c: char) is
229
_at_block_start = false
230
_at_line_start = false
231
_builder.text("{c}")
232
si
233
234
// A name that collides with a keyword, or that would tokenise as a
235
// number, only reached the tree via a backtick escape; the inherited
236
// printer drops the backtick and the result no longer re-parses.
237
// Restore it. Operator names are left to visit(Expressions.IDENTIFIER):
238
// they must stay bare in definition and binary-operation positions,
239
// which route through here too.
240
write_name(name: string) is
241
if Lexical.TOKENIZER.is_reserved_word(name) \/ Lexical.TOKENIZER.is_numeric_identifier(name) then
242
write('`')
243
fi
244
write(name)
245
si
246
247
// An operator referred to as a plain value, rather than applied,
248
// reached the tree via a backtick escape and must keep it to re-parse
249
// as an identifier rather than an operator. This is the one identifier
250
// position where an operator escapes; definition names and binary
251
// operations render the operator bare through the inherited visits.
252
visit(identifier: Expressions.IDENTIFIER) is
253
location(identifier)
254
let inner = identifier.identifier
255
if !inner.is_qualified /\ Lexical.TOKENIZER.is_operator_name(inner.name) then
256
write('`')
257
write(inner.name)
258
else
259
inner.accept(self)
260
fi
261
si
262
263
at_line_start: bool => _at_line_start
264
265
write_line() is
266
flush_trailing_comments()
267
_builder.hard_line()
268
_at_line_start = true
269
si
270
271
indent() is
272
_builder.begin_nest(4)
273
_at_block_start = true
274
si
275
276
outdent() is
277
_builder.end_nest()
278
si
279
280
write_indent() is si
281
282
// --- line tracking ---
283
284
location(location: LOCATION) is
285
note(location)
286
si
287
288
note(location: LOCATION?) is
289
if location? /\ location.start_line > _last_line then
290
_last_line = location.start_line
291
fi
292
si
293
294
// --- trivia interleaving ---
295
296
_has_trivia: bool => _trivia_index < _trivia.count
297
298
// Whether any unemitted trivia still sits before `limit` (a
299
// LOCATION.start value). A body with none can take a one-line
300
// rendering, since there is no comment to keep inside it.
301
_has_trivia_before(limit: int) -> bool =>
302
_has_trivia /\ _peek.location.start < limit
303
304
_peek: TRIVIA => _trivia[_trivia_index]
305
306
_consume() is
307
_trivia_index = _trivia_index + 1
308
si
309
310
// Emit any trivia positioned strictly before `limit` (a LOCATION.start
311
// value) that is not a trailing comment of an already-emitted line.
312
flush_leading(limit: int) is
313
while _has_trivia /\ _peek.location.start < limit do
314
let t = _peek
315
316
if isa TRIVIA.BLANK_LINE(t) then
317
// A blank line written inside an expression the layout
318
// joins onto one line has nowhere to go, and emitting it
319
// here puts it after the whole construct instead - one
320
// more blank line on every reformat. Its source line has
321
// already been passed, which is what distinguishes it
322
// from a blank line genuinely separating two constructs.
323
if !_at_block_start /\ _peek.location.start_line > _last_line then
324
_builder.hard_line()
325
fi
326
_consume()
327
else
328
emit_comment(t)
329
_builder.hard_line()
330
_at_block_start = false
331
_consume()
332
fi
333
od
334
si
335
336
// Emit comments that start on a line we have already emitted: they
337
// trail that line. Called from write_line, before the newline.
338
flush_trailing_comments() is
339
while
340
_has_trivia /\
341
_is_comment(_peek) /\
342
_peek.location.start_line <= _last_line
343
do
344
_builder.text(" ")
345
emit_comment(_peek)
346
_consume()
347
od
348
si
349
350
// Emit everything left over (file-trailing comments).
351
flush_remaining() is
352
while _has_trivia do
353
let t = _peek
354
if _is_comment(t) then
355
emit_comment(t)
356
_builder.hard_line()
357
fi
358
_consume()
359
od
360
si
361
362
_is_comment(t: TRIVIA) -> bool =>
363
isa TRIVIA.LINE_COMMENT(t) \/ isa TRIVIA.DOC_COMMENT(t) \/ isa TRIVIA.BLOCK_COMMENT(t)
364
365
// The leading whitespace every non-blank continuation line of a
366
// block comment has in common.
367
_common_indent(lines: string[]) -> int is
368
let common mut = -1
369
let first mut = true
370
371
for line in lines do
372
if !first /\ line.trim().length > 0 then
373
let indent = line.length - line.trim_start().length
374
375
if common < 0 \/ indent < common then
376
common = indent
377
fi
378
fi
379
380
first = false
381
od
382
383
return if common < 0 then 0 else common fi
384
si
385
386
_strip_indent(line: string, amount: int) -> string is
387
let i mut = 0
388
389
while i < amount /\ i < line.length /\ char.is_white_space(line[i]) do
390
i = i + 1
391
od
392
393
return line[i..<0]
394
si
395
396
emit_comment(t: TRIVIA) is
397
note(t.location)
398
399
if let bc: TRIVIA.BLOCK_COMMENT = t then
400
let lines = bc.text.split(['\n'])
401
402
// A continuation line carries the indentation it was
403
// written at, and the renderer indents it again, so
404
// re-emitting it verbatim moves the comment four columns
405
// right on every reformat. Strip what every continuation
406
// line shares and let the renderer supply it instead; the
407
// relative shape of the comment is preserved.
408
let common = _common_indent(lines)
409
410
let first mut = true
411
for line in lines do
412
if !first then
413
_builder.hard_line()
414
_builder.text(_strip_indent(line, common))
415
else
416
_builder.text(line)
417
fi
418
first = false
419
od
420
elif let lc: TRIVIA.LINE_COMMENT = t then
421
_builder.text(lc.text)
422
elif let dc: TRIVIA.DOC_COMMENT = t then
423
_builder.text(dc.text)
424
elif let sb: TRIVIA.SHEBANG = t then
425
_builder.text(sb.text)
426
fi
427
si
428
si
429
si