2023-10-27 14:14:37 +00:00
|
|
|
package grammar
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
// Location indicates a position in a stream of runes identified by column
|
|
|
|
// within newline-separated rows.
|
|
|
|
type Location struct {
|
|
|
|
Row, Col int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l Location) errf(str string, args ...any) LocatedError {
|
|
|
|
return LocatedError{l, fmt.Errorf(str, args...)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Located wraps a value so that it has a Location attached to it.
|
|
|
|
type Located[T any] struct {
|
|
|
|
Location
|
|
|
|
Value T
|
|
|
|
}
|
|
|
|
|
2023-10-27 16:57:44 +00:00
|
|
|
// Locate returns a Located instance combining the given values.
|
|
|
|
func Locate[T any](l Location, v T) Located[T] {
|
|
|
|
return Located[T]{l, v}
|
|
|
|
}
|
|
|
|
|
2023-10-27 14:14:37 +00:00
|
|
|
// LocatedError is an error related to a specific point within a stream of
|
|
|
|
// runes.
|
|
|
|
type LocatedError Located[error]
|
|
|
|
|
|
|
|
func (e LocatedError) Error() string {
|
|
|
|
return fmt.Sprintf("%d:%d: %v", e.Row, e.Col, e.Value)
|
|
|
|
}
|