-
Notifications
You must be signed in to change notification settings - Fork 440
Make CodeBlockItemListSyntax expressible by string literal #1426
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
Make CodeBlockItemListSyntax expressible by string literal #1426
Conversation
As I mentioned in #1165 (comment), I would prefer to make Also, regarding the implementation, instead of adding
We currently use |
bc01576
to
c37c1a4
Compare
Hi @ahoppen, thanks for your comments! Sorry for misunderstanding your comments in #1165. I have updated the codes according to your suggestions. To prevent the undefined behavior of parsing a code block item, I added an assertion in the As for Thanks in advance! |
c37c1a4
to
cf128b1
Compare
extension CodeBlockItemSyntax: SyntaxParseable { | ||
public static func parse(from parser: inout Parser) -> Self { | ||
guard let node = parser.parseCodeBlockItem() else { | ||
assertionFailure("Invalid code block item") | ||
return Syntax(raw: RawSyntax(parser.parseCodeBlockItem()!)).cast(Self.self) | ||
} | ||
let raw = RawSyntax(parser.parseRemainder(into:node)) | ||
return Syntax(raw: raw).cast(Self.self) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will get overridden once you re-generate this file using the following command.
swift run --package-path CodeGeneration generate-swiftsyntax Sources
You will need to do this by adjusting CodeGeneration/Sources/ParserEntryFile.swift
, probably by providing a parserFunction
to CodeBlockItem
in CommonNodes.swift
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense! I added a parserFunction
to CodeBlockItem
since it is also used in SyntaxExpressibleByStringInterpolationConformancesFile.swift
11c1980
to
a812313
Compare
extension \(raw: node.name): SyntaxParseable { | ||
public static func parse(from parser: inout Parser) -> Self { | ||
guard let node = parser.\(raw: parserFunction)() else { | ||
assertionFailure("Invalid code block item") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not crash when parsing an invalid code block item. Instead, we should form an invalid syntax node. What you could do instead when parseCodeBlockItem
returns nil
is to create a CodeBlockItemSyntax
that contains a MissingExprSyntax
– or any of the other Missing*Syntax
nodes, I don’t have a strong preference here.
Also, I would prefer not to add a special-case in this code generation loop. Instead, what I think would be the better solution is to have a function parseNonOptionalCodeBlockItem
, which does what I described above, at the bottom of Parser+Entry.swift and then use that as the parserFunction
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thanks for your suggestions!
I've updated the codes and added corresponding test cases. If there is anything I can do better for this PR, please feel free to let me know.
a812313
to
d9f155e
Compare
CodeGeneration/Sources/generate-swiftsyntax/templates/swiftparser/ParserEntryFile.swift
Outdated
Show resolved
Hide resolved
d9f155e
to
5b5eb4f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed one missing space, otherwise LGTM.
CodeGeneration/Sources/generate-swiftsyntax/templates/swiftparser/ParserEntryFile.swift
Outdated
Show resolved
Hide resolved
5b5eb4f
to
d2a71ae
Compare
@swift-ci Please test |
@swift-ci Please test Windows |
""" | ||
|
||
XCTAssertTrue(codeBlockItem.is(CodeBlockItemSyntax.self)) | ||
AssertStringsEqualWithDiff( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function has been converted to lowercase. Could you update it and make sure that tests build and pass locally?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohhh...I selected swift-parser-cli
as the scheme and it just skipped SwiftSyntaxBuilderTest
. Sorry for that.
Already made these lowercase.😉
Head branch was pushed to by a user without write access
d2a71ae
to
e182ac6
Compare
@swift-ci please test |
@swift-ci please test Windows |
I think there is something wrong with the Windows CI. Could you please trigger that again? @kimdv |
@swift-ci please test Windows |
Thank you 😁 |
This PR is to enable creating a
CodeBlockItemList
from one multi-line string, which is mentioned in #1165.BTW, I also noticed that there are two linked TODOs in
ConvenienceInitializers.swift
andSyntaxExpressibleByStringInterpolationConformances.swift
I'm a little confused about
We should split FunctionParameter into separate nodes
. Some examples or elaborations would be of much help!