-
Notifications
You must be signed in to change notification settings - Fork 4
/
errors.go
53 lines (44 loc) · 1.38 KB
/
errors.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
package parser
import (
"fmt"
"github.com/Zac-Garby/radon/token"
)
// An Error represents a parsing error. Contains a message and two positions for
// the start and end of the error.
type Error struct {
Message string
Start, End token.Position
}
// Error returns a string representation of an Error, to comply with the error
// interface.
func (e *Error) Error() string {
return fmt.Sprintf("** Parse error ~ [%s-%s] %s", e.Start.String(), e.End.String(), e.Message)
}
// err creates a new Error. Calls fmt.Sprintf on the message with ...args.
func (p *Parser) err(msg string, start, end token.Position, args ...interface{}) {
err := &Error{
Message: fmt.Sprintf(msg, args...),
Start: start,
End: end,
}
p.Errors = append(p.Errors, err)
}
// defaultErr is the same as `p.err`, but assumes the start and end positions to
// be the start and end of the current token.
func (p *Parser) defaultErr(msg string, args ...interface{}) {
p.err(msg, p.cur.Start, p.cur.End, args...)
}
func (p *Parser) peekErr(t token.Type) {
if p.peek.Type == token.Semi {
p.err("unexpected end of line, wanted '%s'", p.peek.Start, p.peek.End, t)
} else {
p.err("expected '%s' but got '%s'", p.peek.Start, p.peek.End, t, p.peek.Type)
}
}
func (p *Parser) unexpected(t token.Type) {
if t == token.Semi {
p.defaultErr("unexpected end of line")
} else {
p.defaultErr("unexpected token: %s", t)
}
}