ginger/gg/grammar/location.go
2023-10-27 18:57:44 +02:00

33 lines
772 B
Go

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
}
// Locate returns a Located instance combining the given values.
func Locate[T any](l Location, v T) Located[T] {
return Located[T]{l, v}
}
// 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)
}