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
}
}
if v.pref.compile_defines.len > 0 {
for cdefine in v.pref.compile_defines {
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))
p = {
p |
@ -214,11 +212,11 @@ fn (v mut V) new_parser(scanner &Scanner) Parser {
cur_fn: EmptyFn
cgen: v.cgen
// x64: v.x64
pref: v.pref
os: v.pref.os
vroot: v.pref.vroot
local_vars: [Var{
}].repeat(MaxLocalVars)
local_vars: [Var{}].repeat(MaxLocalVars)
import_table: new_import_table()
}
$if js {
@ -318,8 +316,8 @@ fn (p &Parser) peek_token() Token {
return p.tokens[p.token_idx]
}
fn (p &Parser) log(s string) {
}
fn (p &Parser) log(s string) {}
/*
if !p.pref.is_verbose {
return
@ -327,6 +325,7 @@ fn (p &Parser) log(s string) {
println(s)
*/
pub fn (p &Parser) save_state() ParserState {
return ParserState{
scanner_file_path: p.scanner.file_path
@ -467,7 +466,6 @@ fn (p mut Parser) parse(pass Pass) {
}
return
}
parsing_start_ticks := time.ticks()
compile_cycles_stuck_mask := u64(0x1FFFFFFF) // 2^29-1 cycles
mut parsing_cycle := u64(1)
@ -494,7 +492,8 @@ fn (p mut Parser) parse(pass Pass) {
// (for example, by DOOM). such fields are
// basically int consts
p.enum_decl(true)
} else {
}
else {
p.error('Nameless enums are not allowed.')
}
}
@ -518,7 +517,8 @@ fn (p mut Parser) parse(pass Pass) {
}
else {
p.error('wrong pub keyword usage')
}}
}
}
}
.key_fn {
p.fn_decl()
@ -547,9 +547,7 @@ fn (p mut Parser) parse(pass Pass) {
p.comp_time()
}
.key_global {
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 {
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.error('use `v --enable-globals ...` to enable globals')
}
p.next()
@ -627,7 +625,8 @@ fn (p mut Parser) parse(pass Pass) {
else {
p.error('unexpected token `${p.strtok()}`')
}
}}
}
}
}
}
@ -847,7 +846,8 @@ fn (p mut Parser) type_decl() {
// Register the rest of them
mut idx := 0
mut done := false
for {//p.tok == .pipe {
for {
// p.tok == .pipe {
idx++
// p.next()
child_type_name := p.check_name()
@ -896,7 +896,6 @@ int typ;
')
}
else {
parent = p.get_type2()
}
nt_pair := p.table.cgen_name_type_pair(name, parent.name)
@ -919,7 +918,6 @@ int typ;
p.fgen_nl()
// }
}
// current token is `(`
fn (p mut Parser) interface_method(field_name, receiver string) &Fn {
mut method := &Fn{
@ -955,8 +953,8 @@ fn key_to_type_cat(tok TokenKind) TypeCategory {
.key_union {
return .union_
}
else {
}}
else {}
}
verror('Unknown token: $tok')
return .builtin
}
@ -1096,7 +1094,6 @@ fn (p mut Parser) get_type() string {
if is_question {
p.check(.question)
}
// multiple returns
if p.tok == .lpar {
// p.warn('`()` are no longer necessary in multiple returns' +
@ -1122,7 +1119,6 @@ fn (p mut Parser) get_type() string {
}
return typ
}
// arrays ([]int)
mut arr_level := 0
for p.tok == .lsbr {
@ -1215,7 +1211,7 @@ fn (p mut Parser) get_type() string {
if t_suggest.len > 0 {
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')
}
}
@ -1561,13 +1557,13 @@ fn (p mut Parser) statement(add_semi bool) string {
else {
// An expression as a statement
typ := p.expression()
if p.inside_if_expr {
}
if p.inside_if_expr {}
else {
p.genln('; ')
}
return typ
}}
}
}
// ? : uses , as statement separators
if p.inside_if_expr && p.tok != .rcbr {
p.gen(', ')
@ -2067,9 +2063,8 @@ fn (p mut Parser) var_expr(v Var) string {
p.gen(p.tok.str())
p.next() // ++/--
// 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)
}
else {
return typ
}
@ -2898,11 +2893,7 @@ fn (p mut Parser) return_st() {
mut cur_fn_typ_chk := p.cur_fn.typ
// multiple returns
if types.len > 1 {
mr_type := if p.cur_fn.typ.starts_with('Option_') {
p.cur_fn.typ[7..]
} else {
p.cur_fn.typ
}
mr_type := if p.cur_fn.typ.starts_with('Option_') { p.cur_fn.typ[7..] } else { p.cur_fn.typ }
expr_type = mr_type
expr_type_chk = types.join(',')
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 == '' {
return
}
// the imports are usually at the start of the file
// p.production_error_with_token_index('the following imports were never used: $output', 0)
if p.pref.is_verbose {
@ -3216,11 +3206,10 @@ fn (p mut Parser) skip_block(inside_first_lcbr bool) {
p.check(.rcbr)
}
fn todo_remove() {
fn todo_remove() {}
// x64.new_gen('f')
}
// }
fn (p mut Parser) check_if_parser_is_stuck(parsing_cycle u64, parsing_start_ticks i64) {
// QTODO
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
*/
}

View File

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