-
Notifications
You must be signed in to change notification settings - Fork 2
/
error.go
100 lines (85 loc) · 1.9 KB
/
error.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
96
97
98
99
100
package shorthand
import (
"fmt"
"strconv"
)
// Error represents an error at a specific location.
type Error interface {
Error() string
// Offset returns the character offset of the error within the experssion.
Offset() uint
// Length returns the length in bytes after the offset where the error ends.
Length() uint
// Pretty prints out a message with a pointer to the source location of the
// error.
Pretty() string
}
type exprErr struct {
source *string
offset uint
length uint
message string
}
func (e *exprErr) Error() string {
return e.message
}
func (e *exprErr) Offset() uint {
return e.offset
}
func (e *exprErr) Length() uint {
return e.length
}
func (e *exprErr) Pretty() string {
// Figure out which absolute line we are on.
lineNo := 1
for i := 0; i < int(e.offset); i++ {
if (*e.source)[i] == '\n' {
lineNo++
}
}
// Determine lines of context to show if multi-line.
start := int(e.offset)
if start > len(*e.source)-1 {
start = len(*e.source) - 1
}
lineStart := 0
lines := 0
for start > 0 {
if (*e.source)[start] == '\n' {
if lines == 0 {
lineStart = start + 1
}
lines++
if lines >= 4 {
start++
break
}
}
start--
}
end := int(e.offset)
for end < len(*e.source) && (*e.source)[end] != '\n' {
end++
}
// Generate a nice error message with context.
msg := e.Error() + " at line " + strconv.Itoa(lineNo) + " col " + strconv.Itoa(int(e.offset-uint(lineStart))+1) + "\n" + (*e.source)[start:end] + "\n"
for i := uint(lineStart); i < e.offset; i++ {
msg += "."
}
for i := 0; i < end-int(e.offset); i++ {
msg += "^"
}
return msg
}
// NewError creates a new error at a specific location.
func NewError(source *string, offset uint, length uint, format string, a ...interface{}) Error {
if length < 1 {
length = 1
}
return &exprErr{
source: source,
offset: offset,
length: length,
message: fmt.Sprintf(format, a...),
}
}