parser: check for module statement errors

pull/4806/head
yuyi 2020-05-10 08:28:56 +08:00 committed by GitHub
parent 85723e3799
commit ba3a631954
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 0 deletions

View File

@ -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')

View File

@ -0,0 +1,4 @@
module main os
fn main() {
println('hello, world')
}

View File

@ -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')

View File

@ -0,0 +1,5 @@
module
main
fn main() {
println('hello, world')
}

View File

@ -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')

View File

@ -0,0 +1,4 @@
module main.os
fn main() {
println('hello, world')
}

View File

@ -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