Skip to content

Commit afe4a9c

Browse files
committed
Fix row and column calculation for multiline strings (#72)
1 parent 70c0c94 commit afe4a9c

4 files changed

Lines changed: 30 additions & 0 deletions

File tree

lexer/lexer.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ func Tokenize(source string) ([]Token, error) {
269269
}
270270

271271
if !appended {
272+
if c0 == "\n" {
273+
row++
274+
ogColumn = startIndex
275+
}
272276
str += c0
273277
i++
274278
}

tests/strings.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tests
22

33
import (
4+
"regexp"
45
"strings"
56
"testing"
67

@@ -136,6 +137,23 @@ func testMultilineStringSuccess(t *testing.T, transpilerFunc transpilerFunc) {
136137
})
137138
}
138139

140+
func testMultilineStringErrorPositionSuccess(t *testing.T, transpilerFunc transpilerFunc) {
141+
transpilerFunc(t, `
142+
func test() {
143+
var x = 3
144+
s := `+"`"+`
145+
hello
146+
multiline
147+
world
148+
`+"`"+`
149+
150+
print(len(x))
151+
}
152+
test()`, func(output string, err error) {
153+
require.Equal(t, "row 10, column 15", regexp.MustCompile(`row \d+, column \d+`).FindString(err.Error()))
154+
})
155+
}
156+
139157
func testItoaSuccess(t *testing.T, transpilerFunc transpilerFunc) {
140158
transpilerFunc(t, `
141159
print("Hello World " + itoa(24))

tests/strings_linux_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func TestMultilineStringSuccess(t *testing.T) {
4848
testMultilineStringSuccess(t, transpileBash)
4949
}
5050

51+
func TestMultilineStringErrorPositionSuccess(t *testing.T) {
52+
testMultilineStringErrorPositionSuccess(t, transpileBash)
53+
}
54+
5155
func TestItoaSuccess(t *testing.T) {
5256
testItoaSuccess(t, transpileBash)
5357
}

tests/strings_windows_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func TestMultilineStringSuccess(t *testing.T) {
4848
testMultilineStringSuccess(t, transpileBatch)
4949
}
5050

51+
func TestMultilineStringErrorPositionSuccess(t *testing.T) {
52+
testMultilineStringErrorPositionSuccess(t, transpileBatch)
53+
}
54+
5155
func TestItoaSuccess(t *testing.T) {
5256
testItoaSuccess(t, transpileBatch)
5357
}

0 commit comments

Comments
 (0)