From 50cb9a655d207b99136fde31ecfa56908e4c57b3 Mon Sep 17 00:00:00 2001 From: Ayush Choudhary <87753540+ayushch80@users.noreply.github.com> Date: Fri, 2 Jan 2026 15:19:52 +0530 Subject: [PATCH] vrepl: fix handling of `//` inside string literals (fix #25603) (#26240) --- cmd/tools/vrepl.v | 53 +++++++++++++++++++++++- vlib/v/slow_tests/repl/line_comment.repl | 30 ++++++++++++++ vlib/v/slow_tests/repl/runner/runner.v | 2 +- 3 files changed, 82 insertions(+), 3 deletions(-) diff --git a/cmd/tools/vrepl.v b/cmd/tools/vrepl.v index ada995bda..441582192 100644 --- a/cmd/tools/vrepl.v +++ b/cmd/tools/vrepl.v @@ -48,7 +48,6 @@ const repl_folder = os.join_path(os.vtmp_dir(), 'repl') const possible_statement_patterns = [ '++', '--', - '//', '/*', 'assert ', 'fn ', @@ -410,6 +409,54 @@ fn print_welcome_screen() { } } +fn remove_comment(oline string) string { + mut inside_string := false + mut escaped := false + mut maybe_comment := false + + mut string_delim := 0 + + mut i := 0 + for i < oline.len { + match oline[i] { + `"`, `'` { + if !escaped { + if inside_string && string_delim == oline[i] { + inside_string = false + } else if !inside_string { + inside_string = true + maybe_comment = false + string_delim = oline[i] + } + } else { + escaped = false + } + } + `\\` { + if inside_string { + escaped = !escaped + } + } + `/` { + if maybe_comment { + i-- + break + } else if !inside_string { + maybe_comment = true + } + } + else { + escaped = false + maybe_comment = false + } + } + + i++ + } + + return oline[..i] +} + fn run_repl(workdir string, vrepl_prefix string) int { if !is_stdin_a_pipe { print_welcome_screen() @@ -440,8 +487,10 @@ fn run_repl(workdir string, vrepl_prefix string) int { } else { prompt = '... ' } + oline := r.get_one_line(prompt) or { break } - line := oline.all_before('//').trim_space() + line := remove_comment(oline).trim_space() + if line == '' { continue } diff --git a/vlib/v/slow_tests/repl/line_comment.repl b/vlib/v/slow_tests/repl/line_comment.repl index 9218e2c41..c63730611 100644 --- a/vlib/v/slow_tests/repl/line_comment.repl +++ b/vlib/v/slow_tests/repl/line_comment.repl @@ -1,5 +1,35 @@ math.pi math.pi // some comment +"a//b" +"a \" // b" +"a 'b' // ok" +"http://example.com" +"http://example.com" // comment +"\\\\//" +"\\\\//" // comment +"\"//\"" +"\"\\\"//\"" +"end\"" // comment +"end\\\"" // comment +"π // inside" +"π \\\" // inside" +1 // 2 // 3 +42//comment ===output=== 3.141592653589793 3.141592653589793 +a//b +a " // b +a 'b' // ok +http://example.com +http://example.com +\\// +\\// +"//" +"\"//" +end" +end\" +π // inside +π \" // inside +1 +42 diff --git a/vlib/v/slow_tests/repl/runner/runner.v b/vlib/v/slow_tests/repl/runner/runner.v index c14bca1b6..962afd6be 100644 --- a/vlib/v/slow_tests/repl/runner/runner.v +++ b/vlib/v/slow_tests/repl/runner/runner.v @@ -56,7 +56,7 @@ pub fn run_repl_file(wd string, vexec string, file string) !string { return error('Could not execute: ${rcmd}') } result := r.output.replace_each(['\r', '', '>>> ', '', '>>>', '', '... ', '', - wd + os.path_separator, '', vexec_folder, '', '\\', '/']).trim_right('\n\r') + wd + os.path_separator, '', vexec_folder, '']).trim_right('\n\r') $if windows { dump(rcmd) dump(r.output) -- 2.39.5