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

pull/7118/head
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() 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) s := scanner.new_scanner(text, comments_mode, pref)
mut p := Parser{ mut p := Parser{
scanner: s scanner: s
comments_mode: comments_mode comments_mode: comments_mode
file_name: path
file_base: os.base(path)
file_name_dir: os.dir(path)
table: table table: table
pref: pref pref: pref
scope: &ast.Scope{ scope: &ast.Scope{
@ -1708,14 +1711,20 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
p.next() p.next()
} }
end_pos := p.tok.position() end_pos := p.tok.position()
const_pos := p.tok.position()
p.check(.key_const) p.check(.key_const)
if p.tok.kind != .lpar { 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() // ( p.next() // (
mut fields := []ast.ConstField{} mut fields := []ast.ConstField{}
mut comments := []ast.Comment{} mut comments := []ast.Comment{}
for { for {
if p.tok.kind == .eof {
p.error_with_pos('const declaration is missing closing `)`', const_pos)
return ast.ConstDecl{}
}
comments = p.eat_comments() comments = p.eat_comments()
if p.tok.kind == .rpar { if p.tok.kind == .rpar {
break break

View File

@ -231,8 +231,11 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr {
} }
} }
else { else {
p.error_with_pos('invalid expression: unexpected $p.tok.kind.str() token', if p.tok.kind != .eof {
p.tok.position()) // 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) return p.expr_with_left(node, precedence, is_stmt_ident)