...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package compiler
16
17 import "fmt"
18
19
20 type Error struct {
21 Context *Context
22 Message string
23 }
24
25
26 func NewError(context *Context, message string) *Error {
27 return &Error{Context: context, Message: message}
28 }
29
30 func (err *Error) locationDescription() string {
31 if err.Context.Node != nil {
32 return fmt.Sprintf("[%d,%d] %s", err.Context.Node.Line, err.Context.Node.Column, err.Context.Description())
33 }
34 return err.Context.Description()
35 }
36
37
38 func (err *Error) Error() string {
39 if err.Context == nil {
40 return err.Message
41 }
42 return err.locationDescription() + " " + err.Message
43 }
44
45
46 type ErrorGroup struct {
47 Errors []error
48 }
49
50
51 func NewErrorGroupOrNil(errors []error) error {
52 if len(errors) == 0 {
53 return nil
54 } else if len(errors) == 1 {
55 return errors[0]
56 } else {
57 return &ErrorGroup{Errors: errors}
58 }
59 }
60
61 func (group *ErrorGroup) Error() string {
62 result := ""
63 for i, err := range group.Errors {
64 if i > 0 {
65 result += "\n"
66 }
67 result += err.Error()
68 }
69 return result
70 }
71
View as plain text