Skip to content

Commit 1c791d3

Browse files
committed
[Parse] Factor out 'isEffectsSpecifier()'
1 parent ed82d18 commit 1c791d3

File tree

4 files changed

+24
-30
lines changed

4 files changed

+24
-30
lines changed

include/swift/Parse/Parser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,6 +1357,9 @@ class Parser {
13571357
SourceLoc &asyncLoc, SourceLoc &throwsLoc,
13581358
bool *rethrows);
13591359

1360+
/// Returns 'true' if \p T is considered effects specifier.
1361+
bool isEffectsSpecifier(const Token &T);
1362+
13601363
//===--------------------------------------------------------------------===//
13611364
// Pattern Parsing
13621365

lib/Parse/ParseExpr.cpp

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,23 +2341,10 @@ ParserStatus Parser::parseClosureSignatureIfPresent(
23412341
inLoc = SourceLoc();
23422342

23432343
// Consume 'async', 'throws', and 'rethrows', but in any order.
2344-
auto consumeAsyncThrows = [&] {
2345-
while (true) {
2346-
if (shouldParseExperimentalConcurrency() &&
2347-
Tok.isContextualKeyword("async")) {
2348-
consumeToken();
2349-
continue;
2350-
}
2351-
if (consumeIf(tok::kw_throws) || consumeIf(tok::kw_rethrows))
2352-
continue;
2353-
2354-
if (Tok.is(tok::code_complete) && !Tok.isAtStartOfLine()) {
2355-
consumeToken();
2356-
continue;
2357-
}
2358-
2359-
break;
2360-
}
2344+
auto consumeEffectsSpecifiers = [&] {
2345+
while (isEffectsSpecifier(Tok) ||
2346+
(Tok.is(tok::code_complete) && !Tok.isAtStartOfLine()))
2347+
consumeToken();
23612348
};
23622349

23632350
// If we have a leading token that may be part of the closure signature, do a
@@ -2381,7 +2368,7 @@ ParserStatus Parser::parseClosureSignatureIfPresent(
23812368

23822369
// Consume the ')', if it's there.
23832370
if (consumeIf(tok::r_paren)) {
2384-
consumeAsyncThrows();
2371+
consumeEffectsSpecifiers();
23852372

23862373
// Parse the func-signature-result, if present.
23872374
if (consumeIf(tok::arrow)) {
@@ -2403,7 +2390,7 @@ ParserStatus Parser::parseClosureSignatureIfPresent(
24032390
return makeParserSuccess();
24042391
}
24052392

2406-
consumeAsyncThrows();
2393+
consumeEffectsSpecifiers();
24072394

24082395
// Parse the func-signature-result, if present.
24092396
if (consumeIf(tok::arrow)) {

lib/Parse/ParsePattern.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,21 @@ Parser::parseFunctionSignature(Identifier SimpleName,
821821
return Status;
822822
}
823823

824+
bool Parser::isEffectsSpecifier(const Token &T) {
825+
// NOTE: If this returns 'true', that token must be handled in
826+
// 'parseEffectsSpecifiers()'.
827+
828+
if (shouldParseExperimentalConcurrency() &&
829+
T.isContextualKeyword("async"))
830+
return true;
831+
832+
if (T.isAny(tok::kw_throws, tok::kw_rethrows) ||
833+
(T.isAny(tok::kw_throw, tok::kw_try) && !T.isAtStartOfLine()))
834+
return true;
835+
836+
return false;
837+
}
838+
824839
ParserStatus Parser::parseEffectsSpecifiers(SourceLoc existingArrowLoc,
825840
SourceLoc &asyncLoc,
826841
SourceLoc &throwsLoc,

lib/Parse/ParseType.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1639,17 +1639,6 @@ bool Parser::isAtFunctionTypeArrow() {
16391639
if (Tok.is(tok::arrow))
16401640
return true;
16411641

1642-
auto isEffectsSpecifier = [this](const Token &T) -> bool {
1643-
if (shouldParseExperimentalConcurrency() &&
1644-
T.isContextualKeyword("async"))
1645-
return true;
1646-
if (T.isAny(tok::kw_throws, tok::kw_rethrows))
1647-
return true;
1648-
if (T.isAny(tok::kw_throw, tok::kw_try) && !T.isAtStartOfLine())
1649-
return true;
1650-
return false;
1651-
};
1652-
16531642
if (isEffectsSpecifier(Tok)) {
16541643
if (peekToken().is(tok::arrow))
16551644
return true;

0 commit comments

Comments
 (0)