fix Issue 22559 - ImportC: support gnu case ranges (#15068)

This commit is contained in:
Walter Bright 2023-04-04 03:17:09 -07:00 committed by GitHub
parent 5a79fff38f
commit 3fd5c8d081
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View file

@ -508,6 +508,14 @@ final class CParser(AST) : Parser!AST
nextToken();
auto exp = cparseAssignExp();
AST.Expression expHigh;
if (token.value == TOK.dotDotDot)
{
/* Case Ranges https://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html
*/
nextToken();
expHigh = cparseAssignExp();
}
check(TOK.colon);
if (flags & ParseStatementFlags.curlyScope)
@ -533,7 +541,10 @@ final class CParser(AST) : Parser!AST
s = cparseStatement(0);
}
s = new AST.ScopeStatement(loc, s, token.loc);
s = new AST.CaseStatement(loc, exp, s);
if (expHigh)
s = new AST.CaseRangeStatement(loc, exp, expHigh, s);
else
s = new AST.CaseStatement(loc, exp, s);
break;
}

View file

@ -0,0 +1,17 @@
// https://gcc.gnu.org/onlinedocs/gcc/Case-Ranges.html
int alpha(char c)
{
switch (c)
{
case 'A' ... 'Z': return 1;
case 'a' ... 'z': return 1;
default: return 0;
}
}
_Static_assert(alpha('A') == 1, "1");
_Static_assert(alpha('B') == 1, "2");
_Static_assert(alpha('z') == 1, "3");
_Static_assert(alpha('z' + 1) == 0, "3");
_Static_assert(alpha('0') == 0, "4");