Skip to content

Commit

Permalink
Updating grammar files for version 0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-pinkus committed Aug 16, 2022
1 parent 0abae4d commit 77c6312
Show file tree
Hide file tree
Showing 9 changed files with 167,986 additions and 179,270 deletions.
62 changes: 62 additions & 0 deletions corpus/classes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1444,3 +1444,65 @@ extension TrustMe: @unchecked Sendable { }
(user_type
(type_identifier)))
(class_body)))

================================================================================
Actors
================================================================================

private actor CounterActor {
private var count = 0

func next() -> Int {
count += 1
return count
}

nonisolated func eventuallyIncrement() {
Task {
await next()
}
}
}

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

(source_file
(class_declaration
(modifiers
(visibility_modifier))
(type_identifier)
(class_body
(property_declaration
(modifiers
(visibility_modifier))
(pattern
(simple_identifier))
(integer_literal))
(function_declaration
(simple_identifier)
(user_type
(type_identifier))
(function_body
(statements
(assignment
(directly_assignable_expression
(simple_identifier))
(integer_literal))
(control_transfer_statement
(simple_identifier)))))
(function_declaration
(modifiers
(member_modifier))
(simple_identifier)
(function_body
(statements
(call_expression
(simple_identifier)
(call_suffix
(lambda_literal
(statements
(await_expression
(call_expression
(simple_identifier)
(call_suffix
(value_arguments))))))))))))))
32 changes: 24 additions & 8 deletions corpus/functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,10 @@ test { @Special [weak self, otherSelf] (a) in }
(simple_identifier)
(call_suffix
(lambda_literal
(attribute
(user_type
(type_identifier)))
(capture_list
(attribute
(user_type
(type_identifier)))
(capture_list_item
(ownership_modifier)
(simple_identifier))
Expand Down Expand Up @@ -792,10 +792,11 @@ func multipleType() -> Foo & Bar { return Foo() }
(value_arguments))))))))

================================================================================
Annotated function parameters in lambdas
Lambdas with annotations
================================================================================

types.flatMap { @Sendable _ in }
let mainClosure = { @MainActor in print("Running on main") }

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

Expand All @@ -807,10 +808,25 @@ types.flatMap { @Sendable _ in }
(simple_identifier)))
(call_suffix
(lambda_literal
(attribute
(user_type
(type_identifier)))
(lambda_function_type
(lambda_function_type_parameters
(lambda_parameter
(attribute
(user_type
(type_identifier)))
(simple_identifier))))))))
(simple_identifier)))))))
(property_declaration
(pattern
(simple_identifier))
(lambda_literal
(attribute
(user_type
(type_identifier)))
(statements
(call_expression
(simple_identifier)
(call_suffix
(value_arguments
(value_argument
(line_string_literal
(line_str_text))))))))))
31 changes: 31 additions & 0 deletions corpus/types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,34 @@ protocol GetType {
(user_type
(type_identifier)
(type_identifier))))))

================================================================================
Existential types
================================================================================

let p: any P = S()
func q(using p: any P) { }

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

(source_file
(property_declaration
(pattern
(simple_identifier))
(type_annotation
(existential_type
(user_type
(type_identifier))))
(call_expression
(simple_identifier)
(call_suffix
(value_arguments))))
(function_declaration
(simple_identifier)
(parameter
(simple_identifier)
(simple_identifier)
(existential_type
(user_type
(type_identifier))))
(function_body)))
33 changes: 25 additions & 8 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,17 @@ module.exports = grammar({
[$._no_expr_pattern_already_bound, $._binding_pattern_with_expr],
[$._no_expr_pattern_already_bound, $._expression],
[$._no_expr_pattern_already_bound, $._binding_pattern_no_expr],

// On encountering a closure starting with `{ @Foo ...`, we don't yet know if that attribute applies to the closure
// type or to a declaration within the closure. What a mess! We just have to hope that if we keep going, only one of
// those will parse (because there will be an `in` or a `let`).
[
$._lambda_type_declaration,
$._local_property_declaration,
$._local_typealias_declaration,
$._local_function_declaration,
$._local_class_declaration,
],
],
extras: ($) => [
$.comment,
Expand Down Expand Up @@ -357,6 +368,7 @@ module.exports = grammar({
$.optional_type,
$.metatype,
$.opaque_type,
$.existential_type,
$.protocol_composition_type
)
),
Expand Down Expand Up @@ -416,6 +428,7 @@ module.exports = grammar({
_quest: ($) => "?",
_immediate_quest: ($) => token.immediate("?"),
opaque_type: ($) => seq("some", $.user_type),
existential_type: ($) => seq("any", $.user_type),
protocol_composition_type: ($) =>
prec.right(
seq(
Expand Down Expand Up @@ -840,14 +853,19 @@ module.exports = grammar({
PRECS.lambda,
seq(
"{",
prec(PRECS.expr, optional(field("captures", $.capture_list))),
optional(seq(optional(field("type", $.lambda_function_type)), "in")),
optional($._lambda_type_declaration),
optional($.statements),
"}"
)
),
capture_list: ($) =>
seq(repeat($.attribute), "[", sep1($.capture_list_item, ","), "]"),
_lambda_type_declaration: ($) =>
seq(
repeat($.attribute),
prec(PRECS.expr, optional(field("captures", $.capture_list))),
optional(field("type", $.lambda_function_type)),
"in"
),
capture_list: ($) => seq("[", sep1($.capture_list_item, ","), "]"),
capture_list_item: ($) =>
choice(
field("name", $.self_expression),
Expand Down Expand Up @@ -881,7 +899,6 @@ module.exports = grammar({
lambda_function_type_parameters: ($) => sep1($.lambda_parameter, ","),
lambda_parameter: ($) =>
seq(
optional($.attribute),
choice(
$.self_expression,
prec(PRECS.expr, field("name", $.simple_identifier)),
Expand Down Expand Up @@ -1146,7 +1163,6 @@ module.exports = grammar({
$.typealias_declaration,
$.function_declaration,
$.class_declaration,
// TODO actor declaration
$.protocol_declaration,
$.operator_declaration,
$.precedence_group_declaration,
Expand Down Expand Up @@ -1298,7 +1314,7 @@ module.exports = grammar({
prec.right(
choice(
seq(
field("declaration_kind", choice("class", "struct")),
field("declaration_kind", choice("class", "struct", "actor")),
field("name", alias($.simple_identifier, $.type_identifier)),
optional($.type_parameters),
optional(seq(":", $._inheritance_specifiers)),
Expand Down Expand Up @@ -1710,7 +1726,8 @@ module.exports = grammar({
),
property_behavior_modifier: ($) => "lazy",
type_modifiers: ($) => repeat1($.attribute),
member_modifier: ($) => choice("override", "convenience", "required"),
member_modifier: ($) =>
choice("override", "convenience", "required", "nonisolated"),
visibility_modifier: ($) =>
seq(
choice("public", "private", "internal", "fileprivate", "open"),
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tree-sitter-swift",
"version": "0.3.0",
"version": "0.3.1",
"description": "A tree-sitter grammar for the Swift programming language.",
"main": "bindings/node/index.js",
"scripts": {
Expand Down
5 changes: 5 additions & 0 deletions queries/highlights.scm
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@
"typealias"
"struct"
"class"
"actor"
"enum"
"protocol"
"extension"
"indirect"
"nonisolated"
"override"
"convenience"
"required"
"some"
] @keyword

Expand Down
Loading

0 comments on commit 77c6312

Please sign in to comment.