1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser: check for import errors

This commit is contained in:
yuyi 2020-05-08 21:01:54 +08:00 committed by GitHub
parent 2f4fc86d58
commit ef505e21ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 0 deletions

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/import_multiple_modules_err.v:1:13: error: cannot import multiple modules at a time
1 | import time math
| ~~~~
2 | fn main() {
3 | println(time.now().unix_time())

View File

@ -0,0 +1,4 @@
import time math
fn main() {
println(time.now().unix_time())
}

View File

@ -0,0 +1,6 @@
vlib/v/checker/tests/import_not_same_line_err.v:2:2: error: `import` and `module` must be at same line
1 | import
2 | time
| ~~~~
3 | fn main() {
4 | println(time.now())

View File

@ -0,0 +1,5 @@
import
time
fn main() {
println(time.now())
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/import_syntax_err.v:1:12: error: module syntax error, please use `x.y.z`
1 | import time, os
| ^
2 | fn main() {
3 | println(time.now())

View File

@ -0,0 +1,4 @@
import time, os
fn main() {
println(time.now())
}

View File

@ -958,15 +958,26 @@ fn (mut p Parser) module_decl() ast.Module {
}
fn (mut p Parser) import_stmt() ast.Import {
import_pos := p.tok.position()
p.check(.key_import)
pos := p.tok.position()
if p.tok.kind == .lpar {
p.error_with_pos('`import()` has been deprecated, use `import x` instead', pos)
}
mut mod_name := p.check_name()
if import_pos.line_nr != pos.line_nr {
p.error_with_pos('`import` and `module` must be at same line', pos)
}
mut mod_alias := mod_name
for p.tok.kind == .dot {
p.next()
pos_t := p.tok.position()
if p.tok.kind != .name {
p.error_with_pos('module syntax error, please use `x.y.z`', pos)
}
if import_pos.line_nr != pos_t.line_nr {
p.error_with_pos('`import` and `submodule` must be at same line', pos)
}
submod_name := p.check_name()
mod_name += '.' + submod_name
mod_alias = submod_name
@ -975,6 +986,14 @@ fn (mut p Parser) import_stmt() ast.Import {
p.next()
mod_alias = p.check_name()
}
pos_t := p.tok.position()
if import_pos.line_nr == pos_t.line_nr {
if p.tok.kind != .name {
p.error_with_pos('module syntax error, please use `x.y.z`', pos_t)
} else {
p.error_with_pos('cannot import multiple modules at a time', pos_t)
}
}
p.imports[mod_alias] = mod_name
p.table.imports << mod_name
node := ast.Import{