From 6c100a0bc3f66a214b95f72878e512bcc6ef2f1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Thu, 3 Dec 2020 20:11:43 +0100 Subject: [PATCH] vls,parser: fix an eof error in parsing invalid const declarations (#7115) --- vlib/v/parser/parser.v | 13 +++++++++++-- vlib/v/parser/pratt.v | 7 +++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index d2a9c3f33c..240566c89e 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -95,11 +95,14 @@ pub fn parse_comptime(text string, table &table.Table, pref &pref.Preferences, s return p.parse() } -pub fn parse_text(text string, table &table.Table, comments_mode scanner.CommentsMode, pref &pref.Preferences, global_scope &ast.Scope) ast.File { +pub fn parse_text(text string, path string, table &table.Table, comments_mode scanner.CommentsMode, pref &pref.Preferences, global_scope &ast.Scope) ast.File { s := scanner.new_scanner(text, comments_mode, pref) mut p := Parser{ scanner: s comments_mode: comments_mode + file_name: path + file_base: os.base(path) + file_name_dir: os.dir(path) table: table pref: pref scope: &ast.Scope{ @@ -1708,14 +1711,20 @@ fn (mut p Parser) const_decl() ast.ConstDecl { p.next() } end_pos := p.tok.position() + const_pos := p.tok.position() p.check(.key_const) if p.tok.kind != .lpar { - p.error('consts must be grouped, e.g.\nconst (\n\ta = 1\n)') + p.error_with_pos('const declaration is missing parentheses `( ... )`', const_pos) + return ast.ConstDecl{} } p.next() // ( 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) + return ast.ConstDecl{} + } comments = p.eat_comments() if p.tok.kind == .rpar { break diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index 22378ee0bd..a2a36f85a7 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -231,8 +231,11 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { } } else { - p.error_with_pos('invalid expression: unexpected $p.tok.kind.str() token', - p.tok.position()) + if p.tok.kind != .eof { + // eof should be handled where it happens + p.error_with_pos('invalid expression: unexpected $p.tok.kind.str() token', + p.tok.position()) + } } } return p.expr_with_left(node, precedence, is_stmt_ident)