Skip to content
← Back

src/syntax/process/printer/annotation_stripper.ghul

1
namespace Syntax.Process.Printer is
2
// Chooses which written type annotations the formatter leaves out, so
3
// a program can be printed with some of what its author wrote for
4
// inference to recover. The sites are numbered in print order - a
5
// local's type, a function literal's parameter or return type, a
6
// call's type arguments - and the selection names them by number:
7
// `all` for every site, `list` for none (the numbering is then what
8
// is wanted), or comma-separated numbers and `a-b` ranges.
9
class ANNOTATION_STRIPPER(selection: string) is
10
sites: Collections.LIST[string]
11
_selected: Collections.SET[int]
12
_all: bool
13
14
init(..) is
15
sites = Collections.LIST[string]()
16
_selected = Collections.SET[int]()
17
_all = selection =~ "all"
18
19
if !_all /\ !(selection =~ "list") then
20
for part in selection.split([',']) do
21
let range = part.trim().split(['-'])
22
23
if range.count == 2 then
24
for i in int.parse(range[0])::int.parse(range[1]) do
25
_selected.add(i)
26
od
27
elif range[0].length > 0 then
28
_selected.add(int.parse(range[0]))
29
fi
30
od
31
fi
32
si
33
34
// Numbers the site and says whether its annotation is left out.
35
// The listing gives the text the annotation occupies, so a tool can
36
// take it out of the source in place rather than reprinting the file:
37
// `type` names the written type, which a separating ':' or '->'
38
// precedes, and `after` names what follows the end of `from` up to
39
// the last ']' at or before the end of `to`. A call's location runs
40
// on past its type arguments, so the end given is not the ']' itself.
41
strip(kind: string, location: Source.LOCATION, mode: string, from: Source.LOCATION, to: Source.LOCATION) -> bool is
42
let n = sites.count
43
44
sites.add("{n} {kind} {location.start_line},{location.start_column} {mode} {from.start_line},{from.start_column},{from.end_line},{from.end_column} {to.end_line},{to.end_column}")
45
46
_all \/ _selected.contains(n)
47
si
48
si
49
si