Skip to content

Commit b470780

Browse files
authored
parser: fix infix expr comment in middle (fix #24183) (#25671)
1 parent ff3f12e commit b470780

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

vlib/v/fmt/tests/infix_expr_with_comments_keep.vv

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,29 @@ fn main() {
22
a := 4 // comment1
33
+ 6 // comment2
44

5-
b := 5 + // comment2
5+
b := 5 + // comment3
66
6
77

8-
c := 4 + 6 // comment3
8+
c := 4 + 6 // comment4
9+
10+
var := 'a' // comment5
11+
+ 'b' // comment6
12+
+ 'c' // comment7
13+
14+
// vfmt off
15+
pattern := r'^('
16+
+ r'(\^|([<>]?=?))?' // match: ^, >, <, =, >=, <=
17+
+ r'((\*)|([0-9]+))' // match: *, 1
18+
+ r'((\.?\*)|(\.[0-9]+))?' // match: *, .*, .1
19+
+ r'((\.?\*)|(\.[0-9]+))?' // match: *, .*, .1
20+
+ r'(-[0-9a-zA-Z.]+)?)' // match prerelease part e.g: -beta.1
21+
+ r'|(\*)$' // match just '*'
22+
// vfmt on
23+
24+
pattern_fmt := r'^(' + r'(\^|([<>]?=?))?' // match: ^, >, <, =, >=, <=
25+
+ r'((\*)|([0-9]+))' // match: *, 1
26+
+ r'((\.?\*)|(\.[0-9]+))?' // match: *, .*, .1
27+
+ r'((\.?\*)|(\.[0-9]+))?' // match: *, .*, .1
28+
+ r'(-[0-9a-zA-Z.]+)?)' // match prerelease part e.g: -beta.1
29+
+ r'|(\*)$' // match just '*'
930
}

vlib/v/parser/expr.v

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,9 +552,8 @@ fn (mut p Parser) check_expr(precedence int) !ast.Expr {
552552
if p.inside_if_cond {
553553
p.if_cond_comments << p.eat_comments()
554554
}
555-
if p.pref.is_fmt && p.tok.kind == .comment && p.peek_tok.kind.is_infix() && !p.inside_infix
556-
&& !p.inside_map_init && !(p.peek_tok.kind == .mul
557-
&& p.peek_tok.pos().line_nr != p.tok.pos().line_nr) {
555+
if p.pref.is_fmt && p.tok.kind == .comment && p.peek_tok.kind.is_infix() && !p.inside_map_init
556+
&& !(p.peek_tok.kind == .mul && p.peek_tok.pos().line_nr != p.tok.pos().line_nr) {
558557
p.left_comments = p.eat_comments()
559558
}
560559
return p.expr_with_left(node, precedence, is_stmt_ident)

0 commit comments

Comments
 (0)