From b2b046121813c66fecd0a4e4bf7855c405cdd6a0 Mon Sep 17 00:00:00 2001 From: Ruofan XU <47302112+SleepyRoy@users.noreply.github.com> Date: Fri, 22 May 2020 00:43:57 +0800 Subject: [PATCH] scanner: fix err pos related to num literal --- .../tests/bin_lit_without_digit_err.out | 2 +- .../checker/tests/bin_lit_without_digit_err.vv | 2 +- .../checker/tests/dec_lit_wrong_digit_err.out | 2 +- .../v/checker/tests/dec_lit_wrong_digit_err.vv | 2 +- .../checker/tests/hex_lit_wrong_digit_err.out | 2 +- .../v/checker/tests/hex_lit_wrong_digit_err.vv | 2 +- vlib/v/scanner/scanner.v | 18 ++++++++++++++---- 7 files changed, 20 insertions(+), 10 deletions(-) diff --git a/vlib/v/checker/tests/bin_lit_without_digit_err.out b/vlib/v/checker/tests/bin_lit_without_digit_err.out index c6c54f29b3..9a91ef6b8c 100644 --- a/vlib/v/checker/tests/bin_lit_without_digit_err.out +++ b/vlib/v/checker/tests/bin_lit_without_digit_err.out @@ -1,5 +1,5 @@ vlib/v/checker/tests/bin_lit_without_digit_err.v:2:14: error: number part of this binary is not provided 1 | fn main() { - 2 | println(0b) + 2 | println(0b**) | ^ 3 | } diff --git a/vlib/v/checker/tests/bin_lit_without_digit_err.vv b/vlib/v/checker/tests/bin_lit_without_digit_err.vv index 34f1b34438..a85579df0c 100644 --- a/vlib/v/checker/tests/bin_lit_without_digit_err.vv +++ b/vlib/v/checker/tests/bin_lit_without_digit_err.vv @@ -1,3 +1,3 @@ fn main() { - println(0b) + println(0b**) } diff --git a/vlib/v/checker/tests/dec_lit_wrong_digit_err.out b/vlib/v/checker/tests/dec_lit_wrong_digit_err.out index 448166d1bb..7f89665eee 100644 --- a/vlib/v/checker/tests/dec_lit_wrong_digit_err.out +++ b/vlib/v/checker/tests/dec_lit_wrong_digit_err.out @@ -1,5 +1,5 @@ vlib/v/checker/tests/dec_lit_wrong_digit_err.v:2:18: error: this number has unsuitable digit `q` 1 | fn main() { - 2 | println(12345q) + 2 | println(12345qrst+10) | ^ 3 | } diff --git a/vlib/v/checker/tests/dec_lit_wrong_digit_err.vv b/vlib/v/checker/tests/dec_lit_wrong_digit_err.vv index 02edf7a22f..c8c958352c 100644 --- a/vlib/v/checker/tests/dec_lit_wrong_digit_err.vv +++ b/vlib/v/checker/tests/dec_lit_wrong_digit_err.vv @@ -1,3 +1,3 @@ fn main() { - println(12345q) + println(12345qrst+10) } diff --git a/vlib/v/checker/tests/hex_lit_wrong_digit_err.out b/vlib/v/checker/tests/hex_lit_wrong_digit_err.out index fec442e13a..68982e68d5 100644 --- a/vlib/v/checker/tests/hex_lit_wrong_digit_err.out +++ b/vlib/v/checker/tests/hex_lit_wrong_digit_err.out @@ -1,5 +1,5 @@ vlib/v/checker/tests/hex_lit_wrong_digit_err.v:2:18: error: this hexadecimal number has unsuitable digit `g` 1 | fn main() { - 2 | println(0x111g) + 2 | println(0x111ghi) | ^ 3 | } diff --git a/vlib/v/checker/tests/hex_lit_wrong_digit_err.vv b/vlib/v/checker/tests/hex_lit_wrong_digit_err.vv index 11a84b14db..1b7b07d868 100644 --- a/vlib/v/checker/tests/hex_lit_wrong_digit_err.vv +++ b/vlib/v/checker/tests/hex_lit_wrong_digit_err.vv @@ -1,3 +1,3 @@ fn main() { - println(0x111g) + println(0x111ghi) } diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 7adce523b4..72a368512e 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -157,6 +157,7 @@ fn filter_num_sep(txt byteptr, start int, end int) string { fn (mut s Scanner) ident_bin_number() string { mut has_wrong_digit := false + mut first_wrong_digit_pos := 0 mut first_wrong_digit := `\0` start_pos := s.pos s.pos += 2 // skip '0b' @@ -168,6 +169,7 @@ fn (mut s Scanner) ident_bin_number() string { } else if !has_wrong_digit { has_wrong_digit = true + first_wrong_digit_pos = s.pos first_wrong_digit = c } } @@ -178,7 +180,7 @@ fn (mut s Scanner) ident_bin_number() string { s.error('number part of this binary is not provided') } else if has_wrong_digit { - s.pos-- // adjust error position + s.pos = first_wrong_digit_pos // adjust error position s.error('this binary number has unsuitable digit `${first_wrong_digit.str()}`') } number := filter_num_sep(s.text.str, start_pos, s.pos) @@ -188,6 +190,7 @@ fn (mut s Scanner) ident_bin_number() string { fn (mut s Scanner) ident_hex_number() string { mut has_wrong_digit := false + mut first_wrong_digit_pos := 0 mut first_wrong_digit := `\0` start_pos := s.pos s.pos += 2 // skip '0x' @@ -199,6 +202,7 @@ fn (mut s Scanner) ident_hex_number() string { } else if !has_wrong_digit { has_wrong_digit = true + first_wrong_digit_pos = s.pos first_wrong_digit = c } } @@ -209,7 +213,7 @@ fn (mut s Scanner) ident_hex_number() string { s.error('number part of this hexadecimal is not provided') } else if has_wrong_digit { - s.pos-- // adjust error position + s.pos = first_wrong_digit_pos // adjust error position s.error('this hexadecimal number has unsuitable digit `${first_wrong_digit.str()}`') } number := filter_num_sep(s.text.str, start_pos, s.pos) @@ -219,6 +223,7 @@ fn (mut s Scanner) ident_hex_number() string { fn (mut s Scanner) ident_oct_number() string { mut has_wrong_digit := false + mut first_wrong_digit_pos := 0 mut first_wrong_digit := `\0` start_pos := s.pos s.pos += 2 // skip '0o' @@ -230,6 +235,7 @@ fn (mut s Scanner) ident_oct_number() string { } else if !has_wrong_digit { has_wrong_digit = true + first_wrong_digit_pos = s.pos first_wrong_digit = c } } @@ -240,7 +246,7 @@ fn (mut s Scanner) ident_oct_number() string { s.error('number part of this octal is not provided') } else if has_wrong_digit { - s.pos-- // adjust error position + s.pos = first_wrong_digit_pos // adjust error position s.error('this octal number has unsuitable digit `${first_wrong_digit.str()}`') } number := filter_num_sep(s.text.str, start_pos, s.pos) @@ -250,6 +256,7 @@ fn (mut s Scanner) ident_oct_number() string { fn (mut s Scanner) ident_dec_number() string { mut has_wrong_digit := false + mut first_wrong_digit_pos := 0 mut first_wrong_digit := `\0` start_pos := s.pos // scan integer part @@ -261,6 +268,7 @@ fn (mut s Scanner) ident_dec_number() string { } else if !has_wrong_digit { has_wrong_digit = true + first_wrong_digit_pos = s.pos first_wrong_digit = c } } @@ -287,6 +295,7 @@ fn (mut s Scanner) ident_dec_number() string { } else if !has_wrong_digit { has_wrong_digit = true + first_wrong_digit_pos = s.pos first_wrong_digit = c } } @@ -333,6 +342,7 @@ fn (mut s Scanner) ident_dec_number() string { } else if !has_wrong_digit { has_wrong_digit = true + first_wrong_digit_pos = s.pos first_wrong_digit = c } } @@ -341,7 +351,7 @@ fn (mut s Scanner) ident_dec_number() string { } if has_wrong_digit { // error check: wrong digit - s.pos-- // adjust error position + s.pos = first_wrong_digit_pos // adjust error position s.error('this number has unsuitable digit `${first_wrong_digit.str()}`') } else if s.text[s.pos - 1] in [`e`, `E`] {