fmt: correctly increase f.line_len for write_indent (#8174)

pull/8178/head
Lukas Neubert 2021-01-18 06:02:29 +01:00 committed by GitHub
parent c936313c4c
commit 16c9cbce7c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 77 additions and 61 deletions

View File

@ -50,23 +50,25 @@ fn (context Context) file2v(bname string, fbytes []byte, bn_max int) string {
mut sb := strings.new_builder(1000)
bn_diff_len := bn_max - bname.len
sb.write('\t${bname}_len' + ' '.repeat(bn_diff_len - 4) + ' = $fbytes.len\n')
mut last_len := sb.len
fbyte := fbytes[0]
sb.write('\t$bname' + ' '.repeat(bn_diff_len) + ' = [byte($fbyte), ')
bnmae_line := '\t$bname' + ' '.repeat(bn_diff_len) + ' = [byte($fbyte), '
sb.write(bnmae_line)
mut line_len := bnmae_line.len + 3
for i := 1; i < fbytes.len; i++ {
b := int(fbytes[i]).str()
sb_diff_len := sb.len - last_len
if i < 30 && sb_diff_len > 86 {
sb.write('$b,\n\t\t')
last_len = sb.len
} else if sb_diff_len > 88 && 92 - sb_diff_len < b.len {
sb.write('$b,\n\t\t')
last_len = sb.len
} else if i == fbytes.len - 1 {
if line_len > 94 {
sb.go_back(1)
sb.write('\n\t\t')
line_len = 8
}
if i == fbytes.len - 1 {
sb.write(b)
line_len += b.len
} else {
sb.write('$b, ')
line_len += b.len + 2
}
}
sb.write(']!\n')
return sb.str()

View File

@ -305,7 +305,8 @@ fn (vd VDoc) gen_html(d doc.Doc) string {
'</style>\n${tabs[0]}<script>' + vd.assets['dark_mode_js'] + '</script>'
} else {
'\n${tabs[0]}<link rel="stylesheet" href="' + vd.assets['doc_css'] + '" />\n${tabs[0]}<link rel="stylesheet" href="' +
vd.assets['normalize_css'] + '" />\n${tabs[0]}<script src="' + vd.assets['dark_mode_js'] + '"></script>'
vd.assets['normalize_css'] + '" />\n${tabs[0]}<script src="' + vd.assets['dark_mode_js'] +
'"></script>'
}).replace('{{ toc_links }}', if cfg.is_multi || vd.docs.len > 1 {
modules_toc_str
} else {

View File

@ -10,8 +10,10 @@ const (
23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9]
de_bruijn64 = u64(0x03f79d71b4ca8b09)
de_bruijn64tab = [byte(0), 1, 56, 2, 57, 49, 28, 3, 61, 58, 42, 50, 38, 29, 17, 4, 62, 47,
59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5, 63, 55, 48, 27, 60, 41, 37, 16, 46, 35,
44, 21, 52, 32, 23, 11, 54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7, 6]
59, 36, 45, 43, 51, 22, 53, 39, 33, 30, 24, 18, 12, 5, 63, 55, 48, 27, 60, 41, 37, 16,
46, 35, 44, 21, 52, 32, 23, 11, 54, 26, 40, 15, 34, 20, 31, 10, 25, 14, 19, 9, 13, 8, 7,
6,
]
)
const (

View File

@ -10,11 +10,12 @@ import v.errors
pub type TypeDecl = AliasTypeDecl | FnTypeDecl | SumTypeDecl
pub type Expr = AnonFn | ArrayDecompose | ArrayInit | AsCast | Assoc | AtExpr | BoolLiteral |
CTempVar | CallExpr | CastExpr | ChanInit | CharLiteral | Comment | ComptimeCall | ComptimeSelector |
ConcatExpr | EnumVal | FloatLiteral | GoExpr | Ident | IfExpr | IfGuardExpr | IndexExpr |
InfixExpr | IntegerLiteral | Likely | LockExpr | MapInit | MatchExpr | None | OrExpr |
ParExpr | PostfixExpr | PrefixExpr | RangeExpr | SelectExpr | SelectorExpr | SizeOf |
SqlExpr | StringInterLiteral | StringLiteral | StructInit | Type | TypeOf | UnsafeExpr
CTempVar | CallExpr | CastExpr | ChanInit | CharLiteral | Comment | ComptimeCall |
ComptimeSelector | ConcatExpr | EnumVal | FloatLiteral | GoExpr | Ident | IfExpr |
IfGuardExpr | IndexExpr | InfixExpr | IntegerLiteral | Likely | LockExpr | MapInit |
MatchExpr | None | OrExpr | ParExpr | PostfixExpr | PrefixExpr | RangeExpr | SelectExpr |
SelectorExpr | SizeOf | SqlExpr | StringInterLiteral | StringLiteral | StructInit |
Type | TypeOf | UnsafeExpr
pub type Stmt = AssertStmt | AssignStmt | Block | BranchStmt | CompFor | ConstDecl | DeferStmt |
EnumDecl | ExprStmt | FnDecl | ForCStmt | ForInStmt | ForStmt | GlobalDecl | GoStmt |
@ -27,7 +28,8 @@ pub type ScopeObject = ConstField | GlobalField | Var
// TOOD: replace table.Param
pub type Node = ConstField | EnumField | Expr | Field | File | GlobalField | IfBranch |
MatchBranch | ScopeObject | SelectBranch | Stmt | StructField | StructInitField | table.Param
MatchBranch | ScopeObject | SelectBranch | Stmt | StructField | StructInitField |
table.Param
pub struct Type {
pub:

View File

@ -191,10 +191,10 @@ fn (mut v Builder) setup_ccompiler_options(ccompiler string) {
// warnings are totally fixed/removed
ccoptions.args = [v.pref.cflags, '-std=gnu99']
ccoptions.wargs = ['-Wall', '-Wextra', '-Wno-unused', '-Wno-missing-braces', '-Walloc-zero',
'-Wcast-qual', '-Wdate-time', '-Wduplicated-branches', '-Wduplicated-cond', '-Wformat=2', '-Winit-self',
'-Winvalid-pch', '-Wjump-misses-init', '-Wlogical-op', '-Wmultichar', '-Wnested-externs', '-Wnull-dereference',
'-Wpacked', '-Wpointer-arith', '-Wshadow', '-Wswitch-default', '-Wswitch-enum', '-Wno-unused-parameter',
'-Wno-unknown-warning-option', '-Wno-format-nonliteral', '-Wno-unused-command-line-argument']
'-Wcast-qual', '-Wdate-time', '-Wduplicated-branches', '-Wduplicated-cond', '-Wformat=2',
'-Winit-self', '-Winvalid-pch', '-Wjump-misses-init', '-Wlogical-op', '-Wmultichar', '-Wnested-externs',
'-Wnull-dereference', '-Wpacked', '-Wpointer-arith', '-Wshadow', '-Wswitch-default', '-Wswitch-enum',
'-Wno-unused-parameter', '-Wno-unknown-warning-option', '-Wno-format-nonliteral', '-Wno-unused-command-line-argument']
if v.pref.os == .ios {
ccoptions.args << '-framework Foundation'
ccoptions.args << '-framework UIKit'

View File

@ -365,8 +365,8 @@ pub fn (c &Checker) get_default_fmt(ftyp table.Type, typ table.Type) byte {
}
if ftyp in [table.string_type, table.bool_type] ||
sym.kind in
[.enum_, .array, .array_fixed, .struct_, .map, .multi_return, .sum_type, .none_] || ftyp.has_flag(.optional) ||
sym.has_method('str')
[.enum_, .array, .array_fixed, .struct_, .map, .multi_return, .sum_type, .none_] ||
ftyp.has_flag(.optional) || sym.has_method('str')
{
return `s`
} else {

View File

@ -22,7 +22,9 @@ const (
const (
valid_comp_if_os = ['windows', 'ios', 'macos', 'mach', 'darwin', 'hpux', 'gnu', 'qnx',
'linux', 'freebsd', 'openbsd', 'netbsd', 'bsd', 'dragonfly', 'android', 'solaris', 'haiku', 'linux_or_macos']
'linux', 'freebsd', 'openbsd', 'netbsd', 'bsd', 'dragonfly', 'android', 'solaris', 'haiku',
'linux_or_macos',
]
valid_comp_if_compilers = ['gcc', 'tinyc', 'clang', 'mingw', 'msvc', 'cplusplus']
valid_comp_if_platforms = ['amd64', 'aarch64', 'x64', 'x32', 'little_endian', 'big_endian']
valid_comp_if_other = ['js', 'debug', 'test', 'glibc', 'prealloc', 'no_bounds_checking']
@ -2551,12 +2553,14 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
}
.mult_assign, .div_assign {
if !left_sym.is_number() &&
!c.table.get_final_type_symbol(left_type_unwrapped).is_int() && left_sym.kind !in [.struct_, .alias]
!c.table.get_final_type_symbol(left_type_unwrapped).is_int() && left_sym.kind !in
[.struct_, .alias]
{
c.error('operator $assign_stmt.op.str() not defined on left operand type `$left_sym.name`',
left.position())
} else if !right_sym.is_number() &&
!c.table.get_final_type_symbol(left_type_unwrapped).is_int() && left_sym.kind !in [.struct_, .alias]
!c.table.get_final_type_symbol(left_type_unwrapped).is_int() && left_sym.kind !in
[.struct_, .alias]
{
c.error('operator $assign_stmt.op.str() not defined on right operand type `$right_sym.name`',
right.position())

View File

@ -95,7 +95,6 @@ pub fn (mut f Fmt) write(s string) {
if !f.buffering {
if f.indent > 0 && f.empty_line {
f.write_indent()
f.line_len += f.indent * 4
}
f.out.write(s)
f.line_len += s.len
@ -143,6 +142,7 @@ fn (mut f Fmt) write_indent() {
f.out.write('\t')
}
}
f.line_len += f.indent * 4
}
// adjustments that can only be done after full line is processed. For now
@ -198,6 +198,7 @@ pub fn (mut f Fmt) wrap_long_line(penalty_idx int, add_indent bool) bool {
f.out.go_back(1)
}
f.write('\n')
f.line_len = 0
if add_indent {
f.indent++
}
@ -205,7 +206,6 @@ pub fn (mut f Fmt) wrap_long_line(penalty_idx int, add_indent bool) bool {
if add_indent {
f.indent--
}
f.line_len = 0
return true
}

View File

@ -15,9 +15,9 @@ import v.depgraph
// NB: keywords after 'new' are reserved in C++
const (
c_reserved = ['delete', 'exit', 'link', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic',
'auto', 'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register',
'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned', 'void',
'volatile', 'while', 'new', 'namespace', 'class', 'typename', 'export']
'auto', 'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long',
'register', 'restrict', 'short', 'signed', 'sizeof', 'static', 'switch', 'typedef', 'union',
'unsigned', 'void', 'volatile', 'while', 'new', 'namespace', 'class', 'typename', 'export']
// same order as in token.Kind
cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le']
// when operands are switched
@ -1692,8 +1692,8 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
// int pos = *(int*)_t190.data;
mut tmp_opt := ''
is_optional := g.is_autofree &&
(assign_stmt.op in [.decl_assign, .assign]) && assign_stmt.left_types.len == 1 && assign_stmt.right[0] is
ast.CallExpr
(assign_stmt.op in [.decl_assign, .assign]) && assign_stmt.left_types.len == 1 &&
assign_stmt.right[0] is ast.CallExpr
if is_optional {
// g.write('/* optional assignment */')
call_expr := assign_stmt.right[0] as ast.CallExpr
@ -1980,7 +1980,8 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
}
// Assignment Operator Overloading
if ((left_sym.kind == .struct_ &&
right_sym.kind == .struct_) || (left_sym.kind == .alias &&
right_sym.kind == .struct_) ||
(left_sym.kind == .alias &&
right_sym.kind == .alias)) &&
assign_stmt.op in [.plus_assign, .minus_assign, .div_assign, .mult_assign, .mod_assign]
{
@ -3569,7 +3570,8 @@ fn (mut g Gen) select_expr(node ast.SelectExpr) {
expr := branch.stmt.expr as ast.InfixExpr
channels << expr.left
if expr.right is ast.Ident ||
expr.right is ast.IndexExpr || expr.right is ast.SelectorExpr || expr.right is ast.StructInit
expr.right is ast.IndexExpr || expr.right is ast.SelectorExpr || expr.right is
ast.StructInit
{
// addressable objects in the `C` output
objs << expr.right
@ -3782,7 +3784,8 @@ fn (mut g Gen) if_expr(node ast.IfExpr) {
first_branch := node.branches[0]
needs_tmp_var := node.is_expr &&
(g.is_autofree || (g.pref.experimental &&
(first_branch.stmts.len > 1 || (first_branch.stmts[0] is ast.ExprStmt &&
(first_branch.stmts.len > 1 ||
(first_branch.stmts[0] is ast.ExprStmt &&
(first_branch.stmts[0] as ast.ExprStmt).expr is ast.IfExpr))))
/*
needs_tmp_var := node.is_expr &&

View File

@ -176,8 +176,10 @@ fn (mut p Parser) match_expr() ast.MatchExpr {
is_else = true
p.next()
} else if (p.tok.kind == .name && !(p.tok.lit == 'C' &&
p.peek_tok.kind == .dot) && (p.tok.lit in table.builtin_type_names || p.tok.lit[0].is_capital() ||
(p.peek_tok.kind == .dot && p.peek_tok2.lit.len > 0 && p.peek_tok2.lit[0].is_capital()))) ||
p.peek_tok.kind == .dot) &&
(p.tok.lit in table.builtin_type_names || p.tok.lit[0].is_capital() ||
(p.peek_tok.kind == .dot &&
p.peek_tok2.lit.len > 0 && p.peek_tok2.lit[0].is_capital()))) ||
p.tok.kind == .lsbr
{
mut types := []table.Type{}

View File

@ -994,8 +994,9 @@ fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt {
return p.partial_assign_stmt(left, left_comments)
} else if tok.kind !in [.key_if, .key_match, .key_lock, .key_rlock, .key_select] &&
left0 !is ast.CallExpr && (is_top_level || p.tok.kind != .rcbr) && left0 !is ast.PostfixExpr &&
!(left0 is ast.InfixExpr && (left0 as ast.InfixExpr).op in [.left_shift, .arrow]) && left0 !is
ast.ComptimeCall && left0 !is ast.SelectorExpr
!(left0 is ast.InfixExpr &&
(left0 as ast.InfixExpr).op in [.left_shift, .arrow]) && left0 !is ast.ComptimeCall &&
left0 !is ast.SelectorExpr
{
p.error_with_pos('expression evaluated but not used', left0.position())
return ast.Stmt{}
@ -1287,7 +1288,8 @@ pub fn (mut p Parser) name_expr() ast.Expr {
}
} else if (p.peek_tok.kind == .lcbr ||
(p.peek_tok.kind == .lt && lit0_is_capital)) &&
(!p.inside_match || (p.inside_select && prev_tok_kind == .arrow && lit0_is_capital)) && !p.inside_match_case &&
(!p.inside_match || (p.inside_select && prev_tok_kind == .arrow && lit0_is_capital)) &&
!p.inside_match_case &&
(!p.inside_if || p.inside_select) &&
(!p.inside_for || p.inside_select)
{ // && (p.tok.lit[0].is_capital() || p.builtin_mod) {

View File

@ -127,13 +127,12 @@ fn test_parse_expr() {
}
input := ['1 == 1', '234234', '2 * 8 + 3', 'a := 3', 'a++', 'b := 4 + 2', 'neg := -a', 'a + a',
'bo := 2 + 3 == 5', '2 + 1', 'q := 1', 'q + 777', '2 + 3', '2+2*4', 'x := 10', 'mut aa := 12',
'ab := 10 + 3 * 9', 's := "hi"', 'x = 11', 'a += 10', '1.2 + 3.4', '4 + 4', '1 + 2 * 5', '-a+1',
'2+2',
]
'ab := 10 + 3 * 9', 's := "hi"', 'x = 11', 'a += 10', '1.2 + 3.4', '4 + 4', '1 + 2 * 5',
'-a+1', '2+2']
expecting := ['1 == 1;', '234234;', '2 * 8 + 3;', 'int a = 3;', 'a++;', 'int b = 4 + 2;', 'int neg = -a;',
'a + a;', 'bool bo = 2 + 3 == 5;', '2 + 1;', 'int q = 1;', 'q + 777;', '2 + 3;', '2 + 2 * 4;',
'int x = 10;', 'int aa = 12;', 'int ab = 10 + 3 * 9;', 'string s = tos3("hi");', 'x = 11;', 'a += 10;',
'1.2 + 3.4;', '4 + 4;', '1 + 2 * 5;', '-a + 1;', '2 + 2;']
'int x = 10;', 'int aa = 12;', 'int ab = 10 + 3 * 9;', 'string s = tos3("hi");', 'x = 11;',
'a += 10;', '1.2 + 3.4;', '4 + 4;', '1 + 2 * 5;', '-a + 1;', '2 + 2;']
mut e := []ast.Stmt{}
table := table.new_table()
vpref := &pref.Preferences{}

View File

@ -1070,9 +1070,9 @@ fn (mut s Scanner) ident_string() string {
}
// Escape `\u`
if c == `u` && (s.text[s.pos + 1] == s.quote ||
s.text[s.pos + 2] == s.quote || s.text[s.pos + 3] == s.quote || s.text[s.pos + 4] == s.quote ||
!s.text[s.pos + 1].is_hex_digit() || !s.text[s.pos + 2].is_hex_digit() || !s.text[s.pos + 3].is_hex_digit() ||
!s.text[s.pos + 4].is_hex_digit())
s.text[s.pos + 2] == s.quote || s.text[s.pos + 3] == s.quote || s.text[s.pos +
4] == s.quote || !s.text[s.pos + 1].is_hex_digit() || !s.text[s.pos + 2].is_hex_digit() ||
!s.text[s.pos + 3].is_hex_digit() || !s.text[s.pos + 4].is_hex_digit())
{
s.error(r'`\u` incomplete unicode character value')
}

View File

@ -43,8 +43,8 @@ pub mut:
fn (f &Fn) method_equals(o &Fn) bool {
return f.params[1..].equals(o.params[1..]) && f.return_type == o.return_type && f.is_variadic ==
o.is_variadic && f.language == o.language && f.is_generic == o.is_generic && f.is_pub == o.is_pub &&
f.mod == o.mod && f.name == o.name
o.is_variadic && f.language == o.language && f.is_generic == o.is_generic && f.is_pub ==
o.is_pub && f.mod == o.mod && f.name == o.name
}
pub struct Param {

View File

@ -316,9 +316,8 @@ pub const (
unsigned_integer_type_idxs = [byte_type_idx, u16_type_idx, u32_type_idx, u64_type_idx]
float_type_idxs = [f32_type_idx, f64_type_idx, float_literal_type_idx]
number_type_idxs = [i8_type_idx, i16_type_idx, int_type_idx, i64_type_idx, byte_type_idx,
u16_type_idx, u32_type_idx, u64_type_idx, f32_type_idx, f64_type_idx, int_literal_type_idx, float_literal_type_idx,
rune_type_idx,
]
u16_type_idx, u32_type_idx, u64_type_idx, f32_type_idx, f64_type_idx, int_literal_type_idx,
float_literal_type_idx, rune_type_idx]
pointer_type_idxs = [voidptr_type_idx, byteptr_type_idx, charptr_type_idx]
string_type_idxs = [string_type_idx, ustring_type_idx]
)
@ -356,10 +355,9 @@ pub const (
pub const (
builtin_type_names = ['void', 'voidptr', 'charptr', 'byteptr', 'i8', 'i16', 'int', 'i64', 'u16',
'u32', 'u64', 'int_literal', 'f32', 'f64', 'float_literal', 'string', 'ustring', 'char', 'byte',
'bool', 'none', 'array', 'array_fixed', 'map', 'chan', 'any', 'struct', 'mapnode', 'size_t', 'rune',
'gohandle',
]
'u32', 'u64', 'int_literal', 'f32', 'f64', 'float_literal', 'string', 'ustring', 'char',
'byte', 'bool', 'none', 'array', 'array_fixed', 'map', 'chan', 'any', 'struct', 'mapnode',
'size_t', 'rune', 'gohandle']
)
pub struct MultiReturn {

View File

@ -424,7 +424,8 @@ fn handle_conn<T>(mut conn net.TcpConn, mut app T) {
// should be called first.
if (req_method_str == '' &&
url_words[0] == method.name && url_words.len == 1) ||
(req_method_str == req.method.str() && url_words[0] == method.name && url_words.len == 1)
(req_method_str == req.method.str() && url_words[0] == method.name && url_words.len ==
1)
{
$if debug {
println('easy match method=$method.name')