parser: improve syntax errors in module declarations

pull/9247/head
Delyan Angelov 2021-03-11 10:40:09 +02:00
parent 6a7ef4f5b2
commit 5a231326d7
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
7 changed files with 16 additions and 14 deletions

View File

@ -1,5 +0,0 @@
vlib/v/checker/tests/module_multiple_names_err.vv:1:13: error: `module x` can only declare one module
1 | module main os
| ~~
2 | fn main() {
3 | println('hello, world')

View File

@ -1,5 +0,0 @@
vlib/v/checker/tests/module_syntax_err.vv:1:12: error: `module x` syntax error
1 | module main.os
| ^
2 | fn main() {
3 | println('hello, world')

View File

@ -1835,12 +1835,14 @@ fn (mut p Parser) module_decl() ast.Module {
// as it creates a wrong position when extended
// to module_pos
n_pos := p.tok.position()
if module_pos.line_nr == n_pos.line_nr && p.tok.kind != .comment {
if p.tok.kind != .name {
p.error_with_pos('`module x` syntax error', n_pos)
if module_pos.line_nr == n_pos.line_nr && p.tok.kind != .comment && p.tok.kind != .eof {
if p.tok.kind == .name {
p.error_with_pos('`module $name`, you can only declare one module, unexpected `$p.tok.lit`',
n_pos)
return mod_node
} else {
p.error_with_pos('`module x` can only declare one module', n_pos)
p.error_with_pos('`module $name`, unexpected `$p.tok.kind` after module name',
n_pos)
return mod_node
}
}

View File

@ -0,0 +1,5 @@
vlib/v/parser/tests/module_multiple_names_err.vv:1:13: error: `module main`, you can only declare one module, unexpected `os`
1 | module main os
| ~~
2 | fn main() {
3 | println('hello, world')

View File

@ -0,0 +1,5 @@
vlib/v/parser/tests/module_syntax_err.vv:1:12: error: `module main`, unexpected `.` after module name
1 | module main.os
| ^
2 | fn main() {
3 | println('hello, world')