mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
simplify vfmt
This commit is contained in:
parent
d9b29bfb4e
commit
e6775913aa
@ -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.error('`$arg.name` is a mutable argument, you need to provide a variable to modify: `$f.name(... mut a...)`')
|
||||||
}
|
}
|
||||||
p.check(.key_mut)
|
p.check(.key_mut)
|
||||||
|
p.fspace()
|
||||||
var_name := p.lit
|
var_name := p.lit
|
||||||
v := p.find_var(var_name) or {
|
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...)`')
|
p.error('`$arg.name` is a mutable argument, you need to provide a variable to modify: `$f.name(... mut a...)`')
|
||||||
|
@ -73,7 +73,6 @@ mut:
|
|||||||
sql_params []string // ("select * from users where id = $1", ***"100"***)
|
sql_params []string // ("select * from users where id = $1", ***"100"***)
|
||||||
sql_types []string // int, string and so on; see sql_params
|
sql_types []string // int, string and so on; see sql_params
|
||||||
is_vh bool // parsing .vh file (for example `const (a int)` is allowed)
|
is_vh bool // parsing .vh file (for example `const (a int)` is allowed)
|
||||||
fmt_dollar bool
|
|
||||||
pub:
|
pub:
|
||||||
mod string
|
mod string
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,8 @@ mut:
|
|||||||
line_ends []int // the positions of source lines ends (i.e. \n signs)
|
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
|
nlines int // total number of lines in the source file that were scanned
|
||||||
is_vh bool // Keep newlines
|
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.
|
// new scanner from file.
|
||||||
@ -67,6 +69,7 @@ fn new_scanner_file(file_path string) &Scanner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mut s := new_scanner(raw_text)
|
mut s := new_scanner(raw_text)
|
||||||
|
s.init_fmt()
|
||||||
s.file_path = file_path
|
s.file_path = file_path
|
||||||
|
|
||||||
return s
|
return s
|
||||||
@ -688,14 +691,14 @@ fn (s mut Scanner) ident_string() string {
|
|||||||
s.error('0 character in a string literal')
|
s.error('0 character in a string literal')
|
||||||
}
|
}
|
||||||
// ${var}
|
// ${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
|
s.inside_string = true
|
||||||
// so that s.pos points to $ at the next step
|
// so that s.pos points to $ at the next step
|
||||||
s.pos -= 2
|
s.pos -= 2
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
// $var
|
// $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.inside_string = true
|
||||||
s.inter_start = true
|
s.inter_start = true
|
||||||
s.pos -= 2
|
s.pos -= 2
|
||||||
|
@ -290,6 +290,9 @@ fn (t Token) str() string {
|
|||||||
return t.lit
|
return t.lit
|
||||||
|
|
||||||
}
|
}
|
||||||
|
if t.tok == .chartoken {
|
||||||
|
return '`$t.lit`'
|
||||||
|
}
|
||||||
if t.tok == .str {
|
if t.tok == .str {
|
||||||
return "'$t.lit'"
|
return "'$t.lit'"
|
||||||
}
|
}
|
||||||
|
@ -86,6 +86,11 @@ fn (p mut Parser) fmt_dec() {
|
|||||||
p.scanner.fmt_indent--
|
p.scanner.fmt_indent--
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[if vfmt]
|
||||||
|
fn (p mut Scanner) init_fmt() {
|
||||||
|
p.is_fmt = true
|
||||||
|
}
|
||||||
|
|
||||||
[if vfmt]
|
[if vfmt]
|
||||||
fn (p mut Parser) fnext() {
|
fn (p mut Parser) fnext() {
|
||||||
if p.tok == .eof {
|
if p.tok == .eof {
|
||||||
@ -95,18 +100,6 @@ fn (p mut Parser) fnext() {
|
|||||||
p.fmt_dec()
|
p.fmt_dec()
|
||||||
}
|
}
|
||||||
mut s := p.strtok()
|
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)
|
p.fgen(s)
|
||||||
// vfmt: increase indentation on `{` unless it's `{}`
|
// vfmt: increase indentation on `{` unless it's `{}`
|
||||||
if p.tok == .lcbr && !p.inside_if_expr && p.peek() != .rcbr {
|
if p.tok == .lcbr && !p.inside_if_expr && p.peek() != .rcbr {
|
||||||
|
Loading…
Reference in New Issue
Block a user