Skip to content

Commit

Permalink
Add borrowing and consuming as keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-pinkus committed Mar 3, 2024
1 parent 9d27321 commit 4b5c54b
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 2 deletions.
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))))

0 comments on commit 4b5c54b

Please sign in to comment.