Skip to content

Commit c545ebb

Browse files
committed
Digit separator doesn't parse with opening brace
Fixes hsutter#862.
1 parent 2d9382d commit c545ebb

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// } on same line
2+
niam: () = { _ = 0'1; }
3+
4+
main: () = {
5+
x := 0'1; // OK
6+
if 1 { x = 1'2; } // same
7+
assert(x == 12);
8+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
3+
//=== Cpp2 type declarations ====================================================
4+
5+
6+
#include "cpp2util.h"
7+
8+
#line 1 "pure2-numeric-literal.cpp2"
9+
10+
11+
//=== Cpp2 type definitions and function declarations ===========================
12+
13+
#line 1 "pure2-numeric-literal.cpp2"
14+
// } on same line
15+
#line 2 "pure2-numeric-literal.cpp2"
16+
auto niam() -> void;
17+
18+
auto main() -> int;
19+
20+
//=== Cpp2 function definitions =================================================
21+
22+
#line 1 "pure2-numeric-literal.cpp2"
23+
24+
#line 2 "pure2-numeric-literal.cpp2"
25+
auto niam() -> void{static_cast<void>(0'1); }
26+
27+
auto main() -> int{
28+
auto x {0'1}; // OK
29+
if (1) {x = 1'2; }// same
30+
cpp2::Default.expects(std::move(x) == 12, "");
31+
}
32+

source/io.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ auto process_cpp2_line(
719719
auto prev2 = ' ';
720720
auto in_string_literal = false;
721721
auto in_char_literal = false;
722+
auto in_numeric_literal = false;
722723

723724
for (auto i = colno_t{0}; i < ssize(line); ++i)
724725
{
@@ -782,10 +783,22 @@ auto process_cpp2_line(
782783
if (prev != '\\' || prev2 == '\\') { in_string_literal = true; }
783784

784785
break;case '\'':
785-
if (prev != '\\' || prev2 == '\\') { in_char_literal = true; }
786+
if (!in_numeric_literal && (prev != '\\' || prev2 == '\\'))
787+
{
788+
in_char_literal = true;
789+
}
786790

787791
break;default: ;
788792
}
793+
if (in_numeric_literal)
794+
{
795+
// Note: this allows _ but that will be caught elsewhere
796+
if (!is_identifier_continue(line[i]) && line[i] != '\'')
797+
in_numeric_literal = false;
798+
}
799+
// either decimal or other numeric literal starting with 0
800+
else if (line[i] >= '0' && line[i] <= '9')
801+
in_numeric_literal = true;
789802
}
790803

791804
prev2 = prev;

0 commit comments

Comments
 (0)