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,95 +1250,94 @@ 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()));
continue;
}
// A '.self' expr.
if (Tok.is(tok::kw_self)) {
Result = makeParserResult(
new (Context) DotSelfExpr(Result.get(), TokLoc, consumeToken()));
continue;
}
// 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)) {
Identifier Name = Context.getIdentifier(Tok.getText());
Result = makeParserResult(
new (Context) UnresolvedDotExpr(Result.get(), TokLoc,
Name, DeclNameLoc(Tok.getLoc()),
/*Implicit=*/false));
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;
// 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;
}
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)) {
Identifier Name = Context.getIdentifier(Tok.getText());
Result = makeParserResult(
new (Context) UnresolvedDotExpr(Result.get(), TokLoc,
Name, DeclNameLoc(Tok.getLoc()),
/*Implicit=*/false));
consumeToken();
}
// 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)) {
DeclNameLoc NameLoc;
DeclName Name = parseUnqualifiedDeclName(/*allowInit=*/true,
assert(Tok.isAny(tok::identifier, tok::kw_init));
DeclNameLoc NameLoc;
DeclName Name = parseUnqualifiedDeclName(/*allowInit=*/true,
NameLoc,
diag::expected_member_name);
if (!Name) return nullptr;
Result = makeParserResult(
new (Context) UnresolvedDotExpr(Result.get(), TokLoc, Name,
NameLoc,
diag::expected_member_name);
if (!Name) return nullptr;
Result = makeParserResult(
new (Context) UnresolvedDotExpr(Result.get(), TokLoc, Name,
NameLoc,
/*Implicit=*/false));
if (canParseAsGenericArgumentList()) {
SmallVector<TypeRepr*, 8> args;
SourceLoc LAngleLoc, RAngleLoc;
if (parseGenericArguments(args, LAngleLoc, RAngleLoc)) {
diagnose(LAngleLoc, diag::while_parsing_as_left_angle_bracket);
}
SmallVector<TypeLoc, 8> locArgs;
for (auto ty : args)
locArgs.push_back(ty);
Result = makeParserResult(new (Context) UnresolvedSpecializeExpr(
Result.get(), LAngleLoc, Context.AllocateCopy(locArgs),
RAngleLoc));
/*Implicit=*/false));
if (canParseAsGenericArgumentList()) {
SmallVector<TypeRepr*, 8> args;
SourceLoc LAngleLoc, RAngleLoc;
if (parseGenericArguments(args, LAngleLoc, RAngleLoc)) {
diagnose(LAngleLoc, diag::while_parsing_as_left_angle_bracket);
}
} 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));
SmallVector<TypeLoc, 8> locArgs;
for (auto ty : args)
locArgs.push_back(ty);
Result = makeParserResult(new (Context) UnresolvedSpecializeExpr(
Result.get(), LAngleLoc, Context.AllocateCopy(locArgs),
RAngleLoc));
}
// If there is an expr-call-suffix, parse it and form a call.