Skip to content
Open
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
22 changes: 22 additions & 0 deletions lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3291,4 +3291,26 @@ func TestTokenOffset(t *testing.T) {
t.Fatalf("unexpected offset. got %d", tokens[4].Position.Offset)
}
})
t.Run("single_comment", func(t *testing.T) {
// "# x\na: 1\n": '#'=1,' '=2,'x'=3,'\n'=4,'a'=5
tokens := lexer.Tokenize("# x\na: 1\n")
tk := tokens[1] // "a" (tokens[0] is the comment)
if tk.Value != "a" {
t.Fatalf("unexpected value. got %q", tk.Value)
}
if tk.Position.Offset != 5 {
t.Fatalf("unexpected offset. got %d, want 5", tk.Position.Offset)
}
})
t.Run("multiple_comments", func(t *testing.T) {
// "# x\n# y\n# z\na: 1\n": "# x\n# y\n# z\n" = 13 chars, 'a' at offset 13
tokens := lexer.Tokenize("# x\n# y\n# z\na: 1\n")
tk := tokens[3] // "a" (tokens[0,1,2] are comments)
if tk.Value != "a" {
t.Fatalf("unexpected value. got %q", tk.Value)
}
if tk.Position.Offset != 13 {
t.Fatalf("unexpected offset. got %d, want 13", tk.Position.Offset)
}
})
}
2 changes: 2 additions & 0 deletions scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ func (s *Scanner) scanComment(ctx *Context) bool {
progress := len([]rune(value))
ctx.addToken(token.Comment(value, string(ctx.obuf), s.pos()))
s.progressColumn(ctx, progress)
s.offset++ // account for the '#' character that was skipped via s.progress
s.progressLine(ctx)
ctx.clear()
return true
Expand All @@ -769,6 +770,7 @@ func (s *Scanner) scanComment(ctx *Context) bool {
ctx.addToken(token.Comment(value, string(ctx.obuf), s.pos()))
progress := len([]rune(value))
s.progressColumn(ctx, progress)
s.offset++ // account for the '#' character that was skipped via s.progress
s.progressLine(ctx)
ctx.clear()
return true
Expand Down
Loading