Skip to content

Commit

Permalink
Merge pull request tree-sitter#446 from eed3si9n/wip/sip-58
Browse files Browse the repository at this point in the history
SIP-58 Named tuples support
  • Loading branch information
eed3si9n authored Dec 16, 2024
2 parents 68773df + 709057b commit fb9aa82
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 3 deletions.
48 changes: 45 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ module.exports = grammar({
[$.class_parameters],
// 'for' operator_identifier ':' _annotated_type • ':' …
[$._type, $.compound_type],
// 'given' '(' operator_identifier ':' _type • ',' …
[$.name_and_type, $.parameter],
[$._simple_expression, $.binding, $.tuple_pattern],
[$._simple_expression, $.tuple_pattern],
[$._simple_expression, $._type_identifier],
// 'if' parenthesized_expression • '{' …
[$._if_condition, $._simple_expression],
// _postfix_expression_choice ':' '(' wildcard • ':' …
Expand Down Expand Up @@ -785,6 +790,19 @@ module.exports = grammar({
),
),

/*
* NameAndType ::= id ':' Type
*/
name_and_type: $ =>
prec.left(
PREC.control,
seq(
field("name", $._identifier),
":",
field("type", $._param_type),
),
),

_block: $ =>
prec.left(
seq(
Expand Down Expand Up @@ -837,6 +855,7 @@ module.exports = grammar({
$.generic_type,
$.projected_type,
$.tuple_type,
$.named_tuple_type,
$.singleton_type,
$.stable_type_identifier,
$._type_identifier,
Expand Down Expand Up @@ -897,6 +916,12 @@ module.exports = grammar({

tuple_type: $ => seq("(", trailingCommaSep1($._type), ")"),

named_tuple_type: $ => seq(
"(",
trailingCommaSep1($.name_and_type),
")",
),

singleton_type: $ =>
prec.left(
PREC.stable_type_id,
Expand Down Expand Up @@ -991,6 +1016,7 @@ module.exports = grammar({
$.interpolated_string_expression,
$.capture_pattern,
$.tuple_pattern,
$.named_tuple_pattern,
$.case_class_pattern,
$.infix_pattern,
$.alternative_pattern,
Expand All @@ -1006,7 +1032,10 @@ module.exports = grammar({
seq(
field("type", choice($._type_identifier, $.stable_type_identifier)),
"(",
field("pattern", trailingCommaSep($._pattern)),
choice(
field("pattern", trailingCommaSep($._pattern)),
field("pattern", trailingCommaSep($.named_pattern)),
),
")",
),

Expand Down Expand Up @@ -1034,15 +1063,28 @@ module.exports = grammar({

typed_pattern: $ =>
prec.right(
-1,
seq(field("pattern", $._pattern), ":", field("type", $._type)),
),

given_pattern: $ => seq("given", field("type", $._type)),

// TODO: Flatten this.
alternative_pattern: $ => prec.left(-1, seq($._pattern, "|", $._pattern)),
alternative_pattern: $ => prec.left(-2, seq($._pattern, "|", $._pattern)),

tuple_pattern: $ => seq(
"(",
trailingCommaSep1($._pattern),
")",
),

named_pattern: $ => prec.left(-1, seq($._identifier, "=", $._pattern)),

tuple_pattern: $ => seq("(", $._pattern, repeat(seq(",", $._pattern)), ")"),
named_tuple_pattern: $ => seq(
"(",
trailingCommaSep1($.named_pattern),
")",
),

// ---------------------------------------------------------------
// Expressions
Expand Down
54 changes: 54 additions & 0 deletions test/corpus/patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,31 @@ val x = y match {
(identifier))
(identifier))))))

================================================================================
Name tuple patterns (Scala 3 syntax)
================================================================================

val x = y match
case (a = A, b = B) => ???

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

(compilation_unit
(val_definition
(identifier)
(match_expression
(identifier)
(indented_cases
(case_clause
(named_tuple_pattern
(named_pattern
(identifier)
(identifier))
(named_pattern
(identifier)
(identifier)))
(operator_identifier))))))

================================================================================
Case class patterns
================================================================================
Expand Down Expand Up @@ -178,6 +203,35 @@ def showNotification(notification: Notification): String = {
(interpolation
(identifier))))))))))

================================================================================
Case class patterns (Scala 3 syntax)
================================================================================

class A:
c match
case c @ City(name = "Hoboken") => c.population

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

(compilation_unit
(class_definition
(identifier)
(template_body
(match_expression
(identifier)
(indented_cases
(case_clause
(capture_pattern
(identifier)
(case_class_pattern
(type_identifier)
(named_pattern
(identifier)
(string))))
(field_expression
(identifier)
(identifier))))))))

================================================================================
Infix patterns
================================================================================
Expand Down
23 changes: 23 additions & 0 deletions test/corpus/types.txt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,29 @@ object Main {
(type_identifier)
(type_identifier))))))

================================================================================
Named tuple types (Scala 3 syntax)
================================================================================

object O:
type A = (name: String, age: Int)

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

(compilation_unit
(object_definition
(identifier)
(template_body
(type_definition
(type_identifier)
(named_tuple_type
(name_and_type
(identifier)
(type_identifier))
(name_and_type
(identifier)
(type_identifier)))))))

================================================================================
Function types
================================================================================
Expand Down

0 comments on commit fb9aa82

Please sign in to comment.