From d1c0ea701236bf201bfc2a97705afcb6444ea103 Mon Sep 17 00:00:00 2001 From: Shannon Rothe Date: Wed, 10 Aug 2022 12:03:34 +1000 Subject: [PATCH 1/2] fix(tokenizer): set correct column when moving back --- token.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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-- } From c7c662152085111dd00a50b06233fbefcd0d558d Mon Sep 17 00:00:00 2001 From: Shannon Rothe Date: Wed, 10 Aug 2022 12:28:15 +1000 Subject: [PATCH 2/2] add simple test --- token_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 token_test.go 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) + } +}