From 0151e94463bc826be6eecac29ad1c588169f9976 Mon Sep 17 00:00:00 2001 From: Vasil Markoukin <1istoobig@gmail.com> Date: Mon, 25 Nov 2024 13:09:52 +0300 Subject: [PATCH] feat: support non-indented cases in Scala3 match expression ---- Addresses #264 --- .gitignore | 4 ++++ grammar.js | 7 ++++++- test/corpus/expressions.txt | 36 ++++++++++++++++++++++++++---------- 3 files changed, 36 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 308fcab..71f786a 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,7 @@ dist/ *.tar.gz *.tgz *.zip + +# scala-cli +.scala-build +.bsp diff --git a/grammar.js b/grammar.js index f30d2b9..51de0af 100644 --- a/grammar.js +++ b/grammar.js @@ -95,6 +95,8 @@ module.exports = grammar({ [$._if_condition, $._simple_expression], // _postfix_expression_choice ':' '(' wildcard • ':' … [$.binding, $._simple_type], + // expression 'match' 'case' _case_pattern ';' _automatic_semicolon expression • 'match' … + [$.match_expression, $._block], ], word: $ => $._alpha_identifier, @@ -806,6 +808,9 @@ module.exports = grammar({ indented_cases: $ => prec.left(seq($._indent, repeat1($.case_clause), $._outdent)), + non_indented_cases: $ => + prec.right(sep1($._automatic_semicolon, $.case_clause)), + _indented_type_cases: $ => prec.left(seq($._indent, repeat1($.type_case_clause), $._outdent)), @@ -1157,7 +1162,7 @@ module.exports = grammar({ optional($.inline_modifier), field("value", $.expression), "match", - field("body", choice($.case_block, $.indented_cases)), + field("body", choice($.case_block, $.indented_cases, $.non_indented_cases)), ), try_expression: $ => diff --git a/test/corpus/expressions.txt b/test/corpus/expressions.txt index 6c5613f..62873cb 100644 --- a/test/corpus/expressions.txt +++ b/test/corpus/expressions.txt @@ -689,12 +689,12 @@ def matchTest(x: Int): String = x match { case 3 => { "3" } - case 4 => + case 4 => ; case A if a == 1 => case A if a == 2 => 2 case ((i, _)) => i - case s"$l1 -- $l2" => + case s"$l1 -- $l2" => l1 + l2 case _ => val x = "many" @@ -791,6 +791,10 @@ def matchTest(x: Int): String = case _ => val x = "many" "more" + y match + case 0 => "zero" + case 1 => "one" + case _ => "idk" -------------------------------------------------------------------------------- @@ -837,6 +841,18 @@ def matchTest(x: Int): String = (val_definition (identifier) (string)) + (string)))) + (match_expression + (identifier) + (non_indented_cases + (case_clause + (integer_literal) + (string)) + (case_clause + (integer_literal) + (string)) + (case_clause + (wildcard) (string))))))) ================================================================================ @@ -850,8 +866,8 @@ class C { a .b // comment1 - /* - comment2 + /* + comment2 */ .c } @@ -1105,11 +1121,11 @@ object O { val l = a => a + 1 val b = (x: Int, y: Int) => { x * y } val f = _ => 2 - foo { i => - val x = 2 + i + foo { i => + val x = 2 + i x } - { x => + { x => val y = 2 * x y * y } @@ -1223,7 +1239,7 @@ Unit expressions ================================================================================ val x = () -def f(): Unit = { ( +def f(): Unit = { ( ); } -------------------------------------------------------------------------------- @@ -1649,9 +1665,9 @@ class A: Inline matches (Scala 3) ================================================================================ -def hello = +def hello = inline c match { - case 1 => + case 1 => } --------------------------------------------------------------------------------