parser, fmt: fix match with multi-commmented branchs (#19898)

This commit is contained in:
yuyi 2023-11-17 21:37:48 +08:00 committed by GitHub
parent b347f546f2
commit 373da77792
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 5 deletions

View File

@ -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(' {')
} }

View 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() {
}

View File

@ -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()
} }
} }
} }