parser: experimental syntax that doesn't require an extra indent

This commit is contained in:
Alexander Medvednikov 2025-06-22 03:22:56 +03:00
parent 21cc3c31fe
commit cb3a106e1d
4 changed files with 49 additions and 45 deletions

View File

@ -1267,6 +1267,7 @@ pub mut:
cond_type Type // type of `x` in `match x {`
expected_type Type // for debugging only
is_sum_type bool
no_lcbr bool // for match statements without {}
}
pub struct MatchBranch {

View File

@ -2858,8 +2858,12 @@ fn (mut f Fmt) match_branch(branch ast.MatchBranch, single_line bool) {
pub fn (mut f Fmt) match_expr(node ast.MatchExpr) {
f.write('match ')
f.expr(node.cond)
f.writeln(' {')
f.indent++
if node.no_lcbr {
f.writeln('')
} else {
f.writeln(' {')
f.indent++
}
f.comments(node.comments)
mut single_line := true
for branch in node.branches {
@ -2886,8 +2890,10 @@ pub fn (mut f Fmt) match_expr(node ast.MatchExpr) {
if else_idx >= 0 {
f.match_branch(node.branches[else_idx], single_line)
}
f.indent--
f.write('}')
if !node.no_lcbr {
f.indent--
f.write('}')
}
}
pub fn (mut f Fmt) offset_of(node ast.OffsetOf) {

View File

@ -375,7 +375,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
len: match_last_pos.pos - match_first_pos.pos + match_last_pos.len
col: match_first_pos.col
}
if p.tok.kind == .rcbr {
if p.tok.kind == .rcbr && !no_lcbr {
p.check(.rcbr)
}
// return ast.StructInit{}
@ -386,6 +386,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
is_sum_type: is_sum_type
pos: pos
comments: comments
no_lcbr: no_lcbr
}
}

View File

@ -1,35 +1,32 @@
fn test_simple_match_expr() {
mut a := ?int(12)
match a? {
12 {
println(a)
}
else {
println('else')
assert false
}
match a?
12 {
println(a)
}
else {
println('else')
assert false
}
match a {
none {
println('none')
assert false
}
else {
println('else')
}
match a
none {
println('none')
assert false
}
else {
println('else')
}
a = none
match a {
none {
println('none')
}
else {
println('else')
assert false
}
match a
none {
println('none')
}
else {
println('else')
assert false
}
mut b := ?string('aaa')
@ -43,24 +40,23 @@ fn test_simple_match_expr() {
}
}
match b {
none {
println('none')
assert false
}
else {
println('else')
}
match b
none {
println('none')
assert false
}
else {
println('else')
}
b = none
match b {
none {
println('none')
}
else {
println('else')
assert false
}
match b
none {
println('none')
}
else {
println('else')
assert false
}
}