Skip to content

Commit b6ee2c2

Browse files
authored
String token end location off-by-one error (google#139)
* String token end location off-by-one error
1 parent d753072 commit b6ee2c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+57
-61
lines changed

main_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,19 +205,19 @@ func TestOneLineError(t *testing.T) {
205205
// TODO(sbarzowski) checking if the whitespace is right is quite unpleasant, what can we do about it?
206206
var minimalErrorTests = []errorFormattingTest{
207207
{"error", `error "x"`, "RUNTIME ERROR: x\n" +
208-
" error:1:1-9 $\n" + // TODO(sbarzowski) if seems we have off-by-one in location
208+
" error:1:1-10 $\n" + // TODO(sbarzowski) if seems we have off-by-one in location
209209
" During evaluation \n" +
210210
""},
211211
{"error_in_func", `local x(n) = if n == 0 then error "x" else x(n - 1); x(3)`, "RUNTIME ERROR: x\n" +
212-
" error_in_func:1:29-37 function <x>\n" +
212+
" error_in_func:1:29-38 function <x>\n" +
213213
" error_in_func:1:44-52 function <x>\n" +
214214
" error_in_func:1:44-52 function <x>\n" +
215215
" error_in_func:1:44-52 function <x>\n" +
216216
" error_in_func:1:54-58 $\n" +
217217
" During evaluation \n" +
218218
""},
219219
{"error_in_error", `error (error "x")`, "RUNTIME ERROR: x\n" +
220-
" error_in_error:1:8-16 $\n" +
220+
" error_in_error:1:8-17 $\n" +
221221
" During evaluation \n" +
222222
""},
223223
}

parser/lexer.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -711,15 +711,13 @@ func Lex(fn string, input string) (tokens, error) {
711711
// String literals
712712
case '"':
713713
stringStartLoc := l.prevLocation()
714-
l.resetTokenStart() // Don't include the quotes in the token data
715714
for r = l.next(); ; r = l.next() {
716715
if r == lexEOF {
717716
return nil, l.makeStaticErrorPoint("Unterminated String", stringStartLoc)
718717
}
719718
if r == '"' {
720-
l.backup()
721-
l.emitToken(tokenStringDouble)
722-
_ = l.next()
719+
// Don't include the quotes in the token data
720+
l.emitFullToken(tokenStringDouble, l.input[l.tokenStart+1:l.pos.byteNo-1], "", "")
723721
l.resetTokenStart()
724722
break
725723
}
@@ -729,15 +727,13 @@ func Lex(fn string, input string) (tokens, error) {
729727
}
730728
case '\'':
731729
stringStartLoc := l.prevLocation()
732-
l.resetTokenStart() // Don't include the quotes in the token data
733730
for r = l.next(); ; r = l.next() {
734731
if r == lexEOF {
735732
return nil, l.makeStaticErrorPoint("Unterminated String", stringStartLoc)
736733
}
737734
if r == '\'' {
738-
l.backup()
739-
l.emitToken(tokenStringSingle)
740-
r = l.next()
735+
// Don't include the quotes in the token data
736+
l.emitFullToken(tokenStringSingle, l.input[l.tokenStart+1:l.pos.byteNo-1], "", "")
741737
l.resetTokenStart()
742738
break
743739
}

testdata/arrcomp_if6.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: x
22
-------------------------------------------------
3-
testdata/arrcomp_if6:1:20-28 $
3+
testdata/arrcomp_if6:1:20-29 $
44

55
[x for x in [1] if error "x"]
66

testdata/bad_index_string.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Unexpected type string, expected number
22
-------------------------------------------------
3-
testdata/bad_index_string:1:2-13 $
3+
testdata/bad_index_string:1:1-13 $
44

55
"xxx"["xxx"]
66

testdata/binaryNot2.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Unexpected type string, expected number
22
-------------------------------------------------
3-
testdata/binaryNot2:1:1-6 $
3+
testdata/binaryNot2:1:1-7 $
44

55
~"xxx"
66

testdata/bitwise_and4.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
RUNTIME ERROR: x
22
-------------------------------------------------
3-
testdata/bitwise_and4:1:5-13 $
3+
testdata/bitwise_and4:1:5-14 $
44

55
1 & error "x"
66

77
-------------------------------------------------
8-
testdata/bitwise_and4:1:1-13 $
8+
testdata/bitwise_and4:1:1-14 $
99

1010
1 & error "x"
1111

testdata/bitwise_or10.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Unexpected type string, expected number
22
-------------------------------------------------
3-
testdata/bitwise_or10:1:2-11 $
3+
testdata/bitwise_or10:1:1-11 $
44

55
"xxx" | 42
66

testdata/bitwise_xor7.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
RUNTIME ERROR: x
22
-------------------------------------------------
3-
testdata/bitwise_xor7:1:5-13 $
3+
testdata/bitwise_xor7:1:5-14 $
44

55
1 ^ error "x"
66

77
-------------------------------------------------
8-
testdata/bitwise_xor7:1:1-13 $
8+
testdata/bitwise_xor7:1:1-14 $
99

1010
1 ^ error "x"
1111

testdata/double_thunk.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: xxx
22
-------------------------------------------------
3-
testdata/double_thunk:1:21-31 thunk <y> from <thunk <x> from <$>>
3+
testdata/double_thunk:1:21-32 thunk <y> from <thunk <x> from <$>>
44

55
local x = local y = error "xxx"; y; x
66

testdata/error.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: 42
22
-------------------------------------------------
3-
testdata/error:1:1-10 $
3+
testdata/error:1:1-11 $
44

55
error "42"
66

testdata/error_from_array.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: xxx
22
-------------------------------------------------
3-
testdata/error_from_array:1:2-12 thunk from <$>
3+
testdata/error_from_array:1:2-13 thunk from <$>
44

55
[error "xxx"][0]
66

testdata/extvar_error.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: xxx
22
-------------------------------------------------
3-
<extvar:errorVar>:1:1-11 $
3+
<extvar:errorVar>:1:1-12 $
44

55
error 'xxx'
66

testdata/function_plus_string.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Couldn't manifest function in JSON output.
22
-------------------------------------------------
3-
testdata/function_plus_string:1:1-23 $
3+
testdata/function_plus_string:1:1-24 $
44

55
(function() 42) + "xxx"
66

testdata/import_computed.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
testdata/import_computed:1:9-16 Computed imports are not allowed
1+
testdata/import_computed:1:8-17 Computed imports are not allowed
22

33
import "a" + "b"
44

testdata/import_failure_directory.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: read testdata: is a directory
22
-------------------------------------------------
3-
testdata/import_failure_directory:3:1-10 $
3+
testdata/import_failure_directory:3:1-11 $
44

55
import '.'
66

testdata/importstr_computed.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
testdata/importstr_computed:1:12-19 Computed imports are not allowed
1+
testdata/importstr_computed:1:11-20 Computed imports are not allowed
22

33
importstr "a" + "b"
44

testdata/insuper4.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
testdata/insuper4:1:2-13 Can't use super outside of an object.
1+
testdata/insuper4:1:1-13 Can't use super outside of an object.
22

33
"x" in super
44

testdata/lazy_operator2.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
RUNTIME ERROR: should happen
22
-------------------------------------------------
3-
testdata/lazy_operator2:1:9-29 $
3+
testdata/lazy_operator2:1:9-30 $
44

55
true && error "should happen"
66

77
-------------------------------------------------
8-
testdata/lazy_operator2:1:1-29 $
8+
testdata/lazy_operator2:1:1-30 $
99

1010
true && error "should happen"
1111

testdata/native4.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: xxx
22
-------------------------------------------------
3-
testdata/native4:1:28-38 thunk from <$>
3+
testdata/native4:1:28-39 thunk from <$>
44

55
std.native("jsonToString")(error "xxx")
66

testdata/nonexistent_import.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Couldn't open import "no chance a file with this name exists": No match locally or in the Jsonnet library paths.
22
-------------------------------------------------
3-
testdata/nonexistent_import:1:1-50 $
3+
testdata/nonexistent_import:1:1-51 $
44

55
importstr 'no chance a file with this name exists'
66

testdata/nonexistent_import_crazy.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Couldn't open import "ąęółńśćźż \" ' \n\n\t\t": No match locally or in the Jsonnet library paths.
22
-------------------------------------------------
3-
testdata/nonexistent_import_crazy:1:1-45 $
3+
testdata/nonexistent_import_crazy:1:1-46 $
44

55
importstr "ąęółńśćźż \" \' \n\n\t\t"
66

testdata/number_divided_by_string.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Unexpected type string, expected number
22
-------------------------------------------------
3-
testdata/number_divided_by_string:1:1-10 $
3+
testdata/number_divided_by_string:1:1-11 $
44

55
42 / "xxx"
66

testdata/number_times_string.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Unexpected type string, expected number
22
-------------------------------------------------
3-
testdata/number_times_string:1:1-10 $
3+
testdata/number_times_string:1:1-11 $
44

55
42 * "xxx"
66

testdata/object_comp_err_elem.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: xxx
22
-------------------------------------------------
3-
testdata/object_comp_err_elem:1:11-21 object <anonymous>
3+
testdata/object_comp_err_elem:1:11-22 object <anonymous>
44

55
{ ["x"]: error "xxx" for x in [1] }
66

testdata/object_comp_err_index.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: xxx
22
-------------------------------------------------
3-
testdata/object_comp_err_index:1:4-14 $
3+
testdata/object_comp_err_index:1:4-15 $
44

55
{ [error "xxx"]: 42 for x in [1] }
66

testdata/object_invariant13.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: x
22
-------------------------------------------------
3-
testdata/object_invariant13:1:10-18 object <anonymous>
3+
testdata/object_invariant13:1:10-19 object <anonymous>
44

55
{ assert error "x" }
66

testdata/object_invariant7.golden

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ RUNTIME ERROR: Attempt to use super when there is no super class.
55
{ x: 5, assert super.x == 5 }
66

77
-------------------------------------------------
8-
<std>:969:29-30 thunk from <thunk <ta> from <function <anonymous>>>
8+
<std>:979:29-30 thunk from <thunk <ta> from <function <anonymous>>>
99

1010
local ta = std.type(a);
1111

1212
-------------------------------------------------
1313
<builtin> builtin function <type>
1414

1515
-------------------------------------------------
16-
<std>:971:33-35 thunk from <function <anonymous>>
16+
<std>:981:33-35 thunk from <function <anonymous>>
1717

1818
if !std.primitiveEquals(ta, tb) then
1919

2020
-------------------------------------------------
2121
<builtin> builtin function <primitiveEquals>
2222

2323
-------------------------------------------------
24-
<std>:971:12-40 function <anonymous>
24+
<std>:981:12-40 function <anonymous>
2525

2626
if !std.primitiveEquals(ta, tb) then
2727

testdata/or4.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
RUNTIME ERROR: xxx
22
-------------------------------------------------
3-
testdata/or4:1:10-20 $
3+
testdata/or4:1:10-21 $
44

55
false || error "xxx"
66

77
-------------------------------------------------
8-
testdata/or4:1:1-20 $
8+
testdata/or4:1:1-21 $
99

1010
false || error "xxx"
1111

testdata/or5.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Unexpected type string, expected boolean
22
-------------------------------------------------
3-
testdata/or5:1:2-14 $
3+
testdata/or5:1:1-14 $
44

55
"xxx" || true
66

testdata/or6.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Unexpected type string, expected boolean
22
-------------------------------------------------
3-
testdata/or6:1:1-14 $
3+
testdata/or6:1:1-15 $
44

55
false || "xxx"
66

testdata/percent_bad.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Operator % cannot be used on types number and string.
22
-------------------------------------------------
3-
<std>:150:13-99 function <anonymous>
3+
<std>:150:13-100 function <anonymous>
44

55
error "Operator % cannot be used on types " + std.type(a) + " and " + std.type(b) + ".",
66

testdata/percent_bad3.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Operator % cannot be used on types function and number.
22
-------------------------------------------------
3-
<std>:150:13-99 function <anonymous>
3+
<std>:150:13-100 function <anonymous>
44

55
error "Operator % cannot be used on types " + std.type(a) + " and " + std.type(b) + ".",
66

testdata/recursive_thunk.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: xxx
22
-------------------------------------------------
3-
testdata/recursive_thunk:1:35-45 function <bar>
3+
testdata/recursive_thunk:1:35-46 function <bar>
44

55
local bar(th, x) = if x == 0 then error "xxx" else th;
66

testdata/std.filter2.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: x
22
-------------------------------------------------
3-
testdata/std.filter2:1:12-20 thunk from <$>
3+
testdata/std.filter2:1:12-21 thunk from <$>
44

55
std.filter(error "x", [])
66

testdata/std.primitiveEquals10.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: x
22
-------------------------------------------------
3-
testdata/std.primitiveEquals10:1:21-29 thunk from <$>
3+
testdata/std.primitiveEquals10:1:21-30 thunk from <$>
44

55
std.primitiveEquals(error "x", 42)
66

testdata/std.primitiveEquals9.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: x
22
-------------------------------------------------
3-
testdata/std.primitiveEquals9:1:25-33 thunk from <$>
3+
testdata/std.primitiveEquals9:1:25-34 thunk from <$>
44

55
std.primitiveEquals(42, error "x")
66

testdata/std.toString5.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: x
22
-------------------------------------------------
3-
testdata/std.toString5:1:14-22 thunk from <$>
3+
testdata/std.toString5:1:14-23 thunk from <$>
44

55
std.toString(error "x")
66

testdata/string_divided_by_number.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Unexpected type string, expected number
22
-------------------------------------------------
3-
testdata/string_divided_by_number:1:2-11 $
3+
testdata/string_divided_by_number:1:1-11 $
44

55
"xxx" / 42
66

testdata/string_index_negative.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Index -1 out of bounds, not within [0, 4)
22
-------------------------------------------------
3-
testdata/string_index_negative:1:2-11 $
3+
testdata/string_index_negative:1:1-11 $
44

55
"abcd"[-1]
66

testdata/string_index_out_of_bounds.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Index 4 out of bounds, not within [0, 4)
22
-------------------------------------------------
3-
testdata/string_index_out_of_bounds:1:2-10 $
3+
testdata/string_index_out_of_bounds:1:1-10 $
44

55
"abcd"[4]
66

0 commit comments

Comments
 (0)