scanner: fix err pos related to num literal

pull/4983/head
Ruofan XU 2020-05-22 00:43:57 +08:00 committed by GitHub
parent 227f039652
commit b2b0461218
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 10 deletions

View File

@ -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 vlib/v/checker/tests/bin_lit_without_digit_err.v:2:14: error: number part of this binary is not provided
1 | fn main() { 1 | fn main() {
2 | println(0b) 2 | println(0b**)
| ^ | ^
3 | } 3 | }

View File

@ -1,3 +1,3 @@
fn main() { fn main() {
println(0b) println(0b**)
} }

View File

@ -1,5 +1,5 @@
vlib/v/checker/tests/dec_lit_wrong_digit_err.v:2:18: error: this number has unsuitable digit `q` vlib/v/checker/tests/dec_lit_wrong_digit_err.v:2:18: error: this number has unsuitable digit `q`
1 | fn main() { 1 | fn main() {
2 | println(12345q) 2 | println(12345qrst+10)
| ^ | ^
3 | } 3 | }

View File

@ -1,3 +1,3 @@
fn main() { fn main() {
println(12345q) println(12345qrst+10)
} }

View File

@ -1,5 +1,5 @@
vlib/v/checker/tests/hex_lit_wrong_digit_err.v:2:18: error: this hexadecimal number has unsuitable digit `g` vlib/v/checker/tests/hex_lit_wrong_digit_err.v:2:18: error: this hexadecimal number has unsuitable digit `g`
1 | fn main() { 1 | fn main() {
2 | println(0x111g) 2 | println(0x111ghi)
| ^ | ^
3 | } 3 | }

View File

@ -1,3 +1,3 @@
fn main() { fn main() {
println(0x111g) println(0x111ghi)
} }

View File

@ -157,6 +157,7 @@ fn filter_num_sep(txt byteptr, start int, end int) string {
fn (mut s Scanner) ident_bin_number() string { fn (mut s Scanner) ident_bin_number() string {
mut has_wrong_digit := false mut has_wrong_digit := false
mut first_wrong_digit_pos := 0
mut first_wrong_digit := `\0` mut first_wrong_digit := `\0`
start_pos := s.pos start_pos := s.pos
s.pos += 2 // skip '0b' s.pos += 2 // skip '0b'
@ -168,6 +169,7 @@ fn (mut s Scanner) ident_bin_number() string {
} }
else if !has_wrong_digit { else if !has_wrong_digit {
has_wrong_digit = true has_wrong_digit = true
first_wrong_digit_pos = s.pos
first_wrong_digit = c 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') s.error('number part of this binary is not provided')
} }
else if has_wrong_digit { 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()}`') s.error('this binary number has unsuitable digit `${first_wrong_digit.str()}`')
} }
number := filter_num_sep(s.text.str, start_pos, s.pos) 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 { fn (mut s Scanner) ident_hex_number() string {
mut has_wrong_digit := false mut has_wrong_digit := false
mut first_wrong_digit_pos := 0
mut first_wrong_digit := `\0` mut first_wrong_digit := `\0`
start_pos := s.pos start_pos := s.pos
s.pos += 2 // skip '0x' s.pos += 2 // skip '0x'
@ -199,6 +202,7 @@ fn (mut s Scanner) ident_hex_number() string {
} }
else if !has_wrong_digit { else if !has_wrong_digit {
has_wrong_digit = true has_wrong_digit = true
first_wrong_digit_pos = s.pos
first_wrong_digit = c 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') s.error('number part of this hexadecimal is not provided')
} }
else if has_wrong_digit { 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()}`') s.error('this hexadecimal number has unsuitable digit `${first_wrong_digit.str()}`')
} }
number := filter_num_sep(s.text.str, start_pos, s.pos) 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 { fn (mut s Scanner) ident_oct_number() string {
mut has_wrong_digit := false mut has_wrong_digit := false
mut first_wrong_digit_pos := 0
mut first_wrong_digit := `\0` mut first_wrong_digit := `\0`
start_pos := s.pos start_pos := s.pos
s.pos += 2 // skip '0o' s.pos += 2 // skip '0o'
@ -230,6 +235,7 @@ fn (mut s Scanner) ident_oct_number() string {
} }
else if !has_wrong_digit { else if !has_wrong_digit {
has_wrong_digit = true has_wrong_digit = true
first_wrong_digit_pos = s.pos
first_wrong_digit = c 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') s.error('number part of this octal is not provided')
} }
else if has_wrong_digit { 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()}`') s.error('this octal number has unsuitable digit `${first_wrong_digit.str()}`')
} }
number := filter_num_sep(s.text.str, start_pos, s.pos) 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 { fn (mut s Scanner) ident_dec_number() string {
mut has_wrong_digit := false mut has_wrong_digit := false
mut first_wrong_digit_pos := 0
mut first_wrong_digit := `\0` mut first_wrong_digit := `\0`
start_pos := s.pos start_pos := s.pos
// scan integer part // scan integer part
@ -261,6 +268,7 @@ fn (mut s Scanner) ident_dec_number() string {
} }
else if !has_wrong_digit { else if !has_wrong_digit {
has_wrong_digit = true has_wrong_digit = true
first_wrong_digit_pos = s.pos
first_wrong_digit = c first_wrong_digit = c
} }
} }
@ -287,6 +295,7 @@ fn (mut s Scanner) ident_dec_number() string {
} }
else if !has_wrong_digit { else if !has_wrong_digit {
has_wrong_digit = true has_wrong_digit = true
first_wrong_digit_pos = s.pos
first_wrong_digit = c first_wrong_digit = c
} }
} }
@ -333,6 +342,7 @@ fn (mut s Scanner) ident_dec_number() string {
} }
else if !has_wrong_digit { else if !has_wrong_digit {
has_wrong_digit = true has_wrong_digit = true
first_wrong_digit_pos = s.pos
first_wrong_digit = c first_wrong_digit = c
} }
} }
@ -341,7 +351,7 @@ fn (mut s Scanner) ident_dec_number() string {
} }
if has_wrong_digit { if has_wrong_digit {
// error check: 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()}`') s.error('this number has unsuitable digit `${first_wrong_digit.str()}`')
} }
else if s.text[s.pos - 1] in [`e`, `E`] { else if s.text[s.pos - 1] in [`e`, `E`] {