scanner: fix error of backslash escaping x in ident_string(fix #6850) (#6994)

pull/7009/head
yuyi 2020-11-29 06:39:45 +08:00 committed by GitHub
parent 32c027a0bf
commit f336c2c5cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 0 deletions

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/string_escape_x_err_a.vv:2:15: error: `\x` used with no following hex digits
1 | fn main() {
2 | println('\x')
| ^
3 | }

View File

@ -0,0 +1,3 @@
fn main() {
println('\x')
}

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/string_escape_x_err_b.vv:2:15: error: `\x` used with no following hex digits
1 | fn main() {
2 | println('\xhh')
| ^
3 | }

View File

@ -0,0 +1,3 @@
fn main() {
println('\xhh')
}

View File

@ -1039,6 +1039,12 @@ fn (mut s Scanner) ident_string() string {
s.error(r'cannot use `\x00` (NULL character) in the string literal') s.error(r'cannot use `\x00` (NULL character) in the string literal')
} }
} }
// Escape `\x`
if prevc == slash &&
c == `x` && s.count_symbol_before(s.pos - 2, slash) % 2 == 0 && !is_raw && !is_cstr &&
(s.text[s.pos + 1] == s.quote || !s.text[s.pos + 1].is_hex_digit()) {
s.error(r'`\x` used with no following hex digits')
}
// ${var} (ignore in vfmt mode) (skip \$) // ${var} (ignore in vfmt mode) (skip \$)
if prevc == `$` && c == `{` && !is_raw && s.count_symbol_before(s.pos - 2, slash) % 2 == 0 { if prevc == `$` && c == `{` && !is_raw && s.count_symbol_before(s.pos - 2, slash) % 2 == 0 {
s.is_inside_string = true s.is_inside_string = true

View File

@ -136,3 +136,8 @@ fn test_ref_ref_array_ref_ref_foo() {
assert result[5] == .amp assert result[5] == .amp
assert result[6] == .name assert result[6] == .name
} }
fn test_escape_string() {
assert '\x61' == 'a'
assert '\x62' == 'b'
}