Skip to content

[libSyntax] Parsing fixes for #if, trailing semicolon, key paths #16155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2454,6 +2454,8 @@ Parser::parseDecl(ParseDeclOptions Flags,

ParserStatus Status;
bool PreviousHadSemi = true;
SyntaxParsingContext DeclListCtx(SyntaxContext,
SyntaxKind::MemberDeclList);
while (Tok.isNot(tok::pound_else, tok::pound_endif, tok::pound_elseif,
tok::eof)) {
if (Tok.is(tok::r_brace)) {
Expand Down Expand Up @@ -3102,7 +3104,6 @@ void Parser::diagnoseConsecutiveIDs(StringRef First, SourceLoc FirstLoc,
ParserStatus Parser::parseDeclItem(bool &PreviousHadSemi,
Parser::ParseDeclOptions Options,
llvm::function_ref<void(Decl*)> handler) {
SyntaxParsingContext DeclContext(SyntaxContext, SyntaxContextKind::Decl);
if (Tok.is(tok::semi)) {
// Consume ';' without preceding decl.
diagnose(Tok, diag::unexpected_separator, ";")
Expand All @@ -3127,7 +3128,10 @@ ParserStatus Parser::parseDeclItem(bool &PreviousHadSemi,
return LineDirectiveStatus;
}

auto Result = parseDecl(Options, handler);
ParserResult<Decl> Result;
SyntaxParsingContext DeclContext(SyntaxContext,
SyntaxKind::MemberDeclListItem);
Result = parseDecl(Options, handler);
if (Result.isParseError())
skipUntilDeclRBrace(tok::semi, tok::pound_endif);
SourceLoc SemiLoc;
Expand All @@ -3148,7 +3152,7 @@ bool Parser::parseDeclList(SourceLoc LBLoc, SourceLoc &RBLoc,
ParserStatus Status;
bool PreviousHadSemi = true;
{
SyntaxParsingContext ListContext(SyntaxContext, SyntaxKind::DeclList);
SyntaxParsingContext ListContext(SyntaxContext, SyntaxKind::MemberDeclList);
while (Tok.isNot(tok::r_brace)) {
Status |= parseDeclItem(PreviousHadSemi, Options, handler);
if (Tok.isAny(tok::eof, tok::pound_endif, tok::pound_else,
Expand Down
8 changes: 6 additions & 2 deletions lib/Parse/ParseExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,6 @@ ParserResult<Expr> Parser::parseExprKeyPath() {
// Consume '\'.
SourceLoc backslashLoc = consumeToken(tok::backslash);
llvm::SaveAndRestore<SourceLoc> slashLoc(SwiftKeyPathSlashLoc, backslashLoc);
SyntaxParsingContext ExprCtx(SyntaxContext, SyntaxContextKind::Expr);

// FIXME: diagnostics
ParserResult<Expr> rootResult, pathResult;
Expand All @@ -598,14 +597,19 @@ ParserResult<Expr> Parser::parseExprKeyPath() {
if (startsWithSymbol(Tok, '.')) {
llvm::SaveAndRestore<Expr*> S(SwiftKeyPathRoot, rootResult.getPtrOrNull());

SyntaxParsingContext ExprContext(SyntaxContext, SyntaxContextKind::Expr);

auto dotLoc = Tok.getLoc();
// For uniformity, \.foo is parsed as if it were MAGIC.foo, so we need to
// make sure the . is there, but parsing the ? in \.? as .? doesn't make
// sense. This is all made more complicated by .?. being considered an
// operator token, and a single one at that (which means
// peekToken().is(tok::identifier) is incorrect: it is true for .?.foo).
if (Tok.getLength() != 1 || !peekToken().is(tok::identifier))
if (Tok.getLength() != 1 || !peekToken().is(tok::identifier)) {
SyntaxParsingContext KeyPathBaseContext(SyntaxContext,
SyntaxKind::KeyPathBaseExpr);
consumeStartingCharacterOfCurrentToken(tok::period);
}

auto inner = makeParserResult(new (Context) KeyPathDotExpr(dotLoc));
bool unusedHasBindOptional = false;
Expand Down
4 changes: 2 additions & 2 deletions test/Syntax/Inputs/serialize_multiple_decls.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"presence": "Present"
},
{
"kind": "DeclList",
"kind": "MemberDeclList",
"layout": [],
"presence": "Present"
},
Expand Down Expand Up @@ -153,7 +153,7 @@
"presence": "Present"
},
{
"kind": "DeclList",
"kind": "MemberDeclList",
"layout": [],
"presence": "Present"
},
Expand Down
Loading