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
//
10
// A selection prefixed `cast:` numbers the targets of explicit casts
11
// instead, and only those: `cast double(n)` printed as `cast(n)`.
12
class ANNOTATION_STRIPPER(selection: string) is
13
sites: Collections.LIST[string]
14
_selected: Collections.SET[int]
15
_all: bool
16
_casts: bool
17
18
init(..) is
19
sites = Collections.LIST[string]()
20
_selected = Collections.SET[int]()
21
_casts = selection.starts_with("cast:")
22
23
let chosen = if _casts then selection.substring(5) else selection fi
24
25
_all = chosen =~ "all"
26
27
if !_all /\ !(chosen =~ "list") then
28
for part in chosen.split([',']) do
29
let range = part.trim().split(['-'])
30
31
if range.count == 2 then
32
for i in int.parse(range[0])::int.parse(range[1]) do
33
_selected.add(i)
34
od
35
elif range[0].length > 0 then
36
_selected.add(int.parse(range[0]))
37
fi
38
od
39
fi
40
si
41
42
// Numbers the site and says whether its annotation is left out.
43
// The listing gives the text the annotation occupies, so a tool can
44
// take it out of the source in place rather than reprinting the file:
45
// `type` names the written type, which a separating ':' or '->'
46
// precedes, and `after` names what follows the end of `from` up to
47
// the last ']' at or before the end of `to`. A call's location runs
48
// on past its type arguments, so the end given is not the ']' itself.
49
strip(kind: string, location: Source.LOCATION, mode: string, from: Source.LOCATION, to: Source.LOCATION) -> bool is
50
if (kind =~ "cast") != _casts then
51
return false
52
fi
53
54
let n = sites.count
55
56
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}")
57
58
_all \/ _selected.contains(n)
59
si
60
si
61
si