mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
refactor the parsing logic for "x.y" to detangle it a bit, NFC.
This commit is contained in:
@@ -1250,24 +1250,51 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) {
|
||||
// Check for a .foo suffix.
|
||||
SourceLoc TokLoc = Tok.getLoc();
|
||||
if (consumeIf(tok::period) || consumeIf(tok::period_prefix)) {
|
||||
// Non-identifier cases.
|
||||
if (Tok.isNot(tok::identifier) && Tok.isNot(tok::integer_literal) &&
|
||||
Tok.isNot(tok::kw_init)) {
|
||||
// A metatype expr.
|
||||
if (Tok.is(tok::kw_dynamicType)) {
|
||||
Result = makeParserResult(
|
||||
new (Context) DynamicTypeExpr(Result.get(), consumeToken(),
|
||||
Type()));
|
||||
|
||||
// Handle "x.42" - a tuple index.
|
||||
if (Tok.is(tok::integer_literal)) {
|
||||
DeclName name = Context.getIdentifier(Tok.getText());
|
||||
SourceLoc nameLoc = consumeToken(tok::integer_literal);
|
||||
|
||||
// Don't allow '.<integer literal>' following a numeric literal
|
||||
// expression.
|
||||
if (Result.isNonNull() && isa<NumberLiteralExpr>(Result.get())) {
|
||||
diagnose(nameLoc, diag::numeric_literal_numeric_member)
|
||||
.highlight(Result.get()->getSourceRange());
|
||||
continue;
|
||||
}
|
||||
|
||||
// A '.self' expr.
|
||||
Result = makeParserResult(
|
||||
new (Context) UnresolvedDotExpr(Result.get(), TokLoc, name,
|
||||
DeclNameLoc(nameLoc),
|
||||
/*Implicit=*/false));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle "x.dynamicType" - A metatype expr.
|
||||
if (Tok.is(tok::kw_dynamicType)) {
|
||||
Result = makeParserResult(
|
||||
new (Context) DynamicTypeExpr(Result.get(), consumeToken(), Type()));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle "x.self" expr.
|
||||
if (Tok.is(tok::kw_self)) {
|
||||
Result = makeParserResult(
|
||||
new (Context) DotSelfExpr(Result.get(), TokLoc, consumeToken()));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Handle "x.<tab>" for code completion.
|
||||
if (Tok.is(tok::code_complete)) {
|
||||
if (CodeCompletion && Result.isNonNull())
|
||||
CodeCompletion->completeDotExpr(Result.get(), /*DotLoc=*/TokLoc);
|
||||
// Eat the code completion token because we handled it.
|
||||
consumeToken(tok::code_complete);
|
||||
Result.setHasCodeCompletion();
|
||||
return Result;
|
||||
}
|
||||
|
||||
// If we have '.<keyword><code_complete>', try to recover by creating
|
||||
// an identifier with the same spelling as the keyword.
|
||||
if (Tok.isKeyword() && peekToken().is(tok::code_complete)) {
|
||||
@@ -1279,34 +1306,14 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) {
|
||||
consumeToken();
|
||||
}
|
||||
|
||||
if (Tok.is(tok::code_complete)) {
|
||||
if (CodeCompletion && Result.isNonNull())
|
||||
CodeCompletion->completeDotExpr(Result.get(), /*DotLoc=*/TokLoc);
|
||||
// Eat the code completion token because we handled it.
|
||||
consumeToken(tok::code_complete);
|
||||
Result.setHasCodeCompletion();
|
||||
return Result;
|
||||
}
|
||||
// Non-identifier cases.
|
||||
if (Tok.isNot(tok::identifier, tok::kw_init)) {
|
||||
checkForInputIncomplete();
|
||||
diagnose(Tok, diag::expected_member_name);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Don't allow '.<integer literal>' following a numeric literal
|
||||
// expression.
|
||||
if (Tok.is(tok::integer_literal) && Result.isNonNull() &&
|
||||
(isa<FloatLiteralExpr>(Result.get()) ||
|
||||
isa<IntegerLiteralExpr>(Result.get()))) {
|
||||
diagnose(Tok, diag::numeric_literal_numeric_member)
|
||||
.highlight(Result.get()->getSourceRange());
|
||||
consumeToken();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (Result.isParseError())
|
||||
continue;
|
||||
|
||||
if (Tok.isAny(tok::identifier, tok::kw_init)) {
|
||||
assert(Tok.isAny(tok::identifier, tok::kw_init));
|
||||
DeclNameLoc NameLoc;
|
||||
DeclName Name = parseUnqualifiedDeclName(/*allowInit=*/true,
|
||||
NameLoc,
|
||||
@@ -1332,14 +1339,6 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) {
|
||||
Result.get(), LAngleLoc, Context.AllocateCopy(locArgs),
|
||||
RAngleLoc));
|
||||
}
|
||||
} else {
|
||||
DeclName name = Context.getIdentifier(Tok.getText());
|
||||
SourceLoc nameLoc = consumeToken(tok::integer_literal);
|
||||
Result = makeParserResult(
|
||||
new (Context) UnresolvedDotExpr(Result.get(), TokLoc, name,
|
||||
DeclNameLoc(nameLoc),
|
||||
/*Implicit=*/false));
|
||||
}
|
||||
|
||||
// If there is an expr-call-suffix, parse it and form a call.
|
||||
if (Tok.isFollowingLParen())
|
||||
|
||||
Reference in New Issue
Block a user