From 11871d954452d728b943f849d4a04f024581b6ed Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 24 Jun 2020 14:35:00 +0200 Subject: [PATCH] fmt: re-run on parser.v and fmt.v --- cmd/tools/vfmt.v | 2 +- vlib/v/fmt/fmt.v | 52 +++++++++++++++++++++++++++--------------- vlib/v/parser/parser.v | 26 +++++++++++++-------- 3 files changed, 50 insertions(+), 30 deletions(-) diff --git a/cmd/tools/vfmt.v b/cmd/tools/vfmt.v index 453b892e43..af6b95c289 100644 --- a/cmd/tools/vfmt.v +++ b/cmd/tools/vfmt.v @@ -44,7 +44,7 @@ fn main() { foptions := FormatOptions{ is_c: '-c' in args is_l: '-l' in args - is_w: '-ww' in args + is_w: '-w' in args is_diff: '-diff' in args is_verbose: '-verbose' in args || '--verbose' in args is_all: '-all' in args || '--all' in args diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 6902b29c8f..3454deb6ad 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -84,7 +84,6 @@ fn (mut f Fmt) find_comment(line_nr int) { } } */ - pub fn (mut f Fmt) write(s string) { if f.expr_recursion == 0 || f.is_inside_interp { if f.indent > 0 && f.empty_line { @@ -118,7 +117,7 @@ pub fn (mut f Fmt) writeln(s string) { f.write(f.expr_bufs[i]) f.wrap_long_line(p, true) } - f.write(f.expr_bufs[f.expr_bufs.len-1]) + f.write(f.expr_bufs[f.expr_bufs.len - 1]) f.expr_bufs = []string{} f.penalties = []int{} f.expr_recursion = 0 @@ -127,7 +126,11 @@ pub fn (mut f Fmt) writeln(s string) { // println(f.indent.str() + s) f.out.write(tabs[f.indent]) } - f.out.writeln(if empty_fifo {''} else {s}) + f.out.writeln(if empty_fifo { + '' + } else { + s + }) f.empty_line = true f.line_len = 0 } @@ -140,9 +143,9 @@ fn (mut f Fmt) adjust_complete_line() { for _, s in f.expr_bufs { l += s.len } - if f.line_len + l <= max_len[max_len.len-1] { + if f.line_len + l <= max_len[max_len.len - 1] { for i, _ in f.penalties { - f.penalties[i] = max_len.len-1 + f.penalties[i] = max_len.len - 1 } } } @@ -401,7 +404,7 @@ pub fn (mut f Fmt) stmt(node ast.Stmt) { } f.writeln('') } - ast.SqlInsertExpr {} + ast.SqlStmt {} ast.StructDecl { f.struct_decl(it) } @@ -445,8 +448,8 @@ pub fn (mut f Fmt) type_decl(node ast.TypeDecl) { } } is_last_arg := i == fn_info.args.len - 1 - should_add_type := is_last_arg || fn_info.args[i + 1].typ != arg.typ || (fn_info.is_variadic && - i == fn_info.args.len - 2) + should_add_type := is_last_arg || fn_info.args[i + 1].typ != arg.typ || + (fn_info.is_variadic && i == fn_info.args.len - 2) if should_add_type { if fn_info.is_variadic && is_last_arg { f.write(' ...' + s) @@ -501,9 +504,11 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { end_pos := field.pos.pos + field.pos.len mut comments_len := 0 // Length of comments between field name and type for comment in field.comments { - if comment.pos.pos >= end_pos { break } + if comment.pos.pos >= end_pos { + break + } if comment.pos.pos > field.pos.pos { - comments_len += '/* ${comment.text} */ '.len + comments_len += '/* $comment.text */ '.len } } if comments_len + field.name.len > max { @@ -562,7 +567,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { f.struct_field_expr(field.default_expr) } // Handle comments after field type (same line) - for j < comments.len && field.pos.line_nr == comments[j].pos.line_nr{ + for j < comments.len && field.pos.line_nr == comments[j].pos.line_nr { f.write(' // ${comments[j].text}') // TODO: handle in a function j++ } @@ -740,7 +745,7 @@ pub fn (mut f Fmt) expr(node ast.Expr) { f.write(f.expr_bufs[i]) f.wrap_long_line(p, true) } - f.write(f.expr_bufs[f.expr_bufs.len-1]) + f.write(f.expr_bufs[f.expr_bufs.len - 1]) f.expr_bufs = []string{} f.penalties = []int{} } @@ -910,7 +915,11 @@ pub fn (mut f Fmt) wrap_long_line(penalty int, add_indent bool) bool { if f.out.buf[f.out.buf.len - 1] == ` ` { f.out.go_back(1) } - f.write('\n' + tabs[f.indent + if add_indent { 1 } else { 0 }]) + f.write('\n' + tabs[f.indent + if add_indent { + 1 + } else { + 0 + }]) f.line_len = 0 return true } @@ -1015,8 +1024,10 @@ pub fn (mut f Fmt) short_module(name string) string { } pub fn (mut f Fmt) if_expr(it ast.IfExpr) { - single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == 1 && - it.branches[1].stmts.len == 1 && (it.is_expr || f.is_assign) + single_line := it.branches.len == 2 && it.has_else && + it.branches[0].stmts.len == 1 && + it.branches[1].stmts.len == 1 && + (it.is_expr || f.is_assign) f.single_line_if = single_line for i, branch in it.branches { if branch.comment.text != '' { @@ -1313,12 +1324,15 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) { if last_line_nr < line_nr { penalty-- } - if i == 0 || it.exprs[i-1] is ast.ArrayInit || it.exprs[i-1] is ast.StructInit || - it.exprs[i-1] is ast.MapInit || it.exprs[i-1] is ast.CallExpr { + if i == 0 || it.exprs[i - 1] is ast.ArrayInit || + it.exprs[i - 1] is ast.StructInit || + it.exprs[i - 1] is ast.MapInit || + it.exprs[i - 1] is ast.CallExpr { penalty-- } - if expr is ast.ArrayInit || expr is ast.StructInit || - expr is ast.MapInit || expr is ast.CallExpr { + if expr is ast.ArrayInit || + expr is ast.StructInit || expr is ast.MapInit || + expr is ast.CallExpr { penalty-- } is_new_line := f.wrap_long_line(penalty, !inc_indent) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 53fa2eefaa..478b6ceef7 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -734,8 +734,9 @@ fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt { left0 := left[0] if p.tok.kind in [.assign, .decl_assign] || p.tok.kind.is_assign() { return p.partial_assign_stmt(left) - } else if is_top_level && tok.kind !in [.key_if, .key_match] && left0 !is ast.CallExpr && - left0 !is ast.PostfixExpr && !(left0 is ast.InfixExpr && (left0 as ast.InfixExpr).op == .left_shift) && + } else if is_top_level && tok.kind !in [.key_if, .key_match] && + left0 !is ast.CallExpr && left0 !is ast.PostfixExpr && !(left0 is ast.InfixExpr && + (left0 as ast.InfixExpr).op == .left_shift) && left0 !is ast.ComptimeCall { p.error_with_pos('expression evaluated but not used', left0.position()) } @@ -848,7 +849,8 @@ pub fn (mut p Parser) name_expr() ast.Expr { else {} } } - if p.peek_tok.kind == .dot && !known_var && (language != .v || p.known_import(p.tok.lit) || + if p.peek_tok.kind == .dot && !known_var && + (language != .v || p.known_import(p.tok.lit) || p.mod.all_after_last('.') == p.tok.lit) { if language == .c { mod = 'C' @@ -867,7 +869,8 @@ pub fn (mut p Parser) name_expr() ast.Expr { } // p.warn('name expr $p.tok.lit $p.peek_tok.str()') // fn call or type cast - if p.peek_tok.kind == .lpar || (p.peek_tok.kind == .lt && p.peek_tok2.kind == .name && + if p.peek_tok.kind == .lpar || + (p.peek_tok.kind == .lt && p.peek_tok2.kind == .name && p.peek_tok3.kind == .gt) { // foo() or foo() mut name := p.tok.lit @@ -877,7 +880,8 @@ pub fn (mut p Parser) name_expr() ast.Expr { name_w_mod := p.prepend_mod(name) // type cast. TODO: finish // if name in table.builtin_type_names { - if !known_var && (name in p.table.type_idxs || name_w_mod in p.table.type_idxs) && + if !known_var && (name in p.table.type_idxs || + name_w_mod in p.table.type_idxs) && name !in ['C.stat', 'C.sigaction'] { // TODO handle C.stat() mut to_typ := p.parse_type() @@ -914,8 +918,7 @@ pub fn (mut p Parser) name_expr() ast.Expr { } else if p.peek_tok.kind == .lcbr && !p.inside_match && !p.inside_match_case && !p.inside_if && !p.inside_for { // && (p.tok.lit[0].is_capital() || p.builtin_mod) { return p.struct_init(false) // short_syntax: false - } else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var && language == - .v) { + } else if p.peek_tok.kind == .dot && (p.tok.lit[0].is_capital() && !known_var && language == .v) { // `Color.green` mut enum_name := p.check_name() if mod != '' { @@ -1332,7 +1335,9 @@ fn (mut p Parser) const_decl() ast.ConstDecl { mut comments := []ast.Comment{} for p.tok.kind == .comment { comments << p.comment() - if p.tok.kind == .rpar {break} + if p.tok.kind == .rpar { + break + } } pos := p.tok.position() name := p.check_name() @@ -1389,8 +1394,9 @@ const ( // left hand side of `=` or `:=` in `a,b,c := 1,2,3` fn (mut p Parser) global_decl() ast.GlobalDecl { if !p.pref.translated && !p.pref.is_livemain && !p.builtin_mod && !p.pref.building_v && - p.mod != 'ui' && p.mod != 'gg2' && p.mod != 'uiold' && !os.getwd().contains('/volt') && !p.pref.enable_globals && - !p.pref.is_fmt && p.mod !in global_enabled_mods { + p.mod != 'ui' && p.mod != 'gg2' && + p.mod != 'uiold' && !p.pref.enable_globals && !p.pref.is_fmt && + p.mod !in global_enabled_mods { p.error('use `v --enable-globals ...` to enable globals') } start_pos := p.tok.position()