mirror of
https://github.com/vlang/v.git
synced 2025-09-16 10:57:25 -04:00
parser, fmt: fix match with multi-commmented branchs (#19898)
This commit is contained in:
parent
b347f546f2
commit
373da77792
@ -2696,13 +2696,13 @@ fn (mut f Fmt) match_branch(branch ast.MatchBranch, single_line bool) {
|
|||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
f.write(estr)
|
f.write(estr)
|
||||||
|
if j < branch.exprs.len - 1 {
|
||||||
|
f.write(', ')
|
||||||
|
}
|
||||||
if j < branch.ecmnts.len && branch.ecmnts[j].len > 0 {
|
if j < branch.ecmnts.len && branch.ecmnts[j].len > 0 {
|
||||||
f.write(' ')
|
f.write(' ')
|
||||||
f.comments(branch.ecmnts[j])
|
f.comments(branch.ecmnts[j])
|
||||||
}
|
}
|
||||||
if j < branch.exprs.len - 1 {
|
|
||||||
f.write(', ')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
f.is_mbranch_expr = false
|
f.is_mbranch_expr = false
|
||||||
} else {
|
} else {
|
||||||
@ -2714,6 +2714,8 @@ fn (mut f Fmt) match_branch(branch ast.MatchBranch, single_line bool) {
|
|||||||
} else {
|
} else {
|
||||||
if single_line {
|
if single_line {
|
||||||
f.write(' { ')
|
f.write(' { ')
|
||||||
|
} else if branch.ecmnts.len > 0 && branch.ecmnts.last().len > 0 {
|
||||||
|
f.writeln('{')
|
||||||
} else {
|
} else {
|
||||||
f.writeln(' {')
|
f.writeln(' {')
|
||||||
}
|
}
|
||||||
|
40
vlib/v/fmt/tests/match_with_multi_commented_branches_keep.vv
Normal file
40
vlib/v/fmt/tests/match_with_multi_commented_branches_keep.vv
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
import strings
|
||||||
|
|
||||||
|
fn escape_string(s string) string {
|
||||||
|
mut res := strings.new_builder(s.len * 2)
|
||||||
|
for ch in s {
|
||||||
|
match ch {
|
||||||
|
0 // NUL (null)
|
||||||
|
{
|
||||||
|
res.write_u8(92) // \
|
||||||
|
res.write_u8(48) // 0
|
||||||
|
}
|
||||||
|
10 { // LF (line feed)
|
||||||
|
res.write_u8(92) // \
|
||||||
|
res.write_u8(110) // n
|
||||||
|
}
|
||||||
|
13 { // CR (carriage return)
|
||||||
|
res.write_u8(92) // \
|
||||||
|
res.write_u8(114) // r
|
||||||
|
}
|
||||||
|
26 { // SUB (substitute)
|
||||||
|
res.write_u8(92) // \
|
||||||
|
res.write_u8(90) // Z
|
||||||
|
}
|
||||||
|
34, // "
|
||||||
|
39, // '
|
||||||
|
92 // \
|
||||||
|
{
|
||||||
|
res.write_u8(92) // \
|
||||||
|
res.write_u8(ch)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
res.write_u8(ch)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res.bytestr()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
}
|
@ -258,13 +258,13 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
|
|||||||
for {
|
for {
|
||||||
// Sum type match
|
// Sum type match
|
||||||
parsed_type := p.parse_type()
|
parsed_type := p.parse_type()
|
||||||
ecmnts << p.eat_comments()
|
|
||||||
types << parsed_type
|
types << parsed_type
|
||||||
exprs << ast.TypeNode{
|
exprs << ast.TypeNode{
|
||||||
typ: parsed_type
|
typ: parsed_type
|
||||||
pos: p.prev_tok.pos()
|
pos: p.prev_tok.pos()
|
||||||
}
|
}
|
||||||
if p.tok.kind != .comma {
|
if p.tok.kind != .comma {
|
||||||
|
ecmnts << p.eat_comments()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
p.check(.comma)
|
p.check(.comma)
|
||||||
@ -275,6 +275,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
|
|||||||
for p.tok.kind == .comma {
|
for p.tok.kind == .comma {
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
|
ecmnts << p.eat_comments()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
is_sum_type = true
|
is_sum_type = true
|
||||||
@ -284,7 +285,6 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
|
|||||||
p.inside_match_case = true
|
p.inside_match_case = true
|
||||||
mut range_pos := p.tok.pos()
|
mut range_pos := p.tok.pos()
|
||||||
expr := p.expr(0)
|
expr := p.expr(0)
|
||||||
ecmnts << p.eat_comments()
|
|
||||||
p.inside_match_case = false
|
p.inside_match_case = false
|
||||||
if p.tok.kind == .dotdot {
|
if p.tok.kind == .dotdot {
|
||||||
p.error_with_pos('match only supports inclusive (`...`) ranges, not exclusive (`..`)',
|
p.error_with_pos('match only supports inclusive (`...`) ranges, not exclusive (`..`)',
|
||||||
@ -306,6 +306,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
|
|||||||
exprs << expr
|
exprs << expr
|
||||||
}
|
}
|
||||||
if p.tok.kind != .comma {
|
if p.tok.kind != .comma {
|
||||||
|
ecmnts << p.eat_comments()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -317,6 +318,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
|
|||||||
for p.tok.kind == .comma {
|
for p.tok.kind == .comma {
|
||||||
p.next()
|
p.next()
|
||||||
}
|
}
|
||||||
|
ecmnts << p.eat_comments()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user