-
Notifications
You must be signed in to change notification settings - Fork 64
/
position.go
95 lines (86 loc) · 2.02 KB
/
position.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package parse
import (
"fmt"
"io"
"strings"
"unicode"
)
// Position returns the line and column number for a certain position in a file. It is useful for recovering the position in a file that caused an error.
// It only treates \n, \r, and \r\n as newlines, which might be different from some languages also recognizing \f, \u2028, and \u2029 to be newlines.
func Position(r io.Reader, offset int) (line, col int, context string) {
l := NewInput(r)
line = 1
for l.Pos() < offset {
c := l.Peek(0)
n := 1
newline := false
if c == '\n' {
newline = true
} else if c == '\r' {
if l.Peek(1) == '\n' {
newline = true
n = 2
} else {
newline = true
}
} else if c >= 0xC0 {
var r rune
if r, n = l.PeekRune(0); r == '\u2028' || r == '\u2029' {
newline = true
}
} else if c == 0 && l.Err() != nil {
break
}
if 1 < n && offset < l.Pos()+n {
break
}
l.Move(n)
if newline {
line++
offset -= l.Pos()
l.Skip()
}
}
col = len([]rune(string(l.Lexeme()))) + 1
context = positionContext(l, line, col)
return
}
func positionContext(l *Input, line, col int) (context string) {
for {
c := l.Peek(0)
if c == 0 && l.Err() != nil || c == '\n' || c == '\r' {
break
}
l.Move(1)
}
rs := []rune(string(l.Lexeme()))
// cut off front or rear of context to stay between 60 characters
limit := 60
offset := 20
ellipsisFront := ""
ellipsisRear := ""
if limit < len(rs) {
if col <= limit-offset {
ellipsisRear = "..."
rs = rs[:limit-3]
} else if col >= len(rs)-offset-3 {
ellipsisFront = "..."
col -= len(rs) - offset - offset - 7
rs = rs[len(rs)-offset-offset-4:]
} else {
ellipsisFront = "..."
ellipsisRear = "..."
rs = rs[col-offset-1 : col+offset]
col = offset + 4
}
}
// replace unprintable characters by a space
for i, r := range rs {
if !unicode.IsGraphic(r) {
rs[i] = '·'
}
}
context += fmt.Sprintf("%5d: %s%s%s\n", line, ellipsisFront, string(rs), ellipsisRear)
context += fmt.Sprintf("%s^", strings.Repeat(" ", 6+col))
return
}