Skip to content

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

Merged

Conversation

StevenWong12
Copy link
Contributor

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 and SyntaxExpressibleByStringInterpolationConformances.swift

// TODO: This should be inlined in SyntaxParseable.init(stringInterpolation:),
// but is currently used in `ConvenienceInitializers.swift`.
// See the corresponding TODO there.
func performParse<SyntaxType: SyntaxProtocol>(

// TODO: We should split FunctionParameter into separate nodes
extension FunctionParameterSyntax {

I'm a little confused about We should split FunctionParameter into separate nodes. Some examples or elaborations would be of much help!

@StevenWong12 StevenWong12 requested a review from ahoppen as a code owner March 21, 2023 03:36
@StevenWong12 StevenWong12 requested review from kimdv and removed request for ahoppen March 21, 2023 07:22
@ahoppen
Copy link
Member

ahoppen commented Mar 21, 2023

As I mentioned in #1165 (comment), I would prefer to make CodeBlockItemSyntax expressible by string literal instead of CodeBlockItemListSyntax. I think that generalizes better because you can then still use result builder constructs such as loops to create the final CodeBlockItemListSyntax.

Also, regarding the implementation, instead of adding parserCodeBlockItemListForBuilder you should make CodeBlockItemSyntax conform to SyntaxParsable in Parser+Entry.swift.

I'm a little confused about We should split FunctionParameter into separate nodes. Some examples or elaborations would be of much help!

We currently use FunctionParameterSyntax for a variety of parameters like function parameters, enum parameters and closure parameters and they all have different needs. Closure parameters for example don’t require a type and enum parameters no parameter label. Thus all the children of FunctionParameterSyntax are optional. If we had a separate node for each of these uses we could get rid of ParameterSubject and make the children that are required for each of these parameter kinds non-optional.

@StevenWong12 StevenWong12 force-pushed the enable_multi_line_codeblocklist branch from bc01576 to c37c1a4 Compare March 22, 2023 07:49
@StevenWong12
Copy link
Contributor Author

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 Parser+Entry.swift. I wonder whether this is reasonable?

As for FunctionParameterSyntax, I would like to make a try to separate it into different nodes. Should I create an issue for that to follow up?

Thanks in advance!

@StevenWong12 StevenWong12 force-pushed the enable_multi_line_codeblocklist branch from c37c1a4 to cf128b1 Compare March 22, 2023 08:00
Comment on lines 68 to 83
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)
}
}

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 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

Copy link
Contributor Author

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

@StevenWong12 StevenWong12 force-pushed the enable_multi_line_codeblocklist branch from 11c1980 to a812313 Compare March 23, 2023 09:47
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")
Copy link
Member

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.

Copy link
Contributor Author

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.

@StevenWong12 StevenWong12 force-pushed the enable_multi_line_codeblocklist branch from a812313 to d9f155e Compare March 24, 2023 08:03
@StevenWong12 StevenWong12 requested review from ahoppen and removed request for kimdv March 24, 2023 08:04
@StevenWong12 StevenWong12 force-pushed the enable_multi_line_codeblocklist branch from d9f155e to 5b5eb4f Compare May 4, 2023 08:45
@StevenWong12 StevenWong12 requested a review from ahoppen May 4, 2023 08:49
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.

I just noticed one missing space, otherwise LGTM.

@StevenWong12 StevenWong12 force-pushed the enable_multi_line_codeblocklist branch from 5b5eb4f to d2a71ae Compare May 5, 2023 05:01
@ahoppen ahoppen enabled auto-merge May 5, 2023 19:38
@ahoppen
Copy link
Member

ahoppen commented May 5, 2023

@swift-ci Please test

@ahoppen
Copy link
Member

ahoppen commented May 5, 2023

@swift-ci Please test Windows

"""

XCTAssertTrue(codeBlockItem.is(CodeBlockItemSyntax.self))
AssertStringsEqualWithDiff(
Copy link
Member

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?

Copy link
Contributor Author

@StevenWong12 StevenWong12 May 6, 2023

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.😉

auto-merge was automatically disabled May 6, 2023 02:11

Head branch was pushed to by a user without write access

@StevenWong12 StevenWong12 force-pushed the enable_multi_line_codeblocklist branch from d2a71ae to e182ac6 Compare May 6, 2023 02:11
@kimdv
Copy link
Contributor

kimdv commented May 6, 2023

@swift-ci please test

@kimdv
Copy link
Contributor

kimdv commented May 6, 2023

@swift-ci please test Windows

@StevenWong12
Copy link
Contributor Author

I think there is something wrong with the Windows CI. Could you please trigger that again? @kimdv

@kimdv
Copy link
Contributor

kimdv commented May 6, 2023

@swift-ci please test Windows

@StevenWong12
Copy link
Contributor Author

Thank you 😁

@ahoppen ahoppen merged commit 6b71c97 into swiftlang:main May 8, 2023
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.

3 participants