Skip to content

Commit 78320a0

Browse files
committed
Unified: Handle Swift's macro expansion expr
1 parent a5a0a11 commit 78320a0

3 files changed

Lines changed: 70 additions & 8 deletions

File tree

‎unified/extractor/src/languages/swift/swift.rs‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,31 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
712712
=>
713713
(argument name_node: (identifier #{lbl})? value: {val})
714714
),
715+
// An additional labeled trailing closure, such as the `c: { ... }` in
716+
// `foo { 42 } c: { ... }`, is turned into an argument to the function.
717+
rule!(
718+
(multipleTrailingClosureElement label: @@label closure: @closure)
719+
=>
720+
(argument name_node: (identifier #{label}) value: {closure})
721+
),
722+
rule!(
723+
(macroExpansionExpr
724+
macroName: @@name
725+
genericArgumentClause: (genericArgumentClause arguments: (genericArgument argument: @type_args)*)?
726+
arguments: _* @args
727+
trailingClosure: _? @trailing
728+
additionalTrailingClosures: _* @additional)
729+
=>
730+
macro_call_expr {
731+
let mut arguments = args;
732+
arguments.extend(trailing.into_iter().map(|trailing| tree!((argument value: {trailing}))));
733+
arguments.extend(additional);
734+
tree!((macro_call_expr
735+
macro_name_node: (identifier #{name})
736+
type_argument: {type_args}
737+
argument: {arguments}))
738+
}
739+
),
715740
// Member access (`list.append`). The `declName` is itself a
716741
// `declReferenceExpr`; pull its `baseName` out as the member identifier.
717742
// A leading-dot access (`.foo`) has no explicit base — the base is an
@@ -1196,10 +1221,6 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
11961221
}
11971222
}
11981223
),
1199-
// Selector expression: `#selector(inner)` -- not yet supported
1200-
// (swift-syntax represents `#selector`/`#keyPath` and other macro
1201-
// expansions uniformly as a `macroExpansionExpr`).
1202-
rule!((macroExpansionExpr) => (unsupported_node)),
12031224
// A nominal type's `inheritanceClause` (`: Base, Proto`) becomes a list
12041225
// of `base_type`s, one per inherited type. Each declaration keyword
12051226
// gets its own rule; the bodies are identical but for the keyword.

‎unified/extractor/tests/corpus/swift/expressions/freestanding-macro-expansions.output‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,45 @@ top_level source="⟨body⟩"
120120
body:
121121
block source="⟨stmt⟩\n\n⟨stmt⟩\n\n⟨stmt⟩"
122122
stmt:
123-
unsupported_node "#expect(value == 42)" source="#expect(value == 42)"
124-
unsupported_node "#custom<Int>(value) {\n value\n} completion: {\n value\n}" source="#custom<Int>(value) {\n value\n} completion: {\n value\n}"
125-
unsupported_node "#Preview {\n let previewValue = 42\n previewValue\n}" source="#Preview {\n let previewValue = 42\n previewValue\n}"
123+
macro_call_expr source="#⟨macro_name_node⟩(⟨argument⟩)"
124+
macro_name_node: identifier "expect" source="expect"
125+
argument:
126+
argument source="⟨value⟩"
127+
value:
128+
binary_expr source="⟨left⟩ ⟨operator⟩ ⟨right⟩"
129+
left: identifier "value" source="value"
130+
operator: infix_operator "==" source="=="
131+
right: int_literal "42" source="42"
132+
macro_call_expr source="#⟨macro_name_node⟩<⟨type_argument⟩>(⟨argument⟩) ⟨argument⟩ ⟨argument⟩"
133+
macro_name_node: identifier "custom" source="custom"
134+
type_argument: identifier "Int" source="Int"
135+
argument:
136+
argument source="⟨value⟩"
137+
value: identifier "value" source="value"
138+
argument source="⟨value⟩"
139+
value:
140+
function_expr source="{\n ⟨body⟩\n}"
141+
body:
142+
block source="⟨stmt⟩"
143+
stmt: identifier "value" source="value"
144+
argument source="⟨name_node⟩: ⟨value⟩"
145+
name_node: identifier "completion" source="completion"
146+
value:
147+
function_expr source="{\n ⟨body⟩\n}"
148+
body:
149+
block source="⟨stmt⟩"
150+
stmt: identifier "value" source="value"
151+
macro_call_expr source="#⟨macro_name_node⟩ ⟨argument⟩"
152+
macro_name_node: identifier "Preview" source="Preview"
153+
argument:
154+
argument source="⟨value⟩"
155+
value:
156+
function_expr source="{\n ⟨body⟩\n}"
157+
body:
158+
block source="⟨stmt⟩\n ⟨stmt⟩"
159+
stmt:
160+
variable_declaration source="⟨modifier⟩ ⟨pattern⟩ = ⟨value⟩"
161+
modifier: modifier "let" source="let"
162+
pattern: identifier "previewValue" source="previewValue"
163+
value: int_literal "42" source="42"
164+
identifier "previewValue" source="previewValue"

‎unified/extractor/tests/corpus/swift/literals/line-magic-literal.output‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ top_level source="⟨body⟩"
3535
variable_declaration source="⟨modifier⟩ ⟨pattern⟩ = ⟨value⟩"
3636
modifier: modifier "let" source="let"
3737
pattern: identifier "currentLine" source="currentLine"
38-
value: unsupported_node "#line" source="#line"
38+
value:
39+
macro_call_expr source="#⟨macro_name_node⟩"
40+
macro_name_node: identifier "line" source="line"

0 commit comments

Comments
 (0)