Skip to content

Commit 0107d48

Browse files
committed
Added a helper function for checking if the next token is an accessor label.
1 parent 1586da6 commit 0107d48

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,19 @@ bool Parser::parseDifferentiableAttributeArguments(
10371037
return false;
10381038
}
10391039

1040+
// Helper function that returns the accessorkind if a token is an accessor label.
1041+
static Optional<AccessorKind> isAccessorLabel(const Token& token) {
1042+
if (token.is(tok::identifier)) {
1043+
StringRef tokText = token.getText();
1044+
for (auto accessor : allAccessorKinds()) {
1045+
if (tokText == getAccessorLabel(accessor)) {
1046+
return accessor;
1047+
}
1048+
}
1049+
}
1050+
return None;
1051+
}
1052+
10401053
/// Helper function that parses 'type-identifier' for `parseQualifiedDeclName`.
10411054
/// Returns true on error. Sets `baseType` to the parsed base type if present,
10421055
/// or to `nullptr` if not. A missing base type is not considered an error.
@@ -1065,14 +1078,8 @@ static bool parseBaseTypeForQualifiedDeclName(Parser &P, TypeRepr *&baseType) {
10651078
// name like an accessor.
10661079
if (P.Tok.is(tok::period)) {
10671080
const Token &nextToken = P.peekToken();
1068-
if(nextToken.is(tok::identifier)) {
1069-
StringRef tokText = nextToken.getText();
1070-
for(auto accessor : allAccessorKinds()) {
1071-
if(tokText == getAccessorLabel(accessor)) {
1072-
return false;
1073-
}
1074-
}
1075-
}
1081+
if (isAccessorLabel(nextToken) != None)
1082+
return false;
10761083
}
10771084

10781085
backtrack.cancelBacktrack();
@@ -1115,24 +1122,20 @@ static bool parseQualifiedDeclName(Parser &P, Diag<> nameParseError,
11151122
if (!original.Name)
11161123
return true;
11171124
}
1118-
1119-
// Parse to see if this is an accessor and set it's type. This is an optional field.
1125+
1126+
// Parse to see if this is an accessor and set it's type. This is an optional field.
11201127
if (P.Tok.is(tok::period)) {
11211128
const Token &nextToken = P.peekToken();
1122-
if(nextToken.is(tok::identifier)) {
1123-
StringRef tokText = nextToken.getText();
1124-
for(auto accessor : allAccessorKinds()) {
1125-
if(tokText == getAccessorLabel(accessor)) {
1126-
original.AccessorKind = accessor;
1127-
P.consumeIf(tok::period);
1128-
P.consumeIf(tok::identifier);
1129-
}
1130-
}
1129+
Optional<AccessorKind> kind = isAccessorLabel(nextToken);
1130+
if (kind != None) {
1131+
original.AccessorKind = kind;
1132+
P.consumeIf(tok::period);
1133+
P.consumeIf(tok::identifier);
11311134
}
11321135
}
11331136

11341137
return false;
1135-
1138+
11361139
}
11371140

11381141
/// Parse a `@derivative(of:)` attribute, returning true on error.

0 commit comments

Comments
 (0)