Skip to content

Commit

Permalink
Merge pull request #248 from wader/parse-unexpected-multibyte-token-o…
Browse files Browse the repository at this point in the history
…ffset

set correct offset for multibyte tokens
  • Loading branch information
itchyny committed Apr 2, 2024
2 parents 8874f53 + 1b5ce7f commit 976241f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
31 changes: 31 additions & 0 deletions compiler_test.go
Expand Up @@ -2,6 +2,7 @@ package gojq_test

import (
"context"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -275,6 +276,36 @@ func TestCodeCompile_OptimizeJumps(t *testing.T) {
}
}

func TestParseErrorTokenOffset(t *testing.T) {
testCases := []struct {
src string
offset int
}{
{src: "^", offset: 1},
{src: " ^", offset: 2},
{src: " ^ ", offset: 2},
{src: "👍", offset: 4},
{src: " 👍", offset: 5},
{src: " 👍 ", offset: 5},
{src: "test👍", offset: 8},
}
for _, tc := range testCases {
t.Run(tc.src, func(t *testing.T) {
_, err := gojq.Parse(tc.src)
if err == nil {
t.Fatal("expected: error")
}
var pe *gojq.ParseError
if !errors.As(err, &pe) {
t.Fatalf("expected: *gojq.ParseError, got %v", err)
}
if pe.Offset != tc.offset {
t.Fatalf("expected: %v, got %v", tc.offset, pe.Offset)
}
})
}
}

func TestCodeRun_Race(t *testing.T) {
query, err := gojq.Parse("range(10)")
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion lexer.go
Expand Up @@ -235,7 +235,8 @@ func (l *lexer) Lex(lval *yySymType) (tokenType int) {
default:
if ch >= utf8.RuneSelf {
r, size := utf8.DecodeRuneInString(l.source[l.offset-1:])
l.offset += size
// -1 to adjust for first byte consumed by next()
l.offset += size - 1
l.token = string(r)
}
}
Expand Down

0 comments on commit 976241f

Please sign in to comment.