Browse Source

Merge pull request 'lexer: introduce Error{}' (#6)

Reviewed-on: #6
pull/7/head v0.3.3
Alejandro Mery 1 year ago
parent
commit
f7e13e0978
  1. 45
      lexer/error.go

45
lexer/error.go

@ -0,0 +1,45 @@
package lexer
import (
"fmt"
"strings"
)
var (
_ error = (*Error)(nil)
)
// Error represents a generic parsing error
type Error struct {
Filename string
Line int
Column int
Content string
Err error
}
func (err Error) Error() string {
var s []string
switch {
case err.Line > 0 || err.Column > 0:
s = append(s, fmt.Sprintf("%s:%v:%v", err.Filename, err.Line, err.Column))
case err.Filename != "":
s = append(s, err.Filename)
}
if err.Err != nil {
s = append(s, err.Err.Error())
}
if err.Content != "" {
s = append(s, fmt.Sprintf("%q", err.Content))
}
return strings.Join(s, ": ")
}
func (err Error) Unwrap() error {
return err.Err
}
Loading…
Cancel
Save