cgen, fmt, scanner: fix and use nested lambda in scanner (#11967)

pull/11971/head
Ruofan XU 2021-09-24 23:21:22 +08:00 committed by GitHub
parent 400ab7876b
commit 834cf40ab2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 19 deletions

View File

@ -43,7 +43,7 @@ pub mut:
use_short_fn_args bool use_short_fn_args bool
single_line_fields bool // should struct fields be on a single line single_line_fields bool // should struct fields be on a single line
it_name string // the name to replace `it` with it_name string // the name to replace `it` with
inside_lambda bool in_lambda_depth int
inside_const bool inside_const bool
is_mbranch_expr bool // match a { x...y { } } is_mbranch_expr bool // match a { x...y { } }
fn_scope &ast.Scope = voidptr(0) 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.is_method {
if node.name in ['map', 'filter', 'all', 'any'] { if node.name in ['map', 'filter', 'all', 'any'] {
f.inside_lambda = true f.in_lambda_depth++
defer { defer {
f.inside_lambda = false f.in_lambda_depth--
} }
} }
if node.left is ast.Ident { 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) 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) f.write(f.it_name)
} else if node.kind == .blank_ident { } else if node.kind == .blank_ident {
f.write('_') f.write('_')

View File

@ -196,7 +196,7 @@ fn (mut g Gen) gen_array_map(node ast.CallExpr) {
} }
} }
ast.CallExpr { ast.CallExpr {
if expr.name in ['map', 'filter'] { if expr.name in ['map', 'filter', 'all', 'any'] {
is_embed_map_filter = true is_embed_map_filter = true
g.stmt_path_pos << g.out.len g.stmt_path_pos << g.out.len
} }
@ -387,7 +387,7 @@ fn (mut g Gen) gen_array_filter(node ast.CallExpr) {
} }
} }
ast.CallExpr { ast.CallExpr {
if expr.name in ['map', 'filter'] { if expr.name in ['map', 'filter', 'all', 'any'] {
is_embed_map_filter = true is_embed_map_filter = true
g.stmt_path_pos << g.out.len g.stmt_path_pos << g.out.len
} }
@ -678,7 +678,7 @@ fn (mut g Gen) gen_array_any(node ast.CallExpr) {
} }
} }
ast.CallExpr { ast.CallExpr {
if expr.name in ['map', 'filter'] { if expr.name in ['map', 'filter', 'all', 'any'] {
is_embed_map_filter = true is_embed_map_filter = true
g.stmt_path_pos << g.out.len g.stmt_path_pos << g.out.len
} }
@ -761,7 +761,7 @@ fn (mut g Gen) gen_array_all(node ast.CallExpr) {
} }
} }
ast.CallExpr { ast.CallExpr {
if expr.name in ['map', 'filter'] { if expr.name in ['map', 'filter', 'all', 'any'] {
is_embed_map_filter = true is_embed_map_filter = true
g.stmt_path_pos << g.out.len g.stmt_path_pos << g.out.len
} }

View File

@ -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 // here we set the limit 100 which should be nice for real cases
// e.g. ...Bar<int, []Foo<int>, Baz_, [20]f64, map[string][]bool>> => // e.g. ...Bar<int, []Foo<int>, Baz_, [20]f64, map[string][]bool>> =>
// <int, Baz_, [20]f64, map[string][]bool => int, Baz_, f64, bool // <int, Baz_, [20]f64, map[string][]bool => int, Baz_, f64, bool
mut is_generic := true is_generic := if s.last_lt >= 0 && s.pos - s.last_lt < 100 {
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(']')) 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 // if any typ is neither Type nor builtin, then the case is non-generic
for typ in typs { typs.all(it.len > 0
if typ.len == 0 && ((it[0].is_capital() && it[1..].bytes().all(it.is_alnum()
|| (!(typ[0].is_capital() && typ[1..].bytes().all(it.is_alnum() || it == `_`)) || it in ast.builtin_type_names))
|| it == `_`)) && typ !in ast.builtin_type_names) {
is_generic = false
break
}
}
} else { } else {
is_generic = false false
} }
if is_generic { if is_generic {
return s.new_token(.gt, '', 1) return s.new_token(.gt, '', 1)