Skip to content

Commit b769f34

Browse files
committed
chore: move common code to function
1 parent e1aeb7f commit b769f34

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

crates/parser/src/lexed_str.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,7 @@ impl<'a> Converter<'a> {
291291
let text = &self.res.text[self.offset + 1..][..len - 1];
292292
let i = text.rfind('"').unwrap();
293293
let text = &text[..i];
294-
rustc_lexer::unescape::unescape_literal(text, Mode::Str, &mut |_, res| {
295-
if let Err(e) = res {
296-
err = error_to_diagnostic_message(e, Mode::Str);
297-
}
298-
});
294+
err = unescape_string_error_message(text, Mode::Str);
299295
}
300296
STRING
301297
}
@@ -306,11 +302,7 @@ impl<'a> Converter<'a> {
306302
let text = &self.res.text[self.offset + 2..][..len - 2];
307303
let i = text.rfind('"').unwrap();
308304
let text = &text[..i];
309-
rustc_lexer::unescape::unescape_literal(text, Mode::ByteStr, &mut |_, res| {
310-
if let Err(e) = res {
311-
err = error_to_diagnostic_message(e, Mode::ByteStr);
312-
}
313-
})
305+
err = unescape_string_error_message(text, Mode::ByteStr);
314306
}
315307
BYTE_STRING
316308
}
@@ -321,11 +313,7 @@ impl<'a> Converter<'a> {
321313
let text = &self.res.text[self.offset + 2..][..len - 2];
322314
let i = text.rfind('"').unwrap();
323315
let text = &text[..i];
324-
rustc_lexer::unescape::unescape_c_string(text, Mode::CStr, &mut |_, res| {
325-
if let Err(e) = res {
326-
err = error_to_diagnostic_message(e, Mode::CStr);
327-
}
328-
})
316+
err = unescape_string_error_message(text, Mode::CStr);
329317
}
330318
C_STRING
331319
}
@@ -391,12 +379,26 @@ fn error_to_diagnostic_message(error: EscapeError, mode: Mode) -> &'static str {
391379
}
392380
}
393381

394-
fn fill_unescape_string_error(text: &str, mode: Mode, mut error_message: &str) {
395-
396-
rustc_lexer::unescape::unescape_c_string(text, mode, &mut |_, res| {
397-
if let Err(e) = res {
398-
error_message = error_to_diagnostic_message(e, mode);
382+
fn unescape_string_error_message(text: &str, mode: Mode) -> &'static str {
383+
let mut error_message = "";
384+
match mode {
385+
Mode::CStr => {
386+
rustc_lexer::unescape::unescape_c_string(text, mode, &mut |_, res| {
387+
if let Err(e) = res {
388+
error_message = error_to_diagnostic_message(e, mode);
389+
}
390+
});
391+
}
392+
Mode::ByteStr | Mode::Str => {
393+
rustc_lexer::unescape::unescape_literal(text, mode, &mut |_, res| {
394+
if let Err(e) = res {
395+
error_message = error_to_diagnostic_message(e, mode);
396+
}
397+
});
399398
}
400-
});
399+
_ => {
400+
// Other Modes are not supported yet or do not apply
401+
}
402+
}
403+
error_message
401404
}
402-

0 commit comments

Comments
 (0)