@@ -598,25 +598,23 @@ pub fn (mut p Parser) comment_stmt() ast.ExprStmt {
598
598
}
599
599
}
600
600
601
- pub fn (mut p Parser) eat_comments () []ast.Comment {
602
- mut comments := []ast.Comment{}
603
- for {
604
- if p.tok.kind != .comment {
605
- break
606
- }
607
- comments << p.comment ()
608
- }
609
- return comments
601
+ struct EatCommentsConfig {
602
+ same_line bool // Only eat comments on the same line as the previous token
603
+ follow_up bool // Comments directly below the previous token as long as there is no empty line
610
604
}
611
605
612
- pub fn (mut p Parser) eat_line_end_comments ( ) []ast.Comment {
613
- line := p.prev_tok.line_nr
606
+ pub fn (mut p Parser) eat_comments (cfg EatCommentsConfig ) []ast.Comment {
607
+ mut line := p.prev_tok.line_nr
614
608
mut comments := []ast.Comment{}
615
609
for {
616
- if p.tok.kind != .comment || p.tok.line_nr > line {
610
+ if p.tok.kind != .comment || (cfg.same_line && p.tok.line_nr > line)
611
+ || (cfg.follow_up && p.tok.line_nr > line + 1 ) {
617
612
break
618
613
}
619
614
comments << p.comment ()
615
+ if cfg.follow_up {
616
+ line = p.prev_tok.line_nr
617
+ }
620
618
}
621
619
return comments
622
620
}
@@ -1558,7 +1556,7 @@ fn (mut p Parser) dot_expr(left ast.Expr) ast.Expr {
1558
1556
//
1559
1557
end_pos := p.prev_tok.position ()
1560
1558
pos := name_pos.extend (end_pos)
1561
- comments := p.eat_line_end_comments ( )
1559
+ comments := p.eat_comments (same_line: true )
1562
1560
mcall_expr := ast.CallExpr{
1563
1561
left: left
1564
1562
name: field_name
@@ -1919,7 +1917,7 @@ fn (mut p Parser) import_stmt() ast.Import {
1919
1917
return import_node
1920
1918
}
1921
1919
}
1922
- import_node.comments = p.eat_line_end_comments ( )
1920
+ import_node.comments = p.eat_comments (same_line: true )
1923
1921
p.imports[mod_alias] = mod_name
1924
1922
// if mod_name !in p.table.imports {
1925
1923
p.table.imports << mod_name
@@ -1992,7 +1990,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
1992
1990
p.error_with_pos ('const declaration is missing closing `)`' , const_pos)
1993
1991
return ast.ConstDecl{}
1994
1992
}
1995
- comments = p.eat_comments ()
1993
+ comments = p.eat_comments ({} )
1996
1994
if p.tok.kind == .rpar {
1997
1995
break
1998
1996
}
@@ -2043,7 +2041,7 @@ fn (mut p Parser) return_stmt() ast.Return {
2043
2041
first_pos := p.tok.position ()
2044
2042
p.next ()
2045
2043
// no return
2046
- mut comments := p.eat_comments ()
2044
+ mut comments := p.eat_comments ({} )
2047
2045
if p.tok.kind == .rcbr {
2048
2046
return ast.Return{
2049
2047
comments: comments
@@ -2085,7 +2083,7 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
2085
2083
mut fields := []ast.GlobalField{}
2086
2084
mut comments := []ast.Comment{}
2087
2085
for {
2088
- comments = p.eat_comments ()
2086
+ comments = p.eat_comments ({} )
2089
2087
if p.tok.kind == .rpar {
2090
2088
break
2091
2089
}
@@ -2147,7 +2145,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
2147
2145
}
2148
2146
name := p.prepend_mod (enum_name)
2149
2147
p.check (.lcbr)
2150
- enum_decl_comments := p.eat_comments ()
2148
+ enum_decl_comments := p.eat_comments ({} )
2151
2149
mut vals := []string {}
2152
2150
// mut default_exprs := []ast.Expr{}
2153
2151
mut fields := []ast.EnumField{}
@@ -2168,8 +2166,8 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
2168
2166
pos: pos
2169
2167
expr: expr
2170
2168
has_expr: has_expr
2171
- comments: p.eat_line_end_comments ( )
2172
- next_comments: p.eat_comments ()
2169
+ comments: p.eat_comments (same_line: true )
2170
+ next_comments: p.eat_comments ({} )
2173
2171
}
2174
2172
}
2175
2173
p.top_level_statement_end ()
@@ -2248,7 +2246,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
2248
2246
// function type: `type mycallback = fn(string, int)`
2249
2247
fn_name := p.prepend_mod (name)
2250
2248
fn_type := p.parse_fn_type (fn_name)
2251
- comments = p.eat_line_end_comments ( )
2249
+ comments = p.eat_comments (same_line: true )
2252
2250
return ast.FnTypeDecl{
2253
2251
name: fn_name
2254
2252
is_pub: is_pub
@@ -2296,7 +2294,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
2296
2294
}
2297
2295
is_public: is_pub
2298
2296
})
2299
- comments = p.eat_line_end_comments ( )
2297
+ comments = p.eat_comments (same_line: true )
2300
2298
return ast.SumTypeDecl{
2301
2299
name: name
2302
2300
is_pub: is_pub
@@ -2332,7 +2330,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
2332
2330
p.error_with_pos ('a type alias can not refer to itself: $name ' , decl_pos.extend (type_alias_pos))
2333
2331
return ast.AliasTypeDecl{}
2334
2332
}
2335
- comments = p.eat_line_end_comments ( )
2333
+ comments = p.eat_comments (same_line: true )
2336
2334
return ast.AliasTypeDecl{
2337
2335
name: name
2338
2336
is_pub: is_pub
0 commit comments