From 834cf40ab26431eec585d2854e99db833fe50a1a Mon Sep 17 00:00:00 2001 From: Ruofan XU <47302112+SleepyRoy@users.noreply.github.com> Date: Fri, 24 Sep 2021 23:21:22 +0800 Subject: [PATCH] cgen, fmt, scanner: fix and use nested lambda in scanner (#11967) --- vlib/v/fmt/fmt.v | 8 ++++---- vlib/v/gen/c/array.v | 8 ++++---- vlib/v/scanner/scanner.v | 16 +++++----------- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index e11197d5e3..e223310956 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -43,7 +43,7 @@ pub mut: use_short_fn_args bool single_line_fields bool // should struct fields be on a single line it_name string // the name to replace `it` with - inside_lambda bool + in_lambda_depth int inside_const bool is_mbranch_expr bool // match a { x...y { } } fn_scope &ast.Scope = voidptr(0) @@ -1539,9 +1539,9 @@ pub fn (mut f Fmt) call_expr(node ast.CallExpr) { } if node.is_method { if node.name in ['map', 'filter', 'all', 'any'] { - f.inside_lambda = true + f.in_lambda_depth++ defer { - f.inside_lambda = false + f.in_lambda_depth-- } } if node.left is ast.Ident { @@ -1724,7 +1724,7 @@ pub fn (mut f Fmt) ident(node ast.Ident) { } } f.write_language_prefix(node.language) - if node.name == 'it' && f.it_name != '' && !f.inside_lambda { // allow `it` in lambdas + if node.name == 'it' && f.it_name != '' && f.in_lambda_depth == 0 { // allow `it` in lambdas f.write(f.it_name) } else if node.kind == .blank_ident { f.write('_') diff --git a/vlib/v/gen/c/array.v b/vlib/v/gen/c/array.v index 8e4ebcead5..47c07b41c8 100644 --- a/vlib/v/gen/c/array.v +++ b/vlib/v/gen/c/array.v @@ -196,7 +196,7 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) { } } ast.CallExpr { - if expr.name in ['map', 'filter'] { + if expr.name in ['map', 'filter', 'all', 'any'] { is_embed_map_filter = true g.stmt_path_pos << g.out.len } @@ -387,7 +387,7 @@ fn (mut g Gen) gen_array_filter(node ast.CallExpr) { } } ast.CallExpr { - if expr.name in ['map', 'filter'] { + if expr.name in ['map', 'filter', 'all', 'any'] { is_embed_map_filter = true g.stmt_path_pos << g.out.len } @@ -678,7 +678,7 @@ fn (mut g Gen) gen_array_any(node ast.CallExpr) { } } ast.CallExpr { - if expr.name in ['map', 'filter'] { + if expr.name in ['map', 'filter', 'all', 'any'] { is_embed_map_filter = true g.stmt_path_pos << g.out.len } @@ -761,7 +761,7 @@ fn (mut g Gen) gen_array_all(node ast.CallExpr) { } } ast.CallExpr { - if expr.name in ['map', 'filter'] { + if expr.name in ['map', 'filter', 'all', 'any'] { is_embed_map_filter = true g.stmt_path_pos << g.out.len } diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 91ef8beac9..5e599090ed 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -926,20 +926,14 @@ fn (mut s Scanner) text_scan() token.Token { // here we set the limit 100 which should be nice for real cases // e.g. ...Bar, Baz_, [20]f64, map[string][]bool>> => // int, Baz_, f64, bool - mut is_generic := true - if s.last_lt >= 0 && s.pos - s.last_lt < 100 { + is_generic := if s.last_lt >= 0 && s.pos - s.last_lt < 100 { typs := s.text[s.last_lt + 1..s.pos].split(',').map(it.trim_space().trim_right('>').after(']')) // if any typ is neither Type nor builtin, then the case is non-generic - for typ in typs { - if typ.len == 0 - || (!(typ[0].is_capital() && typ[1..].bytes().all(it.is_alnum() - || it == `_`)) && typ !in ast.builtin_type_names) { - is_generic = false - break - } - } + typs.all(it.len > 0 + && ((it[0].is_capital() && it[1..].bytes().all(it.is_alnum() + || it == `_`)) || it in ast.builtin_type_names)) } else { - is_generic = false + false } if is_generic { return s.new_token(.gt, '', 1)