Skip to content

libSyntax: add a mechanism to synthesize syntax nodes in SyntaxParsingContext. #16158

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 1 commit into from
Apr 26, 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
8 changes: 8 additions & 0 deletions include/swift/Parse/SyntaxParsingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ class alignas(1 << SyntaxAlignInBits) SyntaxParsingContext {
/// the syntax tree before closing the root context.
void finalizeRoot();

/// Make a missing node corresponding to the given token kind and text, and
/// push this node into the context. The synthesized node can help
/// the creation of valid syntax nodes.
void synthesize(tok Kind, StringRef Text = "");

/// Make a missing node corresponding to the given node kind, and
/// push this node into the context.
void synthesize(SyntaxKind Kind);
};

} // namespace swift
Expand Down
7 changes: 5 additions & 2 deletions lib/Parse/ParseStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,8 +637,11 @@ ParserResult<BraceStmt> Parser::parseBraceItemList(Diag<> ID) {

ParserStatus Status = parseBraceItems(Entries, BraceItemListKind::Brace,
BraceItemListKind::Brace);
parseMatchingToken(tok::r_brace, RBLoc,
diag::expected_rbrace_in_brace_stmt, LBLoc);
if (parseMatchingToken(tok::r_brace, RBLoc,
diag::expected_rbrace_in_brace_stmt, LBLoc)) {
// Synthesize a r-brace if the source doesn't have any.
LocalContext.synthesize(tok::r_brace);
}

return makeParserResult(Status,
BraceStmt::create(Context, LBLoc, Entries, RBLoc));
Expand Down
14 changes: 14 additions & 0 deletions lib/Parse/SyntaxParsingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,20 @@ void SyntaxParsingContext::finalizeRoot() {
getRootData().Storage.clear();
}

void SyntaxParsingContext::synthesize(tok Kind, StringRef Text) {
if (!Enabled)
return;
if (Text.empty())
Text = getTokenText(Kind);
Storage.push_back(RawSyntax::missing(Kind, Text));
}

void SyntaxParsingContext::synthesize(SyntaxKind Kind) {
if (!Enabled)
return;
Storage.push_back(RawSyntax::missing(Kind));
}

SyntaxParsingContext::~SyntaxParsingContext() {
assert(isTopOfContextStack() && "destructed in wrong order");

Expand Down
14 changes: 14 additions & 0 deletions test/Syntax/Outputs/round_trip_invalid.swift.withkinds
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<FunctionDecl>// RUN: rm -rf %t
// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t
// RUN: diff -u %s %t
// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds
// RUN: diff -u %S/Outputs/round_trip_invalid.swift.withkinds %t.withkinds
// RUN: %swift-syntax-test -input-source-filename %s -eof > %t
// RUN: diff -u %s %t
// RUN: %swift-syntax-test -serialize-raw-tree -input-source-filename %s > %t.dump
// RUN: %swift-syntax-test -deserialize-raw-tree -input-source-filename %t.dump -output-filename %t
// RUN: diff -u %s %t

// Function body without closing brace token.
func foo<FunctionSignature><ParameterClause>() </ParameterClause></FunctionSignature><CodeBlock>{<VariableDecl>
var <PatternBinding><IdentifierPattern>a </IdentifierPattern><InitializerClause>= <IntegerLiteralExpr>2</IntegerLiteralExpr></InitializerClause></PatternBinding></VariableDecl></CodeBlock></FunctionDecl>
14 changes: 14 additions & 0 deletions test/Syntax/round_trip_invalid.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: rm -rf %t
// RUN: %swift-syntax-test -input-source-filename %s -parse-gen > %t
// RUN: diff -u %s %t
// RUN: %swift-syntax-test -input-source-filename %s -parse-gen -print-node-kind > %t.withkinds
// RUN: diff -u %S/Outputs/round_trip_invalid.swift.withkinds %t.withkinds
// RUN: %swift-syntax-test -input-source-filename %s -eof > %t
// RUN: diff -u %s %t
// RUN: %swift-syntax-test -serialize-raw-tree -input-source-filename %s > %t.dump
// RUN: %swift-syntax-test -deserialize-raw-tree -input-source-filename %t.dump -output-filename %t
// RUN: diff -u %s %t

// Function body without closing brace token.
func foo() {
var a = 2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add newline at the end? Test for preserving trivia for EOF is important here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do!