Compare commits

..

1 Commits

Author SHA1 Message Date
Cursor Agent d15f1d131f fix parser error highlight offsets for non-suffix slices
Co-authored-by: Thomas Pelletier <thomas@pelletier.dev>
2026-04-12 12:26:40 +00:00
4 changed files with 54 additions and 25 deletions
+14 -18
View File
@@ -74,43 +74,39 @@ universal_binaries:
name_template: jsontoml
archives:
- id: jsontoml
formats:
- tar.xz
ids:
format: tar.xz
builds:
- jsontoml
files:
- none*
name_template: "{{ .Binary }}_{{.Version}}_{{ .Os }}_{{ .Arch }}"
- id: tomljson
formats:
- tar.xz
ids:
format: tar.xz
builds:
- tomljson
files:
- none*
name_template: "{{ .Binary }}_{{.Version}}_{{ .Os }}_{{ .Arch }}"
- id: tomll
formats:
- tar.xz
ids:
format: tar.xz
builds:
- tomll
files:
- none*
name_template: "{{ .Binary }}_{{.Version}}_{{ .Os }}_{{ .Arch }}"
dockers_v2:
dockers:
- id: tools
goos: linux
goarch: amd64
ids:
- jsontoml
- tomljson
- tomll
images:
- "ghcr.io/pelletier/go-toml"
tags:
- "latest"
- "{{ .Tag }}"
- "v{{ .Major }}"
platforms:
- linux/amd64
image_templates:
- "ghcr.io/pelletier/go-toml:latest"
- "ghcr.io/pelletier/go-toml:{{ .Tag }}"
- "ghcr.io/pelletier/go-toml:v{{ .Major }}"
skip_push: false
checksum:
name_template: 'sha256sums.txt'
snapshot:
+3 -4
View File
@@ -1,6 +1,5 @@
FROM scratch
ENV PATH "$PATH:/bin"
ARG TARGETPLATFORM
COPY $TARGETPLATFORM/tomll /bin/tomll
COPY $TARGETPLATFORM/tomljson /bin/tomljson
COPY $TARGETPLATFORM/jsontoml /bin/jsontoml
COPY tomll /bin/tomll
COPY tomljson /bin/tomljson
COPY jsontoml /bin/jsontoml
+21
View File
@@ -286,6 +286,27 @@ func TestDecodeError_Position(t *testing.T) {
}
}
func TestDecodeError_InvalidKeyStartAfterComment(t *testing.T) {
doc := "# comment\n= \"value\""
var out map[string]string
err := Unmarshal([]byte(doc), &out)
assert.Error(t, err)
var derr *DecodeError
if !errors.As(err, &derr) {
t.Fatal("error not in expected format")
}
row, col := derr.Position()
assert.Equal(t, 2, row)
assert.Equal(t, 1, col)
assert.Equal(t, "toml: invalid character at start of key: =", derr.Error())
assert.Equal(t, `1| # comment
2| = "value"
| ~ invalid character at start of key: =`, derr.String())
}
func TestStrictErrorUnwrap(t *testing.T) {
fo := bytes.NewBufferString(`
Missing = 1
+16 -3
View File
@@ -3,6 +3,7 @@ package unstable
import (
"bytes"
"fmt"
"reflect"
"unicode"
"github.com/pelletier/go-toml/v2/internal/characters"
@@ -83,10 +84,22 @@ func (p *Parser) rangeOfToken(token, rest []byte) Range {
}
// subsliceOffset returns the byte offset of subslice b within p.data.
// b must be a suffix (tail) of p.data.
// b must share the same backing array as p.data.
func (p *Parser) subsliceOffset(b []byte) int {
// b is a suffix of p.data, so its offset is len(p.data) - len(b)
return len(p.data) - len(b)
if len(b) == 0 {
// Most callers pass suffix slices, so preserve EOF behavior.
return len(p.data)
}
dataPtr := reflect.ValueOf(p.data).Pointer()
subPtr := reflect.ValueOf(b).Pointer()
offset := int(subPtr - dataPtr)
if offset < 0 || offset+len(b) > len(p.data) {
panic("subslice is not within parser input")
}
return offset
}
// Raw returns the slice corresponding to the bytes in the given range.