scanner: fix escape `\u` (fix #6849) (#7012)

pull/6213/head
yuyi 2020-11-29 23:01:40 +08:00 committed by GitHub
parent 008ce8fc65
commit 0b96cd50e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 5 deletions

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/string_escape_u_err_a.vv:2:15: error: `\u` incomplete unicode character value
1 | fn main() {
2 | println('\u')
| ^
3 | }

View File

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

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/string_escape_u_err_b.vv:2:15: error: `\u` incomplete unicode character value
1 | fn main() {
2 | println('\u345')
| ^
3 | }

View File

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

View File

@ -1039,12 +1039,20 @@ 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` `\u`
if prevc == slash && !is_raw && !is_cstr && s.count_symbol_before(s.pos - 2, slash) % 2 == 0 {
// Escape `\x` // Escape `\x`
if prevc == slash && if c == `x` && (s.text[s.pos + 1] == s.quote || !s.text[s.pos + 1].is_hex_digit()) {
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') s.error(r'`\x` used with no following hex digits')
} }
// Escape `\u`
if c == `u` && (s.text[s.pos + 1] == s.quote ||
s.text[s.pos + 2] == s.quote || s.text[s.pos + 3] == s.quote || s.text[s.pos + 4] == s.quote ||
!s.text[s.pos + 1].is_hex_digit() || !s.text[s.pos + 2].is_hex_digit() || !s.text[s.pos + 3].is_hex_digit() ||
!s.text[s.pos + 4].is_hex_digit()) {
s.error(r'`\u` incomplete unicode character value')
}
}
// ${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