scanner: add check for `_` in num literals (#5849)

pull/5850/head
Swastik Baranwal 2020-07-16 19:59:07 +05:30 committed by GitHub
parent b900577dae
commit f3a505b558
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -156,12 +156,12 @@ fn test_num_separator() {
assert 0b010_ == 2 assert 0b010_ == 2
// oct // oct
assert 0o_173 == 123 assert 0o1_73 == 123
assert -0o_175 == -125 assert -0o17_5 == -125
assert -0o175_ == -125 assert -0o175_ == -125
// hex // hex
assert 0x_FF == 255 assert 0xFF == 255
assert 0xFF_ == 255 assert 0xFF_ == 255
assert 0xF_F == 255 assert 0xF_F == 255

View File

@ -324,6 +324,9 @@ fn (mut s Scanner) ident_bin_number() string {
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'
if s.text[s.pos] == num_sep {
s.error('separator `_` is only valid between digits in a numeric literal')
}
for s.pos < s.text.len { for s.pos < s.text.len {
c := s.text[s.pos] c := s.text[s.pos]
if !c.is_bin_digit() && c != num_sep { if !c.is_bin_digit() && c != num_sep {
@ -355,6 +358,9 @@ fn (mut s Scanner) ident_hex_number() string {
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'
if s.text[s.pos] == num_sep {
s.error('separator `_` is only valid between digits in a numeric literal')
}
for s.pos < s.text.len { for s.pos < s.text.len {
c := s.text[s.pos] c := s.text[s.pos]
if !c.is_hex_digit() && c != num_sep { if !c.is_hex_digit() && c != num_sep {
@ -386,6 +392,9 @@ fn (mut s Scanner) ident_oct_number() string {
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'
if s.text[s.pos] == num_sep {
s.error('separator `_` is only valid between digits in a numeric literal')
}
for s.pos < s.text.len { for s.pos < s.text.len {
c := s.text[s.pos] c := s.text[s.pos]
if !c.is_oct_digit() && c != num_sep { if !c.is_oct_digit() && c != num_sep {