-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Conversation
…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
@swift-ci please smoke test |
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’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
utils/gyb_syntax_support/Traits.py
Outdated
from Child import Child | ||
|
||
|
||
class TRAIT(object): |
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.
Nitpick: Trait
in title case, please.
@swift-ci please smoke test |
utils/gyb_syntax_support/Traits.py
Outdated
|
||
|
||
TRAITS = [ | ||
Trait('DeclGroup', |
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 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 {
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.
Sure, I will rename this to DeclGroupSyntax
.
@swift-ci please smoke test |
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.
Thanks!
…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
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