From eed882950c529bd810b7124b47a9888388e86ba9 Mon Sep 17 00:00:00 2001 From: zakuro Date: Tue, 14 Dec 2021 15:14:43 +0900 Subject: [PATCH] fmt: remove extra comma of branch instead of parse error (#12814) --- vlib/v/fmt/tests/match_expected.vv | 12 ++++++++++++ vlib/v/fmt/tests/match_input.vv | 12 ++++++++++++ vlib/v/parser/if_match.v | 17 +++++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/vlib/v/fmt/tests/match_expected.vv b/vlib/v/fmt/tests/match_expected.vv index 556768f95c..182cb70043 100644 --- a/vlib/v/fmt/tests/match_expected.vv +++ b/vlib/v/fmt/tests/match_expected.vv @@ -36,3 +36,15 @@ fn really_long_branch_exprs() { } } } + +fn match_branch_extra_comma() { + match x { + Foo, Bar {} + int, string {} + } + match n { + 0...5 {} + 2, 3 {} + else {} + } +} diff --git a/vlib/v/fmt/tests/match_input.vv b/vlib/v/fmt/tests/match_input.vv index 3f7cbae675..18ac19dbfd 100644 --- a/vlib/v/fmt/tests/match_input.vv +++ b/vlib/v/fmt/tests/match_input.vv @@ -34,3 +34,15 @@ fn really_long_branch_exprs() { } } } + +fn match_branch_extra_comma() { + match x { + Foo, Bar, {} + int,,, string, {} + } + match n { + 0...5, {} + 2, 3, {} + else {} + } +} diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v index 2ad526fc5a..194f868675 100644 --- a/vlib/v/parser/if_match.v +++ b/vlib/v/parser/if_match.v @@ -197,6 +197,14 @@ fn (mut p Parser) match_expr() ast.MatchExpr { break } p.check(.comma) + if p.pref.is_fmt { + if p.tok.kind == .lcbr { + break + } + for p.tok.kind == .comma { + p.next() + } + } } is_sum_type = true } else { @@ -226,7 +234,16 @@ fn (mut p Parser) match_expr() ast.MatchExpr { if p.tok.kind != .comma { break } + p.check(.comma) + if p.pref.is_fmt { + if p.tok.kind == .lcbr { + break + } + for p.tok.kind == .comma { + p.next() + } + } } } branch_last_pos := p.prev_tok.position()