1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // TODO(gri) consider making this a separate package outside the go directory.
16 // Position describes an arbitrary source position
17 // including the file, line, and column location.
18 // A Position is valid if the line number is > 0.
20 type Position struct {
21 Filename string // filename, if any
22 Offset int // offset, starting at 0
23 Line int // line number, starting at 1
24 Column int // column number, starting at 1 (character count)
28 // IsValid returns true if the position is valid.
29 func (pos *Position) IsValid() bool { return pos.Line > 0 }
32 // String returns a string in one of several forms:
34 // file:line:column valid position with file name
35 // line:column valid position without file name
36 // file invalid position with file name
37 // - invalid position without file name
39 func (pos Position) String() string {
45 s += fmt.Sprintf("%d:%d", pos.Line, pos.Column)
54 // Pos is a compact encoding of a source position within a file set.
55 // It can be converted into a Position for a more convenient, but much
56 // larger, representation.
58 // The Pos value for a given file is a number in the range [base, base+size],
59 // where base and size are specified when adding the file to the file set via
62 // To create the Pos value for a specific source offset, first add
63 // the respective file to the current file set (via FileSet.AddFile)
64 // and then call File.Pos(offset) for that file. Given a Pos value p
65 // for a specific file set fset, the corresponding Position value is
66 // obtained by calling fset.Position(p).
68 // Pos values can be compared directly with the usual comparison operators:
69 // If two Pos values p and q are in the same file, comparing p and q is
70 // equivalent to comparing the respective source file offsets. If p and q
71 // are in different files, p < q is true if the file implied by p was added
72 // to the respective file set before the file implied by q.
77 // The zero value for Pos is NoPos; there is no file and line information
78 // associated with it, and NoPos().IsValid() is false. NoPos is always
79 // smaller than any other Pos value. The corresponding Position value
80 // for NoPos is the zero value for Position.
85 // IsValid returns true if the position is valid.
86 func (p Pos) IsValid() bool {
91 func searchFiles(a []*File, x int) int {
92 return sort.Search(len(a), func(i int) bool { return a[i].base > x }) - 1
96 func (s *FileSet) file(p Pos) *File {
97 if f := s.last; f != nil && f.base <= int(p) && int(p) <= f.base+f.size {
100 if i := searchFiles(s.files, int(p)); i >= 0 {
102 // f.base <= int(p) by definition of searchFiles
103 if int(p) <= f.base+f.size {
112 // File returns the file which contains the position p.
113 // If no such file is found (for instance for p == NoPos),
114 // the result is nil.
116 func (s *FileSet) File(p Pos) (f *File) {
126 func (f *File) position(p Pos) (pos Position) {
127 offset := int(p) - f.base
129 pos.Filename, pos.Line, pos.Column = f.info(offset)
134 // Position converts a Pos in the fileset into a general Position.
135 func (s *FileSet) Position(p Pos) (pos Position) {
137 // TODO(gri) consider optimizing the case where p
138 // is in the last file addded, or perhaps
139 // looked at - will eliminate one level
142 if f := s.file(p); f != nil {
151 type lineInfo struct {
158 // AddLineInfo adds alternative file and line number information for
159 // a given file offset. The offset must be larger than the offset for
160 // the previously added alternative line info and smaller than the
161 // file size; otherwise the information is ignored.
163 // AddLineInfo is typically used to register alternative position
164 // information for //line filename:line comments in source files.
166 func (f *File) AddLineInfo(offset int, filename string, line int) {
168 if i := len(f.infos); i == 0 || f.infos[i-1].offset < offset && offset < f.size {
169 f.infos = append(f.infos, lineInfo{offset, filename, line})
175 // A File is a handle for a file belonging to a FileSet.
176 // A File has a name, size, and line offset table.
180 name string // file name as provided to AddFile
181 base int // Pos value range for this file is [base...base+size]
182 size int // file size as provided to AddFile
184 // lines and infos are protected by set.mutex
190 // Name returns the file name of file f as registered with AddFile.
191 func (f *File) Name() string {
196 // Base returns the base offset of file f as registered with AddFile.
197 func (f *File) Base() int {
202 // Size returns the size of file f as registered with AddFile.
203 func (f *File) Size() int {
208 // LineCount returns the number of lines in file f.
209 func (f *File) LineCount() int {
212 f.set.mutex.RUnlock()
217 // AddLine adds the line offset for a new line.
218 // The line offset must be larger than the offset for the previous line
219 // and smaller than the file size; otherwise the line offset is ignored.
221 func (f *File) AddLine(offset int) {
223 if i := len(f.lines); (i == 0 || f.lines[i-1] < offset) && offset < f.size {
224 f.lines = append(f.lines, offset)
230 // SetLines sets the line offsets for a file and returns true if successful.
231 // The line offsets are the offsets of the first character of each line;
232 // for instance for the content "ab\nc\n" the line offsets are {0, 3}.
233 // An empty file has an empty line offset table.
234 // Each line offset must be larger than the offset for the previous line
235 // and smaller than the file size; otherwise SetLines fails and returns
238 func (f *File) SetLines(lines []int) bool {
239 // verify validity of lines table
241 for i, offset := range lines {
242 if i > 0 && offset <= lines[i-1] || size <= offset {
255 // SetLinesForContent sets the line offsets for the given file content.
256 func (f *File) SetLinesForContent(content []byte) {
259 for offset, b := range content {
261 lines = append(lines, line)
276 // Pos returns the Pos value for the given file offset;
277 // the offset must be <= f.Size().
278 // f.Pos(f.Offset(p)) == p.
280 func (f *File) Pos(offset int) Pos {
282 panic("illegal file offset")
284 return Pos(f.base + offset)
288 // Offset returns the offset for the given file position p;
289 // p must be a valid Pos value in that file.
290 // f.Offset(f.Pos(offset)) == offset.
292 func (f *File) Offset(p Pos) int {
293 if int(p) < f.base || int(p) > f.base+f.size {
294 panic("illegal Pos value")
296 return int(p) - f.base
300 // Line returns the line number for the given file position p;
301 // p must be a Pos value in that file or NoPos.
303 func (f *File) Line(p Pos) int {
304 // TODO(gri) this can be implemented much more efficiently
305 return f.Position(p).Line
309 // Position returns the Position value for the given file position p;
310 // p must be a Pos value in that file or NoPos.
312 func (f *File) Position(p Pos) (pos Position) {
314 if int(p) < f.base || int(p) > f.base+f.size {
315 panic("illegal Pos value")
323 func searchInts(a []int, x int) int {
324 // This function body is a manually inlined version of:
326 // return sort.Search(len(a), func(i int) bool { return a[i] > x }) - 1
328 // With better compiler optimizations, this may not be needed in the
329 // future, but at the moment this change improves the go/printer
330 // benchmark performance by ~30%. This has a direct impact on the
331 // speed of gofmt and thus seems worthwhile (2011-04-29).
334 h := i + (j-i)/2 // avoid overflow when computing h
346 func searchLineInfos(a []lineInfo, x int) int {
347 return sort.Search(len(a), func(i int) bool { return a[i].offset > x }) - 1
351 // info returns the file name, line, and column number for a file offset.
352 func (f *File) info(offset int) (filename string, line, column int) {
354 if i := searchInts(f.lines, offset); i >= 0 {
355 line, column = i+1, offset-f.lines[i]+1
357 if len(f.infos) > 0 {
358 // almost no files have extra line infos
359 if i := searchLineInfos(f.infos, offset); i >= 0 {
361 filename = alt.filename
362 if i := searchInts(f.lines, alt.offset); i >= 0 {
363 line += alt.line - i - 1
371 // A FileSet represents a set of source files.
372 // Methods of file sets are synchronized; multiple goroutines
373 // may invoke them concurrently.
375 type FileSet struct {
376 mutex sync.RWMutex // protects the file set
377 base int // base offset for the next file
378 files []*File // list of files in the order added to the set
379 last *File // cache of last file looked up
383 // NewFileSet creates a new file set.
384 func NewFileSet() *FileSet {
386 s.base = 1 // 0 == NoPos
391 // Base returns the minimum base offset that must be provided to
392 // AddFile when adding the next file.
394 func (s *FileSet) Base() int {
403 // AddFile adds a new file with a given filename, base offset, and file size
404 // to the file set s and returns the file. Multiple files may have the same
405 // name. The base offset must not be smaller than the FileSet's Base(), and
406 // size must not be negative.
408 // Adding the file will set the file set's Base() value to base + size + 1
409 // as the minimum base value for the next file. The following relationship
410 // exists between a Pos value p for a given file offset offs:
412 // int(p) = base + offs
414 // with offs in the range [0, size] and thus p in the range [base, base+size].
415 // For convenience, File.Pos may be used to create file-specific position
416 // values from a file offset.
418 func (s *FileSet) AddFile(filename string, base, size int) *File {
420 defer s.mutex.Unlock()
421 if base < s.base || size < 0 {
422 panic("illegal base or size")
424 // base >= s.base && size >= 0
425 f := &File{s, filename, base, size, []int{0}, nil}
426 base += size + 1 // +1 because EOF also has a position
428 panic("token.Pos offset overflow (> 2G of source code in file set)")
430 // add the file to the file set
432 s.files = append(s.files, f)
438 // Files returns the files added to the file set.
439 func (s *FileSet) Files() <-chan *File {
440 ch := make(chan *File)
445 if i < len(s.files) {