diff --git a/token.go b/token.go index 24d4c6e..42e7e00 100644 --- a/token.go +++ b/token.go @@ -236,7 +236,8 @@ func (t *tokenizer) back() { if t.source[t.index] == '\n' { t.line-- - // TODO: reset col correctly + lines := strings.Split(string(t.source), "\n") + t.col = len(lines[t.line]) } else { t.col-- } diff --git a/token_test.go b/token_test.go new file mode 100644 index 0000000..5daf0ce --- /dev/null +++ b/token_test.go @@ -0,0 +1,16 @@ +package main + +import "testing" + +func TestBack(t *testing.T) { + token := tokenizer{ + source: []rune("hello\nworld"), + line: 1, + col: 0, + index: 6, + } + token.back() + if token.line != 0 && token.col != 5 { + t.Errorf("Expected line 0 and column 5, received line %d and column %d", token.line, token.col) + } +}