Skip to content

SwiftSyntax: Add a trait for those statement nodes with code block as body. #14726

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
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions tools/SwiftSyntax/SyntaxNodes.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,6 @@ public struct UnknownSyntax: _SyntaxBase {
}
}

% for trait in TRAITS:
public protocol ${trait.trait_name} {
% for child in trait.children:
% ret_type = child.type_name
% if child.is_optional:
% ret_type += '?'
% end
var ${child.swift_name}: ${ret_type} { get }
func with${child.name}(_ newChild: ${child.type_name}?) -> Self
% end
}
% end

% for node in SYNTAX_NODES:
% base_type = node.base_type
% if node.is_base():
Expand All @@ -75,12 +62,7 @@ public protocol ${node.name}: Syntax {}
% elif node.collection_element:
% pass
% else:
% traits_list = ""
% if node.traits:
% traits_list = ", ".join(node.traits)
% traits_list = traits_list + ", "
% end
public struct ${node.name}: ${traits_list} ${base_type}, _SyntaxBase, Hashable {
public struct ${node.name}: ${base_type}, _SyntaxBase, Hashable {
% if node.children:
enum Cursor: Int {
% for child in node.children:
Expand Down Expand Up @@ -192,6 +174,31 @@ public struct ${node.name}: ${traits_list} ${base_type}, _SyntaxBase, Hashable {
% end
% end

% for trait in TRAITS:
public protocol ${trait.trait_name}Syntax {
% for child in trait.children:
% ret_type = child.type_name
% if child.is_optional:
% ret_type += '?'
% end
var ${child.swift_name}: ${ret_type} { get }
func with${child.name}(_ newChild: ${child.type_name}?) -> Self
% end
}
% end

% for node in SYNTAX_NODES:
% base_type = node.base_type
% if node.is_base():
% pass
% elif node.collection_element:
% pass
% elif node.traits:
% traits_list = ", ".join(trait + 'Syntax' for trait in node.traits)
extension ${node.name}: ${traits_list} {}
% end
% end

/// MARK: Convenience methods

extension StructDeclSyntax {
Expand Down
2 changes: 1 addition & 1 deletion utils/gyb_syntax_support/CommonNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

# code-block -> '{' stmt-list '}'
Node('CodeBlock', kind='Syntax',
traits=['BracedSyntax'],
traits=['Braced'],
children=[
Child('LeftBrace', kind='LeftBraceToken'),
Child('Statements', kind='CodeBlockItemList'),
Expand Down
18 changes: 9 additions & 9 deletions utils/gyb_syntax_support/DeclNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# typealias-name generic-parameter-clause?
# type-assignment
# typealias-name -> identifier
Node('TypealiasDecl', kind='Decl', traits=['IdentifiedDeclSyntax'],
Node('TypealiasDecl', kind='Decl', traits=['IdentifiedDecl'],
children=[
Child('Attributes', kind='AttributeList',
is_optional=True),
Expand All @@ -36,7 +36,7 @@
# inheritance-clause? type-assignment?
# generic-where-clause?
# associatedtype-name -> identifier
Node('AssociatedtypeDecl', kind='Decl', traits=['IdentifiedDeclSyntax'],
Node('AssociatedtypeDecl', kind='Decl', traits=['IdentifiedDecl'],
children=[
Child('Attributes', kind='AttributeList',
is_optional=True),
Expand Down Expand Up @@ -158,7 +158,7 @@
# '{' class-members '}'
# class-name -> identifier
Node('ClassDecl', kind='Decl',
traits=['DeclGroupSyntax', 'IdentifiedDeclSyntax'],
traits=['DeclGroup', 'IdentifiedDecl'],
children=[
Child('Attributes', kind='AttributeList',
is_optional=True),
Expand All @@ -183,7 +183,7 @@
# '{' struct-members '}'
# struct-name -> identifier
Node('StructDecl', kind='Decl',
traits=['DeclGroupSyntax', 'IdentifiedDeclSyntax'],
traits=['DeclGroup', 'IdentifiedDecl'],
children=[
Child('Attributes', kind='AttributeList',
is_optional=True),
Expand All @@ -201,7 +201,7 @@
]),

Node('ProtocolDecl', kind='Decl',
traits=['DeclGroupSyntax', 'IdentifiedDeclSyntax'],
traits=['DeclGroup', 'IdentifiedDecl'],
children=[
Child('Attributes', kind='AttributeList',
is_optional=True),
Expand All @@ -222,7 +222,7 @@
# generic-where-clause?
# '{' extension-members '}'
# extension-name -> identifier
Node('ExtensionDecl', kind='Decl', traits=['DeclGroupSyntax'],
Node('ExtensionDecl', kind='Decl', traits=['DeclGroup'],
children=[
Child('Attributes', kind='AttributeList',
is_optional=True),
Expand All @@ -237,7 +237,7 @@
Child('Members', kind='MemberDeclBlock'),
]),

Node('MemberDeclBlock', kind='Syntax', traits=['BracedSyntax'],
Node('MemberDeclBlock', kind='Syntax', traits=['Braced'],
children=[
Child('LeftBrace', kind='LeftBraceToken'),
Child('Members', kind='DeclList'),
Expand Down Expand Up @@ -315,7 +315,7 @@
element='Syntax',
element_name='Modifier'),

Node('FunctionDecl', kind='Decl', traits=['IdentifiedDeclSyntax'],
Node('FunctionDecl', kind='Decl', traits=['IdentifiedDecl'],
children=[
Child('Attributes', kind='AttributeList',
is_optional=True),
Expand Down Expand Up @@ -467,7 +467,7 @@

Node('AccessorList', kind="SyntaxCollection", element='AccessorDecl'),

Node('AccessorBlock', kind="Syntax", traits=['BracedSyntax'],
Node('AccessorBlock', kind="Syntax", traits=['Braced'],
children=[
Child('LeftBrace', kind='LeftBraceToken'),
Child('AccessorListOrStmtList', kind='Syntax',
Expand Down
2 changes: 1 addition & 1 deletion utils/gyb_syntax_support/ExprNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@
]),

Node('ClosureExpr', kind='Expr',
traits=['BracedSyntax'],
traits=['Braced'],
children=[
Child('LeftBrace', kind='LeftBraceToken'),
Child('Signature', kind='ClosureSignature', is_optional=True),
Expand Down
10 changes: 9 additions & 1 deletion utils/gyb_syntax_support/StmtNodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

# while-stmt -> label? ':'? 'while' condition-list code-block ';'?
Node('WhileStmt', kind='Stmt',
traits=['WithCodeBlock'],
children=[
Child('LabelName', kind='IdentifierToken',
is_optional=True),
Expand All @@ -24,6 +25,7 @@

# defer-stmt -> 'defer' code-block ';'?
Node('DeferStmt', kind='Stmt',
traits=['WithCodeBlock'],
children=[
Child('DeferKeyword', kind='DeferToken'),
Child('Body', kind='CodeBlock'),
Expand All @@ -41,6 +43,7 @@

# repeat-while-stmt -> label? ':'? 'repeat' code-block 'while' expr ';'?
Node('RepeatWhileStmt', kind='Stmt',
traits=['WithCodeBlock'],
children=[
Child('LabelName', kind='IdentifierToken',
is_optional=True),
Expand All @@ -54,6 +57,7 @@

# guard-stmt -> 'guard' condition-list 'else' code-block ';'?
Node('GuardStmt', kind='Stmt',
traits=['WithCodeBlock'],
children=[
Child('GuardKeyword', kind='GuardToken'),
Child('Conditions', kind='ConditionElementList'),
Expand All @@ -70,6 +74,7 @@
# for-in-stmt -> label? ':'? 'for' 'case'? pattern 'in' expr 'where'?
# expr code-block ';'?
Node('ForInStmt', kind='Stmt',
traits=['WithCodeBlock'],
children=[
Child('LabelName', kind='IdentifierToken',
is_optional=True),
Expand All @@ -91,7 +96,7 @@
# switch-stmt -> identifier? ':'? 'switch' expr '{'
# switch-case-list '}' ';'?
Node('SwitchStmt', kind='Stmt',
traits=['BracedSyntax'],
traits=['Braced'],
children=[
Child('LabelName', kind='IdentifierToken',
is_optional=True),
Expand All @@ -110,6 +115,7 @@

# do-stmt -> identifier? ':'? 'do' code-block catch-clause-list ';'?
Node('DoStmt', kind='Stmt',
traits=['WithCodeBlock'],
children=[
Child('LabelName', kind='IdentifierToken',
is_optional=True),
Expand Down Expand Up @@ -212,6 +218,7 @@
# if-stmt -> identifier? ':'? 'if' condition-list code-block
# else-clause ';'?
Node('IfStmt', kind='Stmt',
traits=['WithCodeBlock'],
children=[
Child('LabelName', kind='IdentifierToken',
is_optional=True),
Expand All @@ -238,6 +245,7 @@

# else-clause -> 'else' code-block
Node('ElseBlock', kind='Syntax',
traits=['WithCodeBlock'],
children=[
Child('ElseKeyword', kind='ElseToken'),
Child('Body', kind='CodeBlock'),
Expand Down
11 changes: 8 additions & 3 deletions utils/gyb_syntax_support/Traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@ def __init__(self, trait_name, children):


TRAITS = [
Trait('DeclGroupSyntax',
Trait('DeclGroup',
children=[
Child('Attributes', kind='AttributeList', is_optional=True),
Child('AccessLevelModifier', kind='DeclModifier',
is_optional=True),
Child('Members', kind='MemberDeclBlock'),
]),

Trait('BracedSyntax',
Trait('Braced',
children=[
Child('LeftBrace', kind='LeftBraceToken'),
Child('RightBrace', kind='RightBraceToken'),
]),

Trait('IdentifiedDeclSyntax',
Trait('IdentifiedDecl',
children=[
Child('Identifier', kind='IdentifierToken'),
]),

Trait('WithCodeBlock',
children=[
Child('Body', kind='CodeBlock'),
]),
]