Skip to content

Commit 6ac702e

Browse files
committed
multi-case: change - to ..
1 parent 436d13d commit 6ac702e

15 files changed

+31
-24
lines changed

parser/c2_parser_switch.c2

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn Expr* Parser.parseCaseCondition(Parser* p, identifier_expr_list.List* list) {
6767
bool multi_case = false;
6868
if (p.tok.kind == Kind.Identifier) {
6969
Token t2 = p.tokenizer.lookahead(1);
70-
if (t2.kind == Kind.Comma || t2.kind == Kind.Minus) multi_case = true;
70+
if (t2.kind == Kind.Comma || t2.kind == Kind.Range) multi_case = true;
7171
}
7272

7373
if (!multi_case) {
@@ -101,7 +101,7 @@ fn Expr* Parser.parseCaseCondition(Parser* p, identifier_expr_list.List* list) {
101101
p.error("expected case condition");
102102
return nil;
103103
}
104-
if (p.tok.kind == Kind.Comma || p.tok.kind == Kind.Minus) {
104+
if (p.tok.kind == Kind.Comma || p.tok.kind == Kind.Range) {
105105
p.error("multi-condition case statements are only allowed with unprefixed enum constants");
106106
}
107107
return e;
@@ -113,7 +113,7 @@ fn Expr* Parser.parseCaseCondition(Parser* p, identifier_expr_list.List* list) {
113113
IdentifierExpr* id1 = p.parseIdentifier();
114114
if (!list.add(id1)) p.error("too many conditions");
115115

116-
if (p.tok.kind == Kind.Minus) { // A - C
116+
if (p.tok.kind == Kind.Range) { // A .. C
117117
p.consumeToken();
118118
p.expectIdentifier();
119119
IdentifierExpr* id2 = p.parseIdentifier();

parser/c2_tokenizer.c2

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -611,9 +611,14 @@ fn void Tokenizer.lex_internal(Tokenizer* t, Token* result) {
611611
case DOT:
612612
result.loc = t.loc_start + cast<SrcLoc>(t.cur - t.input_start);
613613
t.cur++;
614-
if (t.cur[0] == '.' && t.cur[1] == '.') {
615-
t.cur += 2;
616-
result.kind = Kind.Ellipsis;
614+
if (t.cur[0] == '.') {
615+
if (t.cur[1] == '.') {
616+
t.cur += 2;
617+
result.kind = Kind.Ellipsis;
618+
} else {
619+
t.cur++;
620+
result.kind = Kind.Range;
621+
}
617622
} else {
618623
result.kind = Kind.Dot;
619624
}
@@ -907,7 +912,7 @@ fn void Tokenizer.lex_number(Tokenizer* t, Token* result) {
907912
return;
908913
}
909914

910-
if (t.cur[1] == '.') { // floating point
915+
if (t.cur[1] == '.' && t.cur[2] != '.') { // floating point, handle 'case 1..2:'
911916
t.cur++; // skip until .
912917
t.lex_floating_point(result, t.cur);
913918
return;
@@ -928,7 +933,7 @@ fn void Tokenizer.lex_number(Tokenizer* t, Token* result) {
928933
start = t.cur;
929934
while (isdigit(*t.cur)) t.cur++;
930935

931-
if (t.cur[0] == '.') {
936+
if (t.cur[0] == '.' && t.cur[1] != '.') { // handle 'case 1..2:'
932937
t.lex_floating_point(result, start);
933938
return;
934939
}

parser/token.c2

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public type Kind enum u8 {
5050
CaretEqual, // ^=
5151
Question, // ?
5252
Dot, // .
53+
Range, // ..
5354
Ellipsis, // ...
5455
Comma, // ,
5556
Plus, // +
@@ -177,6 +178,7 @@ const char*[] token_names = {
177178
"^=",
178179
"?",
179180
".",
181+
"..",
180182
"...",
181183
",",
182184
"+",

test/c_generator/stmts/multi_condition_switch.c2t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn void test1(Foo f) {
1515
break;
1616
case B, C:
1717
break;
18-
case D-F, G:
18+
case D..F, G:
1919
break;
2020
}
2121
}

test/c_generator/stmts/multi_condition_switch_incr.c2t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Foo += E;
1515

1616
fn void test1(Foo f) {
1717
switch (f) {
18-
case A-C:
18+
case A..C:
1919
break;
2020
case D,E:
2121
break;

test/stmt/multi_case/default_with_all_cases.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn void test1(Foo f) {
1111
break;
1212
case B, C:
1313
break;
14-
case D-G:
14+
case D..G:
1515
break;
1616
default: // @error{default label in switch which covers all enumeration values}
1717
break;

test/stmt/multi_case/duplicate_case.c2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn void test1(Foo f) {
1111
break;
1212
case B, D: // @note{previous case is here}
1313
break;
14-
case C-F, G: // @error{duplicate case value 'D'}
14+
case C..F, G: // @error{duplicate case value 'D'}
1515
break;
1616
}
1717
}
@@ -20,7 +20,7 @@ fn void test2(Foo f) {
2020
switch (f) {
2121
case A:
2222
break;
23-
case C-F, G: // @note{previous case is here}
23+
case C..F, G: // @note{previous case is here}
2424
break;
2525
case B, D: // @error{duplicate case value 'D'}
2626
break;

test/stmt/multi_case/incr_enum.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ State += E;
1111

1212
fn void test1(State s) {
1313
switch (s) {
14-
case A-B:
14+
case A..B:
1515
break;
1616
case C,D,E:
1717
break;

test/stmt/multi_case/no_enum_range.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn void test1(u32 a) {
1010
case Foo.G: fallthrough;
1111
case Foo.A:
1212
break;
13-
case Foo.B - Foo.F: // @error{multi-condition case statements are only allowed with unprefixed enum constants}
13+
case Foo.B .. Foo.F: // @error{multi-condition case statements are only allowed with unprefixed enum constants}
1414
break;
1515
}
1616
}

test/stmt/multi_case/ok.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn void test1(Foo f) {
1111
break;
1212
case B, C:
1313
break;
14-
case D-F, G:
14+
case D..F, G:
1515
break;
1616
}
1717
}

test/stmt/multi_case/overlap.c2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ fn void test1(Foo f) {
99
switch (f) {
1010
case A:
1111
break;
12-
case B-E, // @note{previous case is here}
13-
D-G: // @error{duplicate case value 'D'}
12+
case B..E, // @note{previous case is here}
13+
D..G: // @error{duplicate case value 'D'}
1414
break;
1515
}
1616
}
@@ -19,9 +19,9 @@ fn void test2(Foo f) {
1919
switch (f) {
2020
case A:
2121
break;
22-
case B-E: // @note{previous case is here}
22+
case B..E: // @note{previous case is here}
2323
break;
24-
case D-G: // @error{duplicate case value 'D'}
24+
case D..G: // @error{duplicate case value 'D'}
2525
break;
2626
}
2727
}

test/stmt/multi_case/range_invalid_order.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type Foo enum u8 {
77

88
fn void test1(Foo f) {
99
switch (f) {
10-
case G-A: // @error{enum constant 'A' does not come after 'G'}
10+
case G..A: // @error{enum constant 'A' does not come after 'G'}
1111
break;
1212
}
1313
}

test/stmt/multi_case/range_same_constant.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type Foo enum u8 {
77

88
fn void test1(Foo f) {
99
switch (f) {
10-
case B- // @note{previous case is here}
10+
case B.. // @note{previous case is here}
1111
B: // @error{duplicate case value 'B'}
1212
break;
1313
}

test/stmt/multi_case/unknown_constant.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn void test1(Foo f) {
1111
break;
1212
case B, C:
1313
break;
14-
case D-F, G, H: // @error{enum 'test.Foo' has no constant 'H'}
14+
case D..F, G, H: // @error{enum 'test.Foo' has no constant 'H'}
1515
break;
1616
}
1717
}

test/stmt/switch/multi_number_case.c2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fn void test1() {
66
switch (a) {
77
case 1:
88
break;
9-
case 2-10: // @error{multi-condition case statements are only allowed with unprefixed enum constants}
9+
case 2..10: // @error{multi-condition case statements are only allowed with unprefixed enum constants}
1010
break;
1111
}
1212
}

0 commit comments

Comments
 (0)