chore: move common code to function

This commit is contained in:
Pol Valletbó 2023-10-11 15:45:45 +02:00
parent e1aeb7fa79
commit b769f34f63

View file

@ -291,11 +291,7 @@ impl<'a> Converter<'a> {
let text = &self.res.text[self.offset + 1..][..len - 1]; let text = &self.res.text[self.offset + 1..][..len - 1];
let i = text.rfind('"').unwrap(); let i = text.rfind('"').unwrap();
let text = &text[..i]; let text = &text[..i];
rustc_lexer::unescape::unescape_literal(text, Mode::Str, &mut |_, res| { err = unescape_string_error_message(text, Mode::Str);
if let Err(e) = res {
err = error_to_diagnostic_message(e, Mode::Str);
}
});
} }
STRING STRING
} }
@ -306,11 +302,7 @@ impl<'a> Converter<'a> {
let text = &self.res.text[self.offset + 2..][..len - 2]; let text = &self.res.text[self.offset + 2..][..len - 2];
let i = text.rfind('"').unwrap(); let i = text.rfind('"').unwrap();
let text = &text[..i]; let text = &text[..i];
rustc_lexer::unescape::unescape_literal(text, Mode::ByteStr, &mut |_, res| { err = unescape_string_error_message(text, Mode::ByteStr);
if let Err(e) = res {
err = error_to_diagnostic_message(e, Mode::ByteStr);
}
})
} }
BYTE_STRING BYTE_STRING
} }
@ -321,11 +313,7 @@ impl<'a> Converter<'a> {
let text = &self.res.text[self.offset + 2..][..len - 2]; let text = &self.res.text[self.offset + 2..][..len - 2];
let i = text.rfind('"').unwrap(); let i = text.rfind('"').unwrap();
let text = &text[..i]; let text = &text[..i];
rustc_lexer::unescape::unescape_c_string(text, Mode::CStr, &mut |_, res| { err = unescape_string_error_message(text, Mode::CStr);
if let Err(e) = res {
err = error_to_diagnostic_message(e, Mode::CStr);
}
})
} }
C_STRING 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) { fn unescape_string_error_message(text: &str, mode: Mode) -> &'static str {
let mut error_message = "";
rustc_lexer::unescape::unescape_c_string(text, mode, &mut |_, res| { match mode {
if let Err(e) = res { Mode::CStr => {
error_message = error_to_diagnostic_message(e, mode); 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
} }