From b769f34f6371b13f7ce81cefe65579911331eb16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pol=20Valletb=C3=B3?= Date: Wed, 11 Oct 2023 15:45:45 +0200 Subject: [PATCH] chore: move common code to function --- crates/parser/src/lexed_str.rs | 46 ++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/crates/parser/src/lexed_str.rs b/crates/parser/src/lexed_str.rs index c2e25daf37..4d322f21ae 100644 --- a/crates/parser/src/lexed_str.rs +++ b/crates/parser/src/lexed_str.rs @@ -291,11 +291,7 @@ impl<'a> Converter<'a> { let text = &self.res.text[self.offset + 1..][..len - 1]; let i = text.rfind('"').unwrap(); let text = &text[..i]; - rustc_lexer::unescape::unescape_literal(text, Mode::Str, &mut |_, res| { - if let Err(e) = res { - err = error_to_diagnostic_message(e, Mode::Str); - } - }); + err = unescape_string_error_message(text, Mode::Str); } STRING } @@ -306,11 +302,7 @@ impl<'a> Converter<'a> { let text = &self.res.text[self.offset + 2..][..len - 2]; let i = text.rfind('"').unwrap(); let text = &text[..i]; - rustc_lexer::unescape::unescape_literal(text, Mode::ByteStr, &mut |_, res| { - if let Err(e) = res { - err = error_to_diagnostic_message(e, Mode::ByteStr); - } - }) + err = unescape_string_error_message(text, Mode::ByteStr); } BYTE_STRING } @@ -321,11 +313,7 @@ impl<'a> Converter<'a> { let text = &self.res.text[self.offset + 2..][..len - 2]; let i = text.rfind('"').unwrap(); let text = &text[..i]; - rustc_lexer::unescape::unescape_c_string(text, Mode::CStr, &mut |_, res| { - if let Err(e) = res { - err = error_to_diagnostic_message(e, Mode::CStr); - } - }) + err = unescape_string_error_message(text, Mode::CStr); } C_STRING } @@ -391,12 +379,26 @@ fn error_to_diagnostic_message(error: EscapeError, mode: Mode) -> &'static str { } } -fn fill_unescape_string_error(text: &str, mode: Mode, mut error_message: &str) { - - rustc_lexer::unescape::unescape_c_string(text, mode, &mut |_, res| { - if let Err(e) = res { - error_message = error_to_diagnostic_message(e, mode); +fn unescape_string_error_message(text: &str, mode: Mode) -> &'static str { + let mut error_message = ""; + match mode { + Mode::CStr => { + rustc_lexer::unescape::unescape_c_string(text, mode, &mut |_, res| { + if let Err(e) = res { + error_message = error_to_diagnostic_message(e, mode); + } + }); } - }); + Mode::ByteStr | Mode::Str => { + rustc_lexer::unescape::unescape_literal(text, mode, &mut |_, res| { + if let Err(e) = res { + error_message = error_to_diagnostic_message(e, mode); + } + }); + } + _ => { + // Other Modes are not supported yet or do not apply + } + } + error_message } -