1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vls,parser: fix an eof error in parsing invalid const declarations (#7115)

This commit is contained in:
Daniel Däschle 2020-12-03 20:11:43 +01:00 committed by GitHub
parent 15ffce1317
commit 6c100a0bc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 4 deletions

View File

@ -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

View File

@ -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)