Source file src/go/scanner/errors.go

     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  
     5  package scanner
     6  
     7  import (
     8  	"fmt"
     9  	"go/token"
    10  	"io"
    11  	"sort"
    12  )
    13  
    14  // In an [ErrorList], an error is represented by an *Error.
    15  // The position Pos, if valid, points to the beginning of
    16  // the offending token, and the error condition is described
    17  // by Msg.
    18  type Error struct {
    19  	Pos token.Position
    20  	Msg string
    21  }
    22  
    23  // Error implements the error interface.
    24  func (e Error) Error() string {
    25  	if e.Pos.Filename != "" || e.Pos.IsValid() {
    26  		// don't print "<unknown position>"
    27  		// TODO(gri) reconsider the semantics of Position.IsValid
    28  		return e.Pos.String() + ": " + e.Msg
    29  	}
    30  	return e.Msg
    31  }
    32  
    33  // ErrorList is a list of *Errors.
    34  // The zero value for an ErrorList is an empty ErrorList ready to use.
    35  type ErrorList []*Error
    36  
    37  // Add adds an [Error] with given position and error message to an [ErrorList].
    38  func (p *ErrorList) Add(pos token.Position, msg string) {
    39  	*p = append(*p, &Error{pos, msg})
    40  }
    41  
    42  // Reset resets an [ErrorList] to no errors.
    43  func (p *ErrorList) Reset() { *p = (*p)[0:0] }
    44  
    45  // [ErrorList] implements the sort Interface.
    46  func (p ErrorList) Len() int      { return len(p) }
    47  func (p ErrorList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
    48  
    49  func (p ErrorList) Less(i, j int) bool {
    50  	e := &p[i].Pos
    51  	f := &p[j].Pos
    52  	// Note that it is not sufficient to simply compare file offsets because
    53  	// the offsets do not reflect modified line information (through //line
    54  	// comments).
    55  	if e.Filename != f.Filename {
    56  		return e.Filename < f.Filename
    57  	}
    58  	if e.Line != f.Line {
    59  		return e.Line < f.Line
    60  	}
    61  	if e.Column != f.Column {
    62  		return e.Column < f.Column
    63  	}
    64  	return p[i].Msg < p[j].Msg
    65  }
    66  
    67  // Sort sorts an [ErrorList]. *[Error] entries are sorted by position,
    68  // other errors are sorted by error message, and before any *[Error]
    69  // entry.
    70  func (p ErrorList) Sort() {
    71  	sort.Sort(p)
    72  }
    73  
    74  // RemoveMultiples sorts an [ErrorList] and removes all but the first error per line.
    75  func (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.
    90  func (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.
   102  func (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  func PrintError(w io.Writer, err error) {
   113  	if list, ok := err.(ErrorList); ok {
   114  		for _, e := range list {
   115  			fmt.Fprintf(w, "%s\n", e)
   116  		}
   117  	} else if err != nil {
   118  		fmt.Fprintf(w, "%s\n", err)
   119  	}
   120  }
   121  

View as plain text