scanner: guarantee an early exit when the parser/scanner is stuck

pull/6980/head
Delyan Angelov 2020-11-27 12:44:25 +02:00
parent 413d14f53e
commit e6116c47be
3 changed files with 22 additions and 1 deletions

View File

@ -59,6 +59,7 @@ pub fn compile(command string, pref &pref.Preferences) {
if pref.is_stats { if pref.is_stats {
println('compilation took: ${util.bold(sw.elapsed().milliseconds().str())} ms') println('compilation took: ${util.bold(sw.elapsed().milliseconds().str())} ms')
} }
b.exit_on_invalid_syntax()
// running does not require the parsers anymore // running does not require the parsers anymore
unsafe {b.myfree()} unsafe {b.myfree()}
if pref.is_test || pref.is_run { if pref.is_test || pref.is_run {
@ -74,6 +75,21 @@ fn (mut b Builder) myfree() {
unsafe {b.parsed_files.free()} unsafe {b.parsed_files.free()}
} }
fn (b &Builder) exit_on_invalid_syntax() {
// V should exit with an exit code of 1, when there are errors,
// even when -silent is passed in combination to -check-syntax:
if b.pref.only_check_syntax {
for pf in b.parsed_files {
if pf.errors.len > 0 {
exit(1)
}
}
if b.checker.nr_errors > 0 {
exit(1)
}
}
}
fn (mut b Builder) run_compiled_executable_and_exit() { fn (mut b Builder) run_compiled_executable_and_exit() {
if b.pref.skip_running { if b.pref.skip_running {
return return

View File

@ -111,7 +111,12 @@ pub fn (mut p Parser) call_expr(language table.Language, mod string) ast.CallExp
pub fn (mut p Parser) call_args() []ast.CallArg { pub fn (mut p Parser) call_args() []ast.CallArg {
mut args := []ast.CallArg{} mut args := []ast.CallArg{}
start_pos := p.tok.position()
for p.tok.kind != .rpar { for p.tok.kind != .rpar {
if p.tok.kind == .eof {
p.error_with_pos('unexpected eof reached, while parsing call argument', start_pos)
break
}
is_shared := p.tok.kind == .key_shared is_shared := p.tok.kind == .key_shared
is_atomic := p.tok.kind == .key_atomic is_atomic := p.tok.kind == .key_atomic
is_mut := p.tok.kind == .key_mut || is_shared || is_atomic is_mut := p.tok.kind == .key_mut || is_shared || is_atomic

View File

@ -450,7 +450,7 @@ fn (mut s Scanner) end_of_file() token.Token {
s.eofs++ s.eofs++
if s.eofs > 50 { if s.eofs > 50 {
s.line_nr-- s.line_nr--
s.error('the end of file `$s.file_path` has been reached 50 times already, the v parser is probably stuck.\n' + panic('the end of file `$s.file_path` has been reached 50 times already, the v parser is probably stuck.\n' +
'This should not happen. Please report the bug here, and include the last 2-3 lines of your source code:\n' + 'This should not happen. Please report the bug here, and include the last 2-3 lines of your source code:\n' +
'https://github.com/vlang/v/issues/new?labels=Bug&template=bug_report.md') 'https://github.com/vlang/v/issues/new?labels=Bug&template=bug_report.md')
} }