- Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy patherror.go
More file actions
Latest commit
47 lines (41 loc) · 1.19 KB
/
Copy patherror.go
File metadata and controls
47 lines (41 loc) · 1.19 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
package parse
import (
"bytes"
"fmt"
"io"
)
// Error is a parsing error returned by parser. It contains a message and an offset at which the error occurred.
typeErrorstruct {
Messagestring
Lineint
Columnint
Contextstring
}
// NewError creates a new error
funcNewError(r io.Reader, offsetint, messagestring, a...interface{}) *Error {
line, column, context:=Position(r, offset)
if0<len(a) {
message=fmt.Sprintf(message, a...)
}
return&Error{
Message: message,
Line: line,
Column: column,
Context: context,
}
}
// NewErrorLexer creates a new error from an active Lexer.
funcNewErrorLexer(l*Input, messagestring, a...interface{}) *Error {
r:=bytes.NewBuffer(l.Bytes())
offset:=l.Offset()
returnNewError(r, offset, message, a...)
}
// Position returns the line, column, and context of the error.
// Context is the entire line at which the error occurred.
func (e*Error) Position() (int, int, string) {
returne.Line, e.Column, e.Context
}
// Error returns the error string, containing the context and line + column number.
func (e*Error) Error() string {
returnfmt.Sprintf("%s on line %d and column %d\n%s", e.Message, e.Line, e.Column, e.Context)
}