-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
error.go
104 lines (90 loc) · 2.99 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
101
102
103
104
package participle
import (
"fmt"
"github.com/alecthomas/participle/v2/lexer"
)
// Error represents an error while parsing.
//
// The format of an Error is in the form "[<filename>:][<line>:<pos>:] <message>".
//
// The error will contain positional information if available.
type Error interface {
error
// Unadorned message.
Message() string
// Closest position to error location.
Position() lexer.Position
}
// FormatError formats an error in the form "[<filename>:][<line>:<pos>:] <message>"
func FormatError(err Error) string {
msg := ""
pos := err.Position()
if pos.Filename != "" {
msg += pos.Filename + ":"
}
if pos.Line != 0 || pos.Column != 0 {
msg += fmt.Sprintf("%d:%d:", pos.Line, pos.Column)
}
if msg != "" {
msg += " " + err.Message()
} else {
msg = err.Message()
}
return msg
}
// UnexpectedTokenError is returned by Parse when an unexpected token is encountered.
//
// This is useful for composing parsers in order to detect when a sub-parser has terminated.
type UnexpectedTokenError struct {
Unexpected lexer.Token
at node
}
func (u UnexpectedTokenError) Error() string { return FormatError(u) }
func (u UnexpectedTokenError) Message() string { // nolint: golint
var expected string
if u.at != nil {
expected = fmt.Sprintf(" (expected %s)", u.at)
}
return fmt.Sprintf("unexpected token %q%s", u.Unexpected, expected)
}
func (u UnexpectedTokenError) Position() lexer.Position { return u.Unexpected.Pos } // nolint: golint
type ParseError struct {
Msg string
Pos lexer.Position
}
func (p *ParseError) Error() string { return FormatError(p) }
func (p *ParseError) Message() string { return p.Msg }
func (p *ParseError) Position() lexer.Position { return p.Pos }
// Errorf creates a new Error at the given position.
func Errorf(pos lexer.Position, format string, args ...interface{}) Error {
return &ParseError{Msg: fmt.Sprintf(format, args...), Pos: pos}
}
type wrappingParseError struct {
err error
ParseError
}
func (w *wrappingParseError) Unwrap() error { return w.err }
// Wrapf attempts to wrap an existing error in a new message.
//
// If "err" is a participle.Error, its positional information will be used.
//
// The returned error implements the Unwrap() method supported by the errors package.
func Wrapf(pos lexer.Position, err error, format string, args ...interface{}) Error {
var msg string
if perr, ok := err.(Error); ok {
pos = perr.Position()
msg = fmt.Sprintf("%s: %s", fmt.Sprintf(format, args...), perr.Message())
} else {
msg = fmt.Sprintf("%s: %s", fmt.Sprintf(format, args...), err.Error())
}
return &wrappingParseError{err: err, ParseError: ParseError{Msg: msg, Pos: pos}}
}
// AnnotateError wraps an existing error with a position.
//
// If the existing error is a lexer.Error or participle.Error it will be returned unmodified.
func AnnotateError(pos lexer.Position, err error) error {
if perr, ok := err.(Error); ok {
return perr
}
return &wrappingParseError{err: err, ParseError: ParseError{Msg: err.Error(), Pos: pos}}
}