Repository navigation
Expand file tree
/
Copy pathsource_map.go
More file actions
69 lines (64 loc) · 1.59 KB
/
Copy pathsource_map.go
File metadata and controls
69 lines (64 loc) · 1.59 KB
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
package parser
import "unicode/utf16"
// sourceMap converts byte offsets in the (UTF-8) lexer input into the
// position coordinates used by ScriptDom: UTF-16 code-unit offsets and
// 1-based line/column numbers (columns also count UTF-16 code units).
type sourceMap struct {
// Indexed by byte offset (0..len(input)); continuation bytes of a
// multi-byte rune carry the same values as the rune's first byte.
u16 []int32
line []int32
col []int32
}
func newSourceMap(input string) *sourceMap {
n := len(input)
m := &sourceMap{
u16: make([]int32, n+1),
line: make([]int32, n+1),
col: make([]int32, n+1),
}
u16Off, line, col := int32(0), int32(1), int32(1)
i := 0
for i < n {
r, size := decodeRuneAt(input, i)
w := int32(1)
if utf16.RuneLen(r) == 2 {
w = 2
}
for j := 0; j < size; j++ {
m.u16[i+j] = u16Off
m.line[i+j] = line
m.col[i+j] = col
}
u16Off += w
if r == '\n' {
line++
col = 1
} else if r == '\r' && (i+1 >= n || input[i+1] != '\n') {
// A lone carriage return also starts a new line in ScriptDom.
line++
col = 1
} else {
col += w
}
i += size
}
m.u16[n] = u16Off
m.line[n] = line
m.col[n] = col
return m
}
// at returns the UTF-16 offset, line, and column for a byte offset.
func (m *sourceMap) at(byteOff int) (u16, line, col int) {
if byteOff < 0 {
byteOff = 0
}
if byteOff >= len(m.u16) {
byteOff = len(m.u16) - 1
}
return int(m.u16[byteOff]), int(m.line[byteOff]), int(m.col[byteOff])
}
// u16Len returns the total input length in UTF-16 code units.
func (m *sourceMap) u16Len() int {
return int(m.u16[len(m.u16)-1])
}