2020-04-19 22:54:58 +00:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import "github.com/pkg/errors"
|
|
|
|
|
|
|
|
type ErrorLocation struct {
|
|
|
|
Ranges []Range
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrorLocation) Unwrap() error {
|
|
|
|
return e.error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Range struct {
|
|
|
|
Start Position
|
|
|
|
End Position
|
|
|
|
}
|
|
|
|
|
|
|
|
type Position struct {
|
|
|
|
Line int
|
|
|
|
Character int
|
|
|
|
}
|
|
|
|
|
|
|
|
func withLocation(err error, startLine, endLine int) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-22 01:28:29 +00:00
|
|
|
var el *ErrorLocation
|
2020-04-20 03:54:38 +00:00
|
|
|
if errors.As(err, &el) {
|
|
|
|
return err
|
|
|
|
}
|
2020-04-19 22:54:58 +00:00
|
|
|
return errors.WithStack(&ErrorLocation{
|
|
|
|
error: err,
|
|
|
|
Ranges: toRanges(startLine, endLine),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func toRanges(start, end int) (r []Range) {
|
|
|
|
if end <= start {
|
|
|
|
end = start
|
|
|
|
}
|
|
|
|
for i := start; i <= end; i++ {
|
|
|
|
r = append(r, Range{Start: Position{Line: i}, End: Position{Line: i}})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|