Skip to content

Generate missing string closer quote and delimiter #690

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 4 commits into from
Sep 2, 2022

Conversation

omochi
Copy link
Contributor

@omochi omochi commented Sep 2, 2022

This PR fix #668 #669.

If closer quote and delimiter is missing but opener ones are present,
generate missing token to fix round trip bug and emit missing diagnostics.

@omochi omochi requested a review from ahoppen as a code owner September 2, 2022 06:26
@@ -907,7 +907,9 @@ extension Parser {
at: openDelimiter != nil ? .leadingRaw : .leading,
text: text
) ?? RawTokenSyntax(missing: .stringQuote, arena: arena)
text = text.dropFirst(openQuote.tokenText.count)
if !openQuote.isMissing {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I fixed logic here.

If opener quote missing token is generated, don't drop text.

@@ -917,16 +919,37 @@ extension Parser {
/// Parse close quote.
let closeQuote = self.parseStringLiteralQuote(
at: openDelimiter != nil ? .trailingRaw : .trailing,
text: text[closeStart...]
text: text
Copy link
Contributor Author

Choose a reason for hiding this comment

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

subscript is unnecessary here

if position == .leadingRaw && text[index] == UInt8(ascii: "#") {
if position == .leadingRaw,
index < text.endIndex,
text[index] == UInt8(ascii: "#")
Copy link
Contributor Author

@omochi omochi Sep 2, 2022

Choose a reason for hiding this comment

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

This subscript may access out of bounds.
I added checking of range.


AssertParse(
##"""
#"#^DQ^##^DD^#
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Though I didn't report this input, it was also failed before.

Copy link
Member

Choose a reason for hiding this comment

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

I think you can just use a single marker in these tests because the two you have resolve to the same location currently.

Suggested change
#"#^DQ^##^DD^#
#"#^DIAG^#

If you name it DIAG, you also don’t need to specify the maker name in DiagnosticSpec.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed to share single #^DIAG^#.

@ahoppen ahoppen linked an issue Sep 2, 2022 that may be closed by this pull request
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thanks for fixing these issues. The PR looks good, I’ve got a few stylistic comments.

Comment on lines 937 to 944
return RawTokenSyntax(
kind: .rawStringDelimiter,
text: openDelimiter.tokenText,
leadingTriviaPieces: [],
trailingTriviaPieces: [],
presence: .missing,
arena: arena
)
Copy link
Member

Choose a reason for hiding this comment

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

You could use

Suggested change
return RawTokenSyntax(
kind: .rawStringDelimiter,
text: openDelimiter.tokenText,
leadingTriviaPieces: [],
trailingTriviaPieces: [],
presence: .missing,
arena: arena
)
return RawTokenSyntax(
missing: .rawStringDelimiter,
text: openDelimiter.tokenText,
arena: arena
)

If you add text: SyntaxText? = nil, to RawTokenText.init(missing:arena:) and apply the following change in the initializer.

- text: kind.defaultText ?? "",
+ text: text ?? kind.defaultText ?? "",

I think I’ve got at least two branches locally that do this as well…

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually I looked for RawTokenText(missing:text:arena:) first time 😄.
I added text: and changed to use it.

}

return nil
}()
Copy link
Member

Choose a reason for hiding this comment

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

I’m personally not a huge fan of declaring a closure that you immediately call. I would prefer a paradigm like

let closeDelimiter: RawTokenSyntax?
if let parsedDelimiter = self.parseStringLiteralDelimiter(…) {
  closeDelimiter = parsedDelimiter
} else if let openDelimiter = openDelimiter {
  closeDelimiter = RawTokenSyntax(… missing ...)
} else {
  closeDelimiter = nil
}

That matches the style we use in the rest of the parser.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I didn't check style well.
I changed it not to use closure.


AssertParse(
##"""
#"#^DQ^##^DD^#
Copy link
Member

Choose a reason for hiding this comment

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

I think you can just use a single marker in these tests because the two you have resolve to the same location currently.

Suggested change
#"#^DQ^##^DD^#
#"#^DIAG^#

If you name it DIAG, you also don’t need to specify the maker name in DiagnosticSpec.

@omochi
Copy link
Contributor Author

omochi commented Sep 2, 2022

@ahoppen Thanks for review. I changed styles to follow comments.

@omochi omochi requested a review from ahoppen September 2, 2022 11:00
@ahoppen
Copy link
Member

ahoppen commented Sep 2, 2022

@swift-ci Please test

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thanks for your fixes to the parser 👍

@ahoppen ahoppen merged commit 816d691 into swiftlang:main Sep 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SwiftParser crash when parsing #"""a SwiftParser crash when parsing #"""
2 participants