Skip to content

Commit

Permalink
ast, parser: cleanup in stringify_fn_after_name() (vlang#21799)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Jul 4, 2024
1 parent dceea80 commit 0c2d72f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 34 deletions.
3 changes: 1 addition & 2 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -685,8 +685,7 @@ pub:
is_hidden bool // interface first arg
on_newline bool // whether the argument starts on a new line
pub mut:
typ Type
comments []Comment
typ Type
}

pub fn (p &Param) specifier() string {
Expand Down
31 changes: 6 additions & 25 deletions vlib/v/ast/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ pub fn (t &Table) stringify_fn_decl(node &FnDecl, cur_mod string, m2a map[string

fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_mod string, m2a map[string]string) {
mut add_para_types := true
mut is_wrap_needed := false
if node.generic_names.len > 0 {
if node.is_method {
sym := t.sym(node.params[0].typ)
Expand Down Expand Up @@ -175,22 +174,6 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m
}
is_last_param := i == node.params.len - 1
is_type_only := param.name == ''
should_add_type := true // is_last_param || is_type_only || node.params[i + 1].typ != param.typ ||
// (node.is_variadic && i == node.params.len - 2)
pre_comments := param.comments.filter(it.pos.pos < param.pos.pos)
if pre_comments.len > 0 {
if i == 0 {
is_wrap_needed = true
f.write_string('\n\t')
}
write_comments(pre_comments, mut f)
if !f.last_n(1)[0].is_space() {
f.write_string(' ')
}
}
if is_wrap_needed {
f.write_string('\t')
}
if param.on_newline {
f.write_string('\n\t')
pline++
Expand Down Expand Up @@ -228,15 +211,13 @@ fn (t &Table) stringify_fn_after_name(node &FnDecl, mut f strings.Builder, cur_m
}
s = util.no_cur_mod(s, cur_mod)
s = shorten_full_name_based_on_aliases(s, m2a)
if should_add_type {
if !is_type_only {
f.write_string(' ')
}
if node.is_variadic && is_last_param {
f.write_string('...')
}
f.write_string(s)
if !is_type_only {
f.write_string(' ')
}
if node.is_variadic && is_last_param {
f.write_string('...')
}
f.write_string(s)
}
if !is_last_param {
f.write_string(',')
Expand Down
7 changes: 0 additions & 7 deletions vlib/v/parser/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,6 @@ fn (mut p Parser) fn_params() ([]ast.Param, bool, bool) {
if types_only {
mut param_no := 1
for p.tok.kind != .rpar {
mut comments := p.eat_comments()
if p.tok.kind == .eof {
p.error_with_pos('expecting `)`', p.tok.pos())
return []ast.Param{}, false, false
Expand Down Expand Up @@ -963,7 +962,6 @@ fn (mut p Parser) fn_params() ([]ast.Param, bool, bool) {
p.error_with_pos('expecting `)`', p.prev_tok.pos())
return []ast.Param{}, false, false
}
comments << p.eat_comments()

if p.tok.kind == .comma {
if is_variadic {
Expand All @@ -983,7 +981,6 @@ fn (mut p Parser) fn_params() ([]ast.Param, bool, bool) {
is_mut: is_mut
typ: param_type
type_pos: type_pos
comments: comments
on_newline: prev_param_newline != pos.line_nr
}
prev_param_newline = pos.line_nr
Expand All @@ -995,7 +992,6 @@ fn (mut p Parser) fn_params() ([]ast.Param, bool, bool) {
}
} else {
for p.tok.kind != .rpar {
mut comments := p.eat_comments()
if p.tok.kind == .eof {
p.error_with_pos('expecting `)`', p.tok.pos())
return []ast.Param{}, false, false
Expand All @@ -1008,7 +1004,6 @@ fn (mut p Parser) fn_params() ([]ast.Param, bool, bool) {
}
mut param_pos := [p.tok.pos()]
name := p.check_name()
comments << p.eat_comments()
mut param_names := [name]
if name != '' && p.fn_language == .v && name[0].is_capital() {
p.error_with_pos('parameter name must not begin with upper case letter (`${param_names[0]}`)',
Expand Down Expand Up @@ -1082,7 +1077,6 @@ fn (mut p Parser) fn_params() ([]ast.Param, bool, bool) {
// derive flags, however nr_muls only needs to be set on the array elem type, so clear it on the arg type
typ = ast.new_type(p.table.find_or_register_array(typ)).derive(typ).set_nr_muls(0).set_flag(.variadic)
}
comments << p.eat_comments()
for i, para_name in param_names {
alanguage := p.table.sym(typ).language
if alanguage != .v {
Expand All @@ -1096,7 +1090,6 @@ fn (mut p Parser) fn_params() ([]ast.Param, bool, bool) {
is_shared: is_shared
typ: typ
type_pos: type_pos[i]
comments: comments
on_newline: prev_param_newline != param_pos[i].line_nr
}
prev_param_newline = param_pos[i].line_nr
Expand Down

0 comments on commit 0c2d72f

Please sign in to comment.