github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/go/token/position.go (about) 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 // TODO(gri) consider making this a separate package outside the go directory. 6 7 package token 8 9 import ( 10 "fmt" 11 "sort" 12 "sync" 13 ) 14 15 // ----------------------------------------------------------------------------- 16 // Positions 17 18 // Position describes an arbitrary source position 19 // including the file, line, and column location. 20 // A Position is valid if the line number is > 0. 21 // 22 type Position struct { 23 Filename string // filename, if any 24 Offset int // offset, starting at 0 25 Line int // line number, starting at 1 26 Column int // column number, starting at 1 (character count) 27 } 28 29 // IsValid returns true if the position is valid. 30 func (pos *Position) IsValid() bool { return pos.Line > 0 } 31 32 // String returns a string in one of several forms: 33 // 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 38 // 39 func (pos Position) String() string { 40 s := pos.Filename 41 if pos.IsValid() { 42 if s != "" { 43 s += ":" 44 } 45 s += fmt.Sprintf("%d:%d", pos.Line, pos.Column) 46 } 47 if s == "" { 48 s = "-" 49 } 50 return s 51 } 52 53 // Pos is a compact encoding of a source position within a file set. 54 // It can be converted into a Position for a more convenient, but much 55 // larger, representation. 56 // 57 // The Pos value for a given file is a number in the range [base, base+size], 58 // where base and size are specified when adding the file to the file set via 59 // AddFile. 60 // 61 // To create the Pos value for a specific source offset, first add 62 // the respective file to the current file set (via FileSet.AddFile) 63 // and then call File.Pos(offset) for that file. Given a Pos value p 64 // for a specific file set fset, the corresponding Position value is 65 // obtained by calling fset.Position(p). 66 // 67 // Pos values can be compared directly with the usual comparison operators: 68 // If two Pos values p and q are in the same file, comparing p and q is 69 // equivalent to comparing the respective source file offsets. If p and q 70 // are in different files, p < q is true if the file implied by p was added 71 // to the respective file set before the file implied by q. 72 // 73 type Pos int 74 75 // The zero value for Pos is NoPos; there is no file and line information 76 // associated with it, and NoPos().IsValid() is false. NoPos is always 77 // smaller than any other Pos value. The corresponding Position value 78 // for NoPos is the zero value for Position. 79 // 80 const NoPos Pos = 0 81 82 // IsValid returns true if the position is valid. 83 func (p Pos) IsValid() bool { 84 return p != NoPos 85 } 86 87 // ----------------------------------------------------------------------------- 88 // File 89 90 // A File is a handle for a file belonging to a FileSet. 91 // A File has a name, size, and line offset table. 92 // 93 type File struct { 94 set *FileSet 95 name string // file name as provided to AddFile 96 base int // Pos value range for this file is [base...base+size] 97 size int // file size as provided to AddFile 98 99 // lines and infos are protected by set.mutex 100 lines []int // lines contains the offset of the first character for each line (the first entry is always 0) 101 infos []lineInfo 102 } 103 104 // Name returns the file name of file f as registered with AddFile. 105 func (f *File) Name() string { 106 return f.name 107 } 108 109 // Base returns the base offset of file f as registered with AddFile. 110 func (f *File) Base() int { 111 return f.base 112 } 113 114 // Size returns the size of file f as registered with AddFile. 115 func (f *File) Size() int { 116 return f.size 117 } 118 119 // LineCount returns the number of lines in file f. 120 func (f *File) LineCount() int { 121 f.set.mutex.RLock() 122 n := len(f.lines) 123 f.set.mutex.RUnlock() 124 return n 125 } 126 127 // AddLine adds the line offset for a new line. 128 // The line offset must be larger than the offset for the previous line 129 // and smaller than the file size; otherwise the line offset is ignored. 130 // 131 func (f *File) AddLine(offset int) { 132 f.set.mutex.Lock() 133 if i := len(f.lines); (i == 0 || f.lines[i-1] < offset) && offset < f.size { 134 f.lines = append(f.lines, offset) 135 } 136 f.set.mutex.Unlock() 137 } 138 139 // MergeLine merges a line with the following line. It is akin to replacing 140 // the newline character at the end of the line with a space (to not change the 141 // remaining offsets). To obtain the line number, consult e.g. Position.Line. 142 // MergeLine will panic if given an invalid line number. 143 // 144 func (f *File) MergeLine(line int) { 145 if line <= 0 { 146 panic("illegal line number (line numbering starts at 1)") 147 } 148 f.set.mutex.Lock() 149 defer f.set.mutex.Unlock() 150 if line >= len(f.lines) { 151 panic("illegal line number") 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 returns true if successful. 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 // 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.set.mutex.Lock() 181 f.lines = lines 182 f.set.mutex.Unlock() 183 return true 184 } 185 186 // SetLinesForContent sets the line offsets for the given file content. 187 func (f *File) SetLinesForContent(content []byte) { 188 var lines []int 189 line := 0 190 for offset, b := range content { 191 if line >= 0 { 192 lines = append(lines, line) 193 } 194 line = -1 195 if b == '\n' { 196 line = offset + 1 197 } 198 } 199 200 // set lines table 201 f.set.mutex.Lock() 202 f.lines = lines 203 f.set.mutex.Unlock() 204 } 205 206 // A lineInfo object describes alternative file and line number 207 // information (such as provided via a //line comment in a .go 208 // file) for a given file offset. 209 type lineInfo struct { 210 // fields are exported to make them accessible to gob 211 Offset int 212 Filename string 213 Line int 214 } 215 216 // AddLineInfo adds alternative file and line number information for 217 // a given file offset. The offset must be larger than the offset for 218 // the previously added alternative line info and smaller than the 219 // file size; otherwise the information is ignored. 220 // 221 // AddLineInfo is typically used to register alternative position 222 // information for //line filename:line comments in source files. 223 // 224 func (f *File) AddLineInfo(offset int, filename string, line int) { 225 f.set.mutex.Lock() 226 if i := len(f.infos); i == 0 || f.infos[i-1].Offset < offset && offset < f.size { 227 f.infos = append(f.infos, lineInfo{offset, filename, line}) 228 } 229 f.set.mutex.Unlock() 230 } 231 232 // Pos returns the Pos value for the given file offset; 233 // the offset must be <= f.Size(). 234 // f.Pos(f.Offset(p)) == p. 235 // 236 func (f *File) Pos(offset int) Pos { 237 if offset > f.size { 238 panic("illegal file offset") 239 } 240 return Pos(f.base + offset) 241 } 242 243 // Offset returns the offset for the given file position p; 244 // p must be a valid Pos value in that file. 245 // f.Offset(f.Pos(offset)) == offset. 246 // 247 func (f *File) Offset(p Pos) int { 248 if int(p) < f.base || int(p) > f.base+f.size { 249 panic("illegal Pos value") 250 } 251 return int(p) - f.base 252 } 253 254 // Line returns the line number for the given file position p; 255 // p must be a Pos value in that file or NoPos. 256 // 257 func (f *File) Line(p Pos) int { 258 // TODO(gri) this can be implemented much more efficiently 259 return f.Position(p).Line 260 } 261 262 func searchLineInfos(a []lineInfo, x int) int { 263 return sort.Search(len(a), func(i int) bool { return a[i].Offset > x }) - 1 264 } 265 266 // info returns the file name, line, and column number for a file offset. 267 func (f *File) info(offset int) (filename string, line, column int) { 268 filename = f.name 269 if i := searchInts(f.lines, offset); i >= 0 { 270 line, column = i+1, offset-f.lines[i]+1 271 } 272 if len(f.infos) > 0 { 273 // almost no files have extra line infos 274 if i := searchLineInfos(f.infos, offset); i >= 0 { 275 alt := &f.infos[i] 276 filename = alt.Filename 277 if i := searchInts(f.lines, alt.Offset); i >= 0 { 278 line += alt.Line - i - 1 279 } 280 } 281 } 282 return 283 } 284 285 func (f *File) position(p Pos) (pos Position) { 286 offset := int(p) - f.base 287 pos.Offset = offset 288 pos.Filename, pos.Line, pos.Column = f.info(offset) 289 return 290 } 291 292 // Position returns the Position value for the given file position p; 293 // p must be a Pos value in that file or NoPos. 294 // 295 func (f *File) Position(p Pos) (pos Position) { 296 if p != NoPos { 297 if int(p) < f.base || int(p) > f.base+f.size { 298 panic("illegal Pos value") 299 } 300 pos = f.position(p) 301 } 302 return 303 } 304 305 // ----------------------------------------------------------------------------- 306 // FileSet 307 308 // A FileSet represents a set of source files. 309 // Methods of file sets are synchronized; multiple goroutines 310 // may invoke them concurrently. 311 // 312 type FileSet struct { 313 mutex sync.RWMutex // protects the file set 314 base int // base offset for the next file 315 files []*File // list of files in the order added to the set 316 last *File // cache of last file looked up 317 } 318 319 // NewFileSet creates a new file set. 320 func NewFileSet() *FileSet { 321 return &FileSet{ 322 base: 1, // 0 == NoPos 323 } 324 } 325 326 // Base returns the minimum base offset that must be provided to 327 // AddFile when adding the next file. 328 // 329 func (s *FileSet) Base() int { 330 s.mutex.RLock() 331 b := s.base 332 s.mutex.RUnlock() 333 return b 334 335 } 336 337 // AddFile adds a new file with a given filename, base offset, and file size 338 // to the file set s and returns the file. Multiple files may have the same 339 // name. The base offset must not be smaller than the FileSet's Base(), and 340 // size must not be negative. As a special case, if a negative base is provided, 341 // the current value of the FileSet's Base() is used instead. 342 // 343 // Adding the file will set the file set's Base() value to base + size + 1 344 // as the minimum base value for the next file. The following relationship 345 // exists between a Pos value p for a given file offset offs: 346 // 347 // int(p) = base + offs 348 // 349 // with offs in the range [0, size] and thus p in the range [base, base+size]. 350 // For convenience, File.Pos may be used to create file-specific position 351 // values from a file offset. 352 // 353 func (s *FileSet) AddFile(filename string, base, size int) *File { 354 s.mutex.Lock() 355 defer s.mutex.Unlock() 356 if base < 0 { 357 base = s.base 358 } 359 if base < s.base || size < 0 { 360 panic("illegal base or size") 361 } 362 // base >= s.base && size >= 0 363 f := &File{s, filename, base, size, []int{0}, nil} 364 base += size + 1 // +1 because EOF also has a position 365 if base < 0 { 366 panic("token.Pos offset overflow (> 2G of source code in file set)") 367 } 368 // add the file to the file set 369 s.base = base 370 s.files = append(s.files, f) 371 s.last = f 372 return f 373 } 374 375 // Iterate calls f for the files in the file set in the order they were added 376 // until f returns false. 377 // 378 func (s *FileSet) Iterate(f func(*File) bool) { 379 for i := 0; ; i++ { 380 var file *File 381 s.mutex.RLock() 382 if i < len(s.files) { 383 file = s.files[i] 384 } 385 s.mutex.RUnlock() 386 if file == nil || !f(file) { 387 break 388 } 389 } 390 } 391 392 func searchFiles(a []*File, x int) int { 393 return sort.Search(len(a), func(i int) bool { return a[i].base > x }) - 1 394 } 395 396 func (s *FileSet) file(p Pos) *File { 397 s.mutex.RLock() 398 // common case: p is in last file 399 if f := s.last; f != nil && f.base <= int(p) && int(p) <= f.base+f.size { 400 s.mutex.RUnlock() 401 return f 402 } 403 // p is not in last file - search all files 404 if i := searchFiles(s.files, int(p)); i >= 0 { 405 f := s.files[i] 406 // f.base <= int(p) by definition of searchFiles 407 if int(p) <= f.base+f.size { 408 s.mutex.RUnlock() 409 s.mutex.Lock() 410 s.last = f // race is ok - s.last is only a cache 411 s.mutex.Unlock() 412 return f 413 } 414 } 415 s.mutex.RUnlock() 416 return nil 417 } 418 419 // File returns the file that contains the position p. 420 // If no such file is found (for instance for p == NoPos), 421 // the result is nil. 422 // 423 func (s *FileSet) File(p Pos) (f *File) { 424 if p != NoPos { 425 f = s.file(p) 426 } 427 return 428 } 429 430 // Position converts a Pos in the fileset into a general Position. 431 func (s *FileSet) Position(p Pos) (pos Position) { 432 if p != NoPos { 433 if f := s.file(p); f != nil { 434 pos = f.position(p) 435 } 436 } 437 return 438 } 439 440 // ----------------------------------------------------------------------------- 441 // Helper functions 442 443 func searchInts(a []int, x int) int { 444 // This function body is a manually inlined version of: 445 // 446 // return sort.Search(len(a), func(i int) bool { return a[i] > x }) - 1 447 // 448 // With better compiler optimizations, this may not be needed in the 449 // future, but at the moment this change improves the go/printer 450 // benchmark performance by ~30%. This has a direct impact on the 451 // speed of gofmt and thus seems worthwhile (2011-04-29). 452 // TODO(gri): Remove this when compilers have caught up. 453 i, j := 0, len(a) 454 for i < j { 455 h := i + (j-i)/2 // avoid overflow when computing h 456 // i ≤ h < j 457 if a[h] <= x { 458 i = h + 1 459 } else { 460 j = h 461 } 462 } 463 return i - 1 464 }