Skip to content
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

SwiftSyntax: add a mechanism to define traits of syntax nodes to allow abstract access to popular child kinds. NFC #14668

Merged
merged 3 commits into from
Feb 16, 2018

Conversation

nkcsgexi
Copy link
Member

@nkcsgexi nkcsgexi commented Feb 15, 2018

Swift syntax APIs lack an abstract way of accessing children. The client has to
down-cast a syntax node to the leaf type to access any of its children. However,
some children are common among different syntax kinds, e.g.
DeclAttributeSyntax and DeclMembers. We should allow an abstract way to
access and modify them, so that clients can avoid logic duplication.

This patch adds a mechanism to define new traits and specify satisfied
traits in specific syntax nodes. A trait is a set of common children
and implemented in Swift as a protocol for syntax nodes to conform to.
As a proof-of-concept, we added two traits for now including DeclGroup
and BracedSyntax.

Resolves: #49479 and #49465

…w abstract access to popular child kinds. NFC

Swift syntax APIs lack an abstract way of accessing children. The client has to
down-cast a syntax node to the leaf type to access any of its children. However,
some children are common among different syntax kinds, e.g.
DeclAttributeSyntax and DeclMembers. We should allow an abstract way to
access and modify them, so that clients can avoid logic duplication.

This patch adds a mechanism to define new traits and specify satisfied
traits in specific syntax nodes. A trait is a set of common children
and implemented in Swift as a protocol for syntax nodes to conform to.
As a proof-of-concept, we added two traits for now including DeclGroup
and BracedSyntax.

Resolves: SR-6931 and SR-6916
@nkcsgexi
Copy link
Member Author

@swift-ci please smoke test

@nkcsgexi nkcsgexi changed the title swiftSyntax: add a mechanism to define traits of syntax nodes to allow abstract access to popular child kinds. NFC SwiftSyntax: add a mechanism to define traits of syntax nodes to allow abstract access to popular child kinds. NFC Feb 15, 2018
Copy link
Collaborator

@harlanhaskins harlanhaskins left a comment

Choose a reason for hiding this comment

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

I’ve wanted a nice way to group these and share common nodes. This also makes sure the APIs for things like leftBrace are consistently named.

LGTM modulo nitpicks

from Child import Child


class TRAIT(object):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nitpick: Trait in title case, please.

@nkcsgexi
Copy link
Member Author

@swift-ci please smoke test



TRAITS = [
Trait('DeclGroup',
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 traits should also be postfixed with "Syntax".

Could you make this DeclGroupSyntax?
Or make them DeclGroup and Braced, and postfix in the client?

public protocol ${trait.trait_name}Syntax {

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I will rename this to DeclGroupSyntax.

@nkcsgexi
Copy link
Member Author

@swift-ci please smoke test

Copy link
Member

@rintaro rintaro left a comment

Choose a reason for hiding this comment

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

Thanks!

@nkcsgexi nkcsgexi merged commit 2b61d4e into apple:master Feb 16, 2018
@nkcsgexi nkcsgexi deleted the swift-syntax-traits branch February 16, 2018 00:41
maldahleh pushed a commit to maldahleh/swift that referenced this pull request Oct 26, 2020
…w abstract access to popular child kinds. NFC (apple#14668)

Swift syntax APIs lack an abstract way of accessing children. The client has to
down-cast a syntax node to the leaf type to access any of its children. However,
some children are common among different syntax kinds, e.g.
DeclAttributeSyntax and DeclMembers. We should allow an abstract way to
access and modify them, so that clients can avoid logic duplication.

This patch adds a mechanism to define new traits and specify satisfied
traits in specific syntax nodes. A trait is a set of common children
and implemented in Swift as a protocol for syntax nodes to conform to.
As a proof-of-concept, we added two traits for now including DeclGroupSyntax
and BracedSyntax.

Resolves: SR-6931 and SR-6916
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.

None yet

3 participants