Skip to content
← Back

src/source/body_spans.ghul

1
namespace Source is
2
use Collections
3
4
use Ghul.Pipes
5
6
// The pre-edit spans of the function bodies an incremental EDIT
7
// re-walks. A symbol-use or definition entry inside one of these is
8
// re-recorded fresh by the re-walk, so reconciliation discards it
9
// rather than translating it.
10
//
11
// Spans can nest: the entry synthesised from a file's top-level
12
// statements spans from the first statement to the last, which
13
// encloses the body of any function written between them. So the
14
// spans sort by start position, and `contains` asks whether any span
15
// starting at or before the position reaches it, which the furthest
16
// end among them answers.
17
class BODY_SPANS is
18
_spans: LIST[LOCATION]
19
_furthest_end: LIST[int]
20
_prepared: bool
21
22
init() is
23
_spans = LIST[LOCATION]()
24
_furthest_end = LIST[int]()
25
si
26
27
// Internal locations are synthesised — their packed position is
28
// (1,1) by construction and would otherwise drop any real symbol
29
// declared at line 1 column 1 (a top-level namespace, the first
30
// class in the file) when reconciliation calls `contains` with
31
// file_name unchecked.
32
add(span: LOCATION) is
33
if !span.is_internal then
34
_spans.add(span)
35
_prepared = false
36
fi
37
si
38
39
// Is a (packed) position inside one of the re-walked bodies?
40
contains(position: int) -> bool is
41
_prepare()
42
43
// the spans starting at or before `position`
44
let lo mut = 0
45
let hi mut = _spans.count
46
47
while lo < hi do
48
let mid = (lo + hi) / 2
49
50
if _spans[mid].start <= position then
51
lo = mid + 1
52
else
53
hi = mid
54
fi
55
od
56
57
// the same test LOCATION.contains makes of one span
58
return lo > 0 /\ _furthest_end[lo - 1] >= position - 1
59
si
60
61
_prepare() is
62
if _prepared then
63
return
64
fi
65
66
_spans = LIST[LOCATION](_spans |> sort((a, b) => a.start - b.start))
67
_furthest_end = LIST[int]()
68
69
let furthest mut = 0
70
71
for span in _spans do
72
if span.end > furthest then
73
furthest = span.end
74
fi
75
76
_furthest_end.add(furthest)
77
od
78
79
_prepared = true
80
si
81
si
82
si