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

fmt: keep char literal, ' (#11060)

This commit is contained in:
zakuro 2021-08-06 12:21:28 +09:00 committed by GitHub
parent b95224aa20
commit 490dec222f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 18 additions and 14 deletions

View File

@ -44,7 +44,7 @@ fn (mut r Repl) checks() bool {
mut in_string := false
was_indent := r.indent > 0
for i := 0; i < r.line.len; i++ {
if r.line[i] == `\'` && (i == 0 || r.line[i - 1] != `\\`) {
if r.line[i] == `'` && (i == 0 || r.line[i - 1] != `\\`) {
in_string = !in_string
}
if r.line[i] == `{` && !in_string {

View File

@ -651,7 +651,7 @@ fn test_for_loop_two() {
}
fn test_quote() {
a := `\'`
a := `'`
println('testing double quotes')
b := 'hi'
assert b == 'hi'

View File

@ -111,10 +111,10 @@ pub fn (mut parser Parser) split_parse(data string) {
parser.init()
for chr in data {
// returns true if byte is a " or '
is_quote := chr == `"` || chr == `\'`
is_quote := chr == `"` || chr == `'`
string_code := match chr {
`"` { 1 } // "
`\'` { 2 } // '
`'` { 2 } // '
else { 0 }
}
if parser.lexical_attributes.open_code { // here will verify all needed to know if open_code finishes and string in code

View File

@ -113,7 +113,7 @@ pub fn v_sprintf(str string, pt ...voidptr) string {
}
i++
continue
} else if ch == `\'` {
} else if ch == `'` {
i++
continue
} else if ch == `.` && fc_ch1 >= `1` && fc_ch1 <= `9` {

View File

@ -521,7 +521,11 @@ pub fn (mut f Fmt) expr(node ast.Expr) {
f.chan_init(mut node)
}
ast.CharLiteral {
f.write('`$node.val`')
if node.val == r"\'" {
f.write("`'`")
} else {
f.write('`$node.val`')
}
}
ast.Comment {
f.comment(node, inline: true)

View File

@ -1,3 +0,0 @@
fn char_backtick() {
println(`\``)
}

View File

@ -0,0 +1,3 @@
println(`"`)
printrn(`'`)
println(`\``)

View File

@ -5074,7 +5074,7 @@ fn (mut g Gen) const_decl_precomputed(mod string, name string, ct_value ast.Comp
rune {
rune_code := u32(ct_value)
if rune_code <= 255 {
if rune_code in [`"`, `\\`, `\'`] {
if rune_code in [`"`, `\\`, `'`] {
return false
}
escval := util.smart_quote(byte(rune_code).ascii_str(), false)

View File

@ -13,7 +13,7 @@ import v.vet
import v.errors
const (
single_quote = `\'`
single_quote = `'`
double_quote = `"`
// char used as number separator
num_sep = `_`
@ -677,7 +677,7 @@ fn (mut s Scanner) text_scan() token.Token {
// end of `$expr`
// allow `'$a.b'` and `'$a.c()'`
if s.is_inter_start && next_char == `\\`
&& s.look_ahead(2) !in [`x`, `n`, `r`, `\\`, `t`, `e`, `"`, `\'`] {
&& s.look_ahead(2) !in [`x`, `n`, `r`, `\\`, `t`, `e`, `"`, `'`] {
s.warn('unknown escape sequence \\${s.look_ahead(2)}')
}
if s.is_inter_start && next_char == `(` {

View File

@ -75,7 +75,7 @@ pub fn resolve_env_value(str string, check_for_presence bool) ?string {
if ch.is_letter() || ch.is_digit() || ch == `_` {
env_lit += ch.ascii_str()
} else {
if !(ch == `\'` || ch == `)`) {
if !(ch == `'` || ch == `)`) {
if ch == `$` {
return error('cannot use string interpolation in compile time \$env() expression')
}

View File

@ -130,7 +130,7 @@ fn (mut s Scanner) scan_all() {
continue
}
}
if c in [`\'`, `\"`] && !s.peek_char(`\\`) {
if c in [`'`, `\"`] && !s.peek_char(`\\`) {
s.pos++
str := s.create_string(c)
s.tokenize(.str, str)