Skip to content

Generate grammar doc comments for layout nodes #1771

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
Jun 23, 2023

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Jun 12, 2023

Generate a Grammar and a Children section as doc-comments for layout nodes.

The Grammar section is particularly useful for fairly simple syntax nodes to convey their structure. The Children section allows you to see the children of the syntax node at a glance in the source order.

@ahoppen
Copy link
Member Author

ahoppen commented Jun 12, 2023

@swift-ci Please test

@bnbarham
Copy link
Contributor

bnbarham commented Jun 16, 2023

Cool stuff, interesting idea!

I'm not convinced it's worth having both grammar + children though. In general children seems more useful to me. Do you have any examples where you'd consider the "grammar" as better than children? Right now they're basically duplicates, with children having the advantage of having the name as well.

Comment on lines +223 to +233
### Children

\(GrammarGenerator.childrenList(for: children))
Copy link
Contributor

@WowbaggersLiquidLunch WowbaggersLiquidLunch Jun 18, 2023

Choose a reason for hiding this comment

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

The list of children should already be visible through DocC, under the type/protocol's Instance Properties section. Does it really need to be repeated in the main description too?

Copy link
Member Author

Choose a reason for hiding this comment

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

I think it does because these are sorted in source order while the Instance Properties are sorted alphabetically.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK I think this is a valid reason. Although, maybe in the long term we should have the ability to specify to DocC the ordering of properties.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, if we could do that, I agree that we might be able to remove the children list.

Comment on lines 21 to 27
/// (``AttributeSyntax`` | ``IfConfigDeclSyntax``) `*`
/// ``DeclModifierSyntax``?
/// (`'get'` | `'set'` | `'didSet'` | `'willSet'` | `'unsafeAddress'` | `'addressWithOwner'` | `'addressWithNativeOwner'` | `'unsafeMutableAddress'` | `'mutableAddressWithOwner'` | `'mutableAddressWithNativeOwner'` | `'_read'` | `'_modify'` | `'init'`)
/// ``AccessorParameterSyntax``?
/// ``AccessorEffectSpecifiersSyntax``?
/// ``AccessorInitEffectsSyntax``?
/// ``CodeBlockSyntax``?
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a way to generate the translation between the type/protocol names and the actual grammar terms/components that they represent? I think having such a translation table can help a lot to contributors.

Copy link
Member Author

Choose a reason for hiding this comment

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

What do you mean by “actual grammar terms/components that they represent”?

Copy link
Contributor

Choose a reason for hiding this comment

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

I might be misunderstanding it. What I mean is: it seems to me that not all syntax nodes in the parser correlate one-to-one with the syntax nodes as documented in TSPL.

Copy link
Member Author

Choose a reason for hiding this comment

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

No, there isn’t always a 1:1 correlation between the SwiftSyntax nodes and the grammar rules in TSPL.

private func grammar(for tokenChoice: TokenChoice) -> String {
switch tokenChoice {
case .keyword(text: let text):
return "`'\(text)'`"
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel having quotation marks within the code voice makes it a bit ambiguous what the token actually is.

Copy link
Member Author

Choose a reason for hiding this comment

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

My idea here was to optimize this for rendering in docc and I found that rendering the token name in code voice reads a little nicer in docc. Would you prefer to just use

return "'\(text)'"

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I think I would prefer just return "'\(text)'" or return "`\(text)`". And if the latter is used, syntactic categories can be italicized like in TSPL, to differentiate from token texts.

Copy link
Member Author

Choose a reason for hiding this comment

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

The thing is that we use docc links for links to other syntactic categories, which are always monospaced.

The documentation for StructDeclSyntax shows all kinds of references. Which fonts would you use here?

.joined(separator: "\n")
}

/// Generates a
Copy link
Contributor

Choose a reason for hiding this comment

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

Incomplete doc comment. Should be "/// Generates a list of child nodes"?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch. Thank you

Copy link
Contributor

@WowbaggersLiquidLunch WowbaggersLiquidLunch 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 this. I think this is a good approach in general. I have just some nitpicky-ish comments.

@ahoppen ahoppen force-pushed the ahoppen/generate-grammar branch from 1e8152f to dc058cd Compare June 19, 2023 15:31
@ahoppen
Copy link
Member Author

ahoppen commented Jun 19, 2023

I'm not convinced it's worth having both grammar + children though. In general children seems more useful to me. Do you have any examples where you'd consider the "grammar" as better than children? Right now they're basically duplicates, with children having the advantage of having the name as well.

My idea was that in particular for simple nodes, the grammar shows the structure of the node in a very nice and concise form. But looking at other nodes as well, I agree that it very much breaks down for more complex nodes. @WowbaggersLiquidLunch Do you have opinions on just showing the Children section?

Screenshot 2023-06-19 at 17 35 02

@ahoppen ahoppen force-pushed the ahoppen/generate-grammar branch from dc058cd to 1669a5f Compare June 20, 2023 21:01
@ahoppen
Copy link
Member Author

ahoppen commented Jun 20, 2023

OK, I removed the Grammar section and we’re just left with Children now. That seems to be cleaner.

@ahoppen
Copy link
Member Author

ahoppen commented Jun 20, 2023

@swift-ci Please test

@ahoppen
Copy link
Member Author

ahoppen commented Jun 20, 2023

@swift-ci Please test Windows

@ahoppen ahoppen force-pushed the ahoppen/generate-grammar branch from 1669a5f to d868b0d Compare June 22, 2023 20:44
@ahoppen
Copy link
Member Author

ahoppen commented Jun 22, 2023

@swift-ci Please test

.joined(separator: "\n")
}

/// Generates a
Copy link
Contributor

Choose a reason for hiding this comment

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

Got half way through :)?

Generate a `Grammar` and a `Children` section as doc-comments for layout nodes.

The `Grammar` section is particularly useful for fairly simple syntax nodes to convey their structure. The `Children` section allows you to see the children of the syntax node at a glance in the source order.
@ahoppen ahoppen force-pushed the ahoppen/generate-grammar branch from d868b0d to 9b128ec Compare June 23, 2023 13:49
@ahoppen
Copy link
Member Author

ahoppen commented Jun 23, 2023

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge June 23, 2023 13:49
@ahoppen
Copy link
Member Author

ahoppen commented Jun 23, 2023

@swift-ci Please test Windows

@ahoppen ahoppen merged commit 0fbe758 into swiftlang:main Jun 23, 2023
@WowbaggersLiquidLunch
Copy link
Contributor

My idea was that in particular for simple nodes, the grammar shows the structure of the node in a very nice and concise form. But looking at other nodes as well, I agree that it very much breaks down for more complex nodes. @WowbaggersLiquidLunch Do you have opinions on just showing the Children section?

Screenshot 2023-06-19 at 17 35 02

OK, I removed the Grammar section and we’re just left with Children now. That seems to be cleaner.

Sorry for the late response. I still think it's better to have the Grammar section. It's probably a bit more clutter, but from my experience, having the grammar documented really helps new contributors find the relevant things to modify. Although, instead of "'await' ExprSyntax", I would prefer "await-operator" in the documentation, because that's probably what people will search for to find this syntax node.

Additionally something I just noticed: Currently the grammars are documented on the parse functions, not syntax node types. Are there plans to generated the grammar docs for those functions?

@ahoppen ahoppen deleted the ahoppen/generate-grammar branch June 26, 2023 10:51
@ahoppen
Copy link
Member Author

ahoppen commented Jun 26, 2023

Sorry for the late response. I still think it's better to have the Grammar section. It's probably a bit more clutter, but from my experience, having the grammar documented really helps new contributors find the relevant things to modify.

Which information do you think would be valuable in the Grammar section that’s not present in the Children section?

Although, instead of "'await' ExprSyntax", I would prefer "await-operator" in the documentation, because that's probably what people will search for to find this syntax node.

To be clear: There is a difference between SwiftSyntax and TSPL, just by the way the trees are structured.

For example, if we wanted to model the following production rules in SwiftSyntax, prefix-operator would need to be an enum with two cases, each with associated values and that’s just not how the SwiftSyntax tree is structured.

prefix-expression → prefix-operator? postfix-expression
prefix-expression → in-out-expression

So, there is not equivalent of the

expression → try-operator? await-operator? prefix-expression infix-expressions?

production rule or await-operator in SwiftSyntax, so I don’t think there’s a natural way to place it.

Additionally something I just noticed: Currently the grammars are documented on the parse functions, not syntax node types. Are there plans to generated the grammar docs for those functions?

Since the grammar rules on the parse functions are mostly outdated, I want to remove them now that this PR is merged.

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