Skip to content

Commit a8fb3cc

Browse files
committed
wip
1 parent 895f67b commit a8fb3cc

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2025 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 LabeledExprListSyntax {
16+
public func matchArguments(
17+
against parameters: FunctionParameterClauseSyntax,
18+
isSubscript: Bool = false
19+
) -> MatchedArguments {
20+
var expressions: [[ExprSyntax]] = []
21+
22+
var iterator = makeIterator()
23+
24+
var elements: [ExprSyntax] = []
25+
while let element = iterator.next() {
26+
elements.append(element.expression)
27+
28+
guard element.colon != nil else {
29+
continue
30+
}
31+
32+
expressions.append(elements)
33+
}
34+
35+
var result: [String: [ExprSyntax]] = [:]
36+
for (expressions, parameter) in zip(expressions, parameters.parameters) {
37+
let key = parameter.name
38+
39+
result[key] = expressions
40+
}
41+
42+
return MatchedArguments(matchedArguments: result)
43+
}
44+
}
45+
46+
public struct MatchedArguments {
47+
let matchedArguments: [String: [ExprSyntax]]
48+
49+
public func argument(for internalLabel: String) -> [ExprSyntax]? {
50+
return matchedArguments[internalLabel]
51+
}
52+
}
53+
54+
extension FunctionParameterSyntax {
55+
fileprivate var name: String {
56+
return secondName?.text ?? firstName.text
57+
}
58+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 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 SwiftDiagnostics
14+
import SwiftSyntax
15+
import SwiftSyntaxMacroExpansion
16+
import SwiftSyntaxMacros
17+
import SwiftSyntaxMacrosTestSupport
18+
import XCTest
19+
import _SwiftSyntaxTestSupport
20+
21+
final class MatchedArgumentsTests: XCTestCase {
22+
func testWithSingleName() {
23+
let arg = LabeledExprListSyntax {
24+
LabeledExprSyntax(label: "x", expression: ExprSyntax("2"))
25+
}
26+
27+
let arguments = arg.matchArguments(against: "(x: Int)")
28+
29+
XCTAssertEqual(arguments.argument(for: "x")?.first?.description, "2")
30+
}
31+
32+
func testWithTwoNames() {
33+
let arg = LabeledExprListSyntax {
34+
LabeledExprSyntax(
35+
label: "x",
36+
expression: ExprSyntax("2")
37+
)
38+
}
39+
40+
let arguments = arg.matchArguments(against: "(x y: Int)")
41+
42+
XCTAssertEqual(arguments.argument(for: "y")?.first?.description, "2")
43+
XCTAssertNil(arguments.argument(for: "x"))
44+
}
45+
46+
func testWithMultipleArguments() {
47+
let arg = LabeledExprListSyntax {
48+
LabeledExprSyntax(
49+
label: "x",
50+
expression: ExprSyntax("2")
51+
)
52+
53+
LabeledExprSyntax(
54+
label: "x",
55+
expression: ExprSyntax("3")
56+
)
57+
}
58+
59+
let arguments = arg.matchArguments(against: "(x y: Int, x z: Int)")
60+
61+
XCTAssertEqual(arguments.argument(for: "y")?.first?.description, "2")
62+
XCTAssertEqual(arguments.argument(for: "z")?.first?.description, "3")
63+
}
64+
65+
func testWithVariadicArguments() {
66+
let arg = LabeledExprListSyntax {
67+
LabeledExprSyntax(
68+
label: "x",
69+
expression: ExprSyntax("2")
70+
)
71+
72+
LabeledExprSyntax(
73+
expression: ExprSyntax("3")
74+
)
75+
}
76+
77+
let arguments = arg.matchArguments(against: "(x: Int...)")
78+
79+
XCTAssertEqual(arguments.argument(for: "x")?.first?.description, "2")
80+
XCTAssertEqual(arguments.argument(for: "x")?.last?.description, "3")
81+
}
82+
83+
func testWithSubscript() {
84+
let arg = LabeledExprListSyntax {
85+
LabeledExprSyntax(
86+
label: "x",
87+
expression: ExprSyntax("2")
88+
)
89+
}
90+
91+
let arguments = arg.matchArguments(against: "(x: Int)", isSubscript: true)
92+
93+
XCTAssertEqual(arguments.argument(for: "x")?.first?.description, "2")
94+
}
95+
96+
func testWithMultipleArgumentsWithNoLabel() {
97+
let arg = LabeledExprListSyntax {
98+
LabeledExprSyntax(
99+
expression: ExprSyntax("2")
100+
)
101+
102+
LabeledExprSyntax(
103+
expression: ExprSyntax("3")
104+
)
105+
}
106+
107+
let arguments = arg.matchArguments(against: "(_ x: Int, _ y: Int)")
108+
109+
XCTAssertEqual(arguments.argument(for: "y")?.first?.description, "2")
110+
XCTAssertEqual(arguments.argument(for: "z")?.first?.description, "3")
111+
}
112+
}

0 commit comments

Comments
 (0)