diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 36e4d8a1a2..d8983d5f8a 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -184,13 +184,14 @@ pub: is_pub bool methods []FnDecl pos token.Position + pre_comments []Comment } pub struct StructInitField { pub: expr Expr pos token.Position - comment Comment + comments []Comment pub mut: name string typ table.Type @@ -542,7 +543,7 @@ pub: exprs []Expr // left side stmts []Stmt // right side pos token.Position - comment Comment // comment above `xxx {` + comments []Comment // comment above `xxx {` is_else bool post_comments []Comment } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 5dadd927b9..2c876d2383 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -682,6 +682,7 @@ pub fn (mut f Fmt) interface_decl(node ast.InterfaceDecl) { } name := node.name.after('.') f.writeln('interface $name {') + f.comments_after_last_field(node.pre_comments) for method in node.methods { f.write('\t') f.writeln(method.stringify(f.table, f.cur_mod).after('fn ')) @@ -1508,8 +1509,8 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) { } } for branch in it.branches { - if branch.comment.text != '' { - f.comment(branch.comment, { + for cmnt in branch.comments { + f.comment(cmnt, { inline: true }) f.writeln('') @@ -1780,6 +1781,11 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) { for field in it.fields { f.write('$field.name: ') f.prefix_expr_cast_expr(field.expr) + f.comments(field.comments, { + inline: true + has_nl: false + level: .indent + }) f.writeln('') } f.indent-- diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v index 4fc0358cc0..13ace8394f 100644 --- a/vlib/v/parser/if_match.v +++ b/vlib/v/parser/if_match.v @@ -60,7 +60,11 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr { }) } branches << ast.IfBranch{ - stmts: if prev_guard { p.parse_block_no_scope(false) } else { p.parse_block() } + stmts: if prev_guard { + p.parse_block_no_scope(false) + } else { + p.parse_block() + } pos: start_pos.extend(end_pos) body_pos: body_pos.extend(p.tok.position()) comments: comments @@ -71,7 +75,9 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr { comments = [] break } - if is_comptime { p.check(.dollar) } + if is_comptime { + p.check(.dollar) + } } // `if` or `else if` p.check(.key_if) @@ -192,7 +198,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr { mut branches := []ast.MatchBranch{} for { branch_first_pos := p.tok.position() - comment := p.check_comment() // comment before {} + comments := p.eat_comments() // comments before {} mut exprs := []ast.Expr{} p.open_scope() // final else @@ -318,7 +324,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr { exprs: exprs stmts: stmts pos: pos - comment: comment + comments: comments is_else: is_else post_comments: post_comments } @@ -366,20 +372,24 @@ fn (mut p Parser) select_expr() ast.SelectExpr { mut stmt := ast.Stmt{} if p.tok.kind == .key_else { if has_timeout { - p.error_with_pos('timeout `> t` and `else` are mutually exclusive `select` keys', p.tok.position()) + p.error_with_pos('timeout `> t` and `else` are mutually exclusive `select` keys', + p.tok.position()) } if has_else { - p.error_with_pos('at most one `else` branch allowed in `select` block', p.tok.position()) + p.error_with_pos('at most one `else` branch allowed in `select` block', + p.tok.position()) } is_else = true has_else = true p.next() } else if p.tok.kind == .gt { if has_else { - p.error_with_pos('`else` and timeout `> t` are mutually exclusive `select` keys', p.tok.position()) + p.error_with_pos('`else` and timeout `> t` are mutually exclusive `select` keys', + p.tok.position()) } if has_timeout { - p.error_with_pos('at most one timeout `> t` branch allowed in `select` block', p.tok.position()) + p.error_with_pos('at most one timeout `> t` branch allowed in `select` block', + p.tok.position()) } is_timeout = true has_timeout = true @@ -420,11 +430,13 @@ fn (mut p Parser) select_expr() ast.SelectExpr { match stmt.expr as expr { ast.InfixExpr { if expr.op != .arrow { - p.error_with_pos('select key: `<-` operator expected', expr.pos) + p.error_with_pos('select key: `<-` operator expected', + expr.pos) } } else { - p.error_with_pos('select key: send expression (`ch <- x`) expected', stmt.pos) + p.error_with_pos('select key: send expression (`ch <- x`) expected', + stmt.pos) } } } @@ -433,10 +445,13 @@ fn (mut p Parser) select_expr() ast.SelectExpr { match stmt.right[0] as expr { ast.PrefixExpr { if expr.op != .arrow { - p.error_with_pos('select key: `<-` operator expected', expr.pos) + p.error_with_pos('select key: `<-` operator expected', + expr.pos) } - } else { - p.error_with_pos('select key: receive expression expected', stmt.right[0].position()) + } + else { + p.error_with_pos('select key: receive expression expected', + stmt.right[0].position()) } } } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 8471a3cfbc..f27b2096e3 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1438,7 +1438,7 @@ fn (mut p Parser) module_decl() ast.Module { p.error_with_pos('`module` and `$name` must be at same line', pos) } pos = p.tok.position() - if module_pos.line_nr == pos.line_nr { + if module_pos.line_nr == pos.line_nr && p.tok.kind != .comment { if p.tok.kind != .name { p.error_with_pos('`module x` syntax error', pos) } else { diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index ab99d2ada7..2759d8fb74 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -283,21 +283,22 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit { saved_is_amp := p.is_amp p.is_amp = false for p.tok.kind != .rcbr && p.tok.kind != .rpar { - comment := p.check_comment() mut field_name := '' if no_keys { expr := p.expr(0) + comments := p.eat_comments() // name will be set later in checker fields << ast.StructInitField{ expr: expr pos: expr.position() - comment: comment + comments: comments } } else { first_field_pos := p.tok.position() field_name = p.check_name() p.check(.colon) expr := p.expr(0) + comments := p.eat_comments() last_field_pos := expr.position() field_pos := token.Position{ line_nr: first_field_pos.line_nr @@ -308,13 +309,13 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit { name: field_name expr: expr pos: field_pos + comments: comments } } i++ if p.tok.kind == .comma { p.next() } - p.check_comment() } last_pos := p.tok.position() if !short_syntax { @@ -346,6 +347,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { interface_name := p.prepend_mod(p.check_name()) // println('interface decl $interface_name') p.check(.lcbr) + pre_comments := p.eat_comments() // Declare the type reg_idx := p.table.register_type_symbol(table.TypeSymbol{ kind: .interface_ @@ -416,5 +418,6 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { methods: methods is_pub: is_pub pos: start_pos + pre_comments: pre_comments } }