From f3a505b558a049491017d8dd4847fd4badde9f47 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Thu, 16 Jul 2020 19:59:07 +0530 Subject: [PATCH] scanner: add check for `_` in num literals (#5849) --- vlib/builtin/int_test.v | 6 +++--- vlib/v/scanner/scanner.v | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/vlib/builtin/int_test.v b/vlib/builtin/int_test.v index 257dd5d73b..793986ae3b 100644 --- a/vlib/builtin/int_test.v +++ b/vlib/builtin/int_test.v @@ -156,12 +156,12 @@ fn test_num_separator() { assert 0b010_ == 2 // oct - assert 0o_173 == 123 - assert -0o_175 == -125 + assert 0o1_73 == 123 + assert -0o17_5 == -125 assert -0o175_ == -125 // hex - assert 0x_FF == 255 + assert 0xFF == 255 assert 0xFF_ == 255 assert 0xF_F == 255 diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index b729877732..a9e19cb51a 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -324,6 +324,9 @@ fn (mut s Scanner) ident_bin_number() string { mut first_wrong_digit := `\0` start_pos := s.pos 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 { c := s.text[s.pos] 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` start_pos := s.pos 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 { c := s.text[s.pos] 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` start_pos := s.pos 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 { c := s.text[s.pos] if !c.is_oct_digit() && c != num_sep {