/
source.go
240 lines (189 loc) · 7.69 KB
/
source.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2015 The Serulian Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package compilercommon defines common types and their functions shared across the compiler.
package compilercommon
import (
"fmt"
)
// Position represents a position in an arbitrary source file.
type Position struct {
// LineNumber is the 0-indexed line number.
LineNumber int
// ColumnPosition is the 0-indexed column position on the line.
ColumnPosition int
}
// InputSource represents the path of a source file.
type InputSource string
// RangeForRunePosition returns a source range over this source file.
func (is InputSource) RangeForRunePosition(runePosition int, mapper PositionMapper) SourceRange {
return is.RangeForRunePositions(runePosition, runePosition, mapper)
}
// PositionForRunePosition returns a source position over this source file.
func (is InputSource) PositionForRunePosition(runePosition int, mapper PositionMapper) SourcePosition {
return runeIndexedPosition{is, mapper, runePosition}
}
// PositionFromLineAndColumn returns a source position at the given line and column in this source file.
func (is InputSource) PositionFromLineAndColumn(lineNumber int, columnPosition int, mapper PositionMapper) SourcePosition {
return lcIndexedPosition{is, mapper, Position{lineNumber, columnPosition}}
}
// RangeForRunePositions returns a source range over this source file.
func (is InputSource) RangeForRunePositions(startRune int, endRune int, mapper PositionMapper) SourceRange {
return sourceRange{is, runeIndexedPosition{is, mapper, startRune}, runeIndexedPosition{is, mapper, endRune}}
}
// RangeForLineAndColPositions returns a source range over this source file.
func (is InputSource) RangeForLineAndColPositions(start Position, end Position, mapper PositionMapper) SourceRange {
return sourceRange{is, lcIndexedPosition{is, mapper, start}, lcIndexedPosition{is, mapper, end}}
}
// SourceMappingOption defines the options for whichn source to use when position mapping.
type SourceMappingOption int
const (
// SourceMapTracked indicates that position mapping should occur over the *tracked* source
// contents. This is the default.
SourceMapTracked SourceMappingOption = iota
// SourceMapCurrent indicates that position mapping should occur over the *current* source
// contents. This should only be used by tooling, and is likely to fail in certain cases
// where positions have gotten desynced.
SourceMapCurrent
)
// PositionMapper defines an interface for converting rune position <-> line+col position
// under source files.
type PositionMapper interface {
// RunePositionToLineAndCol converts the given 0-indexed rune position under the given source file
// into a 0-indexed line number and column position.
RunePositionToLineAndCol(runePosition int, path InputSource, sourceOption SourceMappingOption) (int, int, error)
// LineAndColToRunePosition converts the given 0-indexed line number and column position under the
// given source file into a 0-indexed rune position.
LineAndColToRunePosition(lineNumber int, colPosition int, path InputSource, sourceOption SourceMappingOption) (int, error)
// TextForLine returns the text for the specified line number.
TextForLine(lineNumber int, path InputSource, sourceOption SourceMappingOption) (string, error)
}
// SourceRange represents a range inside a source file.
type SourceRange interface {
// Source is the input source for this range.
Source() InputSource
// Start is the starting position of the source range.
Start() SourcePosition
// End is the ending position (inclusive) of the source range. If the same as the Start,
// this range represents a single position.
End() SourcePosition
// ContainsPosition returns true if the given range contains the given position.
ContainsPosition(position SourcePosition) (bool, error)
// AtStartPosition returns a SourceRange located only at the starting position of this range.
AtStartPosition() SourceRange
// String returns a (somewhat) human-readable form of the range.
String() string
}
// SourcePosition represents a single position in a source file.
type SourcePosition interface {
// Source is the input source for this position.
Source() InputSource
// RunePosition returns the 0-indexed rune position in the source file.
RunePosition() (int, error)
// LineAndColumn returns the 0-indexed line number and column position in the source file.
LineAndColumn() (int, int, error)
// LineText returns the text of the line for this position.
LineText() (string, error)
// String returns a (somewhat) human-readable form of the position.
String() string
}
// sourceRange implements the SourceRange interface.
type sourceRange struct {
source InputSource
start SourcePosition
end SourcePosition
}
func (sr sourceRange) Source() InputSource {
return sr.source
}
func (sr sourceRange) Start() SourcePosition {
return sr.start
}
func (sr sourceRange) End() SourcePosition {
return sr.end
}
func (sr sourceRange) AtStartPosition() SourceRange {
return sourceRange{sr.source, sr.start, sr.end}
}
func (sr sourceRange) ContainsPosition(position SourcePosition) (bool, error) {
if position.Source() != sr.source {
return false, nil
}
startRune, err := sr.start.RunePosition()
if err != nil {
return false, err
}
endRune, err := sr.end.RunePosition()
if err != nil {
return false, err
}
positionRune, err := position.RunePosition()
if err != nil {
return false, err
}
return positionRune >= startRune && positionRune <= endRune, nil
}
func (sr sourceRange) String() string {
return fmt.Sprintf("%v -> %v", sr.start, sr.end)
}
// runeIndexedPosition implements the SourcePosition interface over a rune position.
type runeIndexedPosition struct {
source InputSource
mapper PositionMapper
runePosition int
}
func (ris runeIndexedPosition) Source() InputSource {
return ris.source
}
func (ris runeIndexedPosition) RunePosition() (int, error) {
return ris.runePosition, nil
}
func (ris runeIndexedPosition) LineAndColumn() (int, int, error) {
if ris.runePosition == 0 {
return 0, 0, nil
}
if ris.mapper == nil {
return -1, -1, fmt.Errorf("nil mapper")
}
return ris.mapper.RunePositionToLineAndCol(ris.runePosition, ris.source, SourceMapTracked)
}
func (ris runeIndexedPosition) String() string {
return fmt.Sprintf("%s@%v (rune)", ris.source, ris.runePosition)
}
func (ris runeIndexedPosition) LineText() (string, error) {
lineNumber, _, err := ris.LineAndColumn()
if err != nil {
return "", err
}
return ris.mapper.TextForLine(lineNumber, ris.source, SourceMapTracked)
}
// lcIndexedPosition implements the SourcePosition interface over a line and colu,n position.
type lcIndexedPosition struct {
source InputSource
mapper PositionMapper
lcPosition Position
}
func (lcip lcIndexedPosition) Source() InputSource {
return lcip.source
}
func (lcip lcIndexedPosition) String() string {
return fmt.Sprintf("%s@%v:%v (line/col)", lcip.source, lcip.lcPosition.LineNumber, lcip.lcPosition.ColumnPosition)
}
func (lcip lcIndexedPosition) RunePosition() (int, error) {
if lcip.lcPosition.LineNumber == 0 && lcip.lcPosition.ColumnPosition == 0 {
return 0, nil
}
if lcip.mapper == nil {
return -1, fmt.Errorf("nil mapper")
}
return lcip.mapper.LineAndColToRunePosition(lcip.lcPosition.LineNumber, lcip.lcPosition.ColumnPosition, lcip.source, SourceMapTracked)
}
func (lcip lcIndexedPosition) LineAndColumn() (int, int, error) {
return lcip.lcPosition.LineNumber, lcip.lcPosition.ColumnPosition, nil
}
func (lcip lcIndexedPosition) LineText() (string, error) {
if lcip.mapper == nil {
return "", fmt.Errorf("nil mapper")
}
return lcip.mapper.TextForLine(lcip.lcPosition.LineNumber, lcip.source, SourceMapTracked)
}