From e6775913aa0d8e0280919e56d982fa1c07f1eb30 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 11 Nov 2019 08:58:50 +0300 Subject: [PATCH] simplify vfmt --- vlib/compiler/fn.v | 1 + vlib/compiler/parser.v | 1 - vlib/compiler/scanner.v | 7 +++++-- vlib/compiler/token.v | 3 +++ vlib/compiler/vfmt.v | 17 +++++------------ 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/vlib/compiler/fn.v b/vlib/compiler/fn.v index 5d35bedc20..9452aa1ae3 100644 --- a/vlib/compiler/fn.v +++ b/vlib/compiler/fn.v @@ -969,6 +969,7 @@ fn (p mut Parser) fn_call_args(f mut Fn) { p.error('`$arg.name` is a mutable argument, you need to provide a variable to modify: `$f.name(... mut a...)`') } p.check(.key_mut) + p.fspace() var_name := p.lit v := p.find_var(var_name) or { p.error('`$arg.name` is a mutable argument, you need to provide a variable to modify: `$f.name(... mut a...)`') diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index e8765072c4..bc78258b7b 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -73,7 +73,6 @@ mut: sql_params []string // ("select * from users where id = $1", ***"100"***) sql_types []string // int, string and so on; see sql_params is_vh bool // parsing .vh file (for example `const (a int)` is allowed) - fmt_dollar bool pub: mod string } diff --git a/vlib/compiler/scanner.v b/vlib/compiler/scanner.v index 1c7f0d3775..21094db697 100644 --- a/vlib/compiler/scanner.v +++ b/vlib/compiler/scanner.v @@ -42,6 +42,8 @@ mut: line_ends []int // the positions of source lines ends (i.e. \n signs) nlines int // total number of lines in the source file that were scanned is_vh bool // Keep newlines + is_fmt bool // Used only for skipping ${} in strings, since we need literal + // string values when generating formatted code. } // new scanner from file. @@ -67,6 +69,7 @@ fn new_scanner_file(file_path string) &Scanner { } mut s := new_scanner(raw_text) + s.init_fmt() s.file_path = file_path return s @@ -688,14 +691,14 @@ fn (s mut Scanner) ident_string() string { s.error('0 character in a string literal') } // ${var} - if c == `{` && prevc == `$` && !is_raw && s.count_symbol_before(s.pos-2, slash) % 2 == 0 { + if c == `{` && prevc == `$` && !is_raw && !s.is_fmt && s.count_symbol_before(s.pos-2, slash) % 2 == 0 { s.inside_string = true // so that s.pos points to $ at the next step s.pos -= 2 break } // $var - if (c.is_letter() || c == `_`) && prevc == `$` && !is_raw && s.count_symbol_before(s.pos-2, slash) % 2 == 0 { + if (c.is_letter() || c == `_`) && prevc == `$` && !s.is_fmt && !is_raw && s.count_symbol_before(s.pos-2, slash) % 2 == 0 { s.inside_string = true s.inter_start = true s.pos -= 2 diff --git a/vlib/compiler/token.v b/vlib/compiler/token.v index fd39635c92..7ff6b54fc0 100644 --- a/vlib/compiler/token.v +++ b/vlib/compiler/token.v @@ -290,6 +290,9 @@ fn (t Token) str() string { return t.lit } + if t.tok == .chartoken { + return '`$t.lit`' + } if t.tok == .str { return "'$t.lit'" } diff --git a/vlib/compiler/vfmt.v b/vlib/compiler/vfmt.v index 3ac3235642..d1ce784faf 100644 --- a/vlib/compiler/vfmt.v +++ b/vlib/compiler/vfmt.v @@ -86,6 +86,11 @@ fn (p mut Parser) fmt_dec() { p.scanner.fmt_indent-- } +[if vfmt] +fn (p mut Scanner) init_fmt() { + p.is_fmt = true +} + [if vfmt] fn (p mut Parser) fnext() { if p.tok == .eof { @@ -95,18 +100,6 @@ fn (p mut Parser) fnext() { p.fmt_dec() } mut s := p.strtok() - // Need to reconstruct an interpolated string from multiple string and - // dollar tokens. - // 'abc $name zxc' => ['abc', $, name, 'zxc'] => 'abc'$name'zxc' - // need to remove the extra ' - if p.tok == .str && p.peek() == .dollar { - s = s[..s.len - 1] - p.fmt_dollar = true - } - else if p.tok == .str && p.fmt_dollar { - s = s[1..] - p.fmt_dollar = false - } p.fgen(s) // vfmt: increase indentation on `{` unless it's `{}` if p.tok == .lcbr && !p.inside_if_expr && p.peek() != .rcbr {