func (s *FileSet) AddFile(filename string, base, size int) *File {
	s.mutex.Lock()
	defer s.mutex.Unlock()
	if base < 0 {
		base = s.base
	}
	if base < s.base || size < 0 {
		panic("illegal base or size")
	}
	f := &File{set: s, name: filename, base: base, size: size, lines: []int{0}}
	base += size + 1  
	if base < 0 {
		panic("token.Pos offset overflow (> 2G of source code in file set)")
	}
	s.base = base
	s.files = append(s.files, f)
	s.last = f
	return f
}
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. As a special case, if a negative base is provided,
the current value of the FileSet's Base() is used instead.

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.
package token
import (
	"fmt"
	"sort"
	"sync"
)
type Position struct {
	Filename string  
	Offset   int     
	Line     int     
	Column   int     
}
func (pos *Position) IsValid() bool { return pos.Line > 0 }
func (pos Position) String() string {
	s := pos.Filename
	if pos.IsValid() {
		if s != "" {
			s += ":"
		}
		s += fmt.Sprintf("%d:%d", pos.Line, pos.Column)
	}
	if s == "" {
		s = "-"
	}
	return s
}
type Pos int
const NoPos Pos = 0
func (p Pos) IsValid() bool {
	return p != NoPos
}
type File struct {
	set  *FileSet
	name string  
	base int     
	size int     
	mutex sync.Mutex
	lines []int  
	infos []lineInfo
}
func (f *File) Name() string {
	return f.name
}
func (f *File) Base() int {
	return f.base
}
func (f *File) Size() int {
	return f.size
}
func (f *File) LineCount() int {
	f.mutex.Lock()
	n := len(f.lines)
	f.mutex.Unlock()
	return n
}
func (f *File) AddLine(offset int) {
	f.mutex.Lock()
	if i := len(f.lines); (i == 0 || f.lines[i-1] < offset) && offset < f.size {
		f.lines = append(f.lines, offset)
	}
	f.mutex.Unlock()
}
func (f *File) MergeLine(line int) {
	if line <= 0 {
		panic("illegal line number (line numbering starts at 1)")
	}
	f.mutex.Lock()
	defer f.mutex.Unlock()
	if line >= len(f.lines) {
		panic("illegal line number")
	}
	copy(f.lines[line:], f.lines[line+1:])
	f.lines = f.lines[:len(f.lines)-1]
}
func (f *File) SetLines(lines []int) bool {
	size := f.size
	for i, offset := range lines {
		if i > 0 && offset <= lines[i-1] || size <= offset {
			return false
		}
	}
	f.mutex.Lock()
	f.lines = lines
	f.mutex.Unlock()
	return true
}
func (f *File) SetLinesForContent(content []byte) {
	var lines []int
	line := 0
	for offset, b := range content {
		if line >= 0 {
			lines = append(lines, line)
		}
		line = -1
		if b == '\n' {
			line = offset + 1
		}
	}
	f.mutex.Lock()
	f.lines = lines
	f.mutex.Unlock()
}
type lineInfo struct {
	Offset   int
	Filename string
	Line     int
}
func (f *File) AddLineInfo(offset int, filename string, line int) {
	f.mutex.Lock()
	if i := len(f.infos); i == 0 || f.infos[i-1].Offset < offset && offset < f.size {
		f.infos = append(f.infos, lineInfo{offset, filename, line})
	}
	f.mutex.Unlock()
}
func (f *File) Pos(offset int) Pos {
	if offset > f.size {
		panic("illegal file offset")
	}
	return Pos(f.base + offset)
}
func (f *File) Offset(p Pos) int {
	if int(p) < f.base || int(p) > f.base+f.size {
		panic("illegal Pos value")
	}
	return int(p) - f.base
}
func (f *File) Line(p Pos) int {
	return f.Position(p).Line
}
func searchLineInfos(a []lineInfo, x int) int {
	return sort.Search(len(a), func(i int) bool { return a[i].Offset > x }) - 1
}
func (f *File) unpack(offset int, adjusted bool) (filename string, line, column int) {
	f.mutex.Lock()
	defer f.mutex.Unlock()
	filename = f.name
	if i := searchInts(f.lines, offset); i >= 0 {
		line, column = i+1, offset-f.lines[i]+1
	}
	if adjusted && len(f.infos) > 0 {
		if i := searchLineInfos(f.infos, offset); i >= 0 {
			alt := &f.infos[i]
			filename = alt.Filename
			if i := searchInts(f.lines, alt.Offset); i >= 0 {
				line += alt.Line - i - 1
			}
		}
	}
	return
}
func (f *File) position(p Pos, adjusted bool) (pos Position) {
	offset := int(p) - f.base
	pos.Offset = offset
	pos.Filename, pos.Line, pos.Column = f.unpack(offset, adjusted)
	return
}
func (f *File) PositionFor(p Pos, adjusted bool) (pos Position) {
	if p != NoPos {
		if int(p) < f.base || int(p) > f.base+f.size {
			panic("illegal Pos value")
		}
		pos = f.position(p, adjusted)
	}
	return
}
func (f *File) Position(p Pos) (pos Position) {
	return f.PositionFor(p, true)
}
type FileSet struct {
	mutex sync.RWMutex  
	base  int           
	files []*File       
	last  *File         
}
func NewFileSet() *FileSet {
	return &FileSet{
		base: 1,  
	}
}
func (s *FileSet) Base() int {
	s.mutex.RLock()
	b := s.base
	s.mutex.RUnlock()
	return b
}