diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 63953586ae..c1f3a52770 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -2719,7 +2719,6 @@ fn (mut p Parser) const_decl() ast.ConstDecl { if is_pub { p.next() } - end_pos := p.tok.position() const_pos := p.tok.position() p.check(.key_const) is_block := p.tok.kind == .lpar @@ -2729,11 +2728,11 @@ fn (mut p Parser) const_decl() ast.ConstDecl { mut fields := []ast.ConstField{} mut comments := []ast.Comment{} for { - if p.tok.kind == .eof { - p.error_with_pos('const declaration is missing closing `)`', const_pos) + comments = p.eat_comments({}) + if is_block && p.tok.kind == .eof { + p.error('unexpected eof, expecting ´)´') return ast.ConstDecl{} } - comments = p.eat_comments({}) if p.tok.kind == .rpar { break } @@ -2749,6 +2748,10 @@ fn (mut p Parser) const_decl() ast.ConstDecl { p.error('const initializer fn literal is not a constant') return ast.ConstDecl{} } + if p.tok.kind == .eof { + p.error('unexpected eof, expecting an expression') + return ast.ConstDecl{} + } expr := p.expr(0) field := ast.ConstField{ name: full_name @@ -2770,7 +2773,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl { p.check(.rpar) } 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 is_pub: is_pub end_comments: comments diff --git a/vlib/v/parser/tests/const_missing_rpar_a.out b/vlib/v/parser/tests/const_missing_rpar_a.out new file mode 100644 index 0000000000..e3d01862ac --- /dev/null +++ b/vlib/v/parser/tests/const_missing_rpar_a.out @@ -0,0 +1,3 @@ +vlib/v/parser/tests/const_missing_rpar_a.vv:3:1: error: unexpected eof, expecting ´)´ + 1 | const ( + 2 | a = 5 diff --git a/vlib/v/parser/tests/const_missing_rpar_a.vv b/vlib/v/parser/tests/const_missing_rpar_a.vv new file mode 100644 index 0000000000..38043f9f84 --- /dev/null +++ b/vlib/v/parser/tests/const_missing_rpar_a.vv @@ -0,0 +1,2 @@ +const ( + a = 5 diff --git a/vlib/v/parser/tests/const_missing_rpar_b.out b/vlib/v/parser/tests/const_missing_rpar_b.out new file mode 100644 index 0000000000..4bef87eac8 --- /dev/null +++ b/vlib/v/parser/tests/const_missing_rpar_b.out @@ -0,0 +1,3 @@ +vlib/v/parser/tests/const_missing_rpar_b.vv:4:1: error: unexpected eof, expecting ´)´ + 2 | a = 5 + 3 | // foo diff --git a/vlib/v/parser/tests/const_missing_rpar_b.vv b/vlib/v/parser/tests/const_missing_rpar_b.vv new file mode 100644 index 0000000000..dbb66af2f3 --- /dev/null +++ b/vlib/v/parser/tests/const_missing_rpar_b.vv @@ -0,0 +1,3 @@ +const ( + a = 5 + // foo diff --git a/vlib/v/parser/tests/const_only_keyword.out b/vlib/v/parser/tests/const_only_keyword.out new file mode 100644 index 0000000000..1e3de05c03 --- /dev/null +++ b/vlib/v/parser/tests/const_only_keyword.out @@ -0,0 +1,2 @@ +vlib/v/parser/tests/const_only_keyword.vv:2:1: error: unexpected eof, expecting name + 1 | const diff --git a/vlib/v/parser/tests/const_only_keyword.vv b/vlib/v/parser/tests/const_only_keyword.vv new file mode 100644 index 0000000000..aaae4e1105 --- /dev/null +++ b/vlib/v/parser/tests/const_only_keyword.vv @@ -0,0 +1 @@ +const diff --git a/vlib/v/parser/tests/const_unexpected_eof.out b/vlib/v/parser/tests/const_unexpected_eof.out new file mode 100644 index 0000000000..8f90da6364 --- /dev/null +++ b/vlib/v/parser/tests/const_unexpected_eof.out @@ -0,0 +1,2 @@ +vlib/v/parser/tests/const_unexpected_eof.vv:2:1: error: unexpected eof, expecting an expression + 1 | const a = diff --git a/vlib/v/parser/tests/const_unexpected_eof.vv b/vlib/v/parser/tests/const_unexpected_eof.vv new file mode 100644 index 0000000000..6210a527e8 --- /dev/null +++ b/vlib/v/parser/tests/const_unexpected_eof.vv @@ -0,0 +1 @@ +const a =