refactor(ra_syntax.validation): removed code duplication from validate_literal() function

This commit is contained in:
Veetaha 2020-01-14 03:34:38 +02:00
parent d8d8c20077
commit 60251da204

View file

@ -111,57 +111,48 @@ pub(crate) fn validate(root: &SyntaxNode) -> Vec<SyntaxError> {
errors errors
} }
// FIXME: kill duplication
fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) { fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
fn unquote(text: &str, prefix_len: usize, end_delimiter: char) -> Option<&str> {
text.rfind(end_delimiter).and_then(|end| text.get(prefix_len..end))
}
let token = literal.token(); let token = literal.token();
let text = token.text().as_str(); let text = token.text().as_str();
let mut push_err = |prefix_len, (off, err): (usize, unescape::EscapeError)| {
let off = token.text_range().start() + TextUnit::from_usize(off + prefix_len);
acc.push(SyntaxError::new(err.into(), off));
};
match token.kind() { match token.kind() {
BYTE => { BYTE => {
if let Some(end) = text.rfind('\'') { if let Some(Err(e)) = unquote(text, 2, '\'').map(unescape::unescape_byte) {
if let Some(without_quotes) = text.get(2..end) { push_err(2, e);
if let Err((off, err)) = unescape::unescape_byte(without_quotes) {
let off = token.text_range().start() + TextUnit::from_usize(off + 2);
acc.push(SyntaxError::new(err.into(), off))
}
}
} }
} }
CHAR => { CHAR => {
if let Some(end) = text.rfind('\'') { if let Some(Err(e)) = unquote(text, 1, '\'').map(unescape::unescape_char) {
if let Some(without_quotes) = text.get(1..end) { push_err(1, e);
if let Err((off, err)) = unescape::unescape_char(without_quotes) {
let off = token.text_range().start() + TextUnit::from_usize(off + 1);
acc.push(SyntaxError::new(err.into(), off))
}
}
} }
} }
BYTE_STRING => { BYTE_STRING => {
if let Some(end) = text.rfind('\"') { if let Some(without_quotes) = unquote(text, 2, '"') {
if let Some(without_quotes) = text.get(2..end) {
unescape::unescape_byte_str(without_quotes, &mut |range, char| { unescape::unescape_byte_str(without_quotes, &mut |range, char| {
if let Err(err) = char { if let Err(err) = char {
let off = range.start; push_err(2, (range.start, err));
let off = token.text_range().start() + TextUnit::from_usize(off + 2);
acc.push(SyntaxError::new(err.into(), off))
} }
}) })
} }
} }
}
STRING => { STRING => {
if let Some(end) = text.rfind('\"') { if let Some(without_quotes) = unquote(text, 1, '"') {
if let Some(without_quotes) = text.get(1..end) {
unescape::unescape_str(without_quotes, &mut |range, char| { unescape::unescape_str(without_quotes, &mut |range, char| {
if let Err(err) = char { if let Err(err) = char {
let off = range.start; push_err(1, (range.start, err));
let off = token.text_range().start() + TextUnit::from_usize(off + 1);
acc.push(SyntaxError::new(err.into(), off))
} }
}) })
} }
} }
}
_ => (), _ => (),
} }
} }