parser: check `import` in the middle of file error

pull/4807/head^2
yuyi 2020-05-10 12:47:20 +08:00 committed by GitHub
parent 76eec7b6ea
commit 7f69c2fbf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 5 deletions

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/import_middle_err.v:5:1: error: `import x` can only be declared at the beginning of the file
3 | println('hello, world')
4 | }
5 | import os
| ~~~~~~
6 | fn main() {
7 | println(time.now())

View File

@ -0,0 +1,8 @@
import time
fn show() {
println('hello, world')
}
import os
fn main() {
println(time.now())
}

View File

@ -89,18 +89,15 @@ pub fn parse_file(path string, b_table &table.Table, comments_mode scanner.Comme
stmt = com
stmts << stmt
}
// module
mut mstmt := ast.Stmt{}
module_decl := p.module_decl()
mstmt = module_decl
stmts << mstmt
// imports
/*
mut imports := []ast.Import{}
for p.tok.kind == .key_import {
imports << p.import_stmt()
stmts << p.import_stmt()
}
*/
// TODO: import only mode
for {
if p.tok.kind == .eof {
if p.pref.is_script && !p.pref.is_test && p.mod == 'main' && !have_fn_main(stmts) {
@ -320,6 +317,7 @@ pub fn (mut p Parser) top_stmt() ast.Stmt {
return p.interface_decl()
}
.key_import {
p.error_with_pos('`import x` can only be declared at the beginning of the file', p.tok.position())
return p.import_stmt()
}
.key_global {