Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

Commit

Permalink
[Parse] Factor out 'isEffectsSpecifier()'
Browse files Browse the repository at this point in the history
  • Loading branch information
rintaro committed Dec 14, 2020
1 parent ed82d18 commit 1c791d3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 30 deletions.
3 changes: 3 additions & 0 deletions include/swift/Parse/Parser.h
Expand Up @@ -1357,6 +1357,9 @@ class Parser {
SourceLoc &asyncLoc, SourceLoc &throwsLoc,
bool *rethrows);

/// Returns 'true' if \p T is considered effects specifier.
bool isEffectsSpecifier(const Token &T);

//===--------------------------------------------------------------------===//
// Pattern Parsing

Expand Down
25 changes: 6 additions & 19 deletions lib/Parse/ParseExpr.cpp
Expand Up @@ -2341,23 +2341,10 @@ ParserStatus Parser::parseClosureSignatureIfPresent(
inLoc = SourceLoc();

// Consume 'async', 'throws', and 'rethrows', but in any order.
auto consumeAsyncThrows = [&] {
while (true) {
if (shouldParseExperimentalConcurrency() &&
Tok.isContextualKeyword("async")) {
consumeToken();
continue;
}
if (consumeIf(tok::kw_throws) || consumeIf(tok::kw_rethrows))
continue;

if (Tok.is(tok::code_complete) && !Tok.isAtStartOfLine()) {
consumeToken();
continue;
}

break;
}
auto consumeEffectsSpecifiers = [&] {
while (isEffectsSpecifier(Tok) ||
(Tok.is(tok::code_complete) && !Tok.isAtStartOfLine()))
consumeToken();
};

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

// Consume the ')', if it's there.
if (consumeIf(tok::r_paren)) {
consumeAsyncThrows();
consumeEffectsSpecifiers();

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

consumeAsyncThrows();
consumeEffectsSpecifiers();

// Parse the func-signature-result, if present.
if (consumeIf(tok::arrow)) {
Expand Down
15 changes: 15 additions & 0 deletions lib/Parse/ParsePattern.cpp
Expand Up @@ -821,6 +821,21 @@ Parser::parseFunctionSignature(Identifier SimpleName,
return Status;
}

bool Parser::isEffectsSpecifier(const Token &T) {
// NOTE: If this returns 'true', that token must be handled in
// 'parseEffectsSpecifiers()'.

if (shouldParseExperimentalConcurrency() &&
T.isContextualKeyword("async"))
return true;

if (T.isAny(tok::kw_throws, tok::kw_rethrows) ||
(T.isAny(tok::kw_throw, tok::kw_try) && !T.isAtStartOfLine()))
return true;

return false;
}

ParserStatus Parser::parseEffectsSpecifiers(SourceLoc existingArrowLoc,
SourceLoc &asyncLoc,
SourceLoc &throwsLoc,
Expand Down
11 changes: 0 additions & 11 deletions lib/Parse/ParseType.cpp
Expand Up @@ -1639,17 +1639,6 @@ bool Parser::isAtFunctionTypeArrow() {
if (Tok.is(tok::arrow))
return true;

auto isEffectsSpecifier = [this](const Token &T) -> bool {
if (shouldParseExperimentalConcurrency() &&
T.isContextualKeyword("async"))
return true;
if (T.isAny(tok::kw_throws, tok::kw_rethrows))
return true;
if (T.isAny(tok::kw_throw, tok::kw_try) && !T.isAtStartOfLine())
return true;
return false;
};

if (isEffectsSpecifier(Tok)) {
if (peekToken().is(tok::arrow))
return true;
Expand Down

0 comments on commit 1c791d3

Please sign in to comment.