parser: proper unexpected eof errors in const declarations (#9712)

pull/9739/head
Lukas Neubert 2021-04-15 01:31:49 +02:00 committed by GitHub
parent 0099458c0a
commit f4c8f897fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 25 additions and 5 deletions

View File

@ -2719,7 +2719,6 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
if is_pub { if is_pub {
p.next() p.next()
} }
end_pos := p.tok.position()
const_pos := p.tok.position() const_pos := p.tok.position()
p.check(.key_const) p.check(.key_const)
is_block := p.tok.kind == .lpar is_block := p.tok.kind == .lpar
@ -2729,11 +2728,11 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
mut fields := []ast.ConstField{} mut fields := []ast.ConstField{}
mut comments := []ast.Comment{} mut comments := []ast.Comment{}
for { for {
if p.tok.kind == .eof { comments = p.eat_comments({})
p.error_with_pos('const declaration is missing closing `)`', const_pos) if is_block && p.tok.kind == .eof {
p.error('unexpected eof, expecting ´)´')
return ast.ConstDecl{} return ast.ConstDecl{}
} }
comments = p.eat_comments({})
if p.tok.kind == .rpar { if p.tok.kind == .rpar {
break break
} }
@ -2749,6 +2748,10 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
p.error('const initializer fn literal is not a constant') p.error('const initializer fn literal is not a constant')
return ast.ConstDecl{} return ast.ConstDecl{}
} }
if p.tok.kind == .eof {
p.error('unexpected eof, expecting an expression')
return ast.ConstDecl{}
}
expr := p.expr(0) expr := p.expr(0)
field := ast.ConstField{ field := ast.ConstField{
name: full_name name: full_name
@ -2770,7 +2773,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
p.check(.rpar) p.check(.rpar)
} }
return ast.ConstDecl{ return ast.ConstDecl{
pos: start_pos.extend_with_last_line(end_pos, p.prev_tok.line_nr) pos: start_pos.extend_with_last_line(const_pos, p.prev_tok.line_nr)
fields: fields fields: fields
is_pub: is_pub is_pub: is_pub
end_comments: comments end_comments: comments

View File

@ -0,0 +1,3 @@
vlib/v/parser/tests/const_missing_rpar_a.vv:3:1: error: unexpected eof, expecting ´)´
1 | const (
2 | a = 5

View File

@ -0,0 +1,2 @@
const (
a = 5

View File

@ -0,0 +1,3 @@
vlib/v/parser/tests/const_missing_rpar_b.vv:4:1: error: unexpected eof, expecting ´)´
2 | a = 5
3 | // foo

View File

@ -0,0 +1,3 @@
const (
a = 5
// foo

View File

@ -0,0 +1,2 @@
vlib/v/parser/tests/const_only_keyword.vv:2:1: error: unexpected eof, expecting name
1 | const

View File

@ -0,0 +1 @@
const

View File

@ -0,0 +1,2 @@
vlib/v/parser/tests/const_unexpected_eof.vv:2:1: error: unexpected eof, expecting an expression
1 | const a =

View File

@ -0,0 +1 @@
const a =