// Copyright 2009 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package scannerimport ()// In an [ErrorList], an error is represented by an *Error.// The position Pos, if valid, points to the beginning of// the offending token, and the error condition is described// by Msg.typeErrorstruct { Pos token.Position Msg string}// Error implements the error interface.func ( Error) () string {if .Pos.Filename != "" || .Pos.IsValid() {// don't print "<unknown position>" // TODO(gri) reconsider the semantics of Position.IsValidreturn .Pos.String() + ": " + .Msg }return .Msg}// ErrorList is a list of *Errors.// The zero value for an ErrorList is an empty ErrorList ready to use.typeErrorList []*Error// Add adds an [Error] with given position and error message to an [ErrorList].func ( *ErrorList) ( token.Position, string) { * = append(*, &Error{, })}// Reset resets an [ErrorList] to no errors.func ( *ErrorList) () { * = (*)[0:0] }// [ErrorList] implements the sort Interface.func ( ErrorList) () int { returnlen() }func ( ErrorList) (, int) { [], [] = [], [] }func ( ErrorList) (, int) bool { := &[].Pos := &[].Pos// Note that it is not sufficient to simply compare file offsets because // the offsets do not reflect modified line information (through //line // comments).if .Filename != .Filename {return .Filename < .Filename }if .Line != .Line {return .Line < .Line }if .Column != .Column {return .Column < .Column }return [].Msg < [].Msg}// Sort sorts an [ErrorList]. *[Error] entries are sorted by position,// other errors are sorted by error message, and before any *[Error]// entry.func ( ErrorList) () {sort.Sort()}// RemoveMultiples sorts an [ErrorList] and removes all but the first error per line.func ( *ErrorList) () {sort.Sort()vartoken.Position// initial last.Line is != any legal error line := 0for , := range * {if .Pos.Filename != .Filename || .Pos.Line != .Line { = .Pos (*)[] = ++ } } * = (*)[0:]}// An [ErrorList] implements the error interface.func ( ErrorList) () string {switchlen() {case0:return"no errors"case1:return [0].Error() }returnfmt.Sprintf("%s (and %d more errors)", [0], len()-1)}// Err returns an error equivalent to this error list.// If the list is empty, Err returns nil.func ( ErrorList) () error {iflen() == 0 {returnnil }return}// PrintError is a utility function that prints a list of errors to w,// one error per line, if the err parameter is an [ErrorList]. Otherwise// it prints the err string.func ( io.Writer, error) {if , := .(ErrorList); {for , := range {fmt.Fprintf(, "%s\n", ) } } elseif != nil {fmt.Fprintf(, "%s\n", ) }}
The pages are generated with Goldsv0.7.0-preview. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.