1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

simplify vfmt

This commit is contained in:
Alexander Medvednikov 2019-11-11 08:58:50 +03:00
parent d9b29bfb4e
commit e6775913aa
5 changed files with 14 additions and 15 deletions

View File

@ -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...)`')

View File

@ -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
}

View File

@ -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

View File

@ -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'"
}

View File

@ -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 {