| 1 |
|
| 2 |
package file |
| 3 |
|
| 4 |
import ( |
| 5 |
"fmt" |
| 6 |
"strings" |
| 7 |
|
| 8 |
"gopkg.in/sourcemap.v1" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
type Idx int |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
type Position struct { |
| 19 |
Filename string |
| 20 |
Offset int |
| 21 |
Line int |
| 22 |
Column int |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
func (p *Position) isValid() bool { |
| 28 |
return p.Line > 0 |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
func (p *Position) String() string { |
| 38 |
str := p.Filename |
| 39 |
if p.isValid() { |
| 40 |
if str != "" { |
| 41 |
str += ":" |
| 42 |
} |
| 43 |
str += fmt.Sprintf("%d:%d", p.Line, p.Column) |
| 44 |
} |
| 45 |
if str == "" { |
| 46 |
str = "-" |
| 47 |
} |
| 48 |
return str |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
type FileSet struct { |
| 53 |
last *File |
| 54 |
files []*File |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
func (fs *FileSet) AddFile(filename, src string) int { |
| 61 |
base := fs.nextBase() |
| 62 |
file := &File{ |
| 63 |
name: filename, |
| 64 |
src: src, |
| 65 |
base: base, |
| 66 |
} |
| 67 |
fs.files = append(fs.files, file) |
| 68 |
fs.last = file |
| 69 |
return base |
| 70 |
} |
| 71 |
|
| 72 |
func (fs *FileSet) nextBase() int { |
| 73 |
if fs.last == nil { |
| 74 |
return 1 |
| 75 |
} |
| 76 |
return fs.last.base + len(fs.last.src) + 1 |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
func (fs *FileSet) File(idx Idx) *File { |
| 81 |
for _, file := range fs.files { |
| 82 |
if idx <= Idx(file.base+len(file.src)) { |
| 83 |
return file |
| 84 |
} |
| 85 |
} |
| 86 |
return nil |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
func (fs *FileSet) Position(idx Idx) *Position { |
| 91 |
for _, file := range fs.files { |
| 92 |
if idx <= Idx(file.base+len(file.src)) { |
| 93 |
return file.Position(idx - Idx(file.base)) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return nil |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
type File struct { |
| 102 |
sm *sourcemap.Consumer |
| 103 |
name string |
| 104 |
src string |
| 105 |
base int |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func NewFile(filename, src string, base int) *File { |
| 110 |
return &File{ |
| 111 |
name: filename, |
| 112 |
src: src, |
| 113 |
base: base, |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
func (fl *File) WithSourceMap(sm *sourcemap.Consumer) *File { |
| 119 |
fl.sm = sm |
| 120 |
return fl |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
func (fl *File) Name() string { |
| 125 |
return fl.name |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
func (fl *File) Source() string { |
| 130 |
return fl.src |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
func (fl *File) Base() int { |
| 135 |
return fl.base |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
func (fl *File) Position(idx Idx) *Position { |
| 140 |
position := &Position{} |
| 141 |
|
| 142 |
offset := int(idx) - fl.base |
| 143 |
|
| 144 |
if offset >= len(fl.src) || offset < 0 { |
| 145 |
return nil |
| 146 |
} |
| 147 |
|
| 148 |
src := fl.src[:offset] |
| 149 |
|
| 150 |
position.Filename = fl.name |
| 151 |
position.Offset = offset |
| 152 |
position.Line = strings.Count(src, "\n") + 1 |
| 153 |
|
| 154 |
if index := strings.LastIndex(src, "\n"); index >= 0 { |
| 155 |
position.Column = offset - index |
| 156 |
} else { |
| 157 |
position.Column = len(src) + 1 |
| 158 |
} |
| 159 |
|
| 160 |
if fl.sm != nil { |
| 161 |
if f, _, l, c, ok := fl.sm.Source(position.Line, position.Column); ok { |
| 162 |
position.Filename, position.Line, position.Column = f, l, c |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
return position |
| 167 |
} |