1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
14 // An implementation of an ErrorHandler may be provided to the Scanner.
15 // If a syntax error is encountered and a handler was installed, Error
16 // is called with a position and an error message. The position points
17 // to the beginning of the offending token.
19 type ErrorHandler interface {
20 Error(pos token.Position, msg string)
23 // ErrorVector implements the ErrorHandler interface. It maintains a list
24 // of errors which can be retrieved with GetErrorList and GetError. The
25 // zero value for an ErrorVector is an empty ErrorVector ready to use.
27 // A common usage pattern is to embed an ErrorVector alongside a
28 // scanner in a data structure that uses the scanner. By passing a
29 // reference to an ErrorVector to the scanner's Init call, default
30 // error handling is obtained.
32 type ErrorVector struct {
36 // Reset resets an ErrorVector to no errors.
37 func (h *ErrorVector) Reset() { h.errors = h.errors[:0] }
39 // ErrorCount returns the number of errors collected.
40 func (h *ErrorVector) ErrorCount() int { return len(h.errors) }
42 // Within ErrorVector, an error is represented by an Error node. The
43 // position Pos, if valid, points to the beginning of the offending
44 // token, and the error condition is described by Msg.
51 func (e *Error) Error() string {
52 if e.Pos.Filename != "" || e.Pos.IsValid() {
53 // don't print "<unknown position>"
54 // TODO(gri) reconsider the semantics of Position.IsValid
55 return e.Pos.String() + ": " + e.Msg
60 // An ErrorList is a (possibly sorted) list of Errors.
61 type ErrorList []*Error
63 // ErrorList implements the sort Interface.
64 func (p ErrorList) Len() int { return len(p) }
65 func (p ErrorList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
67 func (p ErrorList) Less(i, j int) bool {
70 // Note that it is not sufficient to simply compare file offsets because
71 // the offsets do not reflect modified line information (through //line
73 if e.Filename < f.Filename {
76 if e.Filename == f.Filename {
81 return e.Column < f.Column
87 func (p ErrorList) Error() string {
90 return "unspecified error"
94 return fmt.Sprintf("%s (and %d more errors)", p[0], len(p)-1)
97 // These constants control the construction of the ErrorList
98 // returned by GetErrors.
101 Raw = iota // leave error list unchanged
102 Sorted // sort error list by file, line, and column number
103 NoMultiples // sort error list and leave only the first error per line
106 // GetErrorList returns the list of errors collected by an ErrorVector.
107 // The construction of the ErrorList returned is controlled by the mode
108 // parameter. If there are no errors, the result is nil.
110 func (h *ErrorVector) GetErrorList(mode int) ErrorList {
111 if len(h.errors) == 0 {
115 list := make(ErrorList, len(h.errors))
122 if mode >= NoMultiples {
123 var last token.Position // initial last.Line is != any legal error line
125 for _, e := range list {
126 if e.Pos.Filename != last.Filename || e.Pos.Line != last.Line {
138 // GetError is like GetErrorList, but it returns an error instead
139 // so that a nil result can be assigned to an error variable and
142 func (h *ErrorVector) GetError(mode int) error {
143 if len(h.errors) == 0 {
147 return h.GetErrorList(mode)
150 // ErrorVector implements the ErrorHandler interface.
151 func (h *ErrorVector) Error(pos token.Position, msg string) {
152 h.errors = append(h.errors, &Error{pos, msg})
155 // PrintError is a utility function that prints a list of errors to w,
156 // one error per line, if the err parameter is an ErrorList. Otherwise
157 // it prints the err string.
159 func PrintError(w io.Writer, err error) {
160 if list, ok := err.(ErrorList); ok {
161 for _, e := range list {
162 fmt.Fprintf(w, "%s\n", e)
165 fmt.Fprintf(w, "%s\n", err)