Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions unified/extractor/src/languages/swift/swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ fn make_or_pattern(
}
}

fn parameter_pattern(
ctx: &mut yeast::build::BuildCtx<'_, SwiftContext>,
name: yeast::Id,
) -> yeast::OutputId {
if ctx.source_text(name) == "_" {
tree!((ignore_pattern #{name}))
} else {
tree!((identifier #{name}))
}
}

/// Translate a multi-part identifier (for example `Foo.Bar.Baz`) into a
/// `member_access_expr` chain rooted at a `name_node` for the first
/// part. Panics on an empty input because the grammar's `_+` quantifier
Expand Down Expand Up @@ -669,7 +680,7 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
=>
(parameter
external_name_node: (identifier #{first})
pattern: (identifier #{second})
pattern: {parameter_pattern(&mut ctx, second)}
type: {ty}
default: {val})
),
Expand All @@ -681,7 +692,7 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
=>
(parameter
external_name_node: (identifier #{first}) // duplicate the parameter name
pattern: (identifier #{first})
pattern: {parameter_pattern(&mut ctx, first)}
type: {ty}
default: {val})
),
Expand Down Expand Up @@ -867,14 +878,14 @@ fn translation_rules() -> Vec<Rule<SwiftContext>> {
rule!(
(closureParameter firstName: @@name type: _? @ty)
=>
(parameter pattern: (identifier #{name}) type: {ty})
(parameter pattern: {parameter_pattern(&mut ctx, name)} type: {ty})
),
// A shorthand closure parameter (`x` in `{ x, y in … }`): a bare name
// with no parentheses and no type.
rule!(
(closureShorthandParameter name: @@name)
=>
(parameter pattern: (identifier #{name}))
(parameter pattern: {parameter_pattern(&mut ctx, name)})
),
// ---- Control flow ----
// An `if`/`else` expression. Conditions are joined via `and_chain`; the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ top_level source="⟨body⟩"
function_expr source="{ ⟨parameter⟩, ⟨parameter⟩ in\n\t⟨body⟩\n}"
parameter:
parameter source="⟨pattern⟩"
pattern: identifier "_" source="_"
pattern: ignore_pattern "_" source="_"
parameter source="⟨pattern⟩"
pattern: identifier "count" source="count"
body:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
func ignore(_: Int, _: String) {
}

func ignore(first _: Int, second _: String) {
}

let ignoreClosure: (Int, String) -> Void = { _, _ in
}

let ignoreTypedClosure = { (_: Int, _: String) in
}

---

sourceFile
endOfFileToken: endOfFile
statements:
codeBlockItem
item:
functionDecl
attributes:
body:
codeBlock
leftBrace: {
rightBrace: }
statements:
modifiers:
name: identifier "ignore"
signature:
functionSignature
parameterClause:
functionParameterClause
leftParen: (
rightParen: )
parameters:
functionParameter
colon: :
attributes:
modifiers:
trailingComma: ,
type:
identifierType
name: identifier "Int"
firstName: _
functionParameter
colon: :
attributes:
modifiers:
type:
identifierType
name: identifier "String"
firstName: _
funcKeyword: func
codeBlockItem
item:
functionDecl
attributes:
body:
codeBlock
leftBrace: {
rightBrace: }
statements:
modifiers:
name: identifier "ignore"
signature:
functionSignature
parameterClause:
functionParameterClause
leftParen: (
rightParen: )
parameters:
functionParameter
colon: :
attributes:
modifiers:
trailingComma: ,
type:
identifierType
name: identifier "Int"
firstName: identifier "first"
secondName: _
functionParameter
colon: :
attributes:
modifiers:
type:
identifierType
name: identifier "String"
firstName: identifier "second"
secondName: _
funcKeyword: func
codeBlockItem
item:
variableDecl
attributes:
modifiers:
bindingSpecifier: let
bindings:
patternBinding
initializer:
initializerClause
equal: =
value:
closureExpr
leftBrace: {
rightBrace: }
signature:
closureSignature
attributes:
inKeyword: in
parameterClause:
closureShorthandParameter
name: _
trailingComma: ,
closureShorthandParameter
name: _
statements:
pattern:
identifierPattern
identifier: identifier "ignoreClosure"
typeAnnotation:
typeAnnotation
colon: :
type:
functionType
leftParen: (
rightParen: )
parameters:
tupleTypeElement
trailingComma: ,
type:
identifierType
name: identifier "Int"
tupleTypeElement
type:
identifierType
name: identifier "String"
returnClause:
returnClause
arrow: ->
type:
identifierType
name: identifier "Void"
codeBlockItem
item:
variableDecl
attributes:
modifiers:
bindingSpecifier: let
bindings:
patternBinding
initializer:
initializerClause
equal: =
value:
closureExpr
leftBrace: {
rightBrace: }
signature:
closureSignature
attributes:
inKeyword: in
parameterClause:
closureParameterClause
leftParen: (
rightParen: )
parameters:
closureParameter
colon: :
attributes:
modifiers:
trailingComma: ,
type:
identifierType
name: identifier "Int"
firstName: _
closureParameter
colon: :
attributes:
modifiers:
type:
identifierType
name: identifier "String"
firstName: _
statements:
pattern:
identifierPattern
identifier: identifier "ignoreTypedClosure"

---

top_level source="⟨body⟩"
body:
block source="⟨stmt⟩\n\n⟨stmt⟩\n\n⟨stmt⟩\n\n⟨stmt⟩"
stmt:
function_declaration source="func ⟨name_node⟩(⟨parameter⟩, ⟨parameter⟩) ⟨body⟩"
name_node: identifier "ignore" source="ignore"
parameter:
parameter source="⟨external_name_node⟩⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "_" source="_"
type: identifier "Int" source="Int"
pattern: ignore_pattern "_" source="_"
parameter source="⟨external_name_node⟩⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "_" source="_"
type: identifier "String" source="String"
pattern: ignore_pattern "_" source="_"
body: block "{\n}" source="{\n}"
function_declaration source="func ⟨name_node⟩(⟨parameter⟩, ⟨parameter⟩) ⟨body⟩"
name_node: identifier "ignore" source="ignore"
parameter:
parameter source="⟨external_name_node⟩ ⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "first" source="first"
type: identifier "Int" source="Int"
pattern: ignore_pattern "_" source="_"
parameter source="⟨external_name_node⟩ ⟨pattern⟩: ⟨type⟩"
external_name_node: identifier "second" source="second"
type: identifier "String" source="String"
pattern: ignore_pattern "_" source="_"
body: block "{\n}" source="{\n}"
variable_declaration source="⟨modifier⟩ ⟨pattern⟩: ⟨type⟩ = ⟨value⟩"
modifier: modifier "let" source="let"
pattern: identifier "ignoreClosure" source="ignoreClosure"
type:
function_expr source="(⟨parameter⟩, ⟨parameter⟩) -> ⟨return_type⟩"
parameter:
parameter source="⟨type⟩"
type: identifier "Int" source="Int"
parameter source="⟨type⟩"
type: identifier "String" source="String"
return_type: identifier "Void" source="Void"
value:
function_expr source="{ ⟨parameter⟩, ⟨parameter⟩ in\n}"
parameter:
parameter source="⟨pattern⟩"
pattern: ignore_pattern "_" source="_"
parameter source="⟨pattern⟩"
pattern: ignore_pattern "_" source="_"
body: block source=""
variable_declaration source="⟨modifier⟩ ⟨pattern⟩ = ⟨value⟩"
modifier: modifier "let" source="let"
pattern: identifier "ignoreTypedClosure" source="ignoreTypedClosure"
value:
function_expr source="{ (⟨parameter⟩, ⟨parameter⟩) in\n}"
parameter:
parameter source="⟨pattern⟩: ⟨type⟩"
type: identifier "Int" source="Int"
pattern: ignore_pattern "_" source="_"
parameter source="⟨pattern⟩: ⟨type⟩"
type: identifier "String" source="String"
pattern: ignore_pattern "_" source="_"
body: block source=""
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
func ignore(_: Int, _: String) {
}

func ignore(first _: Int, second _: String) {
}

let ignoreClosure: (Int, String) -> Void = { _, _ in
}

let ignoreTypedClosure = { (_: Int, _: String) in
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,5 @@ top_level source="⟨body⟩"
function_expr source="{ ⟨parameter⟩ in }"
parameter:
parameter source="⟨pattern⟩"
pattern: identifier "_" source="_"
pattern: ignore_pattern "_" source="_"
body: block source=""
Loading