Skip to content

Commit 373da77

Browse files
authored
parser, fmt: fix match with multi-commmented branchs (#19898)
1 parent b347f54 commit 373da77

File tree

3 files changed

+49
-5
lines changed

3 files changed

+49
-5
lines changed

vlib/v/fmt/fmt.v

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2696,13 +2696,13 @@ fn (mut f Fmt) match_branch(branch ast.MatchBranch, single_line bool) {
26962696
f.writeln('')
26972697
}
26982698
f.write(estr)
2699+
if j < branch.exprs.len - 1 {
2700+
f.write(', ')
2701+
}
26992702
if j < branch.ecmnts.len && branch.ecmnts[j].len > 0 {
27002703
f.write(' ')
27012704
f.comments(branch.ecmnts[j])
27022705
}
2703-
if j < branch.exprs.len - 1 {
2704-
f.write(', ')
2705-
}
27062706
}
27072707
f.is_mbranch_expr = false
27082708
} else {
@@ -2714,6 +2714,8 @@ fn (mut f Fmt) match_branch(branch ast.MatchBranch, single_line bool) {
27142714
} else {
27152715
if single_line {
27162716
f.write(' { ')
2717+
} else if branch.ecmnts.len > 0 && branch.ecmnts.last().len > 0 {
2718+
f.writeln('{')
27172719
} else {
27182720
f.writeln(' {')
27192721
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import strings
2+
3+
fn escape_string(s string) string {
4+
mut res := strings.new_builder(s.len * 2)
5+
for ch in s {
6+
match ch {
7+
0 // NUL (null)
8+
{
9+
res.write_u8(92) // \
10+
res.write_u8(48) // 0
11+
}
12+
10 { // LF (line feed)
13+
res.write_u8(92) // \
14+
res.write_u8(110) // n
15+
}
16+
13 { // CR (carriage return)
17+
res.write_u8(92) // \
18+
res.write_u8(114) // r
19+
}
20+
26 { // SUB (substitute)
21+
res.write_u8(92) // \
22+
res.write_u8(90) // Z
23+
}
24+
34, // "
25+
39, // '
26+
92 // \
27+
{
28+
res.write_u8(92) // \
29+
res.write_u8(ch)
30+
}
31+
else {
32+
res.write_u8(ch)
33+
}
34+
}
35+
}
36+
return res.bytestr()
37+
}
38+
39+
fn main() {
40+
}

vlib/v/parser/if_match.v

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,13 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
258258
for {
259259
// Sum type match
260260
parsed_type := p.parse_type()
261-
ecmnts << p.eat_comments()
262261
types << parsed_type
263262
exprs << ast.TypeNode{
264263
typ: parsed_type
265264
pos: p.prev_tok.pos()
266265
}
267266
if p.tok.kind != .comma {
267+
ecmnts << p.eat_comments()
268268
break
269269
}
270270
p.check(.comma)
@@ -275,6 +275,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
275275
for p.tok.kind == .comma {
276276
p.next()
277277
}
278+
ecmnts << p.eat_comments()
278279
}
279280
}
280281
is_sum_type = true
@@ -284,7 +285,6 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
284285
p.inside_match_case = true
285286
mut range_pos := p.tok.pos()
286287
expr := p.expr(0)
287-
ecmnts << p.eat_comments()
288288
p.inside_match_case = false
289289
if p.tok.kind == .dotdot {
290290
p.error_with_pos('match only supports inclusive (`...`) ranges, not exclusive (`..`)',
@@ -306,6 +306,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
306306
exprs << expr
307307
}
308308
if p.tok.kind != .comma {
309+
ecmnts << p.eat_comments()
309310
break
310311
}
311312

@@ -317,6 +318,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
317318
for p.tok.kind == .comma {
318319
p.next()
319320
}
321+
ecmnts << p.eat_comments()
320322
}
321323
}
322324
}

0 commit comments

Comments
 (0)