1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fmt: wrap_long_line()

This commit is contained in:
Alexander Medvednikov 2020-02-21 16:14:15 +01:00
parent 728aeb8092
commit e9931e6f84
2 changed files with 76 additions and 76 deletions

View File

@ -165,7 +165,6 @@ fn (v mut V) new_parser_from_file(path string) Parser {
break break
} }
} }
if v.pref.compile_defines.len > 0 { if v.pref.compile_defines.len > 0 {
for cdefine in v.pref.compile_defines { for cdefine in v.pref.compile_defines {
custom_path_ending := '_d_${cdefine}.v' custom_path_ending := '_d_${cdefine}.v'
@ -176,7 +175,6 @@ fn (v mut V) new_parser_from_file(path string) Parser {
} }
} }
} }
mut p := v.new_parser(new_scanner_file(path)) mut p := v.new_parser(new_scanner_file(path))
p = { p = {
p | p |
@ -214,11 +212,11 @@ fn (v mut V) new_parser(scanner &Scanner) Parser {
cur_fn: EmptyFn cur_fn: EmptyFn
cgen: v.cgen cgen: v.cgen
// x64: v.x64 // x64: v.x64
pref: v.pref pref: v.pref
os: v.pref.os os: v.pref.os
vroot: v.pref.vroot vroot: v.pref.vroot
local_vars: [Var{ local_vars: [Var{}].repeat(MaxLocalVars)
}].repeat(MaxLocalVars)
import_table: new_import_table() import_table: new_import_table()
} }
$if js { $if js {
@ -318,8 +316,8 @@ fn (p &Parser) peek_token() Token {
return p.tokens[p.token_idx] return p.tokens[p.token_idx]
} }
fn (p &Parser) log(s string) { fn (p &Parser) log(s string) {}
}
/* /*
if !p.pref.is_verbose { if !p.pref.is_verbose {
return return
@ -327,6 +325,7 @@ fn (p &Parser) log(s string) {
println(s) println(s)
*/ */
pub fn (p &Parser) save_state() ParserState { pub fn (p &Parser) save_state() ParserState {
return ParserState{ return ParserState{
scanner_file_path: p.scanner.file_path scanner_file_path: p.scanner.file_path
@ -467,7 +466,6 @@ fn (p mut Parser) parse(pass Pass) {
} }
return return
} }
parsing_start_ticks := time.ticks() parsing_start_ticks := time.ticks()
compile_cycles_stuck_mask := u64(0x1FFFFFFF) // 2^29-1 cycles compile_cycles_stuck_mask := u64(0x1FFFFFFF) // 2^29-1 cycles
mut parsing_cycle := u64(1) mut parsing_cycle := u64(1)
@ -494,7 +492,8 @@ fn (p mut Parser) parse(pass Pass) {
// (for example, by DOOM). such fields are // (for example, by DOOM). such fields are
// basically int consts // basically int consts
p.enum_decl(true) p.enum_decl(true)
} else { }
else {
p.error('Nameless enums are not allowed.') p.error('Nameless enums are not allowed.')
} }
} }
@ -518,7 +517,8 @@ fn (p mut Parser) parse(pass Pass) {
} }
else { else {
p.error('wrong pub keyword usage') p.error('wrong pub keyword usage')
}} }
}
} }
.key_fn { .key_fn {
p.fn_decl() p.fn_decl()
@ -547,9 +547,7 @@ fn (p mut Parser) parse(pass Pass) {
p.comp_time() p.comp_time()
} }
.key_global { .key_global {
if !p.pref.translated && !p.pref.is_live && !p.builtin_mod && !p.pref.building_v && if !p.pref.translated && !p.pref.is_live && !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.mod != 'ui' && p.mod != 'gg2' && p.mod != 'uiold' && !os.getwd().contains('/volt') &&
!p.pref.enable_globals {
p.error('use `v --enable-globals ...` to enable globals') p.error('use `v --enable-globals ...` to enable globals')
} }
p.next() p.next()
@ -627,7 +625,8 @@ fn (p mut Parser) parse(pass Pass) {
else { else {
p.error('unexpected token `${p.strtok()}`') p.error('unexpected token `${p.strtok()}`')
} }
}} }
}
} }
} }
@ -847,7 +846,8 @@ fn (p mut Parser) type_decl() {
// Register the rest of them // Register the rest of them
mut idx := 0 mut idx := 0
mut done := false mut done := false
for {//p.tok == .pipe { for {
// p.tok == .pipe {
idx++ idx++
// p.next() // p.next()
child_type_name := p.check_name() child_type_name := p.check_name()
@ -896,7 +896,6 @@ int typ;
') ')
} }
else { else {
parent = p.get_type2() parent = p.get_type2()
} }
nt_pair := p.table.cgen_name_type_pair(name, parent.name) nt_pair := p.table.cgen_name_type_pair(name, parent.name)
@ -919,7 +918,6 @@ int typ;
p.fgen_nl() p.fgen_nl()
// } // }
} }
// current token is `(` // current token is `(`
fn (p mut Parser) interface_method(field_name, receiver string) &Fn { fn (p mut Parser) interface_method(field_name, receiver string) &Fn {
mut method := &Fn{ mut method := &Fn{
@ -955,8 +953,8 @@ fn key_to_type_cat(tok TokenKind) TypeCategory {
.key_union { .key_union {
return .union_ return .union_
} }
else { else {}
}} }
verror('Unknown token: $tok') verror('Unknown token: $tok')
return .builtin return .builtin
} }
@ -1096,7 +1094,6 @@ fn (p mut Parser) get_type() string {
if is_question { if is_question {
p.check(.question) p.check(.question)
} }
// multiple returns // multiple returns
if p.tok == .lpar { if p.tok == .lpar {
// p.warn('`()` are no longer necessary in multiple returns' + // p.warn('`()` are no longer necessary in multiple returns' +
@ -1122,7 +1119,6 @@ fn (p mut Parser) get_type() string {
} }
return typ return typ
} }
// arrays ([]int) // arrays ([]int)
mut arr_level := 0 mut arr_level := 0
for p.tok == .lsbr { for p.tok == .lsbr {
@ -1215,7 +1211,7 @@ fn (p mut Parser) get_type() string {
if t_suggest.len > 0 { if t_suggest.len > 0 {
t_suggest = '. did you mean: ($tc_suggest) `$t_suggest`' t_suggest = '. did you mean: ($tc_suggest) `$t_suggest`'
} }
econtext := if p.pref.is_debug { '('+@FILE+':'+@LINE+')' } else {''} econtext := if p.pref.is_debug { '(' + '/Users/alex/code/v/vlib/compiler/aparser.v' + ':' + '1236' + ')' } else { '' }
p.error('unknown type `$typ`$t_suggest $econtext') p.error('unknown type `$typ`$t_suggest $econtext')
} }
} }
@ -1561,13 +1557,13 @@ fn (p mut Parser) statement(add_semi bool) string {
else { else {
// An expression as a statement // An expression as a statement
typ := p.expression() typ := p.expression()
if p.inside_if_expr { if p.inside_if_expr {}
}
else { else {
p.genln('; ') p.genln('; ')
} }
return typ return typ
}} }
}
// ? : uses , as statement separators // ? : uses , as statement separators
if p.inside_if_expr && p.tok != .rcbr { if p.inside_if_expr && p.tok != .rcbr {
p.gen(', ') p.gen(', ')
@ -2067,9 +2063,8 @@ fn (p mut Parser) var_expr(v Var) string {
p.gen(p.tok.str()) p.gen(p.tok.str())
p.next() // ++/-- p.next() // ++/--
// allow `a := c++` in translated code TODO remove once c2v handles this // allow `a := c++` in translated code TODO remove once c2v handles this
if p.pref.translated { if p.pref.translated {}
// return p.index_expr(typ, fn_ph) // return p.index_expr(typ, fn_ph)
}
else { else {
return typ return typ
} }
@ -2898,11 +2893,7 @@ fn (p mut Parser) return_st() {
mut cur_fn_typ_chk := p.cur_fn.typ mut cur_fn_typ_chk := p.cur_fn.typ
// multiple returns // multiple returns
if types.len > 1 { if types.len > 1 {
mr_type := if p.cur_fn.typ.starts_with('Option_') { mr_type := if p.cur_fn.typ.starts_with('Option_') { p.cur_fn.typ[7..] } else { p.cur_fn.typ }
p.cur_fn.typ[7..]
} else {
p.cur_fn.typ
}
expr_type = mr_type expr_type = mr_type
expr_type_chk = types.join(',') expr_type_chk = types.join(',')
cur_fn_typ_chk = cur_fn_typ_chk.replace('_V_MulRet_', '').replace('_PTR_', '*').replace('_V_', ',') cur_fn_typ_chk = cur_fn_typ_chk.replace('_V_MulRet_', '').replace('_PTR_', '*').replace('_V_', ',')
@ -3174,7 +3165,6 @@ fn (p mut Parser) check_unused_imports() {
if output == '' { if output == '' {
return return
} }
// the imports are usually at the start of the file // the imports are usually at the start of the file
// p.production_error_with_token_index('the following imports were never used: $output', 0) // p.production_error_with_token_index('the following imports were never used: $output', 0)
if p.pref.is_verbose { if p.pref.is_verbose {
@ -3216,11 +3206,10 @@ fn (p mut Parser) skip_block(inside_first_lcbr bool) {
p.check(.rcbr) p.check(.rcbr)
} }
fn todo_remove() { fn todo_remove() {}
// x64.new_gen('f') // x64.new_gen('f')
} // }
fn (p mut Parser) check_if_parser_is_stuck(parsing_cycle u64, parsing_start_ticks i64) { fn (p mut Parser) check_if_parser_is_stuck(parsing_cycle u64, parsing_start_ticks i64) {
// QTODO // QTODO
p.warn('todo...') p.warn('todo...')
@ -3242,4 +3231,5 @@ Please create a GitHub issue: https://github.com/vlang/v/issues/new/choose
} }
p.prev_stuck_token_idx = p.token_idx p.prev_stuck_token_idx = p.token_idx
*/ */
} }

View File

@ -206,6 +206,9 @@ fn (f mut Fmt) expr(node ast.Expr) {
// type_sym := f.table.get_type_symbol(it.typ) // type_sym := f.table.get_type_symbol(it.typ)
f.write('[') f.write('[')
for i, expr in it.exprs { for i, expr in it.exprs {
if i > 0 {
f.wrap_long_line()
}
f.expr(expr) f.expr(expr)
if i < it.exprs.len - 1 { if i < it.exprs.len - 1 {
f.write(', ') f.write(', ')
@ -255,10 +258,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
ast.InfixExpr { ast.InfixExpr {
f.expr(it.left) f.expr(it.left)
f.write(' $it.op.str() ') f.write(' $it.op.str() ')
if f.line_len > max_len { f.wrap_long_line()
f.write('\n' + tabs[f.indent + 1])
f.line_len = 0
}
f.expr(it.right) f.expr(it.right)
} }
ast.IndexExpr { ast.IndexExpr {
@ -271,6 +271,9 @@ fn (f mut Fmt) expr(node ast.Expr) {
f.expr(it.expr) f.expr(it.expr)
f.write('.' + it.name + '(') f.write('.' + it.name + '(')
for i, arg in it.args { for i, arg in it.args {
if i > 0 {
f.wrap_long_line()
}
f.expr(arg) f.expr(arg)
if i < it.args.len - 1 { if i < it.args.len - 1 {
f.write(', ') f.write(', ')
@ -313,6 +316,13 @@ fn (f mut Fmt) expr(node ast.Expr) {
} }
} }
fn (f mut Fmt) wrap_long_line() {
if f.line_len > max_len {
f.write('\n' + tabs[f.indent + 1])
f.line_len = 0
}
}
fn (f mut Fmt) index_expr(node ast.IndexExpr) { fn (f mut Fmt) index_expr(node ast.IndexExpr) {
mut is_range := false mut is_range := false
match node.index { match node.index {