123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package scanner
- import (
- "fmt"
- "io"
- "sort"
- )
- import (
- "gopkg.in/gcfg.v1/token"
- )
- type Error struct {
- Pos token.Position
- Msg string
- }
- func (e Error) Error() string {
- if e.Pos.Filename != "" || e.Pos.IsValid() {
-
-
- return e.Pos.String() + ": " + e.Msg
- }
- return e.Msg
- }
- type ErrorList []*Error
- func (p *ErrorList) Add(pos token.Position, msg string) {
- *p = append(*p, &Error{pos, msg})
- }
- func (p *ErrorList) Reset() { *p = (*p)[0:0] }
- func (p ErrorList) Len() int { return len(p) }
- func (p ErrorList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
- func (p ErrorList) Less(i, j int) bool {
- e := &p[i].Pos
- f := &p[j].Pos
- if e.Filename < f.Filename {
- return true
- }
- if e.Filename == f.Filename {
- return e.Offset < f.Offset
- }
- return false
- }
- func (p ErrorList) Sort() {
- sort.Sort(p)
- }
- func (p *ErrorList) RemoveMultiples() {
- sort.Sort(p)
- var last token.Position
- i := 0
- for _, e := range *p {
- if e.Pos.Filename != last.Filename || e.Pos.Line != last.Line {
- last = e.Pos
- (*p)[i] = e
- i++
- }
- }
- (*p) = (*p)[0:i]
- }
- func (p ErrorList) Error() string {
- switch len(p) {
- case 0:
- return "no errors"
- case 1:
- return p[0].Error()
- }
- return fmt.Sprintf("%s (and %d more errors)", p[0], len(p)-1)
- }
- func (p ErrorList) Err() error {
- if len(p) == 0 {
- return nil
- }
- return p
- }
- func PrintError(w io.Writer, err error) {
- if list, ok := err.(ErrorList); ok {
- for _, e := range list {
- fmt.Fprintf(w, "%s\n", e)
- }
- } else if err != nil {
- fmt.Fprintf(w, "%s\n", err)
- }
- }
|