forked from dop251/goja
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrcfile.go
73 lines (63 loc) · 1.09 KB
/
srcfile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package goja
import (
"fmt"
"sort"
"strings"
)
type Position struct {
Line, Col int
}
type SrcFile struct {
name string
src string
lineOffsets []int
lastScannedOffset int
}
func NewSrcFile(name, src string) *SrcFile {
return &SrcFile{
name: name,
src: src,
}
}
func (f *SrcFile) Position(offset int) Position {
var line int
if offset > f.lastScannedOffset {
f.scanTo(offset)
line = len(f.lineOffsets) - 1
} else {
if len(f.lineOffsets) > 0 {
line = sort.SearchInts(f.lineOffsets, offset)
} else {
line = -1
}
}
if line >= 0 {
if f.lineOffsets[line] > offset {
line--
}
}
var lineStart int
if line >= 0 {
lineStart = f.lineOffsets[line]
}
return Position{
Line: line + 2,
Col: offset - lineStart + 1,
}
}
func (f *SrcFile) scanTo(offset int) {
o := f.lastScannedOffset
for o < offset {
p := strings.Index(f.src[o:], "\n")
if p == -1 {
o = len(f.src)
break
}
o = o + p + 1
f.lineOffsets = append(f.lineOffsets, o)
}
f.lastScannedOffset = o
}
func (p Position) String() string {
return fmt.Sprintf("%d:%d", p.Line, p.Col)
}