diff --git a/vlib/v/checker/tests/import_multiple_modules_err.out b/vlib/v/checker/tests/import_multiple_modules_err.out new file mode 100644 index 0000000000..089c5e49e1 --- /dev/null +++ b/vlib/v/checker/tests/import_multiple_modules_err.out @@ -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()) diff --git a/vlib/v/checker/tests/import_multiple_modules_err.vv b/vlib/v/checker/tests/import_multiple_modules_err.vv new file mode 100644 index 0000000000..d5d8033034 --- /dev/null +++ b/vlib/v/checker/tests/import_multiple_modules_err.vv @@ -0,0 +1,4 @@ +import time math +fn main() { + println(time.now().unix_time()) +} diff --git a/vlib/v/checker/tests/import_not_same_line_err.out b/vlib/v/checker/tests/import_not_same_line_err.out new file mode 100644 index 0000000000..4be8327a2f --- /dev/null +++ b/vlib/v/checker/tests/import_not_same_line_err.out @@ -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()) diff --git a/vlib/v/checker/tests/import_not_same_line_err.vv b/vlib/v/checker/tests/import_not_same_line_err.vv new file mode 100644 index 0000000000..ad6401e21e --- /dev/null +++ b/vlib/v/checker/tests/import_not_same_line_err.vv @@ -0,0 +1,5 @@ +import + time +fn main() { + println(time.now()) +} diff --git a/vlib/v/checker/tests/import_syntax_err.out b/vlib/v/checker/tests/import_syntax_err.out new file mode 100644 index 0000000000..6d53400d04 --- /dev/null +++ b/vlib/v/checker/tests/import_syntax_err.out @@ -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()) diff --git a/vlib/v/checker/tests/import_syntax_err.vv b/vlib/v/checker/tests/import_syntax_err.vv new file mode 100644 index 0000000000..2649ca6bf3 --- /dev/null +++ b/vlib/v/checker/tests/import_syntax_err.vv @@ -0,0 +1,4 @@ +import time, os +fn main() { + println(time.now()) +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index df8bf45470..b934963a73 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -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{