From e6116c47befa3cb533d5ae7c8af7d23c6bede1bc Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Fri, 27 Nov 2020 12:44:25 +0200 Subject: [PATCH] scanner: guarantee an early exit when the parser/scanner is stuck --- vlib/v/builder/compile.v | 16 ++++++++++++++++ vlib/v/parser/fn.v | 5 +++++ vlib/v/scanner/scanner.v | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/vlib/v/builder/compile.v b/vlib/v/builder/compile.v index 6373e764ef..8a687c5bac 100644 --- a/vlib/v/builder/compile.v +++ b/vlib/v/builder/compile.v @@ -59,6 +59,7 @@ pub fn compile(command string, pref &pref.Preferences) { if pref.is_stats { println('compilation took: ${util.bold(sw.elapsed().milliseconds().str())} ms') } + b.exit_on_invalid_syntax() // running does not require the parsers anymore unsafe {b.myfree()} if pref.is_test || pref.is_run { @@ -74,6 +75,21 @@ fn (mut b Builder) myfree() { 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() { if b.pref.skip_running { return diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index dd8a34b7c5..161eaf1933 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -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 { mut args := []ast.CallArg{} + start_pos := p.tok.position() 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_atomic := p.tok.kind == .key_atomic is_mut := p.tok.kind == .key_mut || is_shared || is_atomic diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index bc1017eacd..537d35198c 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -450,7 +450,7 @@ fn (mut s Scanner) end_of_file() token.Token { s.eofs++ if s.eofs > 50 { 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' + 'https://github.com/vlang/v/issues/new?labels=Bug&template=bug_report.md') }