...

Source file src/go/scanner/errors.go

Documentation: go/scanner

     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  var _ error = (*Error)(nil) // *Error (not Error) is the correct type in error.Is tests.
    24  
    25  // Error implements the error interface.
    26  func (e Error) Error() string {
    27  	if e.Pos.Filename != "" || e.Pos.IsValid() {
    28  		// don't print "<unknown position>"
    29  		// TODO(gri) reconsider the semantics of Position.IsValid
    30  		return e.Pos.String() + ": " + e.Msg
    31  	}
    32  	return e.Msg
    33  }
    34  
    35  // ErrorList is a list of *Errors.
    36  // The zero value for an ErrorList is an empty ErrorList ready to use.
    37  type ErrorList []*Error
    38  
    39  // Add adds an [Error] with given position and error message to an [ErrorList].
    40  func (p *ErrorList) Add(pos token.Position, msg string) {
    41  	*p = append(*p, &Error{pos, msg})
    42  }
    43  
    44  // Reset resets an [ErrorList] to no errors.
    45  func (p *ErrorList) Reset() { *p = (*p)[0:0] }
    46  
    47  // [ErrorList] implements the sort Interface.
    48  func (p ErrorList) Len() int      { return len(p) }
    49  func (p ErrorList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
    50  
    51  func (p ErrorList) Less(i, j int) bool {
    52  	e := &p[i].Pos
    53  	f := &p[j].Pos
    54  	// Note that it is not sufficient to simply compare file offsets because
    55  	// the offsets do not reflect modified line information (through //line
    56  	// comments).
    57  	if e.Filename != f.Filename {
    58  		return e.Filename < f.Filename
    59  	}
    60  	if e.Line != f.Line {
    61  		return e.Line < f.Line
    62  	}
    63  	if e.Column != f.Column {
    64  		return e.Column < f.Column
    65  	}
    66  	return p[i].Msg < p[j].Msg
    67  }
    68  
    69  // Sort sorts an [ErrorList]. *[Error] entries are sorted by position,
    70  // other errors are sorted by error message, and before any *[Error]
    71  // entry.
    72  func (p ErrorList) Sort() {
    73  	sort.Sort(p)
    74  }
    75  
    76  // RemoveMultiples sorts an [ErrorList] and removes all but the first error per line.
    77  func (p *ErrorList) RemoveMultiples() {
    78  	sort.Sort(p)
    79  	var last token.Position // initial last.Line is != any legal error line
    80  	i := 0
    81  	for _, e := range *p {
    82  		if e.Pos.Filename != last.Filename || e.Pos.Line != last.Line {
    83  			last = e.Pos
    84  			(*p)[i] = e
    85  			i++
    86  		}
    87  	}
    88  	*p = (*p)[0:i]
    89  }
    90  
    91  // An [ErrorList] implements the error interface.
    92  func (p ErrorList) Error() string {
    93  	switch len(p) {
    94  	case 0:
    95  		return "no errors"
    96  	case 1:
    97  		return p[0].Error()
    98  	}
    99  	return fmt.Sprintf("%s (and %d more errors)", p[0], len(p)-1)
   100  }
   101  
   102  // Err returns an error equivalent to this error list.
   103  // If the list is empty, Err returns nil.
   104  func (p ErrorList) Err() error {
   105  	if len(p) == 0 {
   106  		return nil
   107  	}
   108  	return p
   109  }
   110  
   111  // PrintError is a utility function that prints a list of errors to w,
   112  // one error per line, if the err parameter is an [ErrorList]. Otherwise
   113  // it prints the err string.
   114  func PrintError(w io.Writer, err error) {
   115  	if list, ok := err.(ErrorList); ok {
   116  		for _, e := range list {
   117  			fmt.Fprintf(w, "%s\n", e)
   118  		}
   119  	} else if err != nil {
   120  		fmt.Fprintf(w, "%s\n", err)
   121  	}
   122  }
   123  

View as plain text