From ba3a631954711ddc8501ad4120ab506affe93d6f Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 10 May 2020 08:28:56 +0800 Subject: [PATCH] parser: check for module statement errors --- vlib/v/checker/tests/module_multiple_names_err.out | 5 +++++ vlib/v/checker/tests/module_multiple_names_err.vv | 4 ++++ .../v/checker/tests/module_not_at_same_line_err.out | 6 ++++++ vlib/v/checker/tests/module_not_at_same_line_err.vv | 5 +++++ vlib/v/checker/tests/module_syntax_err.out | 5 +++++ vlib/v/checker/tests/module_syntax_err.vv | 4 ++++ vlib/v/parser/parser.v | 13 +++++++++++++ 7 files changed, 42 insertions(+) create mode 100644 vlib/v/checker/tests/module_multiple_names_err.out create mode 100644 vlib/v/checker/tests/module_multiple_names_err.vv create mode 100644 vlib/v/checker/tests/module_not_at_same_line_err.out create mode 100644 vlib/v/checker/tests/module_not_at_same_line_err.vv create mode 100644 vlib/v/checker/tests/module_syntax_err.out create mode 100644 vlib/v/checker/tests/module_syntax_err.vv diff --git a/vlib/v/checker/tests/module_multiple_names_err.out b/vlib/v/checker/tests/module_multiple_names_err.out new file mode 100644 index 0000000000..539d6c9547 --- /dev/null +++ b/vlib/v/checker/tests/module_multiple_names_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/module_multiple_names_err.v:1:13: error: `module x` can only declare one module + 1 | module main os + | ~~ + 2 | fn main() { + 3 | println('hello, world') diff --git a/vlib/v/checker/tests/module_multiple_names_err.vv b/vlib/v/checker/tests/module_multiple_names_err.vv new file mode 100644 index 0000000000..96158af86b --- /dev/null +++ b/vlib/v/checker/tests/module_multiple_names_err.vv @@ -0,0 +1,4 @@ +module main os +fn main() { + println('hello, world') +} diff --git a/vlib/v/checker/tests/module_not_at_same_line_err.out b/vlib/v/checker/tests/module_not_at_same_line_err.out new file mode 100644 index 0000000000..1f9b13e076 --- /dev/null +++ b/vlib/v/checker/tests/module_not_at_same_line_err.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/module_not_at_same_line_err.v:2:1: error: `module` and `main` must be at same line + 1 | module + 2 | main + | ~~~~ + 3 | fn main() { + 4 | println('hello, world') diff --git a/vlib/v/checker/tests/module_not_at_same_line_err.vv b/vlib/v/checker/tests/module_not_at_same_line_err.vv new file mode 100644 index 0000000000..a0e52f9d9b --- /dev/null +++ b/vlib/v/checker/tests/module_not_at_same_line_err.vv @@ -0,0 +1,5 @@ +module +main +fn main() { + println('hello, world') +} diff --git a/vlib/v/checker/tests/module_syntax_err.out b/vlib/v/checker/tests/module_syntax_err.out new file mode 100644 index 0000000000..071b1b030a --- /dev/null +++ b/vlib/v/checker/tests/module_syntax_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/module_syntax_err.v:1:12: error: `module x` syntax error + 1 | module main.os + | ^ + 2 | fn main() { + 3 | println('hello, world') diff --git a/vlib/v/checker/tests/module_syntax_err.vv b/vlib/v/checker/tests/module_syntax_err.vv new file mode 100644 index 0000000000..8f8c91f20b --- /dev/null +++ b/vlib/v/checker/tests/module_syntax_err.vv @@ -0,0 +1,4 @@ +module main.os +fn main() { + println('hello, world') +} diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 644ef3f356..2ae42b795d 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -966,8 +966,21 @@ fn (mut p Parser) module_decl() ast.Module { mut name := 'main' is_skipped := p.tok.kind != .key_module if !is_skipped { + module_pos := p.tok.position() p.next() + mut pos := p.tok.position() name = p.check_name() + if module_pos.line_nr != pos.line_nr { + p.error_with_pos('`module` and `$name` must be at same line', pos) + } + pos = p.tok.position() + if module_pos.line_nr == pos.line_nr { + if p.tok.kind != .name { + p.error_with_pos('`module x` syntax error', pos) + } else { + p.error_with_pos('`module x` can only declare one module', pos) + } + } } full_mod := p.table.qualify_module(name, p.file_name) p.mod = full_mod