mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
Change import syntax to "import as alias"
This commit is contained in:
parent
163cd8576b
commit
298ca8676f
@ -139,8 +139,8 @@ fn (p mut Parser) parse() {
|
|||||||
// fully qualify the module name, eg base64 to encoding.base64
|
// fully qualify the module name, eg base64 to encoding.base64
|
||||||
fq_mod := p.table.qualify_module(p.mod, p.file_path)
|
fq_mod := p.table.qualify_module(p.mod, p.file_path)
|
||||||
p.table.register_package(fq_mod)
|
p.table.register_package(fq_mod)
|
||||||
// replace "." with "_" for C variable names
|
// replace "." with "__" in module name for C variable names
|
||||||
p.mod = fq_mod.replace('.', '_')
|
p.mod = fq_mod.replace('.', '__')
|
||||||
if p.run == .imports {
|
if p.run == .imports {
|
||||||
for p.tok == .key_import && p.peek() != .key_const {
|
for p.tok == .key_import && p.peek() != .key_const {
|
||||||
p.import_statement()
|
p.import_statement()
|
||||||
@ -308,29 +308,29 @@ fn (p mut Parser) import_statement() {
|
|||||||
if p.tok != .name {
|
if p.tok != .name {
|
||||||
p.error('bad import format')
|
p.error('bad import format')
|
||||||
}
|
}
|
||||||
// aliasing (import b64 encoding.base64)
|
|
||||||
mut alias := ''
|
|
||||||
if p.tok == .name && p.peek() == .name {
|
|
||||||
alias = p.check_name()
|
|
||||||
}
|
|
||||||
mut pkg := p.lit.trim_space()
|
mut pkg := p.lit.trim_space()
|
||||||
|
mut mod_alias := pkg
|
||||||
// submodule support
|
// submodule support
|
||||||
mut depth := 1
|
mut depth := 1
|
||||||
p.next()
|
p.next()
|
||||||
for p.tok == .dot {
|
for p.tok == .dot {
|
||||||
p.check(.dot)
|
p.check(.dot)
|
||||||
submodule := p.check_name()
|
submodule := p.check_name()
|
||||||
if alias == '' { alias = submodule }
|
mod_alias = submodule
|
||||||
pkg += '.' + submodule
|
pkg += '.' + submodule
|
||||||
depth++
|
depth++
|
||||||
if depth > MaxModuleDepth {
|
if depth > MaxModuleDepth {
|
||||||
p.error('module depth of $MaxModuleDepth exceeded: $pkg')
|
p.error('module depth of $MaxModuleDepth exceeded: $pkg')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if alias == '' { alias = pkg }
|
// aliasing (import encoding.base64 as b64)
|
||||||
|
if p.tok == .key_as && p.peek() == .name {
|
||||||
|
p.check(.key_as)
|
||||||
|
mod_alias = p.check_name()
|
||||||
|
}
|
||||||
p.fgenln(' ' + pkg)
|
p.fgenln(' ' + pkg)
|
||||||
// add import to file scope import table
|
// add import to file scope import table
|
||||||
p.import_table.register_alias(alias, pkg)
|
p.import_table.register_alias(mod_alias, pkg)
|
||||||
// Make sure there are no duplicate imports
|
// Make sure there are no duplicate imports
|
||||||
if p.table.imports.contains(pkg) {
|
if p.table.imports.contains(pkg) {
|
||||||
return
|
return
|
||||||
@ -1308,8 +1308,8 @@ fn (p mut Parser) name_expr() string {
|
|||||||
mut pkg := name
|
mut pkg := name
|
||||||
// must be aliased module
|
// must be aliased module
|
||||||
if name != p.mod && p.import_table.known_alias(name) {
|
if name != p.mod && p.import_table.known_alias(name) {
|
||||||
// we replaced "." with "_" in p.mod for C variable names, do same here.
|
// we replaced "." with "__" in p.mod for C variable names, do same here.
|
||||||
pkg = p.import_table.resolve_alias(name).replace('.', '_')
|
pkg = p.import_table.resolve_alias(name).replace('.', '__')
|
||||||
}
|
}
|
||||||
p.next()
|
p.next()
|
||||||
p.check(.dot)
|
p.check(.dot)
|
||||||
@ -1434,7 +1434,7 @@ fn (p mut Parser) name_expr() string {
|
|||||||
// println('name_expr():')
|
// println('name_expr():')
|
||||||
// If orig_name is a pkg, then printing undefined: `pkg` tells us nothing
|
// If orig_name is a pkg, then printing undefined: `pkg` tells us nothing
|
||||||
// if p.table.known_pkg(orig_name) {
|
// if p.table.known_pkg(orig_name) {
|
||||||
if p.table.known_pkg(orig_name) && p.import_table.known_alias(orig_name) {
|
if p.table.known_pkg(orig_name) || p.import_table.known_alias(orig_name) {
|
||||||
name = name.replace('__', '.')
|
name = name.replace('__', '.')
|
||||||
p.error('undefined: `$name`')
|
p.error('undefined: `$name`')
|
||||||
}
|
}
|
||||||
|
@ -687,7 +687,7 @@ fn new_file_import_table(file_path string) *FileImportTable {
|
|||||||
return t
|
return t
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (fit FileImportTable) known_import(mod string) bool {
|
fn (fit &FileImportTable) known_import(mod string) bool {
|
||||||
return fit.imports.exists(mod) || fit.is_aliased(mod)
|
return fit.imports.exists(mod) || fit.is_aliased(mod)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ struct C {
|
|||||||
mut:
|
mut:
|
||||||
b B
|
b B
|
||||||
nums []int
|
nums []int
|
||||||
as []A
|
aarr []A
|
||||||
num int
|
num int
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,13 +40,13 @@ fn test_struct_levels() {
|
|||||||
assert c.b.a.nums[1] == 2
|
assert c.b.a.nums[1] == 2
|
||||||
c.b.a.nums [0] = 7
|
c.b.a.nums [0] = 7
|
||||||
assert c.b.a.nums[0] == 7
|
assert c.b.a.nums[0] == 7
|
||||||
c.as << A{val:8}
|
c.aarr << A{val:8}
|
||||||
assert c.as.len == 1
|
assert c.aarr.len == 1
|
||||||
assert c.as[0].val == 8
|
assert c.aarr[0].val == 8
|
||||||
c.num = 20
|
c.num = 20
|
||||||
assert c.num == 20
|
assert c.num == 20
|
||||||
c.as[0].val = 10
|
c.aarr[0].val = 10
|
||||||
assert c.as[0].val == 10
|
assert c.aarr[0].val == 10
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_struct_str() {
|
fn test_struct_str() {
|
||||||
|
@ -102,6 +102,7 @@ enum Token {
|
|||||||
key_pub
|
key_pub
|
||||||
key_goto
|
key_goto
|
||||||
key_static
|
key_static
|
||||||
|
key_as
|
||||||
keyword_end
|
keyword_end
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -211,6 +212,7 @@ fn build_token_str() []string {
|
|||||||
s[Token.key_global] = '__global'
|
s[Token.key_global] = '__global'
|
||||||
s[Token.key_union] = 'union'
|
s[Token.key_union] = 'union'
|
||||||
s[Token.key_static] = 'static'
|
s[Token.key_static] = 'static'
|
||||||
|
s[Token.key_as] = 'as'
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user