File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -68,6 +68,21 @@ extension EnumCaseParameterSyntax {
68
68
}
69
69
}
70
70
71
+ extension FloatLiteralExprSyntax {
72
+ /// A computed property representing the floating-point value parsed from the associated `literal.text` property.
73
+ ///
74
+ /// - Returns: A double value parsed from the associated`literal.text`, or `nil` if the text cannot be parsed as a double.
75
+ public var representedLiteralValue : Double ? {
76
+ guard !hasError else { return nil }
77
+
78
+ let floatingDigitsWithoutUnderscores = literal. text. filter {
79
+ $0 != " _ "
80
+ }
81
+
82
+ return Double ( floatingDigitsWithoutUnderscores)
83
+ }
84
+ }
85
+
71
86
extension IntegerLiteralExprSyntax {
72
87
public enum Radix {
73
88
case binary
Original file line number Diff line number Diff line change @@ -195,4 +195,23 @@ class SyntaxTests: XCTestCase {
195
195
XCTAssertEqual ( expr. representedLiteralValue, expected, line: line)
196
196
}
197
197
}
198
+
199
+ func testFloatLiteralExprSyntax( ) {
200
+ let testCases : [ UInt : ( String , Double ? ) ] = [
201
+ #line: ( " 2 " , 2 ) ,
202
+ #line: ( " 2_00_00.001 " , 2_00_00.001 ) ,
203
+ #line: ( " 5.3_8 " , 5.3_8 ) ,
204
+ #line: ( " 12e3 " , 12000.0 ) ,
205
+ #line: ( " 32E1 " , 320.0 ) ,
206
+ #line: ( " 0xdEFACE.C0FFEEp+1 " , 0xdEFACE . C0FFEEp+ 1 ) ,
207
+ #line: ( " 0xaffab1e.e1fP-2 " , 0xaffab1e . e1fP- 2 ) ,
208
+ #line: ( " 🥥 " , nil ) ,
209
+ ]
210
+
211
+ for (line, testCase) in testCases {
212
+ let ( value, expected) = testCase
213
+ let expr = FloatLiteralExprSyntax ( literal: . floatLiteral( value) )
214
+ XCTAssertEqual ( expr. representedLiteralValue, expected, line: line)
215
+ }
216
+ }
198
217
}
You can’t perform that action at this time.
0 commit comments