main
Raw Download raw file
  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.
  4
  5package scanner
  6
  7import (
  8	"fmt"
  9	"io"
 10	"sort"
 11)
 12
 13import (
 14	"github.com/go-git/gcfg/token"
 15)
 16
 17// In an ErrorList, an error is represented by an *Error.
 18// The position Pos, if valid, points to the beginning of
 19// the offending token, and the error condition is described
 20// by Msg.
 21//
 22type Error struct {
 23	Pos token.Position
 24	Msg string
 25}
 26
 27// Error implements the error interface.
 28func (e Error) Error() string {
 29	if e.Pos.Filename != "" || e.Pos.IsValid() {
 30		// don't print "<unknown position>"
 31		// TODO(gri) reconsider the semantics of Position.IsValid
 32		return e.Pos.String() + ": " + e.Msg
 33	}
 34	return e.Msg
 35}
 36
 37// ErrorList is a list of *Errors.
 38// The zero value for an ErrorList is an empty ErrorList ready to use.
 39//
 40type ErrorList []*Error
 41
 42// Add adds an Error with given position and error message to an ErrorList.
 43func (p *ErrorList) Add(pos token.Position, msg string) {
 44	*p = append(*p, &Error{pos, msg})
 45}
 46
 47// Reset resets an ErrorList to no errors.
 48func (p *ErrorList) Reset() { *p = (*p)[0:0] }
 49
 50// ErrorList implements the sort Interface.
 51func (p ErrorList) Len() int      { return len(p) }
 52func (p ErrorList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
 53
 54func (p ErrorList) Less(i, j int) bool {
 55	e := &p[i].Pos
 56	f := &p[j].Pos
 57	if e.Filename < f.Filename {
 58		return true
 59	}
 60	if e.Filename == f.Filename {
 61		return e.Offset < f.Offset
 62	}
 63	return false
 64}
 65
 66// Sort sorts an ErrorList. *Error entries are sorted by position,
 67// other errors are sorted by error message, and before any *Error
 68// entry.
 69//
 70func (p ErrorList) Sort() {
 71	sort.Sort(p)
 72}
 73
 74// RemoveMultiples sorts an ErrorList and removes all but the first error per line.
 75func (p *ErrorList) RemoveMultiples() {
 76	sort.Sort(p)
 77	var last token.Position // initial last.Line is != any legal error line
 78	i := 0
 79	for _, e := range *p {
 80		if e.Pos.Filename != last.Filename || e.Pos.Line != last.Line {
 81			last = e.Pos
 82			(*p)[i] = e
 83			i++
 84		}
 85	}
 86	(*p) = (*p)[0:i]
 87}
 88
 89// An ErrorList implements the error interface.
 90func (p ErrorList) Error() string {
 91	switch len(p) {
 92	case 0:
 93		return "no errors"
 94	case 1:
 95		return p[0].Error()
 96	}
 97	return fmt.Sprintf("%s (and %d more errors)", p[0], len(p)-1)
 98}
 99
100// Err returns an error equivalent to this error list.
101// If the list is empty, Err returns nil.
102func (p ErrorList) Err() error {
103	if len(p) == 0 {
104		return nil
105	}
106	return p
107}
108
109// PrintError is a utility function that prints a list of errors to w,
110// one error per line, if the err parameter is an ErrorList. Otherwise
111// it prints the err string.
112//
113func PrintError(w io.Writer, err error) {
114	if list, ok := err.(ErrorList); ok {
115		for _, e := range list {
116			fmt.Fprintf(w, "%s\n", e)
117		}
118	} else if err != nil {
119		fmt.Fprintf(w, "%s\n", err)
120	}
121}