154d80392f
Instead of requiring downstream consumers to re-derive the byte offset from pointer arithmetic on the Highlight slice, compute and cache the offset inside the parser at error-capture time via setErrOffset(). This is safer because: - The parser is the one place where the backing-array guarantee is known to hold (Highlight is always a subslice of the parse buffer) - Downstream consumers (wrapDecodeError) can use the cached offset directly, avoiding the need for pointer comparison - Errors created outside the parser (strict.go) set the offset from existing Raw ranges, which are already correct by construction Add ParserError.SetOffset/Offset methods for setting and retrieving the cached offset. Update wrapDecodeError to prefer the cached offset when available, falling back to subsliceOffset for backward compatibility. Co-authored-by: Thomas Pelletier <thomas@pelletier.dev>
119 lines
2.0 KiB
Go
119 lines
2.0 KiB
Go
package toml
|
|
|
|
import (
|
|
"github.com/pelletier/go-toml/v2/internal/tracker"
|
|
"github.com/pelletier/go-toml/v2/unstable"
|
|
)
|
|
|
|
type strict struct {
|
|
Enabled bool
|
|
|
|
// Tracks the current key being processed.
|
|
key tracker.KeyTracker
|
|
|
|
missing []unstable.ParserError
|
|
|
|
// Reference to the document for computing key ranges.
|
|
doc []byte
|
|
}
|
|
|
|
func (s *strict) EnterTable(node *unstable.Node) {
|
|
if !s.Enabled {
|
|
return
|
|
}
|
|
|
|
s.key.UpdateTable(node)
|
|
}
|
|
|
|
func (s *strict) EnterArrayTable(node *unstable.Node) {
|
|
if !s.Enabled {
|
|
return
|
|
}
|
|
|
|
s.key.UpdateArrayTable(node)
|
|
}
|
|
|
|
func (s *strict) EnterKeyValue(node *unstable.Node) {
|
|
if !s.Enabled {
|
|
return
|
|
}
|
|
|
|
s.key.Push(node)
|
|
}
|
|
|
|
func (s *strict) ExitKeyValue(node *unstable.Node) {
|
|
if !s.Enabled {
|
|
return
|
|
}
|
|
|
|
s.key.Pop(node)
|
|
}
|
|
|
|
func (s *strict) MissingTable(node *unstable.Node) {
|
|
if !s.Enabled {
|
|
return
|
|
}
|
|
|
|
highlight, offset := s.keyLocation(node)
|
|
pe := unstable.ParserError{
|
|
Highlight: highlight,
|
|
Message: "missing table",
|
|
Key: s.key.Key(),
|
|
}
|
|
pe.SetOffset(offset)
|
|
s.missing = append(s.missing, pe)
|
|
}
|
|
|
|
func (s *strict) MissingField(node *unstable.Node) {
|
|
if !s.Enabled {
|
|
return
|
|
}
|
|
|
|
highlight, offset := s.keyLocation(node)
|
|
pe := unstable.ParserError{
|
|
Highlight: highlight,
|
|
Message: "missing field",
|
|
Key: s.key.Key(),
|
|
}
|
|
pe.SetOffset(offset)
|
|
s.missing = append(s.missing, pe)
|
|
}
|
|
|
|
func (s *strict) Error(doc []byte) error {
|
|
if !s.Enabled || len(s.missing) == 0 {
|
|
return nil
|
|
}
|
|
|
|
err := &StrictMissingError{
|
|
Errors: make([]DecodeError, 0, len(s.missing)),
|
|
}
|
|
|
|
for _, derr := range s.missing {
|
|
derr := derr
|
|
err.Errors = append(err.Errors, *wrapDecodeError(doc, &derr))
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func (s *strict) keyLocation(node *unstable.Node) ([]byte, int) {
|
|
k := node.Key()
|
|
|
|
hasOne := k.Next()
|
|
if !hasOne {
|
|
panic("should not be called with empty key")
|
|
}
|
|
|
|
firstRaw := k.Node().Raw
|
|
lastRaw := firstRaw
|
|
|
|
for k.Next() {
|
|
lastRaw = k.Node().Raw
|
|
}
|
|
|
|
start := firstRaw.Offset
|
|
end := lastRaw.Offset + lastRaw.Length
|
|
|
|
return s.doc[start:end], int(start)
|
|
}
|