Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add numeric separator _ #379

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions parsers/c/c_lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,18 @@ let integer_constant =
| hexadecimal_constant
| binary_constant (* since C2x *)

(* Separate parsing for CN numeric constants *)

let cn_decimal_constant = nonzero_digit ( digit | '_' )*
let cn_octal_constant = '0' (octal_digit | '_')*
let cn_hexadecimal_constant = hexadecimal_prefix (hexadecimal_digit | '_')+
let cn_binary_constant = binary_prefix (binary_digit | '_')+

let cn_integer_constant =
cn_decimal_constant
| cn_octal_constant
| cn_hexadecimal_constant
| cn_binary_constant (* since C2x *)

(* STD §6.4.3#1 *)
let hex_quad = hexadecimal_digit hexadecimal_digit
Expand Down Expand Up @@ -440,9 +452,9 @@ and initial flags = parse
| (integer_constant as str) long_long_suffix unsigned_suffix
{ CONSTANT (Cabs.CabsInteger_const (str, Some Cabs.CabsSuffix_ULL)) }
(* For CN. Copying and adjusting Kayvan's code from above. *)
| (integer_constant as str) 'u' (cn_integer_width as n)
| (cn_integer_constant as str) 'u' (cn_integer_width as n)
{ CN_CONSTANT (str, `U, int_of_string n) }
| (integer_constant as str) 'i' (cn_integer_width as n)
| (cn_integer_constant as str) 'i' (cn_integer_width as n)
{ CN_CONSTANT (str, `I, int_of_string n) }
(* /For CN. *)
| (integer_constant as str)
Expand Down
3 changes: 2 additions & 1 deletion parsers/c/c_parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -1840,7 +1840,8 @@ prim_expr:
let sign = match sign with
| `U -> Cerb_frontend.Cn.CN_unsigned
| `I -> Cerb_frontend.Cn.CN_signed in
let v = Z.of_string str in
let str_clean = String.concat "" (String.split_on_char '_' str) in
let v = Z.of_string str_clean in
Cerb_frontend.Cn.(CNExpr (Cerb_location.point $startpos, CNExpr_const (CNConst_bits ((sign,n),v))))
}
| ident= cn_variable
Expand Down
10 changes: 10 additions & 0 deletions tests/cn/numeric_separator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
void numeric_separator()
/*@ requires
10i32 == 10_i32; // Decimal
0x10i32 == 0x1_0i32; // Hex
010i32 == 01_0i32; // Octal
0b10i32 == 0B1_0i32; // Binary
@*/
{
;
}
Loading