diff --git a/gg/decoder.go b/gg/decoder.go index 6d41d80..e3acbcf 100644 --- a/gg/decoder.go +++ b/gg/decoder.go @@ -53,9 +53,7 @@ var ( Rune('-'), positiveNumber, func(neg Located[rune], posNum Located[string]) Located[string] { - return Located[string]{ - neg.Location, string(neg.Value) + posNum.Value, - } + return Locate(neg.Location, string(neg.Value)+posNum.Value) }, ) @@ -68,7 +66,7 @@ var ( panic(fmt.Errorf("parsing %q as int: %w", str, err)) } - return Located[Value]{str.Location, Number(i)} + return Locate(str.Location, Number(i)) }, ) ) @@ -93,7 +91,7 @@ var ( for _, r := range tail { name = append(name, r.Value) } - return Located[Value]{head.Location, Name(string(name))} + return Locate(head.Location, Name(string(name))) }, ) ) @@ -228,7 +226,7 @@ var graphSym, value = func() ( gs.g = new(Graph) } - return Located[Value]{r.Location, Value{Graph: gs.g}} + return Locate(r.Location, Value{Graph: gs.g}) }, ) diff --git a/gg/grammar/location.go b/gg/grammar/location.go index 1c45171..9e0c64e 100644 --- a/gg/grammar/location.go +++ b/gg/grammar/location.go @@ -18,6 +18,11 @@ type Located[T any] struct { 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]