Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add borrowing and consuming as keywords #367

Merged
merged 1 commit into from
Mar 3, 2024
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
Add borrowing and consuming as keywords
  • Loading branch information
alex-pinkus committed Mar 3, 2024
commit 97d309d1f5bbe15ea6ad5e9ddc773181717a67a2
16 changes: 14 additions & 2 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ module.exports = grammar({
// interpretations to see what it encounters after that.
[$.willset_didset_block],
[$._expression_without_willset_didset, $._expression_with_willset_didset],

// `borrowing` and `consuming` are legal as identifiers, but are also legal modifiers
[$.simple_identifier, $.parameter_modifier],
[$.parameter_modifiers],
],
extras: ($) => [
$.comment,
Expand Down Expand Up @@ -283,7 +287,8 @@ module.exports = grammar({
/\$[0-9]+/,
token(seq("$", LEXICAL_IDENTIFIER)),
"actor",
"lazy"
"lazy",
$._parameter_ownership_modifier
),
identifier: ($) => sep1($.simple_identifier, $._dot),
// Literals
Expand Down Expand Up @@ -1894,9 +1899,16 @@ module.exports = grammar({
mutation_modifier: ($) => choice("mutating", "nonmutating"),
property_modifier: ($) => choice("static", "dynamic", "optional", "class"),
inheritance_modifier: ($) => choice("final"),
parameter_modifier: ($) => choice("inout", "@escaping", "@autoclosure"),
parameter_modifier: ($) =>
choice(
"inout",
"@escaping",
"@autoclosure",
$._parameter_ownership_modifier
),
ownership_modifier: ($) =>
choice("weak", "unowned", "unowned(safe)", "unowned(unsafe)"),
_parameter_ownership_modifier: ($) => choice("borrowing", "consuming"),
use_site_target: ($) =>
seq(
choice(
Expand Down
239 changes: 239 additions & 0 deletions test/corpus/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -929,3 +929,242 @@ public macro AutoApply() = #externalMacro(
(simple_identifier))
(line_string_literal
(line_str_text))))))))

================================================================================
Parameter ownership modifiers in `func`
================================================================================

func foo(_: borrowing Foo) {}
func foo(_: consuming Foo) {}
func foo(_: inout Foo) {}

--------------------------------------------------------------------------------

(source_file
(function_declaration
(simple_identifier)
(parameter
(simple_identifier)
(parameter_modifiers
(parameter_modifier))
(user_type
(type_identifier)))
(function_body))
(function_declaration
(simple_identifier)
(parameter
(simple_identifier)
(parameter_modifiers
(parameter_modifier))
(user_type
(type_identifier)))
(function_body))
(function_declaration
(simple_identifier)
(parameter
(simple_identifier)
(parameter_modifiers
(parameter_modifier))
(user_type
(type_identifier)))
(function_body)))

================================================================================
Parameter ownership modifiers in a closure
================================================================================

bar { (a: borrowing Foo) in a.foo() }
bar { (a: consuming Foo) in a.foo() }
bar { (a: inout Foo) in a.foo() }

--------------------------------------------------------------------------------

(source_file
(call_expression
(simple_identifier)
(call_suffix
(lambda_literal
(lambda_function_type
(lambda_function_type_parameters
(lambda_parameter
(simple_identifier)
(parameter_modifiers
(parameter_modifier))
(user_type
(type_identifier)))))
(statements
(call_expression
(navigation_expression
(simple_identifier)
(navigation_suffix
(simple_identifier)))
(call_suffix
(value_arguments)))))))
(call_expression
(simple_identifier)
(call_suffix
(lambda_literal
(lambda_function_type
(lambda_function_type_parameters
(lambda_parameter
(simple_identifier)
(parameter_modifiers
(parameter_modifier))
(user_type
(type_identifier)))))
(statements
(call_expression
(navigation_expression
(simple_identifier)
(navigation_suffix
(simple_identifier)))
(call_suffix
(value_arguments)))))))
(call_expression
(simple_identifier)
(call_suffix
(lambda_literal
(lambda_function_type
(lambda_function_type_parameters
(lambda_parameter
(simple_identifier)
(parameter_modifiers
(parameter_modifier))
(user_type
(type_identifier)))))
(statements
(call_expression
(navigation_expression
(simple_identifier)
(navigation_suffix
(simple_identifier)))
(call_suffix
(value_arguments))))))))

================================================================================
Parameter ownership modifiers in a function type
================================================================================

let f: (borrowing Foo) -> Void = { a in a.foo() }
let f: (consuming Foo) -> Void = { a in a.foo() }
let f: (inout Foo) -> Void = { a in a.foo() }

--------------------------------------------------------------------------------

(source_file
(property_declaration
(value_binding_pattern)
(pattern
(simple_identifier))
(type_annotation
(function_type
(tuple_type
(tuple_type_item
(parameter_modifiers
(parameter_modifier))
(user_type
(type_identifier))))
(user_type
(type_identifier))))
(lambda_literal
(lambda_function_type
(lambda_function_type_parameters
(lambda_parameter
(simple_identifier))))
(statements
(call_expression
(navigation_expression
(simple_identifier)
(navigation_suffix
(simple_identifier)))
(call_suffix
(value_arguments))))))
(property_declaration
(value_binding_pattern)
(pattern
(simple_identifier))
(type_annotation
(function_type
(tuple_type
(tuple_type_item
(parameter_modifiers
(parameter_modifier))
(user_type
(type_identifier))))
(user_type
(type_identifier))))
(lambda_literal
(lambda_function_type
(lambda_function_type_parameters
(lambda_parameter
(simple_identifier))))
(statements
(call_expression
(navigation_expression
(simple_identifier)
(navigation_suffix
(simple_identifier)))
(call_suffix
(value_arguments))))))
(property_declaration
(value_binding_pattern)
(pattern
(simple_identifier))
(type_annotation
(function_type
(tuple_type
(tuple_type_item
(parameter_modifiers
(parameter_modifier))
(user_type
(type_identifier))))
(user_type
(type_identifier))))
(lambda_literal
(lambda_function_type
(lambda_function_type_parameters
(lambda_parameter
(simple_identifier))))
(statements
(call_expression
(navigation_expression
(simple_identifier)
(navigation_suffix
(simple_identifier)))
(call_suffix
(value_arguments)))))))

================================================================================
Ownership modifiers for `self`
================================================================================

struct Foo {
consuming func foo() {} // `consuming` self
borrowing func foo() {} // `borrowing` self
mutating func foo() {} // modify self with `inout` semantics
}

--------------------------------------------------------------------------------

(source_file
(class_declaration
(type_identifier)
(class_body
(function_declaration
(modifiers
(parameter_modifier))
(simple_identifier)
(function_body))
(comment)
(function_declaration
(modifiers
(parameter_modifier))
(simple_identifier)
(function_body))
(comment)
(function_declaration
(modifiers
(mutation_modifier))
(simple_identifier)
(function_body))
(comment))))
Loading