refactor the parsing logic for "x.y" to detangle it a bit, NFC.

This commit is contained in:
Chris Lattner
2016-05-02 21:33:29 -07:00
parent a0efe4fcb2
commit 561c379eba

View File

@@ -1250,24 +1250,51 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) {
// Check for a .foo suffix. // Check for a .foo suffix.
SourceLoc TokLoc = Tok.getLoc(); SourceLoc TokLoc = Tok.getLoc();
if (consumeIf(tok::period) || consumeIf(tok::period_prefix)) { if (consumeIf(tok::period) || consumeIf(tok::period_prefix)) {
// Non-identifier cases.
if (Tok.isNot(tok::identifier) && Tok.isNot(tok::integer_literal) && // Handle "x.42" - a tuple index.
Tok.isNot(tok::kw_init)) { if (Tok.is(tok::integer_literal)) {
// A metatype expr. DeclName name = Context.getIdentifier(Tok.getText());
if (Tok.is(tok::kw_dynamicType)) { SourceLoc nameLoc = consumeToken(tok::integer_literal);
Result = makeParserResult(
new (Context) DynamicTypeExpr(Result.get(), consumeToken(), // Don't allow '.<integer literal>' following a numeric literal
Type())); // expression.
if (Result.isNonNull() && isa<NumberLiteralExpr>(Result.get())) {
diagnose(nameLoc, diag::numeric_literal_numeric_member)
.highlight(Result.get()->getSourceRange());
continue; 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)) { if (Tok.is(tok::kw_self)) {
Result = makeParserResult( Result = makeParserResult(
new (Context) DotSelfExpr(Result.get(), TokLoc, consumeToken())); new (Context) DotSelfExpr(Result.get(), TokLoc, consumeToken()));
continue; 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 // If we have '.<keyword><code_complete>', try to recover by creating
// an identifier with the same spelling as the keyword. // an identifier with the same spelling as the keyword.
if (Tok.isKeyword() && peekToken().is(tok::code_complete)) { if (Tok.isKeyword() && peekToken().is(tok::code_complete)) {
@@ -1279,34 +1306,14 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) {
consumeToken(); consumeToken();
} }
if (Tok.is(tok::code_complete)) { // Non-identifier cases.
if (CodeCompletion && Result.isNonNull()) if (Tok.isNot(tok::identifier, tok::kw_init)) {
CodeCompletion->completeDotExpr(Result.get(), /*DotLoc=*/TokLoc);
// Eat the code completion token because we handled it.
consumeToken(tok::code_complete);
Result.setHasCodeCompletion();
return Result;
}
checkForInputIncomplete(); checkForInputIncomplete();
diagnose(Tok, diag::expected_member_name); diagnose(Tok, diag::expected_member_name);
return nullptr; return nullptr;
} }
// Don't allow '.<integer literal>' following a numeric literal assert(Tok.isAny(tok::identifier, tok::kw_init));
// 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)) {
DeclNameLoc NameLoc; DeclNameLoc NameLoc;
DeclName Name = parseUnqualifiedDeclName(/*allowInit=*/true, DeclName Name = parseUnqualifiedDeclName(/*allowInit=*/true,
NameLoc, NameLoc,
@@ -1332,14 +1339,6 @@ ParserResult<Expr> Parser::parseExprPostfix(Diag<> ID, bool isExprBasic) {
Result.get(), LAngleLoc, Context.AllocateCopy(locArgs), Result.get(), LAngleLoc, Context.AllocateCopy(locArgs),
RAngleLoc)); 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 there is an expr-call-suffix, parse it and form a call.
if (Tok.isFollowingLParen()) if (Tok.isFollowingLParen())