-
Notifications
You must be signed in to change notification settings - Fork 433
/
Copy pathConvenience.swift
194 lines (176 loc) · 6.14 KB
/
Convenience.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
extension ClosureCaptureSyntax {
/// Creates a ``ClosureCaptureSyntax`` with a `name`, and automatically adds an `equal` token to it since the name is non-optional.
///
/// - SeeAlso: ``ClosureCaptureSyntax/init(leadingTrivia:_:specifier:_:name:_:equal:_:expression:_:trailingComma:_:trailingTrivia:)``.
///
public init(
leadingTrivia: Trivia? = nil,
specifier: ClosureCaptureSpecifierSyntax? = nil,
name: TokenSyntax,
equal: TokenSyntax = TokenSyntax.equalToken(),
expression: some ExprSyntaxProtocol,
trailingComma: TokenSyntax? = nil,
trailingTrivia: Trivia? = nil
) {
self.init(
leadingTrivia: leadingTrivia,
specifier: specifier,
name: name as TokenSyntax?,
equal: equal,
expression: expression,
trailingComma: trailingComma,
trailingTrivia: trailingTrivia
)
}
}
extension EnumCaseParameterSyntax {
/// Creates an ``EnumCaseParameterSyntax`` with a `firstName`, and automatically adds a `colon` to it.
///
/// - SeeAlso: For more information on the arguments, see ``EnumCaseParameterSyntax/init(leadingTrivia:_:modifiers:_:firstName:_:secondName:_:colon:_:type:_:defaultArgument:_:trailingComma:_:trailingTrivia:)``
///
public init(
leadingTrivia: Trivia? = nil,
modifiers: DeclModifierListSyntax = [],
firstName: TokenSyntax,
secondName: TokenSyntax? = nil,
colon: TokenSyntax = TokenSyntax.colonToken(),
type: some TypeSyntaxProtocol,
defaultValue: InitializerClauseSyntax? = nil,
trailingComma: TokenSyntax? = nil,
trailingTrivia: Trivia? = nil
) {
self.init(
leadingTrivia: leadingTrivia,
modifiers: modifiers,
firstName: firstName as TokenSyntax?,
secondName: secondName,
colon: colon,
type: type,
defaultValue: defaultValue,
trailingComma: trailingComma,
trailingTrivia: trailingTrivia
)
}
}
extension FloatLiteralExprSyntax {
/// A computed property representing the floating-point value parsed from the associated `literal.text` property.
///
/// - Returns: A double value parsed from the associated`literal.text`, or `nil` if the text cannot be parsed as a double.
public var representedLiteralValue: Double? {
guard !hasError else { return nil }
let floatingDigitsWithoutUnderscores = literal.text.filter {
$0 != "_"
}
return Double(floatingDigitsWithoutUnderscores)
}
}
extension IntegerLiteralExprSyntax {
public enum Radix: Sendable {
case binary
case octal
case decimal
case hex
public var size: Int {
switch self {
case .binary: return 2
case .octal: return 8
case .decimal: return 10
case .hex: return 16
}
}
/// The prefix that is used to express an integer literal with this
/// radix in Swift source code, e.g., "0x" for hexadecimal.
public var literalPrefix: String {
switch self {
case .binary: return "0b"
case .octal: return "0o"
case .hex: return "0x"
case .decimal: return ""
}
}
fileprivate var offset: Int {
switch self {
case .binary, .hex, .octal:
return 2
case .decimal:
return 0
}
}
}
public var radix: Radix {
let text = self.literal.text
if text.starts(with: "0b") {
return .binary
} else if text.starts(with: "0o") {
return .octal
} else if text.starts(with: "0x") {
return .hex
} else {
return .decimal
}
}
/// A computed property representing the integer value parsed from the associated `literal.text` property, considering the specified radix.
///
/// - Returns: An integer value parsed from the associated `literal.text`, or `nil` if the text cannot be parsed as an integer.
public var representedLiteralValue: Int? {
guard !hasError else { return nil }
let text = literal.text
let radix = self.radix
let digitsStartIndex = text.index(text.startIndex, offsetBy: radix.offset)
let textWithoutPrefix = text.suffix(from: digitsStartIndex)
let textWithoutPrefixOrUnderscores = textWithoutPrefix.filter {
$0 != "_"
}
return Int(textWithoutPrefixOrUnderscores, radix: radix.size)
}
}
extension MemberAccessExprSyntax {
/// Creates a new ``MemberAccessExprSyntax`` where the accessed member is represented by
/// an identifier without specifying argument labels.
///
/// A member access can specify function argument labels, which is required
/// when the name would be ambiguous otherwise. For example, given multiple overloads
/// ```swift
/// struct Person {
/// func consume(drink: Drink) {}
/// func consume(food: Food) {}
/// }
/// ```
///
/// `consume(drink:)` is required to explicitly reference the consume function
/// that takes a drink.
///
/// Given how common it is to not need the argument names, this initializer is
/// provided as a convenience to avoid having to create a ``DeclReferenceExprSyntax``
/// for the member name.
public init(
leadingTrivia: Trivia? = nil,
base: (some ExprSyntaxProtocol)? = ExprSyntax?.none,
period: TokenSyntax = .periodToken(),
name: TokenSyntax,
trailingTrivia: Trivia? = nil
) {
self.init(
leadingTrivia: leadingTrivia,
base: base,
period: period,
declName: DeclReferenceExprSyntax(baseName: name),
trailingTrivia: trailingTrivia
)
}
}
//==========================================================================//
// IMPORTANT: If you are tempted to add an extension here, please insert //
// it in alphabetical order above //
//==========================================================================//