Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix token offset #524

Merged
merged 1 commit into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3186,3 +3186,32 @@ a: |invalid`,
})
}
}

func TestTokenOffset(t *testing.T) {
t.Run("crlf", func(t *testing.T) {
content := "project:\r\n version: 1.2.3\r\n"
tokens := lexer.Tokenize(content)
if len(tokens) != 5 {
t.Fatalf("invalid token num. got %d", len(tokens))
}
if tokens[4].Value != "1.2.3" {
t.Fatalf("unexpected value. got %q", tokens[4].Value)
}
if tokens[4].Position.Offset != 22 {
t.Fatalf("unexpected offset. got %d", tokens[4].Position.Offset)
}
})
t.Run("lf", func(t *testing.T) {
content := "project:\n version: 1.2.3\n"
tokens := lexer.Tokenize(content)
if len(tokens) != 5 {
t.Fatalf("invalid token num. got %d", len(tokens))
}
if tokens[4].Value != "1.2.3" {
t.Fatalf("unexpected value. got %q", tokens[4].Value)
}
if tokens[4].Position.Offset != 21 {
t.Fatalf("unexpected offset. got %d", tokens[4].Position.Offset)
}
})
}
5 changes: 4 additions & 1 deletion scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,10 @@ func (s *Scanner) scanDocument(ctx *Context, c rune) error {

func (s *Scanner) scanNewLine(ctx *Context, c rune) {
if len(ctx.buf) > 0 && s.savedPos == nil {
bufLen := len(ctx.bufferedSrc())
s.savedPos = s.pos()
s.savedPos.Column -= len(ctx.bufferedSrc())
s.savedPos.Column -= bufLen
s.savedPos.Offset -= bufLen
}

// if the following case, origin buffer has unnecessary two spaces.
Expand All @@ -631,6 +633,7 @@ func (s *Scanner) scanNewLine(ctx *Context, c rune) {
if c == '\r' && ctx.nextChar() == '\n' {
ctx.addOriginBuf('\r')
s.progress(ctx, 1)
s.offset++
c = '\n'
}

Expand Down
4 changes: 2 additions & 2 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,8 @@ func (t *Token) Clone() *Token {
// Dump outputs token information to stdout for debugging.
func (t *Token) Dump() {
fmt.Printf(
"[TYPE]:%q [CHARTYPE]:%q [INDICATOR]:%q [VALUE]:%q [ORG]:%q [POS(line:column:level)]: %d:%d:%d\n",
t.Type, t.CharacterType, t.Indicator, t.Value, t.Origin, t.Position.Line, t.Position.Column, t.Position.IndentLevel,
"[TYPE]:%q [CHARTYPE]:%q [INDICATOR]:%q [VALUE]:%q [ORG]:%q [POS(line:column:level:offset)]: %d:%d:%d:%d\n",
t.Type, t.CharacterType, t.Indicator, t.Value, t.Origin, t.Position.Line, t.Position.Column, t.Position.IndentLevel, t.Position.Offset,
)
}

Expand Down
Loading