scanner: fix number literal calling method

pull/4166/head
SleepyRoy 2020-03-31 18:47:32 +08:00 committed by GitHub
parent b62035e3d0
commit 9b9c1cc834
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 82 additions and 24 deletions

View File

@ -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,19 +240,30 @@ fn (s mut Scanner) ident_dec_number() string {
// scan fractional part
if s.pos < s.text.len && s.text[s.pos] == `.` {
s.pos++
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 {
break
}
else if !has_wrong_digit {
has_wrong_digit = true
first_wrong_digit = c
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 {
has_wrong_digit = true
first_wrong_digit = c
}
}
s.pos++
}
}
s.pos++
}
else if !(s.text[s.pos] in [`)`, `e`, `E`]) {
call_method = true
s.pos--
}
}
}
// scan exponential part
mut has_exponential_part := false
@ -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')
}

View 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.
}

View File

@ -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,19 +247,30 @@ fn (s mut Scanner) ident_dec_number() string {
// scan fractional part
if s.pos < s.text.len && s.text[s.pos] == `.` {
s.pos++
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 {
break
}
else if !has_wrong_digit {
has_wrong_digit = true
first_wrong_digit = c
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 {
has_wrong_digit = true
first_wrong_digit = c
}
}
s.pos++
}
}
s.pos++
}
else if !(s.text[s.pos] in [`)`, `e`, `E`]) {
call_method = true
s.pos--
}
}
}
// scan exponential part
mut has_exponential_part := false
@ -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')
}