Skip to content

Commit 3dc69f8

Browse files
committed
Add convenience initializers for simple expr values
1 parent 1888022 commit 3dc69f8

12 files changed

+645
-3
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
15+
extension BooleanLiteralExpr {
16+
public init(_ value: Bool) {
17+
self.init(booleanLiteral: value ? Tokens.true : Tokens.false)
18+
}
19+
}
20+
21+
extension BooleanLiteralExpr: ExpressibleByBooleanLiteral {
22+
public init(booleanLiteral value: Bool) {
23+
self.init(value)
24+
}
25+
}

Sources/SwiftSyntaxBuilder/BuildablesConvenienceInitializers.swift.gyb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# -*- mode: Swift -*-
66
# Ignore the following admonition it applies to the resulting .swift file only
77
}%
8-
//// Automatically Generated From DeclBuildables.swift.gyb.
8+
//// Automatically Generated From BuildablesConvenienceInitializers.swift.gyb.
99
//// Do Not Edit Directly!
1010
//===----------------------------------------------------------------------===//
1111
//
@@ -75,7 +75,7 @@ extension ${node.syntax_kind} {
7575
% if child.is_optional:
7676
% init_parameters.append("%s: %s.map(Tokens.%s)" % (child.swift_name, child.swift_name, lowercase_first_word(token.name)))
7777
% else:
78-
% init_parameters.append("%s: Tokens.%s(%s)" % (child.swift_name, token.name, child.swift_name))
78+
% init_parameters.append("%s: Tokens.%s(%s)" % (child.swift_name, lowercase_first_word(token.name), child.swift_name))
7979
% end
8080
% else:
8181
% init_parameters.append("%s: %s" % (child.swift_name, child.swift_name))
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
%{
2+
from gyb_syntax_support import *
3+
from gyb_syntax_support.kinds import SYNTAX_BASE_KINDS, lowercase_first_word, syntax_buildable_child_type
4+
NODE_MAP = create_node_map()
5+
# -*- mode: Swift -*-
6+
# Ignore the following admonition it applies to the resulting .swift file only
7+
}%
8+
//// Automatically Generated From ExprConvenienceInitializers.swift.gyb.
9+
//// Do Not Edit Directly!
10+
//===----------------------------------------------------------------------===//
11+
//
12+
// This source file is part of the Swift.org open source project
13+
//
14+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
15+
// Licensed under Apache License v2.0 with Runtime Library Exception
16+
//
17+
// See https://swift.org/LICENSE.txt for license information
18+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
19+
//
20+
//===----------------------------------------------------------------------===//
21+
22+
import SwiftSyntax
23+
24+
% for node in SYNTAX_NODES:
25+
% if node.is_buildable() and node.base_kind is 'Expr':
26+
% has_syntax_collection_child = False
27+
% has_token_choices_child = False
28+
% has_token_as_child = False
29+
% for child in node.children:
30+
% child_node = NODE_MAP.get(child.syntax_kind)
31+
% if child_node and child_node.is_syntax_collection():
32+
% has_syntax_collection_child = True
33+
% end
34+
% if child.token_choices is not []:
35+
% has_token_choices_child = True
36+
% end
37+
% if SYNTAX_TOKEN_MAP.get(child.syntax_kind) or child.syntax_kind is "IdentifierToken":
38+
% has_token_as_child = True
39+
% end
40+
% end
41+
% if not has_syntax_collection_child and has_token_as_child:
42+
extension ${node.syntax_kind} {
43+
public init(
44+
% init_parameters = []
45+
% for child in node.children:
46+
% child_node = NODE_MAP.get(child.syntax_kind)
47+
% token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
48+
% if child.syntax_kind is "IdentifierToken" or (token and not token.text):
49+
% # Allow initializing identifier or a token without a text with String value
50+
% param_type = "String?" if child.is_optional else "String"
51+
% init_parameters.append("%s: %s" % (child.swift_name, param_type))
52+
% elif not token or child.is_optional:
53+
% # When there is no token or the child is optional, add parameter in init
54+
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
55+
% default_value = ""
56+
% if child.is_optional:
57+
% default_value = " = nil"
58+
% init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
59+
% end
60+
% end
61+
${',\n '.join(init_parameters)}
62+
) {
63+
self.init(
64+
% init_parameters = []
65+
% for child in node.children:
66+
% child_node = NODE_MAP.get(child.syntax_kind)
67+
% token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
68+
% if child.syntax_kind is "IdentifierToken":
69+
% if child.is_optional:
70+
% init_parameters.append("%s: %s.map({ SyntaxFactory.makeIdentifier($0) })" % (child.swift_name, child.swift_name))
71+
% else:
72+
% init_parameters.append("%s: SyntaxFactory.makeIdentifier(%s)" % (child.swift_name, child.swift_name))
73+
% end
74+
% elif token:
75+
% if not token.text:
76+
% if child.is_optional:
77+
% init_parameters.append("%s: %s.map(Tokens.%s)" % (child.swift_name, lowercase_first_word(child.swift_name), lowercase_first_word(token.name)))
78+
% else:
79+
% init_parameters.append("%s: Tokens.%s(%s)" % (child.swift_name, lowercase_first_word(token.name), child.swift_name))
80+
% end
81+
% else:
82+
% init_parameters.append("%s: Tokens.%s" % (child.swift_name, lowercase_first_word(token.name)))
83+
% end
84+
% else:
85+
% init_parameters.append("%s: %s" % (child.swift_name, child.swift_name))
86+
% end
87+
% end
88+
${',\n '.join(init_parameters)}
89+
)
90+
}
91+
}
92+
93+
% end
94+
% end
95+
% end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
15+
extension FloatLiteralExpr {
16+
public init(_ value: Float) {
17+
self.init(floatingDigits: String(value))
18+
}
19+
}
20+
21+
extension FloatLiteralExpr: ExpressibleByFloatLiteral {
22+
public init(floatLiteral value: Float) {
23+
self.init(value)
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
15+
extension IntegerLiteralExpr {
16+
public init(_ value: Int) {
17+
self.init(digits: String(value))
18+
}
19+
}
20+
21+
extension IntegerLiteralExpr: ExpressibleByIntegerLiteral {
22+
public init(integerLiteral value: Int) {
23+
self.init(value)
24+
}
25+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
15+
extension StringLiteralExpr {
16+
public init(_ value: String) {
17+
let content = SyntaxFactory.makeToken(TokenKind.stringSegment(value), presence: .present)
18+
let segment = StringSegment(content: content)
19+
let segments = StringLiteralSegments([segment])
20+
21+
self.init(openQuote: Tokens.stringQuote,
22+
segments: segments,
23+
closeQuote: Tokens.stringQuote)
24+
}
25+
}
26+
27+
extension StringLiteralExpr: ExpressibleByStringLiteral {
28+
public init(stringLiteral value: String) {
29+
self.init(value)
30+
}
31+
}

Sources/SwiftSyntaxBuilder/gyb_generated/BuildablesConvenienceInitializers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//// Automatically Generated From DeclBuildables.swift.gyb.
1+
//// Automatically Generated From BuildablesConvenienceInitializers.swift.gyb.
22
//// Do Not Edit Directly!
33
//===----------------------------------------------------------------------===//
44
//

0 commit comments

Comments
 (0)