From df45932c0374642a18bfd9f4a8bf5ca5f6d5115c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 5 Apr 2020 16:38:29 +0200 Subject: [PATCH] vfmt: fix same line stmt comments --- vlib/v/ast/ast.v | 1 + vlib/v/fmt/fmt.v | 8 +++++++- vlib/v/gen/cgen.v | 6 ++---- vlib/v/scanner/scanner.v | 18 +++++++++++------- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index c5e213adcc..4b23a2d5e9 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -677,6 +677,7 @@ pub: is_multi bool line_nr int pos token.Position + same_line bool } pub struct ConcatExpr { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 550de110f2..9160ce794d 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -681,7 +681,13 @@ fn (f mut Fmt) or_expr(or_block ast.OrExpr) { fn (f mut Fmt) comment(node ast.Comment) { if !node.text.contains('\n') { - f.writeln('// $node.text')// $node.pos.line_nr') + is_separate_line := node.text.starts_with('|') + if is_separate_line { + f.writeln('// ${node.text[1..]}')// $node.pos.line_nr') + } else { + f.out.go_back(1) + f.writeln('// $node.text') + } return } lines := node.text.split_into_lines() diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 6d6eaa32f9..cdf5c45717 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -11,8 +11,8 @@ import ( v.util ) +// Full list of C reserved words, from: https://en.cppreference.com/w/c/keyword const ( - // Full list of C reserved words, from: https://en.cppreference.com/w/c/keyword c_reserved = ['delete', 'exit', 'unix', 'error', 'calloc', 'malloc', 'free', 'panic', 'auto', 'char', 'default', 'do', 'double', 'extern', 'float', 'inline', 'int', 'long', 'register', @@ -59,9 +59,6 @@ const ( '\t\t\t\t\t\t\t\t'] ) -fn foo(file []ast.File) {} -fn foo2(file []int) {} - pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string { // println('start cgen2') mut g := Gen{ @@ -228,6 +225,7 @@ pub fn (g mut Gen) write_typedef_types() { g.definitions.writeln(');') } } + // else { continue } diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 5dcfb51837..7a2216ff91 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -732,17 +732,21 @@ pub fn (s mut Scanner) scan() token.Token { start := s.pos + 1 s.ignore_line() s.line_comment = s.text[start + 1..s.pos] - // if s.comments_mode == .parse_comments { - // println('line c $s.line_comment') - // } comment := s.line_comment.trim_space() - // s.line_comment = comment if s.comments_mode == .parse_comments { - // println('line c "$comment" z=') + // Find out if this comment is on its own line (for vfmt) + mut is_separate_line_comment := true + for j := start-2; s.text[j] != `\n`; j-- { + if !(s.text[j] in [`\t`, ` `]) { + is_separate_line_comment = false + } + } + if is_separate_line_comment { + comment = '|' + comment + } + s.pos-- // fix line_nr, \n was read, and the comment is marked // on the next line - s.pos-- - // println("'" + s.text[s.pos].str() + "'") s.line_nr-- return s.new_token(.comment, comment) }