mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
scanner: fix number literal calling method
This commit is contained in:
parent
b62035e3d0
commit
9b9c1cc834
@ -214,6 +214,7 @@ fn (s mut Scanner) ident_oct_number() string {
|
||||
fn (s mut Scanner) ident_dec_number() string {
|
||||
mut has_wrong_digit := false
|
||||
mut first_wrong_digit := `\0`
|
||||
mut call_method := false // true for, e.g., 12.str(), 12.3.str(), 12e-3.str()
|
||||
start_pos := s.pos
|
||||
// scan integer part
|
||||
for s.pos < s.text.len {
|
||||
@ -239,10 +240,15 @@ fn (s mut Scanner) ident_dec_number() string {
|
||||
// scan fractional part
|
||||
if s.pos < s.text.len && s.text[s.pos] == `.` {
|
||||
s.pos++
|
||||
if s.pos < s.text.len {
|
||||
if s.text[s.pos].is_digit() {
|
||||
for s.pos < s.text.len {
|
||||
c := s.text[s.pos]
|
||||
if !c.is_digit() {
|
||||
if !c.is_letter() || c in [`e`, `E`] || s.inside_string {
|
||||
if c == `.` && s.pos + 1 < s.text.len && !s.text[s.pos+1].is_digit() && s.text[s.pos+1] != `)` {
|
||||
call_method = true
|
||||
}
|
||||
break
|
||||
}
|
||||
else if !has_wrong_digit {
|
||||
@ -253,6 +259,12 @@ fn (s mut Scanner) ident_dec_number() string {
|
||||
s.pos++
|
||||
}
|
||||
}
|
||||
else if !(s.text[s.pos] in [`)`, `e`, `E`]) {
|
||||
call_method = true
|
||||
s.pos--
|
||||
}
|
||||
}
|
||||
}
|
||||
// scan exponential part
|
||||
mut has_exponential_part := false
|
||||
if s.expect('e', s.pos) || s.expect('E', s.pos) {
|
||||
@ -265,6 +277,9 @@ fn (s mut Scanner) ident_dec_number() string {
|
||||
c := s.text[s.pos]
|
||||
if !c.is_digit() {
|
||||
if !c.is_letter() || s.inside_string {
|
||||
if c == `.` && s.pos + 1 < s.text.len && !s.text[s.pos+1].is_digit() && s.text[s.pos+1] != `)` {
|
||||
call_method = true
|
||||
}
|
||||
break
|
||||
}
|
||||
else if !has_wrong_digit {
|
||||
@ -280,7 +295,7 @@ fn (s mut Scanner) ident_dec_number() string {
|
||||
has_exponential_part = true
|
||||
}
|
||||
// error check: 1.23.4, 123.e+3.4
|
||||
if s.pos < s.text.len && s.text[s.pos] == `.` {
|
||||
if s.pos < s.text.len && s.text[s.pos] == `.` && !call_method {
|
||||
if has_exponential_part {
|
||||
s.error('exponential part should be integer')
|
||||
}
|
||||
|
28
vlib/compiler/tests/num_lit_call_method.v
Normal file
28
vlib/compiler/tests/num_lit_call_method.v
Normal file
@ -0,0 +1,28 @@
|
||||
fn int_lit_call_method() {
|
||||
x1 := 1234.str()
|
||||
assert x1 == '1234'
|
||||
x2 := -0xffff.str()
|
||||
assert x2 == '-65535'
|
||||
x3 := 0b1001001.str()
|
||||
assert x3 == '73'
|
||||
x4 := 0o653262.str()
|
||||
assert x4 == '218802'
|
||||
x5 := 0.str()
|
||||
assert x5 == '0'
|
||||
}
|
||||
|
||||
fn float_lit_call_method() {
|
||||
x1 := -123.66.str()
|
||||
assert x1 == '-1.2366e+02'
|
||||
x2 := 12.5e-2.str()
|
||||
assert x2 == '1.25e-01'
|
||||
x3 := .789.str()
|
||||
assert x3 == '7.89e-01'
|
||||
x4 := .003e2.str()
|
||||
assert x4 == '3.e-01'
|
||||
x5 := 2.e-3.str()
|
||||
assert x5 == '2.e-03'
|
||||
x6 := 5.0.str()
|
||||
assert x6 == '5.e+00'
|
||||
// x7 := 5..str() Syntax `5.` is allowed, but do not call method on it (`5..str()` is parsed as a range). Use `5.0.str()` instead.
|
||||
}
|
@ -221,6 +221,7 @@ fn (s mut Scanner) ident_oct_number() string {
|
||||
fn (s mut Scanner) ident_dec_number() string {
|
||||
mut has_wrong_digit := false
|
||||
mut first_wrong_digit := `\0`
|
||||
mut call_method := false // true for, e.g., 12.str(), 12.3.str(), 12e-3.str()
|
||||
start_pos := s.pos
|
||||
// scan integer part
|
||||
for s.pos < s.text.len {
|
||||
@ -246,10 +247,15 @@ fn (s mut Scanner) ident_dec_number() string {
|
||||
// scan fractional part
|
||||
if s.pos < s.text.len && s.text[s.pos] == `.` {
|
||||
s.pos++
|
||||
if s.pos < s.text.len {
|
||||
if s.text[s.pos].is_digit() {
|
||||
for s.pos < s.text.len {
|
||||
c := s.text[s.pos]
|
||||
if !c.is_digit() {
|
||||
if !c.is_letter() || c in [`e`, `E`] || s.inside_string {
|
||||
if c == `.` && s.pos + 1 < s.text.len && !s.text[s.pos+1].is_digit() && s.text[s.pos+1] != `)` {
|
||||
call_method = true
|
||||
}
|
||||
break
|
||||
}
|
||||
else if !has_wrong_digit {
|
||||
@ -260,6 +266,12 @@ fn (s mut Scanner) ident_dec_number() string {
|
||||
s.pos++
|
||||
}
|
||||
}
|
||||
else if !(s.text[s.pos] in [`)`, `e`, `E`]) {
|
||||
call_method = true
|
||||
s.pos--
|
||||
}
|
||||
}
|
||||
}
|
||||
// scan exponential part
|
||||
mut has_exponential_part := false
|
||||
if s.expect('e', s.pos) || s.expect('E', s.pos) {
|
||||
@ -272,6 +284,9 @@ fn (s mut Scanner) ident_dec_number() string {
|
||||
c := s.text[s.pos]
|
||||
if !c.is_digit() {
|
||||
if !c.is_letter() || s.inside_string {
|
||||
if c == `.` && s.pos + 1 < s.text.len && !s.text[s.pos+1].is_digit() && s.text[s.pos+1] != `)` {
|
||||
call_method = true
|
||||
}
|
||||
break
|
||||
}
|
||||
else if !has_wrong_digit {
|
||||
@ -287,7 +302,7 @@ fn (s mut Scanner) ident_dec_number() string {
|
||||
has_exponential_part = true
|
||||
}
|
||||
// error check: 1.23.4, 123.e+3.4
|
||||
if s.pos < s.text.len && s.text[s.pos] == `.` {
|
||||
if s.pos < s.text.len && s.text[s.pos] == `.` && !call_method {
|
||||
if has_exponential_part {
|
||||
s.error('exponential part should be integer')
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user