...

Source file src/go/token/position.go

Documentation: go/token

     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.
     4  
     5  package token
     6  
     7  import (
     8  	"fmt"
     9  	"sort"
    10  	"sync"
    11  	"sync/atomic"
    12  )
    13  
    14  // -----------------------------------------------------------------------------
    15  // Positions
    16  
    17  // Position describes an arbitrary source position
    18  // including the file, line, and column location.
    19  // 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 (byte count)
    25  }
    26  
    27  // IsValid reports whether the position is valid.
    28  func (pos *Position) IsValid() bool { return pos.Line > 0 }
    29  
    30  // String returns a string in one of several forms:
    31  //
    32  //	file:line:column    valid position with file name
    33  //	file:line           valid position with file name but no column (column == 0)
    34  //	line:column         valid position without file name
    35  //	line                valid position without file name and no column (column == 0)
    36  //	file                invalid position with file name
    37  //	-                   invalid position without file name
    38  func (pos Position) String() string {
    39  	s := pos.Filename
    40  	if pos.IsValid() {
    41  		if s != "" {
    42  			s += ":"
    43  		}
    44  		s += fmt.Sprintf("%d", pos.Line)
    45  		if pos.Column != 0 {
    46  			s += fmt.Sprintf(":%d", pos.Column)
    47  		}
    48  	}
    49  	if s == "" {
    50  		s = "-"
    51  	}
    52  	return s
    53  }
    54  
    55  // Pos is a compact encoding of a source position within a file set.
    56  // It can be converted into a Position for a more convenient, but much
    57  // larger, representation.
    58  //
    59  // The Pos value for a given file is a number in the range [base, base+size],
    60  // where base and size are specified when a file is added to the file set.
    61  // The difference between a Pos value and the corresponding file base
    62  // corresponds to the byte offset of that position (represented by the Pos value)
    63  // from the beginning of the file. Thus, the file base offset is the Pos value
    64  // representing the first byte in the file.
    65  //
    66  // To create the Pos value for a specific source offset (measured in bytes),
    67  // first add the respective file to the current file set using FileSet.AddFile
    68  // and then call File.Pos(offset) for that file. Given a Pos value p
    69  // for a specific file set fset, the corresponding Position value is
    70  // obtained by calling fset.Position(p).
    71  //
    72  // Pos values can be compared directly with the usual comparison operators:
    73  // If two Pos values p and q are in the same file, comparing p and q is
    74  // equivalent to comparing the respective source file offsets. If p and q
    75  // are in different files, p < q is true if the file implied by p was added
    76  // to the respective file set before the file implied by q.
    77  type Pos int
    78  
    79  // The zero value for Pos is NoPos; there is no file and line information
    80  // associated with it, and NoPos.IsValid() is false. NoPos is always
    81  // smaller than any other Pos value. The corresponding Position value
    82  // for NoPos is the zero value for Position.
    83  const NoPos Pos = 0
    84  
    85  // IsValid reports whether the position is valid.
    86  func (p Pos) IsValid() bool {
    87  	return p != NoPos
    88  }
    89  
    90  // -----------------------------------------------------------------------------
    91  // File
    92  
    93  // A File is a handle for a file belonging to a FileSet.
    94  // A File has a name, size, and line offset table.
    95  type File struct {
    96  	name string // file name as provided to AddFile
    97  	base int    // Pos value range for this file is [base...base+size]
    98  	size int    // file size as provided to AddFile
    99  
   100  	// lines and infos are protected by mutex
   101  	mutex sync.Mutex
   102  	lines []int // lines contains the offset of the first character for each line (the first entry is always 0)
   103  	infos []lineInfo
   104  }
   105  
   106  // Name returns the file name of file f as registered with AddFile.
   107  func (f *File) Name() string {
   108  	return f.name
   109  }
   110  
   111  // Base returns the base offset of file f as registered with AddFile.
   112  func (f *File) Base() int {
   113  	return f.base
   114  }
   115  
   116  // Size returns the size of file f as registered with AddFile.
   117  func (f *File) Size() int {
   118  	return f.size
   119  }
   120  
   121  // LineCount returns the number of lines in file f.
   122  func (f *File) LineCount() int {
   123  	f.mutex.Lock()
   124  	n := len(f.lines)
   125  	f.mutex.Unlock()
   126  	return n
   127  }
   128  
   129  // AddLine adds the line offset for a new line.
   130  // The line offset must be larger than the offset for the previous line
   131  // and smaller than the file size; otherwise the line offset is ignored.
   132  func (f *File) AddLine(offset int) {
   133  	f.mutex.Lock()
   134  	if i := len(f.lines); (i == 0 || f.lines[i-1] < offset) && offset < f.size {
   135  		f.lines = append(f.lines, offset)
   136  	}
   137  	f.mutex.Unlock()
   138  }
   139  
   140  // MergeLine merges a line with the following line. It is akin to replacing
   141  // the newline character at the end of the line with a space (to not change the
   142  // remaining offsets). To obtain the line number, consult e.g. Position.Line.
   143  // MergeLine will panic if given an invalid line number.
   144  func (f *File) MergeLine(line int) {
   145  	if line < 1 {
   146  		panic(fmt.Sprintf("invalid line number %d (should be >= 1)", line))
   147  	}
   148  	f.mutex.Lock()
   149  	defer f.mutex.Unlock()
   150  	if line >= len(f.lines) {
   151  		panic(fmt.Sprintf("invalid line number %d (should be < %d)", line, len(f.lines)))
   152  	}
   153  	// To merge the line numbered <line> with the line numbered <line+1>,
   154  	// we need to remove the entry in lines corresponding to the line
   155  	// numbered <line+1>. The entry in lines corresponding to the line
   156  	// numbered <line+1> is located at index <line>, since indices in lines
   157  	// are 0-based and line numbers are 1-based.
   158  	copy(f.lines[line:], f.lines[line+1:])
   159  	f.lines = f.lines[:len(f.lines)-1]
   160  }
   161  
   162  // SetLines sets the line offsets for a file and reports whether it succeeded.
   163  // The line offsets are the offsets of the first character of each line;
   164  // for instance for the content "ab\nc\n" the line offsets are {0, 3}.
   165  // An empty file has an empty line offset table.
   166  // Each line offset must be larger than the offset for the previous line
   167  // and smaller than the file size; otherwise SetLines fails and returns
   168  // false.
   169  // Callers must not mutate the provided slice after SetLines returns.
   170  func (f *File) SetLines(lines []int) bool {
   171  	// verify validity of lines table
   172  	size := f.size
   173  	for i, offset := range lines {
   174  		if i > 0 && offset <= lines[i-1] || size <= offset {
   175  			return false
   176  		}
   177  	}
   178  
   179  	// set lines table
   180  	f.mutex.Lock()
   181  	f.lines = lines
   182  	f.mutex.Unlock()
   183  	return true
   184  }
   185  
   186  // SetLinesForContent sets the line offsets for the given file content.
   187  // It ignores position-altering //line comments.
   188  func (f *File) SetLinesForContent(content []byte) {
   189  	var lines []int
   190  	line := 0
   191  	for offset, b := range content {
   192  		if line >= 0 {
   193  			lines = append(lines, line)
   194  		}
   195  		line = -1
   196  		if b == '\n' {
   197  			line = offset + 1
   198  		}
   199  	}
   200  
   201  	// set lines table
   202  	f.mutex.Lock()
   203  	f.lines = lines
   204  	f.mutex.Unlock()
   205  }
   206  
   207  // LineStart returns the Pos value of the start of the specified line.
   208  // It ignores any alternative positions set using AddLineColumnInfo.
   209  // LineStart panics if the 1-based line number is invalid.
   210  func (f *File) LineStart(line int) Pos {
   211  	if line < 1 {
   212  		panic(fmt.Sprintf("invalid line number %d (should be >= 1)", line))
   213  	}
   214  	f.mutex.Lock()
   215  	defer f.mutex.Unlock()
   216  	if line > len(f.lines) {
   217  		panic(fmt.Sprintf("invalid line number %d (should be < %d)", line, len(f.lines)))
   218  	}
   219  	return Pos(f.base + f.lines[line-1])
   220  }
   221  
   222  // A lineInfo object describes alternative file, line, and column
   223  // number information (such as provided via a //line directive)
   224  // for a given file offset.
   225  type lineInfo struct {
   226  	// fields are exported to make them accessible to gob
   227  	Offset       int
   228  	Filename     string
   229  	Line, Column int
   230  }
   231  
   232  // AddLineInfo is like AddLineColumnInfo with a column = 1 argument.
   233  // It is here for backward-compatibility for code prior to Go 1.11.
   234  func (f *File) AddLineInfo(offset int, filename string, line int) {
   235  	f.AddLineColumnInfo(offset, filename, line, 1)
   236  }
   237  
   238  // AddLineColumnInfo adds alternative file, line, and column number
   239  // information for a given file offset. The offset must be larger
   240  // than the offset for the previously added alternative line info
   241  // and smaller than the file size; otherwise the information is
   242  // ignored.
   243  //
   244  // AddLineColumnInfo is typically used to register alternative position
   245  // information for line directives such as //line filename:line:column.
   246  func (f *File) AddLineColumnInfo(offset int, filename string, line, column int) {
   247  	f.mutex.Lock()
   248  	if i := len(f.infos); i == 0 || f.infos[i-1].Offset < offset && offset < f.size {
   249  		f.infos = append(f.infos, lineInfo{offset, filename, line, column})
   250  	}
   251  	f.mutex.Unlock()
   252  }
   253  
   254  // Pos returns the Pos value for the given file offset;
   255  // the offset must be <= f.Size().
   256  // f.Pos(f.Offset(p)) == p.
   257  func (f *File) Pos(offset int) Pos {
   258  	if offset > f.size {
   259  		panic(fmt.Sprintf("invalid file offset %d (should be <= %d)", offset, f.size))
   260  	}
   261  	return Pos(f.base + offset)
   262  }
   263  
   264  // Offset returns the offset for the given file position p;
   265  // p must be a valid Pos value in that file.
   266  // f.Offset(f.Pos(offset)) == offset.
   267  func (f *File) Offset(p Pos) int {
   268  	if int(p) < f.base || int(p) > f.base+f.size {
   269  		panic(fmt.Sprintf("invalid Pos value %d (should be in [%d, %d])", p, f.base, f.base+f.size))
   270  	}
   271  	return int(p) - f.base
   272  }
   273  
   274  // Line returns the line number for the given file position p;
   275  // p must be a Pos value in that file or NoPos.
   276  func (f *File) Line(p Pos) int {
   277  	return f.Position(p).Line
   278  }
   279  
   280  func searchLineInfos(a []lineInfo, x int) int {
   281  	return sort.Search(len(a), func(i int) bool { return a[i].Offset > x }) - 1
   282  }
   283  
   284  // unpack returns the filename and line and column number for a file offset.
   285  // If adjusted is set, unpack will return the filename and line information
   286  // possibly adjusted by //line comments; otherwise those comments are ignored.
   287  func (f *File) unpack(offset int, adjusted bool) (filename string, line, column int) {
   288  	f.mutex.Lock()
   289  	defer f.mutex.Unlock()
   290  	filename = f.name
   291  	if i := searchInts(f.lines, offset); i >= 0 {
   292  		line, column = i+1, offset-f.lines[i]+1
   293  	}
   294  	if adjusted && len(f.infos) > 0 {
   295  		// few files have extra line infos
   296  		if i := searchLineInfos(f.infos, offset); i >= 0 {
   297  			alt := &f.infos[i]
   298  			filename = alt.Filename
   299  			if i := searchInts(f.lines, alt.Offset); i >= 0 {
   300  				// i+1 is the line at which the alternative position was recorded
   301  				d := line - (i + 1) // line distance from alternative position base
   302  				line = alt.Line + d
   303  				if alt.Column == 0 {
   304  					// alternative column is unknown => relative column is unknown
   305  					// (the current specification for line directives requires
   306  					// this to apply until the next PosBase/line directive,
   307  					// not just until the new newline)
   308  					column = 0
   309  				} else if d == 0 {
   310  					// the alternative position base is on the current line
   311  					// => column is relative to alternative column
   312  					column = alt.Column + (offset - alt.Offset)
   313  				}
   314  			}
   315  		}
   316  	}
   317  	return
   318  }
   319  
   320  func (f *File) position(p Pos, adjusted bool) (pos Position) {
   321  	offset := int(p) - f.base
   322  	pos.Offset = offset
   323  	pos.Filename, pos.Line, pos.Column = f.unpack(offset, adjusted)
   324  	return
   325  }
   326  
   327  // PositionFor returns the Position value for the given file position p.
   328  // If adjusted is set, the position may be adjusted by position-altering
   329  // //line comments; otherwise those comments are ignored.
   330  // p must be a Pos value in f or NoPos.
   331  func (f *File) PositionFor(p Pos, adjusted bool) (pos Position) {
   332  	if p != NoPos {
   333  		if int(p) < f.base || int(p) > f.base+f.size {
   334  			panic(fmt.Sprintf("invalid Pos value %d (should be in [%d, %d])", p, f.base, f.base+f.size))
   335  		}
   336  		pos = f.position(p, adjusted)
   337  	}
   338  	return
   339  }
   340  
   341  // Position returns the Position value for the given file position p.
   342  // Calling f.Position(p) is equivalent to calling f.PositionFor(p, true).
   343  func (f *File) Position(p Pos) (pos Position) {
   344  	return f.PositionFor(p, true)
   345  }
   346  
   347  // -----------------------------------------------------------------------------
   348  // FileSet
   349  
   350  // A FileSet represents a set of source files.
   351  // Methods of file sets are synchronized; multiple goroutines
   352  // may invoke them concurrently.
   353  //
   354  // The byte offsets for each file in a file set are mapped into
   355  // distinct (integer) intervals, one interval [base, base+size]
   356  // per file. Base represents the first byte in the file, and size
   357  // is the corresponding file size. A Pos value is a value in such
   358  // an interval. By determining the interval a Pos value belongs
   359  // to, the file, its file base, and thus the byte offset (position)
   360  // the Pos value is representing can be computed.
   361  //
   362  // When adding a new file, a file base must be provided. That can
   363  // be any integer value that is past the end of any interval of any
   364  // file already in the file set. For convenience, FileSet.Base provides
   365  // such a value, which is simply the end of the Pos interval of the most
   366  // recently added file, plus one. Unless there is a need to extend an
   367  // interval later, using the FileSet.Base should be used as argument
   368  // for FileSet.AddFile.
   369  type FileSet struct {
   370  	mutex sync.RWMutex         // protects the file set
   371  	base  int                  // base offset for the next file
   372  	files []*File              // list of files in the order added to the set
   373  	last  atomic.Pointer[File] // cache of last file looked up
   374  }
   375  
   376  // NewFileSet creates a new file set.
   377  func NewFileSet() *FileSet {
   378  	return &FileSet{
   379  		base: 1, // 0 == NoPos
   380  	}
   381  }
   382  
   383  // Base returns the minimum base offset that must be provided to
   384  // AddFile when adding the next file.
   385  func (s *FileSet) Base() int {
   386  	s.mutex.RLock()
   387  	b := s.base
   388  	s.mutex.RUnlock()
   389  	return b
   390  
   391  }
   392  
   393  // AddFile adds a new file with a given filename, base offset, and file size
   394  // to the file set s and returns the file. Multiple files may have the same
   395  // name. The base offset must not be smaller than the FileSet's Base(), and
   396  // size must not be negative. As a special case, if a negative base is provided,
   397  // the current value of the FileSet's Base() is used instead.
   398  //
   399  // Adding the file will set the file set's Base() value to base + size + 1
   400  // as the minimum base value for the next file. The following relationship
   401  // exists between a Pos value p for a given file offset offs:
   402  //
   403  //	int(p) = base + offs
   404  //
   405  // with offs in the range [0, size] and thus p in the range [base, base+size].
   406  // For convenience, File.Pos may be used to create file-specific position
   407  // values from a file offset.
   408  func (s *FileSet) AddFile(filename string, base, size int) *File {
   409  	// Allocate f outside the critical section.
   410  	f := &File{name: filename, size: size, lines: []int{0}}
   411  
   412  	s.mutex.Lock()
   413  	defer s.mutex.Unlock()
   414  	if base < 0 {
   415  		base = s.base
   416  	}
   417  	if base < s.base {
   418  		panic(fmt.Sprintf("invalid base %d (should be >= %d)", base, s.base))
   419  	}
   420  	f.base = base
   421  	if size < 0 {
   422  		panic(fmt.Sprintf("invalid size %d (should be >= 0)", size))
   423  	}
   424  	// base >= s.base && size >= 0
   425  	base += size + 1 // +1 because EOF also has a position
   426  	if base < 0 {
   427  		panic("token.Pos offset overflow (> 2G of source code in file set)")
   428  	}
   429  	// add the file to the file set
   430  	s.base = base
   431  	s.files = append(s.files, f)
   432  	s.last.Store(f)
   433  	return f
   434  }
   435  
   436  // Iterate calls f for the files in the file set in the order they were added
   437  // until f returns false.
   438  func (s *FileSet) Iterate(f func(*File) bool) {
   439  	for i := 0; ; i++ {
   440  		var file *File
   441  		s.mutex.RLock()
   442  		if i < len(s.files) {
   443  			file = s.files[i]
   444  		}
   445  		s.mutex.RUnlock()
   446  		if file == nil || !f(file) {
   447  			break
   448  		}
   449  	}
   450  }
   451  
   452  func searchFiles(a []*File, x int) int {
   453  	return sort.Search(len(a), func(i int) bool { return a[i].base > x }) - 1
   454  }
   455  
   456  func (s *FileSet) file(p Pos) *File {
   457  	// common case: p is in last file.
   458  	if f := s.last.Load(); f != nil && f.base <= int(p) && int(p) <= f.base+f.size {
   459  		return f
   460  	}
   461  
   462  	s.mutex.RLock()
   463  	defer s.mutex.RUnlock()
   464  
   465  	// p is not in last file - search all files
   466  	if i := searchFiles(s.files, int(p)); i >= 0 {
   467  		f := s.files[i]
   468  		// f.base <= int(p) by definition of searchFiles
   469  		if int(p) <= f.base+f.size {
   470  			// Update cache of last file. A race is ok,
   471  			// but an exclusive lock causes heavy contention.
   472  			s.last.Store(f)
   473  			return f
   474  		}
   475  	}
   476  	return nil
   477  }
   478  
   479  // File returns the file that contains the position p.
   480  // If no such file is found (for instance for p == NoPos),
   481  // the result is nil.
   482  func (s *FileSet) File(p Pos) (f *File) {
   483  	if p != NoPos {
   484  		f = s.file(p)
   485  	}
   486  	return
   487  }
   488  
   489  // PositionFor converts a Pos p in the fileset into a Position value.
   490  // If adjusted is set, the position may be adjusted by position-altering
   491  // //line comments; otherwise those comments are ignored.
   492  // p must be a Pos value in s or NoPos.
   493  func (s *FileSet) PositionFor(p Pos, adjusted bool) (pos Position) {
   494  	if p != NoPos {
   495  		if f := s.file(p); f != nil {
   496  			return f.position(p, adjusted)
   497  		}
   498  	}
   499  	return
   500  }
   501  
   502  // Position converts a Pos p in the fileset into a Position value.
   503  // Calling s.Position(p) is equivalent to calling s.PositionFor(p, true).
   504  func (s *FileSet) Position(p Pos) (pos Position) {
   505  	return s.PositionFor(p, true)
   506  }
   507  
   508  // -----------------------------------------------------------------------------
   509  // Helper functions
   510  
   511  func searchInts(a []int, x int) int {
   512  	// This function body is a manually inlined version of:
   513  	//
   514  	//   return sort.Search(len(a), func(i int) bool { return a[i] > x }) - 1
   515  	//
   516  	// With better compiler optimizations, this may not be needed in the
   517  	// future, but at the moment this change improves the go/printer
   518  	// benchmark performance by ~30%. This has a direct impact on the
   519  	// speed of gofmt and thus seems worthwhile (2011-04-29).
   520  	// TODO(gri): Remove this when compilers have caught up.
   521  	i, j := 0, len(a)
   522  	for i < j {
   523  		h := int(uint(i+j) >> 1) // avoid overflow when computing h
   524  		// i ≤ h < j
   525  		if a[h] <= x {
   526  			i = h + 1
   527  		} else {
   528  			j = h
   529  		}
   530  	}
   531  	return i - 1
   532  }
   533  

View as plain text