Skip to content

Commit cb1f63f

Browse files
author
Lukas Neubert
authored
parser: replace eat_line_end_comments() with configurable eat_comments() (#8636)
1 parent 5abd49d commit cb1f63f

File tree

7 files changed

+51
-53
lines changed

7 files changed

+51
-53
lines changed

vlib/v/parser/assign.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme
9999
p.next()
100100
mut comments := []ast.Comment{cap: 2 * left_comments.len + 1}
101101
comments << left_comments
102-
comments << p.eat_comments()
102+
comments << p.eat_comments({})
103103
mut right_comments := []ast.Comment{}
104104
mut right := []ast.Expr{cap: left.len}
105105
if p.tok.kind == .key_go {
@@ -113,7 +113,7 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme
113113
right, right_comments = p.expr_list()
114114
}
115115
comments << right_comments
116-
end_comments := p.eat_line_end_comments()
116+
end_comments := p.eat_comments(same_line: true)
117117
mut has_cross_var := false
118118
if op == .decl_assign {
119119
// a, b := a + 1, b

vlib/v/parser/containers.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn (mut p Parser) array_init() ast.ArrayInit {
4141
// [1,2,3] or [const]byte
4242
for i := 0; p.tok.kind !in [.rsbr, .eof]; i++ {
4343
exprs << p.expr(0)
44-
ecmnts << p.eat_comments()
44+
ecmnts << p.eat_comments({})
4545
if p.tok.kind == .comma {
4646
p.next()
4747
}

vlib/v/parser/fn.v

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub fn (mut p Parser) call_expr(language table.Language, mod string) ast.CallExp
8787
if fn_name in p.imported_symbols {
8888
fn_name = p.imported_symbols[fn_name]
8989
}
90-
comments := p.eat_line_end_comments()
90+
comments := p.eat_comments(same_line: true)
9191
pos.update_last_line(p.prev_tok.line_nr)
9292
return ast.CallExpr{
9393
name: fn_name
@@ -121,7 +121,7 @@ pub fn (mut p Parser) call_args() []ast.CallArg {
121121
if is_mut {
122122
p.next()
123123
}
124-
mut comments := p.eat_comments()
124+
mut comments := p.eat_comments({})
125125
arg_start_pos := p.tok.position()
126126
mut array_decompose := false
127127
if p.tok.kind == .ellipsis {
@@ -146,7 +146,7 @@ pub fn (mut p Parser) call_args() []ast.CallArg {
146146
comments = []ast.Comment{}
147147
}
148148
pos := arg_start_pos.extend(p.prev_tok.position())
149-
comments << p.eat_comments()
149+
comments << p.eat_comments({})
150150
args << ast.CallArg{
151151
is_mut: is_mut
152152
share: table.sharetype_from_flags(is_shared, is_atomic)

vlib/v/parser/if_match.v

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
3434
p.tok.position()
3535
}
3636
if p.tok.kind == .key_else {
37-
comments << p.eat_comments()
37+
comments << p.eat_comments({})
3838
p.check(.key_else)
39-
comments << p.eat_comments()
39+
comments << p.eat_comments({})
4040
if p.tok.kind == .key_match {
4141
p.error('cannot use `match` with `if` statements')
4242
return ast.IfExpr{}
@@ -84,7 +84,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
8484
p.error('cannot use `match` with `if` statements')
8585
return ast.IfExpr{}
8686
}
87-
comments << p.eat_comments()
87+
comments << p.eat_comments({})
8888
mut cond := ast.Expr{}
8989
mut is_guard := false
9090
// `if x := opt() {`
@@ -93,9 +93,9 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
9393
is_guard = true
9494
var_pos := p.tok.position()
9595
var_name := p.check_name()
96-
comments << p.eat_comments()
96+
comments << p.eat_comments({})
9797
p.check(.decl_assign)
98-
comments << p.eat_comments()
98+
comments << p.eat_comments({})
9999
expr := p.expr(0)
100100
cond = ast.IfGuardExpr{
101101
var_name: var_name
@@ -111,7 +111,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
111111
prev_guard = false
112112
cond = p.expr(0)
113113
}
114-
comments << p.eat_comments()
114+
comments << p.eat_comments({})
115115
end_pos := p.prev_tok.position()
116116
body_pos := p.tok.position()
117117
p.inside_if = false
@@ -129,7 +129,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
129129
if is_guard {
130130
p.close_scope()
131131
}
132-
comments = p.eat_comments()
132+
comments = p.eat_comments({})
133133
if is_comptime {
134134
if p.tok.kind == .key_else {
135135
p.error('use `\$else` instead of `else` in compile-time `if` branches')
@@ -168,7 +168,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
168168
if !no_lcbr {
169169
p.check(.lcbr)
170170
}
171-
comments := p.eat_comments() // comments before the first branch
171+
comments := p.eat_comments({}) // comments before the first branch
172172
mut branches := []ast.MatchBranch{}
173173
for p.tok.kind != .eof {
174174
branch_first_pos := p.tok.position()
@@ -188,7 +188,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
188188
for {
189189
// Sum type match
190190
parsed_type := p.parse_type()
191-
ecmnts << p.eat_comments()
191+
ecmnts << p.eat_comments({})
192192
types << parsed_type
193193
exprs << ast.Type{
194194
typ: parsed_type
@@ -205,7 +205,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
205205
for {
206206
p.inside_match_case = true
207207
expr := p.expr(0)
208-
ecmnts << p.eat_comments()
208+
ecmnts << p.eat_comments({})
209209
p.inside_match_case = false
210210
if p.tok.kind == .dotdot {
211211
p.error_with_pos('match only supports inclusive (`...`) ranges, not exclusive (`..`)',
@@ -238,7 +238,7 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
238238
p.close_scope()
239239
p.inside_match_body = false
240240
pos := branch_first_pos.extend_with_last_line(branch_last_pos, p.prev_tok.line_nr)
241-
post_comments := p.eat_comments()
241+
post_comments := p.eat_comments({})
242242
branches << ast.MatchBranch{
243243
exprs: exprs
244244
ecmnts: ecmnts
@@ -405,7 +405,7 @@ fn (mut p Parser) select_expr() ast.SelectExpr {
405405
pos: branch_first_pos.pos
406406
len: branch_last_pos.pos - branch_first_pos.pos + branch_last_pos.len
407407
}
408-
post_comments := p.eat_comments()
408+
post_comments := p.eat_comments({})
409409
pos.update_last_line(p.prev_tok.line_nr)
410410
if post_comments.len > 0 {
411411
pos.last_line = post_comments.last().pos.last_line

vlib/v/parser/parser.v

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -598,25 +598,23 @@ pub fn (mut p Parser) comment_stmt() ast.ExprStmt {
598598
}
599599
}
600600

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
610604
}
611605

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
614608
mut comments := []ast.Comment{}
615609
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) {
617612
break
618613
}
619614
comments << p.comment()
615+
if cfg.follow_up {
616+
line = p.prev_tok.line_nr
617+
}
620618
}
621619
return comments
622620
}
@@ -1558,7 +1556,7 @@ fn (mut p Parser) dot_expr(left ast.Expr) ast.Expr {
15581556
//
15591557
end_pos := p.prev_tok.position()
15601558
pos := name_pos.extend(end_pos)
1561-
comments := p.eat_line_end_comments()
1559+
comments := p.eat_comments(same_line: true)
15621560
mcall_expr := ast.CallExpr{
15631561
left: left
15641562
name: field_name
@@ -1919,7 +1917,7 @@ fn (mut p Parser) import_stmt() ast.Import {
19191917
return import_node
19201918
}
19211919
}
1922-
import_node.comments = p.eat_line_end_comments()
1920+
import_node.comments = p.eat_comments(same_line: true)
19231921
p.imports[mod_alias] = mod_name
19241922
// if mod_name !in p.table.imports {
19251923
p.table.imports << mod_name
@@ -1992,7 +1990,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
19921990
p.error_with_pos('const declaration is missing closing `)`', const_pos)
19931991
return ast.ConstDecl{}
19941992
}
1995-
comments = p.eat_comments()
1993+
comments = p.eat_comments({})
19961994
if p.tok.kind == .rpar {
19971995
break
19981996
}
@@ -2043,7 +2041,7 @@ fn (mut p Parser) return_stmt() ast.Return {
20432041
first_pos := p.tok.position()
20442042
p.next()
20452043
// no return
2046-
mut comments := p.eat_comments()
2044+
mut comments := p.eat_comments({})
20472045
if p.tok.kind == .rcbr {
20482046
return ast.Return{
20492047
comments: comments
@@ -2085,7 +2083,7 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
20852083
mut fields := []ast.GlobalField{}
20862084
mut comments := []ast.Comment{}
20872085
for {
2088-
comments = p.eat_comments()
2086+
comments = p.eat_comments({})
20892087
if p.tok.kind == .rpar {
20902088
break
20912089
}
@@ -2147,7 +2145,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
21472145
}
21482146
name := p.prepend_mod(enum_name)
21492147
p.check(.lcbr)
2150-
enum_decl_comments := p.eat_comments()
2148+
enum_decl_comments := p.eat_comments({})
21512149
mut vals := []string{}
21522150
// mut default_exprs := []ast.Expr{}
21532151
mut fields := []ast.EnumField{}
@@ -2168,8 +2166,8 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
21682166
pos: pos
21692167
expr: expr
21702168
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({})
21732171
}
21742172
}
21752173
p.top_level_statement_end()
@@ -2248,7 +2246,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
22482246
// function type: `type mycallback = fn(string, int)`
22492247
fn_name := p.prepend_mod(name)
22502248
fn_type := p.parse_fn_type(fn_name)
2251-
comments = p.eat_line_end_comments()
2249+
comments = p.eat_comments(same_line: true)
22522250
return ast.FnTypeDecl{
22532251
name: fn_name
22542252
is_pub: is_pub
@@ -2296,7 +2294,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
22962294
}
22972295
is_public: is_pub
22982296
})
2299-
comments = p.eat_line_end_comments()
2297+
comments = p.eat_comments(same_line: true)
23002298
return ast.SumTypeDecl{
23012299
name: name
23022300
is_pub: is_pub
@@ -2332,7 +2330,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
23322330
p.error_with_pos('a type alias can not refer to itself: $name', decl_pos.extend(type_alias_pos))
23332331
return ast.AliasTypeDecl{}
23342332
}
2335-
comments = p.eat_line_end_comments()
2333+
comments = p.eat_comments(same_line: true)
23362334
return ast.AliasTypeDecl{
23372335
name: name
23382336
is_pub: is_pub

vlib/v/parser/pratt.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
1818
is_stmt_ident := p.is_stmt_ident
1919
p.is_stmt_ident = false
2020
if !p.pref.is_fmt {
21-
p.eat_comments()
21+
p.eat_comments({})
2222
}
2323
// Prefix
2424
match p.tok.kind {

vlib/v/parser/struct.v

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
234234
field_pos = field_start_pos.extend(type_pos)
235235
}
236236
// Comments after type (same line)
237-
comments << p.eat_comments()
237+
comments << p.eat_comments({})
238238
if p.tok.kind == .lsbr {
239239
// attrs are stored in `p.attrs`
240240
p.attributes()
@@ -252,7 +252,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
252252
else {}
253253
}
254254
has_default_expr = true
255-
comments << p.eat_comments()
255+
comments << p.eat_comments({})
256256
}
257257
// TODO merge table and ast Fields?
258258
ast_fields << ast.StructField{
@@ -343,7 +343,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
343343
if !short_syntax {
344344
p.check(.lcbr)
345345
}
346-
pre_comments := p.eat_comments()
346+
pre_comments := p.eat_comments({})
347347
mut fields := []ast.StructInitField{}
348348
mut i := 0
349349
no_keys := p.peek_tok.kind != .colon && p.tok.kind != .rcbr && p.tok.kind != .ellipsis // `Vec{a,b,c}
@@ -364,19 +364,19 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
364364
// name will be set later in checker
365365
expr = p.expr(0)
366366
field_pos = expr.position()
367-
comments = p.eat_line_end_comments()
367+
comments = p.eat_comments(same_line: true)
368368
} else if is_update_expr {
369369
// struct updating syntax; f2 := Foo{ ...f, name: 'f2' }
370370
p.check(.ellipsis)
371371
update_expr = p.expr(0)
372-
update_expr_comments << p.eat_line_end_comments()
372+
update_expr_comments << p.eat_comments(same_line: true)
373373
has_update_expr = true
374374
} else {
375375
first_field_pos := p.tok.position()
376376
field_name = p.check_name()
377377
p.check(.colon)
378378
expr = p.expr(0)
379-
comments = p.eat_line_end_comments()
379+
comments = p.eat_comments(same_line: true)
380380
last_field_pos := expr.position()
381381
field_len := if last_field_pos.len > 0 {
382382
last_field_pos.pos - first_field_pos.pos + last_field_pos.len
@@ -393,8 +393,8 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
393393
if p.tok.kind == .comma {
394394
p.next()
395395
}
396-
comments << p.eat_line_end_comments()
397-
nline_comments << p.eat_comments()
396+
comments << p.eat_comments(same_line: true)
397+
nline_comments << p.eat_comments({})
398398
if !is_update_expr {
399399
fields << ast.StructInitField{
400400
name: field_name
@@ -435,7 +435,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
435435
interface_name := p.prepend_mod(p.check_name()).clone()
436436
// println('interface decl $interface_name')
437437
p.check(.lcbr)
438-
pre_comments := p.eat_comments()
438+
pre_comments := p.eat_comments({})
439439
// Declare the type
440440
reg_idx := p.table.register_type_symbol(
441441
is_public: is_pub
@@ -506,8 +506,8 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
506506
if p.tok.kind.is_start_of_type() && p.tok.line_nr == line_nr {
507507
method.return_type = p.parse_type()
508508
}
509-
mcomments := p.eat_line_end_comments()
510-
mnext_comments := p.eat_comments()
509+
mcomments := p.eat_comments(same_line: true)
510+
mnext_comments := p.eat_comments({})
511511
method.comments = mcomments
512512
method.next_comments = mnext_comments
513513
methods << method

0 commit comments

Comments
 (0)