Skip to content

Commit 075708a

Browse files
committed
Add convenience initializers for simple expr values
1 parent 1888022 commit 075708a

12 files changed

+641
-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: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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_as_child = False
28+
% for child in node.children:
29+
% child_node = NODE_MAP.get(child.syntax_kind)
30+
% if child_node and child_node.is_syntax_collection():
31+
% has_syntax_collection_child = True
32+
% end
33+
% if SYNTAX_TOKEN_MAP.get(child.syntax_kind) or child.syntax_kind is "IdentifierToken":
34+
% has_token_as_child = True
35+
% end
36+
% end
37+
% if not has_syntax_collection_child and has_token_as_child:
38+
extension ${node.syntax_kind} {
39+
public init(
40+
% init_parameters = []
41+
% for child in node.children:
42+
% child_node = NODE_MAP.get(child.syntax_kind)
43+
% token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
44+
% if child.syntax_kind is "IdentifierToken" or (token and not token.text):
45+
% # Allow initializing identifier or a token without a text with String value
46+
% param_type = "String?" if child.is_optional else "String"
47+
% init_parameters.append("%s: %s" % (child.swift_name, param_type))
48+
% elif not token or child.is_optional:
49+
% # When there is no token or the child is optional, add parameter in init
50+
% param_type = syntax_buildable_child_type(child.type_name, child.syntax_kind, child.is_token(), child.is_optional)
51+
% default_value = ""
52+
% if child.is_optional:
53+
% default_value = " = nil"
54+
% init_parameters.append("%s: %s%s" % (child.swift_name, param_type, default_value))
55+
% end
56+
% end
57+
${',\n '.join(init_parameters)}
58+
) {
59+
self.init(
60+
% init_parameters = []
61+
% for child in node.children:
62+
% child_node = NODE_MAP.get(child.syntax_kind)
63+
% token = SYNTAX_TOKEN_MAP.get(child.syntax_kind)
64+
% if child.syntax_kind is "IdentifierToken":
65+
% if child.is_optional:
66+
% init_parameters.append("%s: %s.map({ SyntaxFactory.makeIdentifier($0) })" % (child.swift_name, child.swift_name))
67+
% else:
68+
% init_parameters.append("%s: SyntaxFactory.makeIdentifier(%s)" % (child.swift_name, child.swift_name))
69+
% end
70+
% elif token:
71+
% if not token.text:
72+
% if child.is_optional:
73+
% init_parameters.append("%s: %s.map(Tokens.%s)" % (child.swift_name, lowercase_first_word(child.swift_name), lowercase_first_word(token.name)))
74+
% else:
75+
% init_parameters.append("%s: Tokens.%s(%s)" % (child.swift_name, lowercase_first_word(token.name), child.swift_name))
76+
% end
77+
% else:
78+
% init_parameters.append("%s: Tokens.%s" % (child.swift_name, lowercase_first_word(token.name)))
79+
% end
80+
% else:
81+
% init_parameters.append("%s: %s" % (child.swift_name, child.swift_name))
82+
% end
83+
% end
84+
${',\n '.join(init_parameters)}
85+
)
86+
}
87+
}
88+
89+
% end
90+
% end
91+
% 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)