Optimise term parsers

This commit is contained in:
RumovZ 2021-01-13 14:15:57 +01:00
parent 447ff6931c
commit 3aa15a7139

View File

@ -223,8 +223,9 @@ fn unquoted_term(s: &str) -> IResult<Node> {
)(s) )(s)
{ {
if tail.starts_with('\\') { if tail.starts_with('\\') {
let escaped = (if tail.len() > 1 { &tail[0..2] } else { "" }).to_string(); // trailing backslash followed by whitespace or nothing
Err(parse_failure(s, UnknownEscape(format!("\\{}", escaped)))) let seq = (if tail.len() > 1 { &tail[0..2] } else { r"\" }).to_string();
Err(parse_failure(s, UnknownEscape(seq)))
} else if term.eq_ignore_ascii_case("and") { } else if term.eq_ignore_ascii_case("and") {
Ok((tail, Node::And)) Ok((tail, Node::And))
} else if term.eq_ignore_ascii_case("or") { } else if term.eq_ignore_ascii_case("or") {
@ -233,8 +234,9 @@ fn unquoted_term(s: &str) -> IResult<Node> {
Ok((tail, Node::Search(search_node_for_text(term)?))) Ok((tail, Node::Search(search_node_for_text(term)?)))
} }
} else if s.starts_with('\\') { } else if s.starts_with('\\') {
let escaped = (if s.len() > 1 { &s[0..2] } else { "" }).to_string(); // leading backslash followed by whitespace or nothing
Err(parse_failure(s, UnknownEscape(format!("\\{}", escaped)))) let seq = (if s.len() > 1 { &s[0..2] } else { r"\" }).to_string();
Err(parse_failure(s, UnknownEscape(seq)))
} else { } else {
Err(nom::Err::Error(ParseError::Nom(s, NomErrorKind::Verify))) Err(nom::Err::Error(ParseError::Nom(s, NomErrorKind::Verify)))
} }
@ -246,21 +248,21 @@ fn quoted_term_str(s: &str) -> IResult<&str> {
if let Ok((tail, inner)) = if let Ok((tail, inner)) =
escaped::<_, ParseError, _, _, _, _>(is_not(r#""\"#), '\\', anychar)(opened) escaped::<_, ParseError, _, _, _, _>(is_not(r#""\"#), '\\', anychar)(opened)
{ {
if tail.is_empty() { if let Ok((remaining, _)) = char::<_, ParseError>('"')(tail) {
Err(parse_failure(s, UnclosedQuote))
} else if let Ok((remaining, _)) = char::<_, ParseError>('"')(tail) {
Ok((remaining, inner)) Ok((remaining, inner))
} else { } else {
Err(parse_failure(s, UnclosedQuote)) Err(parse_failure(s, UnclosedQuote))
} }
} else { } else {
match opened.chars().next().unwrap() { Err(parse_failure(
'"' => Err(parse_failure(s, EmptyQuote)), s,
// '\' followed by nothing match opened.chars().next().unwrap() {
'\\' => Err(parse_failure(s, UnclosedQuote)), '"' => EmptyQuote,
// everything else is accepted by escaped // '\' followed by nothing
_ => unreachable!(), '\\' => UnclosedQuote,
} _ => unreachable!(),
},
))
} }
} }