Copyright 2010 The Go Authors. All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
TODO(gri) consider making this a separate package outside the go directory.

package token

import (
	
	
	
)
----------------------------------------------------------------------------- Positions
Position describes an arbitrary source position including the file, line, and column location. A Position is valid if the line number is > 0.
type Position struct {
	Filename string // filename, if any
	Offset   int    // offset, starting at 0
	Line     int    // line number, starting at 1
	Column   int    // column number, starting at 1 (character count)
}
IsValid returns true if the position is valid.
func ( *Position) () bool { return .Line > 0 }
String returns a string in one of several forms: file:line:column valid position with file name line:column valid position without file name file invalid position with file name - invalid position without file name
func ( Position) () string {
	 := .Filename
	if .IsValid() {
		if  != "" {
			 += ":"
		}
		 += fmt.Sprintf("%d:%d", .Line, .Column)
	}
	if  == "" {
		 = "-"
	}
	return 
}
Pos is a compact encoding of a source position within a file set. It can be converted into a Position for a more convenient, but much larger, representation. The Pos value for a given file is a number in the range [base, base+size], where base and size are specified when adding the file to the file set via AddFile. To create the Pos value for a specific source offset, first add the respective file to the current file set (via FileSet.AddFile) and then call File.Pos(offset) for that file. Given a Pos value p for a specific file set fset, the corresponding Position value is obtained by calling fset.Position(p). Pos values can be compared directly with the usual comparison operators: If two Pos values p and q are in the same file, comparing p and q is equivalent to comparing the respective source file offsets. If p and q are in different files, p < q is true if the file implied by p was added to the respective file set before the file implied by q.
type Pos int
The zero value for Pos is NoPos; there is no file and line information associated with it, and NoPos().IsValid() is false. NoPos is always smaller than any other Pos value. The corresponding Position value for NoPos is the zero value for Position.
const NoPos Pos = 0
IsValid returns true if the position is valid.
func ( Pos) () bool {
	return  != NoPos
}
----------------------------------------------------------------------------- File
A File is a handle for a file belonging to a FileSet. A File has a name, size, and line offset table.
type File struct {
	set  *FileSet
	name string // file name as provided to AddFile
	base int    // Pos value range for this file is [base...base+size]
	size int    // file size as provided to AddFile
lines and infos are protected by set.mutex
Name returns the file name of file f as registered with AddFile.
func ( *File) () string {
	return .name
}
Base returns the base offset of file f as registered with AddFile.
func ( *File) () int {
	return .base
}
Size returns the size of file f as registered with AddFile.
func ( *File) () int {
	return .size
}
LineCount returns the number of lines in file f.
func ( *File) () int {
	.set.mutex.RLock()
	 := len(.lines)
	.set.mutex.RUnlock()
	return 
}
AddLine adds the line offset for a new line. The line offset must be larger than the offset for the previous line and smaller than the file size; otherwise the line offset is ignored.
func ( *File) ( int) {
	.set.mutex.Lock()
	if  := len(.lines); ( == 0 || .lines[-1] < ) &&  < .size {
		.lines = append(.lines, )
	}
	.set.mutex.Unlock()
}
SetLines sets the line offsets for a file and returns true if successful. The line offsets are the offsets of the first character of each line; for instance for the content "ab\nc\n" the line offsets are {0, 3}. An empty file has an empty line offset table. Each line offset must be larger than the offset for the previous line and smaller than the file size; otherwise SetLines fails and returns false.
verify validity of lines table
	 := .size
	for ,  := range  {
		if  > 0 &&  <= [-1] ||  <=  {
			return false
		}
	}
set lines table
	.set.mutex.Lock()
	.lines = 
	.set.mutex.Unlock()
	return true
}
SetLinesForContent sets the line offsets for the given file content.
func ( *File) ( []byte) {
	var  []int
	 := 0
	for ,  := range  {
		if  >= 0 {
			 = append(, )
		}
		 = -1
		if  == '\n' {
			 =  + 1
		}
	}
set lines table
	.set.mutex.Lock()
	.lines = 
	.set.mutex.Unlock()
}
A lineInfo object describes alternative file and line number information (such as provided via a //line comment in a .go file) for a given file offset.
fields are exported to make them accessible to gob
AddLineInfo adds alternative file and line number information for a given file offset. The offset must be larger than the offset for the previously added alternative line info and smaller than the file size; otherwise the information is ignored. AddLineInfo is typically used to register alternative position information for //line filename:line comments in source files.
func ( *File) ( int,  string,  int) {
	.set.mutex.Lock()
	if  := len(.infos);  == 0 || .infos[-1].Offset <  &&  < .size {
		.infos = append(.infos, lineInfo{, , })
	}
	.set.mutex.Unlock()
}
Pos returns the Pos value for the given file offset; the offset must be <= f.Size(). f.Pos(f.Offset(p)) == p.
func ( *File) ( int) Pos {
	if  > .size {
		panic("illegal file offset")
	}
	return Pos(.base + )
}
Offset returns the offset for the given file position p; p must be a valid Pos value in that file. f.Offset(f.Pos(offset)) == offset.
func ( *File) ( Pos) int {
	if int() < .base || int() > .base+.size {
		panic("illegal Pos value")
	}
	return int() - .base
}
Line returns the line number for the given file position p; p must be a Pos value in that file or NoPos.
TODO(gri) this can be implemented much more efficiently
	return .Position().Line
}

func ( []lineInfo,  int) int {
	return sort.Search(len(), func( int) bool { return [].Offset >  }) - 1
}
info returns the file name, line, and column number for a file offset.
func ( *File) ( int) ( string, ,  int) {
	 = .name
	if  := searchInts(.lines, );  >= 0 {
		,  = +1, -.lines[]+1
	}
almost no files have extra line infos
		if  := searchLineInfos(.infos, );  >= 0 {
			 := &.infos[]
			 = .Filename
			if  := searchInts(.lines, .Offset);  >= 0 {
				 += .Line -  - 1
			}
		}
	}
	return
}

func ( *File) ( Pos) ( Position) {
	 := int() - .base
	.Offset = 
	.Filename, .Line, .Column = .info()
	return
}
Position returns the Position value for the given file position p; p must be a Pos value in that file or NoPos.
func ( *File) ( Pos) ( Position) {
	if  != NoPos {
		if int() < .base || int() > .base+.size {
			panic("illegal Pos value")
		}
		 = .position()
	}
	return
}
----------------------------------------------------------------------------- FileSet
A FileSet represents a set of source files. Methods of file sets are synchronized; multiple goroutines may invoke them concurrently.
type FileSet struct {
	mutex sync.RWMutex // protects the file set
	base  int          // base offset for the next file
	files []*File      // list of files in the order added to the set
	last  *File        // cache of last file looked up
}
NewFileSet creates a new file set.
func () *FileSet {
	 := new(FileSet)
	.base = 1 // 0 == NoPos
	return 
}
Base returns the minimum base offset that must be provided to AddFile when adding the next file.
func ( *FileSet) () int {
	.mutex.RLock()
	 := .base
	.mutex.RUnlock()
	return 

}
AddFile adds a new file with a given filename, base offset, and file size to the file set s and returns the file. Multiple files may have the same name. The base offset must not be smaller than the FileSet's Base(), and size must not be negative. Adding the file will set the file set's Base() value to base + size + 1 as the minimum base value for the next file. The following relationship exists between a Pos value p for a given file offset offs: int(p) = base + offs with offs in the range [0, size] and thus p in the range [base, base+size]. For convenience, File.Pos may be used to create file-specific position values from a file offset.
func ( *FileSet) ( string, ,  int) *File {
	.mutex.Lock()
	defer .mutex.Unlock()
	if  < .base ||  < 0 {
		panic("illegal base or size")
base >= s.base && size >= 0
	 := &File{, , , , []int{0}, nil}
	 +=  + 1 // +1 because EOF also has a position
	if  < 0 {
		panic("token.Pos offset overflow (> 2G of source code in file set)")
add the file to the file set
	.base = 
	.files = append(.files, )
	.last = 
	return 
}
Iterate calls f for the files in the file set in the order they were added until f returns false.
func ( *FileSet) ( func(*File) bool) {
	for  := 0; ; ++ {
		var  *File
		.mutex.RLock()
		if  < len(.files) {
			 = .files[]
		}
		.mutex.RUnlock()
		if  == nil || !() {
			break
		}
	}
}

func ( []*File,  int) int {
	return sort.Search(len(), func( int) bool { return [].base >  }) - 1
}

common case: p is in last file
	if  := .last;  != nil && .base <= int() && int() <= .base+.size {
		return 
p is not in last file - search all files
	if  := searchFiles(.files, int());  >= 0 {
f.base <= int(p) by definition of searchFiles
		if int() <= .base+.size {
			.last = 
			return 
		}
	}
	return nil
}
File returns the file that contains the position p. If no such file is found (for instance for p == NoPos), the result is nil.
func ( *FileSet) ( Pos) ( *File) {
	if  != NoPos {
		.mutex.RLock()
		 = .file()
		.mutex.RUnlock()
	}
	return
}
Position converts a Pos in the fileset into a general Position.
func ( *FileSet) ( Pos) ( Position) {
	if  != NoPos {
		.mutex.RLock()
		if  := .file();  != nil {
			 = .position()
		}
		.mutex.RUnlock()
	}
	return
}
----------------------------------------------------------------------------- Helper functions

This function body is a manually inlined version of: return sort.Search(len(a), func(i int) bool { return a[i] > x }) - 1 With better compiler optimizations, this may not be needed in the future, but at the moment this change improves the go/printer benchmark performance by ~30%. This has a direct impact on the speed of gofmt and thus seems worthwhile (2011-04-29). TODO(gri): Remove this when compilers have caught up.
	,  := 0, len()
	for  <  {
i ≤ h < j
		if [] <=  {
			 =  + 1
		} else {
			 = 
		}
	}
	return  - 1