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

Commit 1315ce8

Browse files
authored
Merge pull request swiftlang#34624 from pi1024e/parse-cleanup
Streamline ParseType Logic
2 parents a0cb311 + 1406932 commit 1315ce8

File tree

1 file changed

+64
-67
lines changed

1 file changed

+64
-67
lines changed

lib/Parse/ParseType.cpp

Lines changed: 64 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,8 @@ ParserResult<TypeRepr> Parser::parseSILBoxType(GenericParamList *generics,
275275
return makeParserError();
276276
Fields.push_back({VarOrLetLoc, Mutable, fieldTy.get()});
277277

278-
if (consumeIf(tok::comma))
279-
continue;
280-
281-
break;
278+
if (!consumeIf(tok::comma))
279+
break;
282280
}
283281
}
284282

@@ -302,9 +300,8 @@ ParserResult<TypeRepr> Parser::parseSILBoxType(GenericParamList *generics,
302300
if (!argTy.getPtrOrNull())
303301
return makeParserError();
304302
Args.push_back(argTy.get());
305-
if (consumeIf(tok::comma))
306-
continue;
307-
break;
303+
if (!consumeIf(tok::comma))
304+
break;
308305
}
309306
if (!startsWithGreater(Tok)) {
310307
diagnose(Tok, diag::sil_box_expected_r_angle);
@@ -506,9 +503,8 @@ ParserResult<TypeRepr> Parser::parseType(Diag<> MessageID,
506503
if (!argTy.getPtrOrNull())
507504
return false;
508505
SubsTypesVec.push_back(argTy.get());
509-
if (consumeIf(tok::comma))
510-
continue;
511-
break;
506+
if (!consumeIf(tok::comma))
507+
break;
512508
}
513509
if (!startsWithGreater(Tok)) {
514510
diagnose(Tok, diag::sil_function_subst_expected_r_angle);
@@ -597,27 +593,31 @@ ParserResult<TypeRepr> Parser::parseDeclResultType(Diag<> MessageID) {
597593

598594
auto result = parseType(MessageID);
599595

600-
if (!result.isParseErrorOrHasCompletion() && Tok.is(tok::r_square)) {
601-
auto diag = diagnose(Tok, diag::extra_rbracket);
602-
diag.fixItInsert(result.get()->getStartLoc(), getTokenText(tok::l_square));
603-
consumeToken();
604-
return makeParserErrorResult(new (Context)
605-
ErrorTypeRepr(getTypeErrorLoc()));
606-
} else if (!result.isParseErrorOrHasCompletion() && Tok.is(tok::colon)) {
607-
auto colonTok = consumeToken();
608-
auto secondType = parseType(diag::expected_dictionary_value_type);
609-
610-
auto diag = diagnose(colonTok, diag::extra_colon);
611-
diag.fixItInsert(result.get()->getStartLoc(), getTokenText(tok::l_square));
612-
if (!secondType.isParseErrorOrHasCompletion()) {
613-
if (Tok.is(tok::r_square)) {
614-
consumeToken();
615-
} else {
616-
diag.fixItInsertAfter(secondType.get()->getEndLoc(), getTokenText(tok::r_square));
596+
if (!result.isParseErrorOrHasCompletion()) {
597+
if (Tok.is(tok::r_square)) {
598+
auto diag = diagnose(Tok, diag::extra_rbracket);
599+
diag.fixItInsert(result.get()->getStartLoc(), getTokenText(tok::l_square));
600+
consumeToken();
601+
return makeParserErrorResult(new (Context)
602+
ErrorTypeRepr(getTypeErrorLoc()));
603+
}
604+
605+
if (Tok.is(tok::colon)) {
606+
auto colonTok = consumeToken();
607+
auto secondType = parseType(diag::expected_dictionary_value_type);
608+
609+
auto diag = diagnose(colonTok, diag::extra_colon);
610+
diag.fixItInsert(result.get()->getStartLoc(), getTokenText(tok::l_square));
611+
if (!secondType.isParseErrorOrHasCompletion()) {
612+
if (Tok.is(tok::r_square)) {
613+
consumeToken();
614+
} else {
615+
diag.fixItInsertAfter(secondType.get()->getEndLoc(), getTokenText(tok::r_square));
616+
}
617617
}
618+
return makeParserErrorResult(new (Context)
619+
ErrorTypeRepr(getTypeErrorLoc()));
618620
}
619-
return makeParserErrorResult(new (Context)
620-
ErrorTypeRepr(getTypeErrorLoc()));
621621
}
622622
return result;
623623
}
@@ -747,28 +747,26 @@ Parser::parseTypeIdentifier(bool isParsingQualifiedDeclBaseType) {
747747
Status.setHasCodeCompletionAndIsError();
748748
break;
749749
}
750-
if (!peekToken().isContextualKeyword("Type")
751-
&& !peekToken().isContextualKeyword("Protocol")) {
752-
// If parsing a qualified declaration name, break before parsing the
753-
// period before the final declaration name component.
754-
if (isParsingQualifiedDeclBaseType) {
755-
// If qualified name base type cannot be parsed from the current
756-
// point (i.e. the next type identifier is not followed by a '.'),
757-
// then the next identifier is the final declaration name component.
758-
BacktrackingScope backtrack(*this);
759-
consumeStartingCharacterOfCurrentToken(tok::period);
760-
if (!canParseBaseTypeForQualifiedDeclName())
761-
break;
762-
}
763-
// Consume the period.
764-
consumeToken();
765-
continue;
750+
if (peekToken().isContextualKeyword("Type") ||
751+
peekToken().isContextualKeyword("Protocol"))
752+
break;
753+
// If parsing a qualified declaration name, break before parsing the
754+
// period before the final declaration name component.
755+
if (isParsingQualifiedDeclBaseType) {
756+
// If qualified name base type cannot be parsed from the current
757+
// point (i.e. the next type identifier is not followed by a '.'),
758+
// then the next identifier is the final declaration name component.
759+
BacktrackingScope backtrack(*this);
760+
consumeStartingCharacterOfCurrentToken(tok::period);
761+
if (!canParseBaseTypeForQualifiedDeclName())
762+
break;
766763
}
767-
} else if (Tok.is(tok::code_complete)) {
768-
if (!Tok.isAtStartOfLine())
769-
Status.setHasCodeCompletionAndIsError();
770-
break;
764+
// Consume the period.
765+
consumeToken();
766+
continue;
771767
}
768+
if (Tok.is(tok::code_complete) && !Tok.isAtStartOfLine())
769+
Status.setHasCodeCompletionAndIsError();
772770
break;
773771
}
774772

@@ -1322,13 +1320,14 @@ bool Parser::isOptionalToken(const Token &T) const {
13221320
// A postfix '?' by itself is obviously optional.
13231321
if (T.is(tok::question_postfix))
13241322
return true;
1325-
13261323
// A postfix or bound infix operator token that begins with '?' can be
1327-
// optional too. We'll munch off the '?', so long as it is left-bound with
1328-
// the type (i.e., parsed as a postfix or unspaced binary operator).
1329-
if ((T.is(tok::oper_postfix) || T.is(tok::oper_binary_unspaced)) &&
1330-
T.getText().startswith("?"))
1331-
return true;
1324+
// optional too.
1325+
if (T.is(tok::oper_postfix) || T.is(tok::oper_binary_unspaced)) {
1326+
// We'll munch off the '?', so long as it is left-bound with
1327+
// the type (i.e., parsed as a postfix or unspaced binary operator).
1328+
return T.getText().startswith("?");
1329+
}
1330+
13321331
return false;
13331332
}
13341333

@@ -1338,12 +1337,13 @@ bool Parser::isImplicitlyUnwrappedOptionalToken(const Token &T) const {
13381337
if (T.is(tok::exclaim_postfix) || T.is(tok::sil_exclamation))
13391338
return true;
13401339
// A postfix or bound infix operator token that begins with '!' can be
1341-
// implicitly unwrapped optional too. We'll munch off the '!', so long as it
1342-
// is left-bound with the type (i.e., parsed as a postfix or unspaced binary
1343-
// operator).
1344-
if ((T.is(tok::oper_postfix) || T.is(tok::oper_binary_unspaced)) &&
1345-
T.getText().startswith("!"))
1346-
return true;
1340+
// implicitly unwrapped optional too.
1341+
if (T.is(tok::oper_postfix) || T.is(tok::oper_binary_unspaced)) {
1342+
// We'll munch off the '!', so long as it is left-bound with
1343+
// the type (i.e., parsed as a postfix or unspaced binary operator).
1344+
return T.getText().startswith("!");
1345+
}
1346+
13471347
return false;
13481348
}
13491349

@@ -1548,9 +1548,7 @@ bool Parser::canParseType() {
15481548
}
15491549

15501550
if (consumeIf(tok::arrow)) {
1551-
if (!canParseType())
1552-
return false;
1553-
return true;
1551+
return canParseType();
15541552
}
15551553

15561554
return true;
@@ -1580,9 +1578,8 @@ bool Parser::canParseSimpleTypeIdentifier() {
15801578
consumeToken();
15811579

15821580
// Parse an optional generic argument list.
1583-
if (startsWithLess(Tok))
1584-
if (!canParseGenericArguments())
1585-
return false;
1581+
if (startsWithLess(Tok) && !canParseGenericArguments())
1582+
return false;
15861583

15871584
return true;
15881585
}

0 commit comments

Comments
 (0)