mirror of
https://github.com/vlang/v.git
synced 2025-09-13 14:32:26 +03:00
parser: use a token.KeywordsMatcherTrie matching checks, instead of the map checks for name in p.imported_symbols
This commit is contained in:
parent
2ac3478296
commit
b588bacf78
6 changed files with 21 additions and 11 deletions
|
@ -61,7 +61,7 @@ fn (mut p Parser) enum_decl() ast.EnumDecl {
|
|||
end_pos)
|
||||
return ast.EnumDecl{}
|
||||
}
|
||||
if enum_name in p.imported_symbols {
|
||||
if p.is_imported_symbol(enum_name) {
|
||||
p.error_with_pos('cannot register enum `${enum_name}`, this type was already imported',
|
||||
end_pos)
|
||||
return ast.EnumDecl{}
|
||||
|
|
|
@ -89,7 +89,7 @@ fn (mut p Parser) call_expr(language ast.Language, mod string) ast.CallExpr {
|
|||
}
|
||||
or_kind = if is_not { .propagate_result } else { .propagate_option }
|
||||
}
|
||||
if fn_name in p.imported_symbols {
|
||||
if p.is_imported_symbol(fn_name) {
|
||||
check := !p.imported_symbols_used[fn_name]
|
||||
fn_name = p.imported_symbols[fn_name]
|
||||
if check {
|
||||
|
@ -393,7 +393,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
|
|||
}
|
||||
}
|
||||
if !p.pref.is_fmt {
|
||||
if name in p.imported_symbols {
|
||||
if p.is_imported_symbol(name) {
|
||||
p.error_with_pos('cannot redefine imported function `${name}`', name_pos)
|
||||
return ast.FnDecl{
|
||||
scope: unsafe { nil }
|
||||
|
|
|
@ -36,7 +36,7 @@ fn (mut p Parser) register_used_import(alias string) {
|
|||
fn (mut p Parser) register_used_import_for_symbol_name(sym_name string) {
|
||||
short_import_name := sym_name.all_before_last('.').all_after_last('.')
|
||||
short_symbol_name := sym_name.all_after_last('.')
|
||||
if short_symbol_name in p.imported_symbols {
|
||||
if p.is_imported_symbol(short_symbol_name) {
|
||||
p.imported_symbols_used[short_symbol_name] = true
|
||||
}
|
||||
for alias, mod in p.imports {
|
||||
|
@ -322,12 +322,13 @@ fn (mut p Parser) import_syms(mut parent ast.Import) {
|
|||
for p.tok.kind == .name {
|
||||
pos := p.tok.pos()
|
||||
alias := p.check_name()
|
||||
if alias in p.imported_symbols {
|
||||
if p.is_imported_symbol(alias) {
|
||||
p.error_with_pos('cannot register symbol `${alias}`, it was already imported',
|
||||
pos)
|
||||
return
|
||||
}
|
||||
p.imported_symbols[alias] = parent.mod + '.' + alias
|
||||
p.rebuild_imported_symbols_matcher(alias)
|
||||
// so we can work with this in fmt+checker
|
||||
parent.syms << ast.ImportSymbol{
|
||||
pos: pos
|
||||
|
@ -347,3 +348,12 @@ fn (mut p Parser) import_syms(mut parent ast.Import) {
|
|||
}
|
||||
p.next()
|
||||
}
|
||||
|
||||
fn (mut p Parser) rebuild_imported_symbols_matcher(name string) {
|
||||
p.imported_symbols_trie = token.new_keywords_matcher_from_array_trie(p.imported_symbols.keys())
|
||||
}
|
||||
|
||||
@[inline]
|
||||
fn (mut p Parser) is_imported_symbol(name string) bool {
|
||||
return p.imported_symbols_trie.matches(name)
|
||||
}
|
||||
|
|
|
@ -636,7 +636,7 @@ fn (mut p Parser) parse_any_type(language ast.Language, is_ptr bool, check_dot b
|
|||
} else if p.expr_mod != '' && !p.inside_generic_params {
|
||||
// p.expr_mod is from the struct and not from the generic parameter
|
||||
name = p.expr_mod + '.' + name
|
||||
} else if name in p.imported_symbols {
|
||||
} else if p.is_imported_symbol(name) {
|
||||
check := !p.imported_symbols_used[name]
|
||||
name = p.imported_symbols[name]
|
||||
if check {
|
||||
|
|
|
@ -86,6 +86,7 @@ mut:
|
|||
implied_imports []string // imports that the user's code uses but omitted to import explicitly, used by `vfmt`
|
||||
imported_symbols map[string]string
|
||||
imported_symbols_used map[string]bool
|
||||
imported_symbols_trie token.KeywordsMatcherTrie
|
||||
is_amp bool // for generating the right code for `&Foo{}`
|
||||
returns bool
|
||||
is_stmt_ident bool // true while the beginning of a statement is an ident/selector
|
||||
|
@ -592,8 +593,7 @@ fn (mut p Parser) check_name() string {
|
|||
name := p.tok.lit
|
||||
if p.tok.kind != .name && p.peek_tok.kind == .dot && name in p.imports {
|
||||
p.register_used_import(name)
|
||||
} else if p.tok.kind == .name && p.peek_tok.kind == .dot && name in p.imported_symbols
|
||||
&& !p.imported_symbols_used[name] {
|
||||
} else if p.tok.kind == .name && p.is_imported_symbol(name) && !p.imported_symbols_used[name] {
|
||||
// symbols like Enum.field_name
|
||||
p.register_used_import_for_symbol_name(p.imported_symbols[name])
|
||||
}
|
||||
|
@ -2698,7 +2698,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
|
|||
return ast.FnTypeDecl{}
|
||||
}
|
||||
}
|
||||
if name in p.imported_symbols {
|
||||
if p.is_imported_symbol(name) {
|
||||
p.error_with_pos('cannot register alias `${name}`, this type was already imported',
|
||||
end_pos)
|
||||
return ast.AliasTypeDecl{}
|
||||
|
|
|
@ -73,7 +73,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
|
|||
p.error_with_pos('struct names must have more than one character', name_pos)
|
||||
return ast.StructDecl{}
|
||||
}
|
||||
if name in p.imported_symbols {
|
||||
if p.is_imported_symbol(name) {
|
||||
p.error_with_pos('cannot register struct `${name}`, this type was already imported',
|
||||
name_pos)
|
||||
return ast.StructDecl{}
|
||||
|
@ -651,7 +651,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
|
|||
mut pre_comments := p.eat_comments()
|
||||
p.check(.lcbr)
|
||||
pre_comments << p.eat_comments()
|
||||
if modless_name in p.imported_symbols {
|
||||
if p.is_imported_symbol(modless_name) {
|
||||
p.error_with_pos('cannot register interface `${interface_name}`, this type was already imported',
|
||||
name_pos)
|
||||
return ast.InterfaceDecl{}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue