fmt: keep _ separator in number literals (#7495)

pull/7504/head
zakuro 2020-12-23 21:48:43 +09:00 committed by GitHub
parent bf8cf8b817
commit a6e6c48c36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 10 deletions

View File

@ -10,9 +10,9 @@ fn sum_types(a []Expr) {
}
fn main() {
x := 0xdeadbeef
u := 9978654321
o := 0o664
x := 0xdead_beef
u := 9_978_654_321
o := 0o66_4
eprintln(' hex constant in decimal: $x')
eprintln(' u constant in decimal: $u')
eprintln('octal constant in decimal: $o')

View File

@ -39,8 +39,7 @@ pub mut:
line_ends []int // the positions of source lines ends (i.e. \n signs)
nr_lines int // total number of lines in the source file that were scanned
is_vh bool // Keep newlines
is_fmt bool // Used only for skipping ${} in strings, since we need literal
// string values when generating formatted code.
is_fmt bool // Used for v fmt.
comments_mode CommentsMode
is_inside_toplvl_statement bool // *only* used in comments_mode: .toplevel_comments, toggled by parser
all_tokens []token.Token // *only* used in comments_mode: .toplevel_comments, contains all tokens
@ -174,8 +173,12 @@ fn (mut s Scanner) ident_name() string {
return name
}
fn filter_num_sep(txt byteptr, start int, end int) string {
fn (s Scanner) num_lit(start int, end int) string {
if s.is_fmt {
return s.text[start..end]
}
unsafe {
txt := s.text.str
mut b := malloc(end - start + 1) // add a byte for the endstring 0
mut i1 := 0
for i := start; i < end; i++ {
@ -223,7 +226,7 @@ fn (mut s Scanner) ident_bin_number() string {
s.pos = first_wrong_digit_pos // adjust error position
s.error('this binary number has unsuitable digit `$first_wrong_digit.str()`')
}
number := filter_num_sep(s.text.str, start_pos, s.pos)
number := s.num_lit(start_pos, s.pos)
s.pos--
return number
}
@ -265,7 +268,7 @@ fn (mut s Scanner) ident_hex_number() string {
s.pos = first_wrong_digit_pos // adjust error position
s.error('this hexadecimal number has unsuitable digit `$first_wrong_digit.str()`')
}
number := filter_num_sep(s.text.str, start_pos, s.pos)
number := s.num_lit(start_pos, s.pos)
s.pos--
return number
}
@ -304,7 +307,7 @@ fn (mut s Scanner) ident_oct_number() string {
s.pos = first_wrong_digit_pos // adjust error position
s.error('this octal number has unsuitable digit `$first_wrong_digit.str()`')
}
number := filter_num_sep(s.text.str, start_pos, s.pos)
number := s.num_lit(start_pos, s.pos)
s.pos--
return number
}
@ -416,7 +419,7 @@ fn (mut s Scanner) ident_dec_number() string {
s.error('too many decimal points in number')
}
}
number := filter_num_sep(s.text.str, start_pos, s.pos)
number := s.num_lit(start_pos, s.pos)
s.pos--
return number
}