Skip to content

Commit cb3a106

Browse files
committed
parser: experimental syntax that doesn't require an extra indent
1 parent 21cc3c3 commit cb3a106

4 files changed

Lines changed: 49 additions & 45 deletions

File tree

‎vlib/v/ast/ast.v‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,7 @@ pub mut:
12671267
cond_type Type // type of `x` in `match x {`
12681268
expected_type Type // for debugging only
12691269
is_sum_type bool
1270+
no_lcbr bool // for match statements without {}
12701271
}
12711272

12721273
pub struct MatchBranch {

‎vlib/v/fmt/fmt.v‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2858,8 +2858,12 @@ fn (mut f Fmt) match_branch(branch ast.MatchBranch, single_line bool) {
28582858
pub fn (mut f Fmt) match_expr(node ast.MatchExpr) {
28592859
f.write('match ')
28602860
f.expr(node.cond)
2861-
f.writeln(' {')
2862-
f.indent++
2861+
if node.no_lcbr {
2862+
f.writeln('')
2863+
} else {
2864+
f.writeln(' {')
2865+
f.indent++
2866+
}
28632867
f.comments(node.comments)
28642868
mut single_line := true
28652869
for branch in node.branches {
@@ -2886,8 +2890,10 @@ pub fn (mut f Fmt) match_expr(node ast.MatchExpr) {
28862890
if else_idx >= 0 {
28872891
f.match_branch(node.branches[else_idx], single_line)
28882892
}
2889-
f.indent--
2890-
f.write('}')
2893+
if !node.no_lcbr {
2894+
f.indent--
2895+
f.write('}')
2896+
}
28912897
}
28922898

28932899
pub fn (mut f Fmt) offset_of(node ast.OffsetOf) {

‎vlib/v/parser/if_match.v‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
375375
len: match_last_pos.pos - match_first_pos.pos + match_last_pos.len
376376
col: match_first_pos.col
377377
}
378-
if p.tok.kind == .rcbr {
378+
if p.tok.kind == .rcbr && !no_lcbr {
379379
p.check(.rcbr)
380380
}
381381
// return ast.StructInit{}
@@ -386,6 +386,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
386386
is_sum_type: is_sum_type
387387
pos: pos
388388
comments: comments
389+
no_lcbr: no_lcbr
389390
}
390391
}
391392

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
11
fn test_simple_match_expr() {
22
mut a := ?int(12)
3-
match a? {
4-
12 {
5-
println(a)
6-
}
7-
else {
8-
println('else')
9-
assert false
10-
}
3+
match a?
4+
12 {
5+
println(a)
6+
}
7+
else {
8+
println('else')
9+
assert false
1110
}
1211

13-
match a {
14-
none {
15-
println('none')
16-
assert false
17-
}
18-
else {
19-
println('else')
20-
}
12+
match a
13+
none {
14+
println('none')
15+
assert false
16+
}
17+
else {
18+
println('else')
2119
}
2220

2321
a = none
2422

25-
match a {
26-
none {
27-
println('none')
28-
}
29-
else {
30-
println('else')
31-
assert false
32-
}
23+
match a
24+
none {
25+
println('none')
26+
}
27+
else {
28+
println('else')
29+
assert false
3330
}
3431

3532
mut b := ?string('aaa')
@@ -43,24 +40,23 @@ fn test_simple_match_expr() {
4340
}
4441
}
4542

46-
match b {
47-
none {
48-
println('none')
49-
assert false
50-
}
51-
else {
52-
println('else')
53-
}
43+
match b
44+
none {
45+
println('none')
46+
assert false
47+
}
48+
else {
49+
println('else')
5450
}
5551

5652
b = none
57-
match b {
58-
none {
59-
println('none')
60-
}
61-
else {
62-
println('else')
63-
assert false
64-
}
53+
match b
54+
none {
55+
println('none')
56+
}
57+
else {
58+
println('else')
59+
assert false
6560
}
61+
6662
}

0 commit comments

Comments
 (0)