mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
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
42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
from Child import Child
|
|
from Node import Node # noqa: I201
|
|
|
|
COMMON_NODES = [
|
|
Node('Decl', kind='Syntax'),
|
|
Node('Expr', kind='Syntax'),
|
|
Node('Stmt', kind='Syntax'),
|
|
Node('Type', kind='Syntax'),
|
|
Node('Pattern', kind='Syntax'),
|
|
Node('UnknownDecl', kind='Decl'),
|
|
Node('UnknownExpr', kind='Expr'),
|
|
Node('UnknownStmt', kind='Stmt'),
|
|
Node('UnknownType', kind='Type'),
|
|
Node('UnknownPattern', kind='Pattern'),
|
|
|
|
# code-block-item = (decl | stmt | expr) ';'?
|
|
Node('CodeBlockItem', kind='Syntax',
|
|
children=[
|
|
Child('Item', kind='Syntax',
|
|
node_choices=[
|
|
Child('Decl', kind='Decl'),
|
|
Child('Stmt', kind='Stmt'),
|
|
Child('Expr', kind='Expr'),
|
|
]),
|
|
Child('Semicolon', kind='SemicolonToken',
|
|
is_optional=True),
|
|
]),
|
|
|
|
# code-block-item-list -> code-block-item code-block-item-list?
|
|
Node('CodeBlockItemList', kind='SyntaxCollection',
|
|
element='CodeBlockItem'),
|
|
|
|
# code-block -> '{' stmt-list '}'
|
|
Node('CodeBlock', kind='Syntax',
|
|
traits=['BracedSyntax'],
|
|
children=[
|
|
Child('LeftBrace', kind='LeftBraceToken'),
|
|
Child('Statements', kind='CodeBlockItemList'),
|
|
Child('RightBrace', kind='RightBraceToken'),
|
|
]),
|
|
]
|